~ruther/nixos-config

ref: 438f048929f6991e3cca1a5b1614da6cad431925 nixos-config/modules/desktop/qtile/config/config.py -rw-r--r-- 24.3 KiB
438f0489 — Frantisek Bohacek chore: update 1 year, 23 days ago
                                                                                
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
import re
import os
import subprocess
import psutil
import libqtile
from libqtile import layout, bar, qtile
from libqtile.backend.base import Window
from libqtile.core.manager import Qtile
from libqtile.config import Click, Drag, Group, KeyChord, EzKey, EzKeyChord, Match, Screen, ScratchPad, DropDown, Key
from libqtile.lazy import lazy
from libqtile.utils import guess_terminal
from libqtile import hook
from libqtile.log_utils import logger
from libqtile.backend.wayland import InputConfig
import qtile_extras.widget as widget
from qtile_extras.widget.decorations import BorderDecoration, PowerLineDecoration, RectDecoration
from tasklist import TaskList
from mpris2widget import Mpris2
from bluetooth import Bluetooth
import xmonadcustom
from nixenvironment import setupLocation, configLocation, sequenceDetectorExec
import time

import screeninfo

colors = {
    'primary': '51afef',
    'active': '8babf0',
    'inactive': '555e60',
    'secondary': '55eddc',
    'background_primary': '222223',
    'background_secondary': '444444',
    'urgent': 'c45500',
    'white': 'd5d5d5',
    'grey': '737373',
    'black': '121212',
}

# #####################################
# Utility functions
@lazy.function
def focus_window_by_class(qtile: Qtile, wmclass: str):
    match = Match(wm_class=wmclass)
    windows = [w for w in qtile.windows_map.values() if isinstance(w, Window) and match.compare(w)]
    if len(windows) == 0:
        return

    window = windows[0]
    group = window.group
    group.toscreen()
    group.focus(window)

@lazy.function
def warp_cursor_to_focused_window(qtile: Qtile):
    current_window = qtile.current_window
    win_size = current_window.get_size()
    win_pos = current_window.get_position()

    x = win_pos[0] + win_size[0] // 2
    y = win_pos[1] + win_size[1] // 2

    qtile.core.warp_pointer(x, y)

@lazy.function
def go_to_screen(qtile: Qtile, index: int):
    current_screen = qtile.current_screen
    screen = qtile.screens[index]

    logger.warning(screen)
    logger.warning(current_screen)
    if current_screen == screen:
        x = screen.x + screen.width // 2
        y = screen.y + screen.height // 2
        qtile.core.warp_pointer(x, y)
    else:
        qtile.to_screen(index)

    qtile.current_window.focus()


@lazy.function
def go_to_group(qtile: Qtile, group_name: str, switch_monitor: bool = False):
    found = False
    current_group = qtile.current_group
    if group_name == current_group.name:
        warp_cursor_to_focused_window()
        return

    current_screen = qtile.current_screen
    target_screen = current_screen

    for screen in qtile.screens:
        if screen.group.name == group_name:
            target_screen = screen
            if switch_monitor:
                qtile.focus_screen(screen.index)
            found = True
            break

    current_bar = current_screen.top
    target_bar = target_screen.top

    if found and current_bar != target_bar and isinstance(target_bar, libqtile.bar.Bar) and isinstance(current_bar, libqtile.bar.Bar):
        # found on other monitor, so switch bars
        target_bar_show = target_bar.is_show()
        current_bar_show = current_bar.is_show()

        current_bar.show(target_bar_show)
        target_bar.show(current_bar_show)

    qtile.groups_map[group_name].toscreen()

    for window in current_group.windows:
        if window.fullscreen:
            window.toggle_fullscreen()
            # time.sleep(0.1)
            window.toggle_fullscreen()

    if not switch_monitor or not found:
        window: Window
        for window in qtile.groups_map[group_name].windows:
            if window.fullscreen:
                window.toggle_fullscreen()
                # time.sleep(0.1)
                window.toggle_fullscreen()

