aboutsummaryrefslogtreecommitdiff
path: root/cusmodules/wallpaper.py
blob: 226fe52f4afb31b1f8ee614c6450799c1d5b4a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python

#Copyright (C) 2024  Michael Kantor

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <https://www.gnu.org/licenses/>.

import os
import sys
import json
import toml
import signal
from PIL import Image
from pathlib import Path
from random import randint
from subprocess import run

import colors

def dmenu(input_, prompt):
    result = os.popen(f'echo "{input_}" | dmenu -p "{prompt}"').read().strip("\n")

    return result

def gen_pylogo(top_color, bottom_color):
    home = str(Path.home())
    run(['convert', f'{home}/.config/qtile/topsnake.png', '+level-colors', f"gray,'{top_color}'", '-transparent', 'gray', '/tmp/topsnake.png'])

    run(['convert', f'{home}/.config/qtile/bottomsnake.png', '+level-colors', f"gray,'{bottom_color}'", '-transparent', 'gray', '/tmp/bottomsnake.png'])

    run(['convert', '-size', '600x600', '-composite', '/tmp/bottomsnake.png', '/tmp/topsnake.png', '/tmp/python.png'])

    #run(['convert', '-size', '610x610', '-composite', f'{home}/.config/qtile/border.png', '/tmp/python.png', '/tmp/python.png'])

    run(['rm', '-r', '/tmp/topsnake.png', '/tmp/bottomsnake.png'])

def preset(wall, mode):
    global home
    presets = os.popen(f"ls {home}/Pictures/Wallpapers/presets").read().strip('\n').split('\n')
    wall = wall[:-3]
    wall += 'json'

    if mode == 'find':
        return wall in presets

    elif mode == 'set':
        wall = presets.index(wall)
        return presets[wall]

def new(name, folder):
    os.system(f"ln -s {home}/Pictures/Wallpapers/wallpapers/{name} {home}/Pictures/Wallpapers/wallpaperdirs/{folder}/{name}")

def pywal(wall, loc):
    global home

    if wall == 'archstylized.png':
        nitro(wall, loc)

        os.system(f'wal --theme {home}/.config/qtile/colors/defaultcolors.json')

        return 0


    else:
        pass

    os.system(f'wal -i {loc}/{wall}')

def nitro(wall, loc):
    global home
    os.system(f'nitrogen --set-scaled {loc}/{wall}')

def random(walls):
    wallcount = len(walls) - 1
    ranint = randint(0, wallcount)
    ranwall = walls[ranint]
    
    return ranwall

