-
-
Notifications
You must be signed in to change notification settings - Fork 692
/
manager.py
1560 lines (1342 loc) · 53.6 KB
/
manager.py
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2008, Aldo Cortesi. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
import asyncio
import io
import logging
import os
import pickle
import shlex
import shutil
import signal
import subprocess
import tempfile
from typing import TYPE_CHECKING
import libqtile
from libqtile import bar, hook, ipc, utils
from libqtile.backend import base
from libqtile.command import interface
from libqtile.command.base import (
CommandError,
CommandException,
CommandObject,
ItemT,
)
from libqtile.command.client import InteractiveCommandClient
from libqtile.command.interface import IPCCommandServer, QtileCommandInterface
from libqtile.config import Click, Drag, Key, KeyChord, Match, Rule
from libqtile.config import ScratchPad as ScratchPadConfig
from libqtile.config import Screen
from libqtile.core.lifecycle import lifecycle
from libqtile.core.loop import LoopContext, QtileEventLoopPolicy
from libqtile.core.state import QtileState
from libqtile.dgroups import DGroups
from libqtile.extension.base import _Extension
from libqtile.group import _Group
from libqtile.log_utils import logger
from libqtile.scratchpad import ScratchPad
from libqtile.utils import get_cache_dir, send_notification
from libqtile.widget.base import _Widget
if TYPE_CHECKING:
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing_extensions import Literal
from libqtile.layout.base import Layout
class Qtile(CommandObject):
"""This object is the `root` of the command graph"""
current_screen: Screen
dgroups: DGroups
_eventloop: asyncio.AbstractEventLoop
def __init__(
self,
kore: base.Core,
config, # mypy doesn't like the config's dynamic attributes
no_spawn: bool = False,
state: Optional[str] = None,
socket_path: Optional[str] = None,
):
self.core = kore
self.config = config
self.no_spawn = no_spawn
self._state: Optional[Union[QtileState, str]] = state
self.socket_path = socket_path
self._drag: Optional[Tuple] = None
self.mouse_map: Dict[int, List[Union[Click, Drag]]] = {}
self.windows_map: Dict[int, base.WindowType] = {}
self.widgets_map: Dict[str, _Widget] = {}
self.groups_map: Dict[str, _Group] = {}
self.groups: List[_Group] = []
self.keys_map: Dict[Tuple[int, int], Union[Key, KeyChord]] = {}
self.chord_stack: List[KeyChord] = []
self.screens: List[Screen] = []
libqtile.init(self)
self._stopped_event: Optional[asyncio.Event] = None
self.server = IPCCommandServer(self)
def load_config(self, initial=False) -> None:
try:
self.config.load()
self.config.validate()
except Exception as e:
send_notification("Configuration error", str(e))
if hasattr(self.core, "wmname"):
self.core.wmname = getattr(self.config, "wmname", "qtile") # type: ignore
self.dgroups = DGroups(self, self.config.groups, self.config.dgroups_key_binder)
_Widget.global_defaults = self.config.widget_defaults
_Extension.global_defaults = self.config.extension_defaults
for installed_extension in _Extension.installed_extensions:
installed_extension._configure(self)
for i in self.groups:
self.groups_map[i.name] = i
for grp in self.config.groups:
if isinstance(grp, ScratchPadConfig):
sp = ScratchPad(grp.name, grp.dropdowns, grp.label, grp.single)
sp._configure([self.config.floating_layout],
self.config.floating_layout, self)
self.groups.append(sp)
self.groups_map[sp.name] = sp
self._process_screens(reloading=not initial)
# Map and Grab keys
for key in self.config.keys:
self.grab_key(key)
for button in self.config.mouse:
self.grab_button(button)
# no_spawn is set after the very first startup; we only want to run the
# startup hook once.
if not self.no_spawn:
hook.fire("startup_once")
self.no_spawn = True
hook.fire("startup")
if self._state:
if isinstance(self._state, str):
try:
with open(self._state, 'rb') as f:
st = pickle.load(f)
st.apply(self)
except: # noqa: E722
logger.exception("failed restoring state")
finally:
os.remove(self._state)
else:
self._state.apply(self)
self.core.distribute_windows(initial)
if self._state:
for screen in self.screens:
screen.group.layout_all()
self._state = None
self.update_desktops()
hook.subscribe.setgroup(self.update_desktops)
if self.config.reconfigure_screens:
hook.subscribe.screen_change(self.cmd_reconfigure_screens)
if initial:
hook.fire("startup_complete")
def _prepare_socket_path(
self,
socket_path: Optional[str] = None,
) -> str:
if socket_path is None:
socket_path = ipc.find_sockfile(self.core.display_name)
if os.path.exists(socket_path):
os.unlink(socket_path)
return socket_path
def loop(self) -> None:
asyncio.run(self.async_loop())
async def async_loop(self) -> None:
"""Run the event loop
Finalizes the Qtile instance on exit.
"""
self._eventloop = asyncio.get_running_loop()
# Set the event loop policy to facilitate access to main event loop
asyncio.set_event_loop_policy(QtileEventLoopPolicy(self))
self._stopped_event = asyncio.Event()
self.core.setup_listener(self)
try:
async with LoopContext({
signal.SIGTERM: self.stop,
signal.SIGINT: self.stop,
signal.SIGHUP: self.stop,
}), ipc.Server(
self._prepare_socket_path(self.socket_path),
self.server.call,
):
self.load_config(initial=True)
await self._stopped_event.wait()
finally:
self.finalize()
self.core.remove_listener()
def stop(self) -> None:
hook.fire("shutdown")
lifecycle.behavior = lifecycle.behavior.TERMINATE
self.core.graceful_shutdown()
self._stop()
def restart(self) -> None:
hook.fire("restart")
lifecycle.behavior = lifecycle.behavior.RESTART
state_file = os.path.join(tempfile.gettempdir(), 'qtile-state')
with open(state_file, 'wb') as f:
self.dump_state(f)
lifecycle.state_file = state_file
self._stop()
def _stop(self) -> None:
logger.debug('Stopping qtile')
if self._stopped_event is not None:
self._stopped_event.set()
def dump_state(self, buf) -> None:
try:
pickle.dump(QtileState(self), buf, protocol=0)
except: # noqa: E722
logger.exception('Unable to pickle qtile state')
def cmd_reload_config(self) -> None:
"""
Reload the configuration file.
"""
logger.debug('Reloading the configuration file')
try:
self.config.load()
except Exception as error:
logger.error("Configuration error: {}".format(error))
send_notification("Configuration error", str(error))
return
self._state = QtileState(self, restart=False)
self._finalize_configurables()
hook.clear()
self.ungrab_keys()
self.chord_stack.clear()
self.core.ungrab_buttons()
self.mouse_map.clear()
self.groups_map.clear()
self.groups.clear()
self.screens.clear()
self.load_config()
def _finalize_configurables(self) -> None:
"""
Finalize objects that are instantiated within the config file. In addition to
shutdown, these are finalized and then regenerated when reloading the config.
"""
try:
for widget in self.widgets_map.values():
widget.finalize()
self.widgets_map.clear()
for layout in self.config.layouts:
layout.finalize()
for screen in self.screens:
for gap in screen.gaps:
gap.finalize()
except: # noqa: E722
logger.exception('exception during finalize')
hook.clear()
def finalize(self) -> None:
self._finalize_configurables()
self.core.finalize()
def _process_screens(self, reloading=False) -> None:
current_groups = [s.group for s in self.screens if hasattr(s, "group")]
screens = []
if hasattr(self.config, 'fake_screens'):
screen_info = [(s.x, s.y, s.width, s.height) for s in self.config.fake_screens]
config = self.config.fake_screens
else:
# Alias screens with the same x and y coordinates, taking largest
xywh = {} # type: Dict[Tuple[int, int], Tuple[int, int]]
for sx, sy, sw, sh in self.core.get_screen_info():
pos = (sx, sy)
width, height = xywh.get(pos, (0, 0))
xywh[pos] = (max(width, sw), max(height, sh))
screen_info = [(x, y, w, h) for (x, y), (w, h) in xywh.items()]
config = self.config.screens
for i, (x, y, w, h) in enumerate(screen_info):
if i + 1 > len(config):
scr = Screen()
else:
scr = config[i]
if not hasattr(self, "current_screen") or reloading:
self.current_screen = scr
reloading = False
if len(self.groups) < i + 1:
name = f"autogen_{i + 1}"
self.add_group(name)
logger.warning(f"Too few groups in config. Added group: {name}")
if i < len(current_groups):
grp = current_groups[i]
else:
for grp in self.groups:
if not grp.screen:
break
scr._configure(self, i, x, y, w, h, grp)
screens.append(scr)
for screen in self.screens:
if screen not in screens:
for gap in screen.gaps:
if isinstance(gap, bar.Bar) and gap.window:
gap.kill_window()
self.screens = screens
def cmd_reconfigure_screens(self, ev: Any = None) -> None:
"""
This can be used to set up screens again during run time. Intended usage is to
be called when the screen_change hook is fired, responding to changes in
physical monitor setup by configuring qtile.screens accordingly. The ev kwarg is
ignored; it is here in case this function is hooked directly to screen_change.
"""
logger.info("Reconfiguring screens.")
self._process_screens()
for group in self.groups:
if group.screen:
if group.screen in self.screens:
group.layout_all()
else:
group.hide()
def paint_screen(
self, screen: Screen, image_path: str, mode: Optional[str] = None
) -> None:
self.core.painter.paint(screen, image_path, mode)
def process_key_event(self, keysym: int, mask: int) -> None:
key = self.keys_map.get((keysym, mask), None)
if key is None:
logger.debug("Ignoring unknown keysym: {keysym}, mask: {mask}".format(keysym=keysym, mask=mask))
return
if isinstance(key, KeyChord):
self.grab_chord(key)
else:
for cmd in key.commands:
if cmd.check(self):
status, val = self.server.call(
(cmd.selectors, cmd.name, cmd.args, cmd.kwargs)
)
if status in (interface.ERROR, interface.EXCEPTION):
logger.error("KB command error %s: %s" % (cmd.name, val))
if self.chord_stack and (self.chord_stack[-1].mode == "" or key.key == "Escape"):
self.cmd_ungrab_chord()
return
def grab_keys(self) -> None:
"""Re-grab all of the keys configured in the key map
Useful when a keyboard mapping event is received.
"""
self.core.ungrab_keys()
for key in self.keys_map.values():
self.grab_key(key)
def grab_key(self, key: Union[Key, KeyChord]) -> None:
"""Grab the given key event"""
keysym, mask_key = self.core.grab_key(key)
self.keys_map[(keysym, mask_key)] = key
def ungrab_key(self, key: Union[Key, KeyChord]) -> None:
"""Ungrab a given key event"""
keysym, mask_key = self.core.ungrab_key(key)
self.keys_map.pop((keysym, mask_key))
def ungrab_keys(self) -> None:
"""Ungrab all key events"""
self.core.ungrab_keys()
self.keys_map.clear()
def grab_chord(self, chord: KeyChord) -> None:
self.chord_stack.append(chord)
if self.chord_stack:
hook.fire("enter_chord", chord.mode)
self.ungrab_keys()
for key in chord.submappings:
self.grab_key(key)
def cmd_ungrab_chord(self) -> None:
"""Leave a chord mode"""
hook.fire("leave_chord")
self.ungrab_keys()
if not self.chord_stack:
logger.debug("cmd_ungrab_chord was called when no chord mode was active")
return
# The first pop is necessary: Otherwise we would be stuck in a mode;
# we could not leave it: the code below would re-enter the old mode.
self.chord_stack.pop()
# Find another named mode or load the root keybindings:
while self.chord_stack:
chord = self.chord_stack.pop()
if chord.mode != "":
self.grab_chord(chord)
break
else:
for key in self.config.keys:
self.grab_key(key)
def cmd_ungrab_all_chords(self) -> None:
"""Leave all chord modes and grab the root bindings"""
hook.fire("leave_chord")
self.ungrab_keys()
self.chord_stack.clear()
for key in self.config.keys:
self.grab_key(key)
def grab_button(self, button: Union[Click, Drag]) -> None:
"""Grab the given mouse button event"""
try:
button.modmask = self.core.grab_button(button)
except utils.QtileError:
logger.warning(f"Unknown modifier(s): {button.modifiers}")
return
if button.button_code not in self.mouse_map:
self.mouse_map[button.button_code] = []
self.mouse_map[button.button_code].append(button)
def update_desktops(self) -> None:
try:
index = self.groups.index(self.current_group)
# TODO: we should really only except ValueError here, AttributeError is
# an annoying chicken and egg because we're accessing current_screen
# (via current_group), and when we set up the initial groups, there
# aren't any screens yet. This can probably be changed when #475 is
# fixed.
except (ValueError, AttributeError):
index = 0
self.core.update_desktops(self.groups, index)
def add_group(
self,
name: str,
layout: Optional[str] = None,
layouts: Optional[List[Layout]] = None,
label: Optional[str] = None,
) -> bool:
if name not in self.groups_map.keys():
g = _Group(name, layout, label=label)
self.groups.append(g)
if not layouts:
layouts = self.config.layouts
g._configure(layouts, self.config.floating_layout, self)
self.groups_map[name] = g
hook.fire("addgroup", name)
hook.fire("changegroup")
self.update_desktops()
return True
return False
def delete_group(self, name: str) -> None:
# one group per screen is needed
if len(self.groups) == len(self.screens):
raise ValueError("Can't delete all groups.")
if name in self.groups_map.keys():
group = self.groups_map[name]
if group.screen and hasattr(group.screen, "previous_group"):
target = group.screen.previous_group
else:
target = group.get_previous_group()
# Find a group that's not currently on a screen to bring to the
# front. This will terminate because of our check above.
while target.screen:
target = target.get_previous_group()
for i in list(group.windows):
i.togroup(target.name)
if self.current_group.name == name:
self.current_screen.set_group(target, save_prev=False)
self.groups.remove(group)
del(self.groups_map[name])
hook.fire("delgroup", name)
hook.fire("changegroup")
self.update_desktops()
def register_widget(self, w: _Widget) -> None:
"""Register a bar widget
If a widget with the same name already exists, this will silently
ignore that widget. However, this is not necessarily a bug. By default
a widget's name is just ``self.__class__.lower()``, so putting multiple
widgets of the same class will alias and one will be inaccessible.
Since more than one groupbox widget is useful when you have more than
one screen, this is a not uncommon occurrence. If you want to use the
debug info for widgets with the same name, set the name yourself.
"""
if w.name:
if w.name in self.widgets_map:
return
self.widgets_map[w.name] = w
@property
def current_layout(self) -> Layout:
return self.current_group.layout
@property
def current_group(self) -> _Group:
return self.current_screen.group
@property
def current_window(self) -> Optional[base.Window]:
return self.current_screen.group.current_window
def reserve_space(
self,
reserved_space: Tuple[int, int, int, int], # [left, right, top, bottom]
screen: Screen,
) -> None:
"""
Reserve some space at the edge(s) of a screen.
"""
for i, pos in enumerate(["left", "right", "top", "bottom"]):
if reserved_space[i]:
gap = getattr(screen, pos)
if isinstance(gap, bar.Bar):
gap.adjust_for_strut(reserved_space[i])
elif isinstance(gap, bar.Gap):
gap.size += reserved_space[i]
if gap.size <= 0:
setattr(screen, pos, None)
else:
setattr(screen, pos, bar.Gap(reserved_space[i]))
screen.resize()
def free_reserved_space(
self,
reserved_space: Tuple[int, int, int, int], # [left, right, top, bottom]
screen: Screen,
):
"""
Free up space that has previously been reserved at the edge(s) of a screen.
"""
# mypy can't work out that the new tuple is also length 4 (see mypy #7509)
self.reserve_space(tuple(-i for i in reserved_space), screen) # type: ignore
def manage(self, win: base.WindowType) -> None:
if isinstance(win, base.Internal):
self.windows_map[win.wid] = win
return
if win.wid in self.windows_map:
return
hook.fire("client_new", win)
# Window may be defunct because
# it's been declared static in hook.
if win.defunct:
return
self.windows_map[win.wid] = win
if self.current_screen and not isinstance(win, base.Static):
# Window may have been bound to a group in the hook.
if not win.group and self.current_screen.group:
self.current_screen.group.add(win, focus=win.can_steal_focus)
self.core.update_client_list(self.windows_map)
hook.fire("client_managed", win)
def unmanage(self, wid: int) -> None:
c = self.windows_map.get(wid)
if c:
hook.fire("client_killed", c)
if isinstance(c, base.Static):
if c.reserved_space:
self.free_reserved_space(c.reserved_space, c.screen)
elif isinstance(c, base.Window):
if c.group:
c.group.remove(c)
del self.windows_map[wid]
self.core.update_client_list(self.windows_map)
def find_screen(self, x: int, y: int) -> Optional[Screen]:
"""Find a screen based on the x and y offset"""
result = []
for i in self.screens:
if i.x <= x <= i.x + i.width and \
i.y <= y <= i.y + i.height:
result.append(i)
if len(result) == 1:
return result[0]
return None
def find_closest_screen(self, x: int, y: int) -> Screen:
"""
If find_screen returns None, then this basically extends a
screen vertically and horizontally and see if x,y lies in the
band.
Only works if it can find a SINGLE closest screen, else we
revert to _find_closest_closest.
Useful when dragging a window out of a screen onto another but
having leftmost corner above viewport.
"""
normal = self.find_screen(x, y)
if normal is not None:
return normal
x_match = []
y_match = []
for i in self.screens:
if i.x <= x <= i.x + i.width:
x_match.append(i)
if i.y <= y <= i.y + i.height:
y_match.append(i)
if len(x_match) == 1:
return x_match[0]
if len(y_match) == 1:
return y_match[0]
return self._find_closest_closest(x, y, x_match + y_match)
def _find_closest_closest(
self, x: int, y: int, candidate_screens: List[Screen]
) -> Screen:
"""
if find_closest_screen can't determine one, we've got multiple
screens, so figure out who is closer. We'll calculate using
the square of the distance from the center of a screen.
Note that this could return None if x, y is right/below all
screens (shouldn't happen but we don't do anything about it
here other than returning None)
"""
closest_distance: Optional[float] = None # because mypy only considers first value
if not candidate_screens:
# try all screens
candidate_screens = self.screens
# if left corner is below and right of screen
# it can't really be a candidate
candidate_screens = [
s for s in candidate_screens
if x < s.x + s.width and y < s.y + s.height
]
closest_screen = candidate_screens[0]
for s in candidate_screens:
middle_x = s.x + s.width / 2
middle_y = s.y + s.height / 2
distance = (x - middle_x) ** 2 + (y - middle_y) ** 2
if closest_distance is None or distance < closest_distance:
closest_distance = distance
closest_screen = s
return closest_screen
def process_button_click(
self, button_code: int, modmask: int, x: int, y: int
) -> bool:
handled = False
for m in self.mouse_map.get(button_code, []):
if not m.modmask == modmask:
continue
if isinstance(m, Click):
for i in m.commands:
if i.check(self):
status, val = self.server.call(
(i.selectors, i.name, i.args, i.kwargs))
if status in (interface.ERROR, interface.EXCEPTION):
logger.error(
"Mouse command error %s: %s" % (i.name, val)
)
handled = True
elif isinstance(m, Drag):
if m.start:
i = m.start
status, val = self.server.call(
(i.selectors, i.name, i.args, i.kwargs))
if status in (interface.ERROR, interface.EXCEPTION):
logger.error(
"Mouse command error %s: %s" % (i.name, val)
)
continue
else:
val = (0, 0)
self._drag = (x, y, val[0], val[1], m.commands)
self.core.grab_pointer()
handled = True
return handled
def process_button_release(self, button_code: int, modmask: int) -> bool:
if self._drag is not None:
for m in self.mouse_map.get(button_code, []):
if isinstance(m, Drag):
self._drag = None
self.core.ungrab_pointer()
return True
return False
def process_button_motion(self, x: int, y: int) -> None:
if self._drag is None:
return
ox, oy, rx, ry, cmd = self._drag
dx = x - ox
dy = y - oy
if dx or dy:
for i in cmd:
if i.check(self):
status, val = self.server.call((
i.selectors,
i.name,
i.args + (rx + dx, ry + dy),
i.kwargs
))
if status in (interface.ERROR, interface.EXCEPTION):
logger.error(
"Mouse command error %s: %s" % (i.name, val)
)
def warp_to_screen(self) -> None:
if self.current_screen:
scr = self.current_screen
self.core.warp_pointer(scr.x + scr.dwidth // 2, scr.y + scr.dheight // 2)
def focus_screen(self, n: int, warp: bool = True) -> None:
"""Have Qtile move to screen and put focus there"""
if n >= len(self.screens):
return
old = self.current_screen
self.current_screen = self.screens[n]
if old != self.current_screen:
hook.fire("current_screen_change")
hook.fire("setgroup")
old.group.layout_all()
self.current_group.focus(self.current_window, warp)
if self.current_window is None and warp:
self.warp_to_screen()
def move_to_group(self, group: str) -> None:
"""Create a group if it doesn't exist and move
the current window there"""
if self.current_window and group:
self.add_group(group)
self.current_window.togroup(group)
def _items(self, name: str) -> ItemT:
if name == "group":
return True, list(self.groups_map.keys())
elif name == "layout":
return True, list(range(len(self.current_group.layouts)))
elif name == "widget":
return False, list(self.widgets_map.keys())
elif name == "bar":
return False, [x.position for x in self.current_screen.gaps]
elif name == "window":
return True, list(self.windows_map.keys())
elif name == "screen":
return True, list(range(len(self.screens)))
elif name == "core":
return True, []
return None
def _select(self, name: str, sel: Optional[Union[str, int]]) -> Optional[CommandObject]:
if name == "group":
if sel is None:
return self.current_group
else:
return self.groups_map.get(sel) # type: ignore
elif name == "layout":
if sel is None:
return self.current_group.layout
else:
return utils.lget(self.current_group.layouts, sel)
elif name == "widget":
return self.widgets_map.get(sel) # type: ignore
elif name == "bar":
return getattr(self.current_screen, sel) # type: ignore
elif name == "window":
if sel is None:
return self.current_window
else:
return self.windows_map.get(sel) # type: ignore
elif name == "screen":
if sel is None:
return self.current_screen
else:
return utils.lget(self.screens, sel)
elif name == "core":
return self.core
return None
def call_soon(self, func: Callable, *args) -> asyncio.Handle:
""" A wrapper for the event loop's call_soon which also flushes the core's
event queue after func is called. """
def f():
func(*args)
self.core.flush()
return self._eventloop.call_soon(f)
def call_soon_threadsafe(self, func: Callable, *args) -> asyncio.Handle:
""" Another event loop proxy, see `call_soon`. """
def f():
func(*args)
self.core.flush()
return self._eventloop.call_soon_threadsafe(f)
def call_later(self, delay, func: Callable, *args) -> asyncio.TimerHandle:
""" Another event loop proxy, see `call_soon`. """
def f():
func(*args)
self.core.flush()
return self._eventloop.call_later(delay, f)
def run_in_executor(self, func: Callable, *args):
""" A wrapper for running a function in the event loop's default
executor. """
return self._eventloop.run_in_executor(None, func, *args)
def cmd_debug(self) -> None:
"""Set log level to DEBUG"""
logger.setLevel(logging.DEBUG)
logger.debug('Switching to DEBUG threshold')
def cmd_info(self) -> None:
"""Set log level to INFO"""
logger.setLevel(logging.INFO)
logger.info('Switching to INFO threshold')
def cmd_warning(self) -> None:
"""Set log level to WARNING"""
logger.setLevel(logging.WARNING)
logger.warning('Switching to WARNING threshold')
def cmd_error(self) -> None:
"""Set log level to ERROR"""
logger.setLevel(logging.ERROR)
logger.error('Switching to ERROR threshold')
def cmd_critical(self) -> None:
"""Set log level to CRITICAL"""
logger.setLevel(logging.CRITICAL)
logger.critical('Switching to CRITICAL threshold')
def cmd_loglevel(self) -> int:
return logger.level
def cmd_loglevelname(self) -> str:
return logging.getLevelName(logger.level)
def cmd_pause(self) -> None:
"""Drops into pdb"""
import pdb
pdb.set_trace()
def cmd_groups(self) -> Dict[str, Dict[str, Any]]:
"""Return a dictionary containing information for all groups
Examples
========
groups()
"""
return {i.name: i.info() for i in self.groups}
def cmd_display_kb(self, *args) -> str:
"""Display table of key bindings"""
class FormatTable:
def __init__(self):
self.max_col_size = []
self.rows = []
def add(self, row):
n = len(row) - len(self.max_col_size)
if n > 0:
self.max_col_size += [0] * n
for i, f in enumerate(row):
if len(f) > self.max_col_size[i]:
self.max_col_size[i] = len(f)
self.rows.append(row)
def getformat(self):
format_string = " ".join("%-{0:d}s".format(max_col_size + 2) for max_col_size in self.max_col_size)
return format_string + "\n", len(self.max_col_size)
def expandlist(self, list, n):
if not list:
return ["-" * max_col_size for max_col_size in self.max_col_size]
n -= len(list)
if n > 0:
list += [""] * n
return list
def __str__(self):
format, n = self.getformat()
return "".join([format % tuple(self.expandlist(row, n)) for row in self.rows])
result = FormatTable()
result.add(["Mode", "KeySym", "Mod", "Command", "Desc"])
result.add([])
rows = []
def walk_binding(k: Union[Key, KeyChord], mode: str) -> None:
nonlocal rows
modifiers, name = ", ".join(k.modifiers), k.key
if isinstance(k, Key):
if not k.commands:
return
allargs = ", ".join(
[value.__name__ if callable(value) else repr(value)
for value in k.commands[0].args] +
["%s = %s" % (keyword, repr(value)) for keyword, value in k.commands[0].kwargs.items()]
)
rows.append((mode, name, modifiers,
"{:s}({:s})".format(k.commands[0].name, allargs), k.desc))
return
if isinstance(k, KeyChord):
new_mode_s = k.mode if k.mode else "<unnamed>"
new_mode = (k.mode if mode == "<root>" else
"{}>{}".format(mode, k.mode if k.mode else "_"))
rows.append((mode, name, modifiers, "", "Enter {:s} mode".format(new_mode_s)))
for s in k.submappings:
walk_binding(s, new_mode)
return
raise TypeError("Unexpected type: {}".format(type(k)))
for k in self.config.keys:
walk_binding(k, "<root>")
rows.sort()
for row in rows:
result.add(row)
return str(result)
def cmd_list_widgets(self) -> List[str]:
"""List of all addressible widget names"""
return list(self.widgets_map.keys())
def cmd_to_layout_index(
self, index: str, name: Optional[str] = None
) -> None:
"""Switch to the layout with the given index in self.layouts.
Parameters
==========
index :
Index of the layout in the list of layouts.
name :
Group name. If not specified, the current group is assumed.
"""
if name is not None:
group = self.groups_map[name]
else:
group = self.current_group
group.use_layout(index)
def cmd_next_layout(self, name: Optional[str] = None) -> None:
"""Switch to the next layout.
Parameters
==========
name :
Group name. If not specified, the current group is assumed
"""
if name is not None:
group = self.groups_map[name]
else:
group = self.current_group
group.use_next_layout()