# expands list of keys with the rest of regular keys,
# mainly usable for KeyChords, where you want any other key
# to exit the key chord instead.
def expand_with_rest_keys(keys: list[EzKey], global_prefix: str) -> list[EzKey]:
    all_keys = ['<semicolon>', '<return>', '<space>'] + [chr(c) for c in range(ord('a'), ord('z') + 1)]
    prefixes = ['', 'M-', 'M-S-', 'M-C-', 'C-', 'S-']

    for prefix in prefixes:
        for potentially_add_key in all_keys:
            potentially_add_key = prefix + potentially_add_key
            if potentially_add_key == global_prefix:
                continue

            found = False
            for existing_key in keys:
                if existing_key.key.lower() == potentially_add_key:
                    found = True
                    break

            if not found:
                keys.append(EzKey(potentially_add_key, lazy.spawn(f'notify-send "Not registered key {global_prefix} {potentially_add_key}"')))

    return keys


# #####################################
# Environment
mod = 'mod4'
terminal = 'alacritty'

layout_theme = {
    'border_focus': colors['active'],
    'border_normal': colors['inactive'],
    'border_width': 1,
    'margin': 3,
}

layouts = [
    xmonadcustom.MonadTall(**layout_theme),
    layout.Max(**layout_theme),
    xmonadcustom.MonadWide(**layout_theme),
]

widget_defaults = dict(
    font = 'Roboto Bold',
    fontsize = 13,
    padding = 3,
    background = colors['background_primary'],
    foreground = colors['white'],
)
extension_defaults = widget_defaults.copy()

def create_top_bar(systray = False):
    powerline = {
        'decorations': [
            PowerLineDecoration(path = 'forward_slash')
        ]
    }

    widgets = [
        widget.Sep(padding = 5, size_percent = 0, background = colors['background_secondary']),
        widget.CurrentScreen(
            active_text = 'I',
            active_color = colors['active'],
            padding = 3,
            fontsize = 16,
            background = colors['background_secondary'],
        ),
        widget.GroupBox(
            markup = False,
            highlight_method = 'line',
            rounded = False,
            margin_x = 2,
            disable_drag = True,
            use_mouse_wheel = True,
            active = colors['white'],
            inactive = colors['grey'],
            urgent_alert_method = 'line',
            urgent_border = colors['urgent'],
            this_current_screen_border = colors['active'],
            this_screen_border = colors['secondary'],
            other_screen_border = colors['inactive'],
            other_current_screen_border = '6989c0',
            background = colors['background_secondary'],
        ),
        widget.CurrentScreen(
            active_text = 'I',
            active_color = colors['active'],
            padding = 3,
            fontsize = 16,
            background = colors['background_secondary'],
            decorations = [
                PowerLineDecoration(path = 'forward_slash'),
            ],
        ),
        widget.Sep(
            linewidth=2,
            size_percent=0,
            padding=5,
        ),
        widget.Prompt(),
        widget.WindowName(
            foreground = colors['primary'],
            width = bar.CALCULATED,
            padding = 10,
            empty_group_string = 'Desktop',
            max_chars = 160,
            decorations = [
                RectDecoration(
                    colour = colors['black'],
                    radius = 0,
                    padding_y = 4,
                    padding_x = 0,
                    filled = True,
                    clip = True,
                ),
            ],
        ),
        widget.Spacer(),
        widget.Chord(
            padding = 15,
            decorations = [
                RectDecoration(
                    colour = colors['black'],
                    radius = 0,
                    padding_y = 4,
                    padding_x = 6,
                    filled = True,
                    clip = True,
                ),
            ]
        ),
        # widget.Net(
        #     interface = 'enp24s0',
        #     prefix='M',
        #     format = '{down:6.2f} {down_suffix:<2}↓↑{up:6.2f} {up_suffix:<2}',
        #     background = colors['background_secondary'],
        #     **powerline,
        # ),
        widget.Memory(
            format = '{MemFree: .0f}{mm}',
            fmt = '{} free',
            **powerline,
        ),
        widget.CPU(
            format = '{load_percent} %',
            fmt = '   {}',
            background = colors['background_secondary'],
            **powerline,
        ),
        widget.DF(
            update_interval = 60,
            partition = '/',
            #format = '[{p}] {uf}{m} ({r:.0f}%)',
            format = '{uf}{m} free',
            fmt = '   {}',
            visible_on_warn = False,
            **powerline,
        ),
        widget.GenPollText(
            func = lambda: subprocess.check_output(['xkblayout-state', 'print', '%s']).decode('utf-8').upper(),
            fmt = '⌨ {}',
            update_interval = 0.5,
            **powerline,
        ),
        widget.Clock(
            timezone='Europe/Prague',
            foreground = colors['primary'],
            format='%A, %B %d - %H:%M:%S',
            background = colors['background_secondary'],
            **powerline
        ),
        widget.Volume(
            fmt = '🕫  {}',
        ),
        widget.Sep(
            foreground = colors['background_secondary'],
            size_percent = 70,
            linewidth = 3,
        ),
        Bluetooth(
            hci = '/dev_88_C9_E8_49_93_16',
            format_connected = '  {battery} %',
            format_disconnected = '  Disconnected',
            format_unpowered = ''
        ),
    ]

    if systray:
        widgets.append(widget.Sep(
            foreground = colors['background_secondary'],
            size_percent = 70,
            linewidth = 2,
        ))
        widgets.append(widget.Systray())
        widgets.append(widget.Sep(padding = 5, size_percent = 0))

    return bar.Bar(widgets, 30)