if __name__ == '__main__':
    #Set dir variables
    home = str(Path.home())
    stdwalls = f'{home}/Pictures/Wallpapers/wallpapers'
    hallo = f'{home}/Pictures/Wallpapers/halloweenwallpapers'
    xmas = f'{home}/Pictures/Wallpapers/xmaswallpapers'

    if len(sys.argv) >= 2:
        if sys.argv[1] == 'none':
            wallpapers = os.popen(f'ls {stdwalls}').read().strip('\n').split('\n')
            wall = random(wallpapers)

            if preset(wall, 'find'):
                os.system(f'wal --theme {home}/Pictures/Wallpapers/presets/{preset(wall, "set")}')
                run([f'{home}/.local/bin/pywalfox', 'update'])
        
            else:

                pywal(wall, stdwalls)

            with open(f"{home}/.cache/wal/colors.json", 'r') as f:
                data = json.load(f)

                top_color = data["colors"].get('color8')

                bottom_color = data["colors"].get('color6')

            gen_pylogo(top_color, bottom_color)

        elif sys.argv[1] == 'halloween':
            curr_dir = hallo

            wallpapers = os.popen(f'ls {curr_dir}').read().strip('\n').split('\n')
    
            walls = random(wallpapers)

            nitro(walls, curr_dir)

            colors.setColor(home, f'{home}/.config/qtile/colors', 'halloweencolors.json', norestart = True)

            run([f'{home}/.local/bin/pywalfox', 'update'])

            sys.exit(0)

        elif sys.argv[1] == 'xmas':
            curr_dir = xmas

            wallpapers = os.popen(f'ls {curr_dir}').read().strip('\n').split('\n')
    
            walls = random(wallpapers)

            nitro(walls, curr_dir)

            colors.setColor(home, f'{home}/.config/qtile/colors', 'xmascolors.json', norestart = True)

            run([f'{home}/.local/bin/pywalfox', 'update'])

            sys.exit(0)

        elif sys.argv[1] == 'new':
            new(sys.argv[2], sys.argv[3])

        elif '-i' == sys.argv[1]:
            if sys.argv[2][0] == '/':
                indx = sys.argv[2].rindex('/')
                curr_dir  = sys.argv[2][:indx]
                wall = sys.argv[2][indx + 1:]

            else:
                curr_dir = os.getcwd()
                wall = sys.argv[2]

            pywal(wall, curr_dir)

            run([f'{home}/.local/bin/pywalfox', 'update'])

            with open(f"{home}/.cache/wal/colors.json", 'r') as f:
                data = json.load(f)

                top_color = data["colors"].get('color8')

                bottom_color = data["colors"].get('color6')

            gen_pylogo(top_color, bottom_color)

            try:
                #subprocess.check_call(['qtile', 'shell', '-c', 'restart()'])
                #run(['kill', '-SIGUSR1', os.popen('pidof qtile').read().strip('\n')])
                #run(['qtile', 'shell', '-c', 'restart()'])
                run([f'{home}/git/qtile/bin/qtile', 'shell', '-c', 'reload_config()'])

            except:
                os.system('killall dwm')

        elif 'a' in sys.argv[1]:
            curr_dir = sys.argv[1] + 'wallpapers'

            wallpapers = os.popen(f'ls {home}/Pictures/Wallpapers/{curr_dir}').read().strip('\n').split('\n')
            nitro(random(wallpapers), curr_dir)

        sys.exit(0)

    else:
        pass

    #Inital prompt
    walldirs = "All\n"
    walldirs += os.popen(f"ls {home}/Pictures/Wallpapers/wallpaperdirs/").read().strip("\n")
    #wallpaperchoice = os.popen(f'echo "{walldirs}" | dmenu -p "Wallpaper:"').read().strip("\n")
    wallpaperchoice = dmenu(walldirs, "Wallpaper:")

    #Closes on ESC
    if wallpaperchoice == '':
        sys.exit(0)
    
    #Sets to other dirs
    elif wallpaperchoice == "halloween":
        #wallpaperchoice = os.popen(f'ls {hallo}/ | dmenu -p "Wallpaper:"').read().strip('\n')
        wallpaperchoice = dmenu(os.popen(f"ls {hallo}").read().strip('\n'), "Wallpaper:")        

        if wallpaperchoice == '':
            sys.exit(0)
    
        curr_dir = hallo

    elif wallpaperchoice == "xmas":
        #wallpaperchoice = os.popen(f'ls {xmas}/ | dmenu -p "Wallpaper:"').read().strip('\n')
        wallpaperchoice = dmenu(os.popen(f"ls {xmas}").read().strip('\n'), "Wallpaper:")        
        
        if wallpaperchoice == '':
            sys.exit(0)
    
        curr_dir = xmas

    elif wallpaperchoice == "All":
        #wallpaperchoice = os.popen(f'ls {stdwalls}/ | dmenu -p "Wallpaper:"').read().strip('\n')
        wallpaperchoice = dmenu(os.popen(f"ls {stdwalls}").read().strip('\n'), "Wallpaper:")        
        
        if wallpaperchoice == '':
            sys.exit(0)
    
        curr_dir = stdwalls

    else:
        #wallpaperchoice = os.popen(f'ls {home}/Pictures/Wallpapers/wallpaperdirs/{wallpaperchoice} | dmenu -p "Wallpaper:"').read().strip('\n')
        wallpaperchoice = dmenu(os.popen(f"ls {home}/Pictures/Wallpapers/wallpaperdirs/{wallpaperchoice}").read().strip('\n'), "Wallpaper:")        

        if wallpaperchoice == '':
            sys.exit(0)
    
        curr_dir = stdwalls

    #Split to list 
    wallargs = wallpaperchoice.split(' ')
    
    #Handle random selection
    if 'random' in wallargs:
        wallpapers = os.popen(f'ls {home}/Pictures/Wallpapers/{curr_dir}').read().strip('\n').split('\n')
    
        wallargs[0] = random(wallpapers)

    #Set wallpaper with nitrogen
    if ':nitro' in wallargs:
        nitro(wallargs[0], curr_dir)
        sys.exit(0)

    elif preset(wallargs[0], 'find') and ':auto' not in wallargs:
        os.system(f'wal --theme {home}/Pictures/Wallpapers/presets/{preset(wallargs[0], "set")}')

        run([f'{home}/.local/bin/pywalfox', 'update'])

    #Set wallpaper with pywal
    else:
        pywal(wallargs[0], curr_dir)
        run([f'{home}/.local/bin/pywalfox', 'update'])

    with open(f"{home}/.cache/wal/colors.json", 'r') as f:
        data = json.load(f)

        top_color = data["colors"].get('color8')

        bottom_color = data["colors"].get('color6')

    gen_pylogo(top_color, bottom_color)

    try:
        print('restarting qtile')
        #subprocess.check_call(['qtile', 'shell', '-c', 'restart()'])
        #run(['qtile', 'shell', '-c', 'restart()'])
        #run(['kill', '-SIGUSR1', os.popen('pidof qtile').read().strip('\n')])
        run([f'{home}/git/qtile/bin/qtile', 'shell', '-c', 'reload_config()'])
        #os.system('qtile shell -c "restart()"')

    except:
        print('killing dwm')
        os.system('killall dwm')
        #pass