def create_bottom_bar():
    powerline = {
        'decorations': [
            PowerLineDecoration(path = 'forward_slash')
        ]
    }

    return bar.Bar([
        TaskList(
            parse_text = lambda text : re.split(' [–—-] ', text)[-1],
            highlight_method = 'line',
            txt_floating = '🗗 ',
            txt_maximized = '🗖 ',
            txt_minimized = '🗕 ',
            borderwidth = 3,
        ),
        widget.Spacer(),
        Mpris2(
            format = '{xesam:title}',
            playerfilter = '.*Firefox.*',
            scroll = False,
            paused_text = '', #'   {track}',
            playing_text = '    {track}',
            padding = 10,
            decorations = [
                RectDecoration(
                    colour = colors['black'],
                    radius = 0,
                    padding_y = 4,
                    padding_x = 5,
                    filled = True,
                    clip = True,
                ),
            ],
        ),
        Mpris2(
            format = '{xesam:title} - {xesam:artist}',
            objname = 'org.mpris.MediaPlayer2.spotify',
            scroll = False,
            paused_text = '', #'   {track}',
            playing_text = '  {track}', # '   {track}',
            padding = 10,
            decorations = [
                RectDecoration(
                    colour = colors['black'],
                    radius = 0,
                    padding_y = 4,
                    padding_x = 5,
                    filled = True,
                    clip = True,
                ),
            ],
        ),
        widget.Sep(
            size_percent = 0,
            padding = 5,
            **powerline,
        ),
        widget.Wttr(
            location = {'Odolena_Voda': ''},
            format = '%t %c',
            background = colors['background_secondary'],
            **powerline,
        ),
    ], 30)

def init_screens():
    wallpaper = f'{setupLocation}/wall.png'

    screens_info = screeninfo.get_monitors()
    screens_count = len(screens_info)
    screens = [None] * screens_count

    logger.warning(f'setting up {screens_count} screens')

    for i in range(0, screens_count):
        screen_info = screens_info[i]
        systray = False
        if screens_count <= 2 and i == 0:
            systray = True
            print(f'Putting systray on {i}')
        elif i == 1:
            systray  = True
            print(f'Putting systray on {i}')

        top_bar = create_top_bar(systray = systray)

        screens[i] = Screen(top=top_bar, bottom=create_bottom_bar(), wallpaper=f'{setupLocation}/wall.png', width=screen_info.width, height=screen_info.height)

    return screens

screens = init_screens()

# Keys
keys = []

# up, down, main navigation
keys.extend([
    EzKey('M-j', lazy.layout.down(), desc = 'Move focus down'),
    EzKey('M-k', lazy.layout.up(), desc = 'Move focus up'),
    EzKey('M-S-j', lazy.layout.shuffle_down(), desc = 'Move focus down'),
    EzKey('M-S-k', lazy.layout.shuffle_up(), desc = 'Move focus up'),
    EzKey('M-C-h', lazy.layout.shrink_main(), desc = 'Move focus down'),
    EzKey('M-C-l', lazy.layout.grow_main(), desc = 'Move focus up'),
    EzKey('M-m', lazy.layout.focus_first(), desc = 'Focus main window'),
    EzKey('M-u', lazy.next_urgent(), desc = 'Focus urgent window'),
])

keys.extend([
    EzKey('M-n', lazy.layout.normalize()),
    EzKey('M-S-m', lazy.window.toggle_minimize()),
    EzKey('M-t', lazy.window.disable_floating()),
    EzKey('M-f', lazy.window.toggle_fullscreen()),
    EzKey('M-S-f', lazy.to_layout_index(1)),
    EzKey('M-<Return>', lazy.layout.swap_main()),
    EzKey('M-<Space>', lazy.next_layout()),
    EzKey('M-S-<Space>', lazy.to_layout_index(0), desc = 'Default layout'),
    EzKey('M-<comma>', lazy.layout.increase_nmaster()),
    EzKey('M-<period>', lazy.layout.decrease_nmaster()),
])

# Spwning programs
keys.extend([
    EzKey('M-<semicolon>', lazy.spawn('rofi -show drun')),
    EzKey('A-<semicolon>', lazy.spawn('rofi -show windowcd -modi window,windowcd')),
    EzKey('M-S-<semicolon>', lazy.spawn('rofi -show window -modi window,windowcd')),
    EzKey('M-S-<Return>', lazy.spawn(terminal)),
])

keys.extend([
    # social navigation
    EzKeyChord('M-a', expand_with_rest_keys([
       EzKey('b', focus_window_by_class('discord')),
       EzKey('n', focus_window_by_class('element')),
       EzKey('m', focus_window_by_class('element')),

       # notifications
       EzKey('l', lazy.spawn(f'{setupLocation}/scripts/notifications/clear-popups.sh')),
       EzKey('p', lazy.spawn(f'{setupLocation}/scripts/notifications/pause.sh')),
       EzKey('u', lazy.spawn(f'{setupLocation}/scripts/notifications/unpause.sh')),
       EzKey('t', lazy.spawn(f'{setupLocation}/scripts/notifications/show-center.sh')),

       EzKey('e', lazy.spawn('emacsclient -c')),
    ], 'M-a'), name = 'a')
])

keys.extend([
    EzKeyChord('M-s', expand_with_rest_keys([
       EzKey('e', lazy.spawn('emacsclient -c')),
       EzKey('c', lazy.group['scratchpad'].dropdown_toggle('ipcam')),
       EzKey('s', lazy.group['scratchpad'].dropdown_toggle('spotify')),
       EzKey('b', lazy.group['scratchpad'].dropdown_toggle('bluetooth')),
       EzKey('a', lazy.group['scratchpad'].dropdown_toggle('audio')),
       EzKey('m', lazy.group['scratchpad'].dropdown_toggle('mail')),
       EzKey('p', lazy.group['scratchpad'].dropdown_toggle('proton')),
    ], 'M-s'), name = 's')
])

# bars
keys.extend([
    EzKey('M-b', lazy.hide_show_bar('top')),
    EzKey('M-v', lazy.hide_show_bar('bottom')),
])

# media keys
keys.extend([
    EzKey('<XF86AudioPlay>', lazy.spawn(f'{sequenceDetectorExec} -g mpris play')),
    EzKey('<XF86AudioPause>', lazy.spawn(f'{sequenceDetectorExec} -g mpris pause')),
    EzKey('<XF86AudioStop>', lazy.spawn(f'{sequenceDetectorExec} -g mpris stop')),
    EzKey('<XF86AudioNext>', lazy.spawn(f'{sequenceDetectorExec} -g mpris next')),
    EzKey('<XF86AudioPrev>', lazy.spawn(f'{sequenceDetectorExec} -g mpris prev')),
    EzKey('<XF86AudioMute>', lazy.spawn('amixer -D pulse set Master 1+ toggle')),
    EzKey('<XF86MonBrightnessUp>', lazy.spawn(f'{configLocation}/brightness.sh up')),
    EzKey('<XF86MonBrightnessDown>', lazy.spawn(f'{configLocation}/brightness.sh down')),
])

# Printscreen
keys.extend([
    EzKey('<Print>', lazy.spawn('flameshot gui')),
])

# Locking
keys.extend([
    EzKey('M-S-n', lazy.spawn('loginctl lock-session')),
])

# Qtile control
keys.extend([
    EzKey('M-S-c', lazy.window.kill()),
    EzKey('M-C-r', lazy.reload_config()),
    EzKey('M-C-S-r', lazy.restart()),
    EzKey('M-C-q', lazy.shutdown()),
])

if len(screens) >= 4:
    monitor_navigation_keys = ['q', 'w', 'e', 'r']
else:
    monitor_navigation_keys = ['w', 'e', 'r']

for i, key in enumerate(monitor_navigation_keys):
    keys.extend([
        EzKey(f'M-{key}', go_to_screen(i), desc = f'Move focus to screen {i}'),
        EzKey(f'M-S-{key}', lazy.window.toscreen(i), desc = f'Move window to screen {i}'),
    ])

if qtile.core.name == 'x11':
    keys.append(EzKey('M-S-z', lazy.spawn('clipmenu')))
elif qtile.core.name == 'wayland':
    keys.append(
        EzKey(
            'M-S-z',
            lazy.spawn('sh -c "cliphist list | dmenu -l 10 | cliphist decode | wl-copy"')
        )
    )

group_defaults = {
    '9': {
        'layout': 'max'
    }
}
logger.info(group_defaults.get('9'))
groups = [Group(i) if not (i in group_defaults.keys()) else Group(i, **group_defaults.get(i)) for i in '123456789']

for i in groups:
    keys.extend(
        [
            EzKey(
                f'M-{i.name}',
                go_to_group(i.name),
                desc='Switch to group {}'.format(i.name),
            ),
            Key(
                [mod, 'shift'],
                i.name,
                lazy.window.togroup(i.name, switch_group=False),
                desc='Switch to & move focused window to group {}'.format(i.name),
            ),
        ]
    )

scratch_pad_middle = {
    'x': 0.2, 'y': 0.2,
    'height': 0.6, 'width': 0.6,
}

groups.append(
    ScratchPad('scratchpad', [
        DropDown(
            'spotify',
            ['spotify'],
            on_focus_lost_hide = True,
            **scratch_pad_middle
        ),
        DropDown(
            'ipcam',
            ['/home/ruther/doc/utils/ip-cam.sh'],
            on_focus_lost_hide = True,
            **scratch_pad_middle
        ),
        DropDown(
            'bluetooth',
            ['blueman-manager'],
            on_focus_lost_hide = True,
            **scratch_pad_middle
        ),
        DropDown(
            'audio',
            ['pavucontrol'],
            on_focus_lost_hide = True,
            **scratch_pad_middle
        ),
        DropDown(
            'mail',
            ['thunderbird'],
            on_focus_lost_hide = True,
            x = 0.025, y = 0.025,
            width = 0.95, height = 0.95,
            opacity = 1,
        ),
        # DropDown(
        #     'proton',
        #     ['firefoxpwa', 'site', 'launch', '01HBD772V37WPQ3B2T7TQJ81PM'],
        #     match = Match(wm_class = 'FFPWA-01HBD772V37WPQ3B2T7TQJ81PM'),
        #     on_focus_lost_hide = True,
        #     x = 0.025, y = 0.025,
        #     width = 0.95, height = 0.95,
        #     opacity = 1,
        # ),
    ])
)

# Drag floating layouts.
mouse = [
    Drag([mod], 'Button1', lazy.window.set_position_floating(), start=lazy.window.get_position()),
    Drag([mod], 'Button3', lazy.window.set_size_floating(), start=lazy.window.get_size()),
    Click([mod], 'Button2', lazy.window.bring_to_front()),
]

dgroups_key_binder = None
dgroups_app_rules = []  # type: list
follow_mouse_focus = False
cursor_warp = False
floating_layout = layout.Floating(
    float_rules=[
        *layout.Floating.default_float_rules,
    ]
)
auto_fullscreen = True
focus_on_window_activation = 'never'
reconfigure_screens = True
auto_minimize = True
bring_front_click = False

wl_input_rules = {}
wmname = 'LG3D'

from threading import Timer

def debounce(wait):
    """ Decorator that will postpone a functions
        execution until after wait seconds
        have elapsed since the last time it was invoked. """
    def decorator(fn):
        def debounced(*args, **kwargs):
            def call_it():
                fn(*args, **kwargs)
            try:
                debounced.t.cancel()
            except(AttributeError):
                pass
            debounced.t = Timer(wait, call_it)
            debounced.t.start()
        return debounced
    return decorator


# Monitors changing connected displays and the lid.
# Calls autorandr to change the outputs, and QTile
# restart
async def _observe_monitors():
    from pydbus import SystemBus
    from gi.repository import GLib
    from libqtile.utils import add_signal_receiver
    from dbus_next.message import Message
    import pyudev

    @debounce(0.2)
    def call_autorandr():
        subprocess.call(['autorandr', '--change', '--default', 'horizontal'])
        time.sleep(0.3)
        subprocess.call(['qtile', 'cmd-obj', '-o', 'cmd', '-f', 'restart'])

    def on_upower_event(message: Message):
        args = message.body
        properties = args[1]
        logger.info(message.body)
        if 'LidIsClosed' in properties:
            call_autorandr()

    def on_drm_event(action=None, device=None):
        if action == "change":
            call_autorandr()

    context = pyudev.Context()
    monitor = pyudev.Monitor.from_netlink(context)
    monitor.filter_by(subsystem = 'drm')
    monitor.enable_receiving()

    # bus = SystemBus()
    # upower = bus.get('org.freedesktop.UPower', '/org/freedesktop/UPower')
    # upower.PropertiesChanged.connect(on_upower_event)

    logger.warning("Adding signal receiver")
    subscribe = await add_signal_receiver(
        on_upower_event,
        session_bus = False,
        signal_name = "PropertiesChanged",
        path = '/org/freedesktop/UPower',
        dbus_interface = 'org.freedesktop.DBus.Properties',
    )
    logger.warning(f"Add signal receiver: {subscribe}")

    observer = pyudev.MonitorObserver(monitor, on_drm_event)
    observer.start()

# Swallow windows,
# when a process with window spawns
# another process with a window as a child, minimize the first
# winddow. Turn off the minimization after the child process
# is done.
# @hook.subscribe.client_new
#   I don't like this much :( hence I commented it out
def _swallow(window):
    pid = window.window.get_net_wm_pid()
    ppid = psutil.Process(pid).ppid()
    cpids = {c.window.get_net_wm_pid(): wid for wid, c in window.qtile.windows_map.items()}
    for i in range(5):
        if not ppid:
            return
        if ppid in cpids:
            parent = window.qtile.windows_map.get(cpids[ppid])
            parent.minimized = True
            window.parent = parent
            return
        ppid = psutil.Process(ppid).ppid()

# @hook.subscribe.client_killed
def _unswallow(window):
    if hasattr(window, 'parent'):
        window.parent.minimized = False

# Startup setup,
# windows to correct workspaces,
# start autostart.sh
@hook.subscribe.startup_once
def autostart():
    subprocess.call([f'{configLocation}/autostart.sh'])


@hook.subscribe.startup
async def observer_start():
    await _observe_monitors()

firefoxInstance = 0
@hook.subscribe.client_new
def startup_applications(client: Window):
    global firefoxInstance

    if not isinstance(client, Window):
        return

    if client.match(Match(wm_class = 'firefox')) and firefoxInstance <= 1:
        client.togroup(groups[firefoxInstance].name)
        firefoxInstance += 1
    elif client.match(Match(wm_class = 'discord')) or client.match(Match(wm_class = 'telegram-desktop')) or client.match(Match(wm_class = 'element')):
        client.togroup(groups[8].name)

# Turn off fullscreen on unfocus
@hook.subscribe.client_focus
def exit_fullscreen_on_focus_changed(client: Window):
    windows = client.group.windows
    window: Window
    for window in windows:
        if window != client and window.fullscreen:
            window.toggle_fullscreen()


@hook.subscribe.startup_complete
def hide_bottom_bar():
    for screen in qtile.screens:
        bar = screen.bottom
        if isinstance(bar, libqtile.bar.Bar):
            bar.show(False)

# Start scratchpads
@hook.subscribe.startup_complete
def scratchpad_startup():
    scratchpad: ScratchPad = qtile.groups_map['scratchpad']
    for dropdown_name, dropdown_config in scratchpad._dropdownconfig.items():
        scratchpad._spawn(dropdown_config)
        def wrapper(name):
            def hide_dropdown(_):
                dropdown = scratchpad.dropdowns.get(name)
                if dropdown:
                    dropdown.hide()
                    hook.unsubscribe.client_managed(hide_dropdown)
            return hide_dropdown

        hook.subscribe.client_managed(wrapper(dropdown_name))
Do not follow this link