-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
380 lines (289 loc) · 9.02 KB
/
main.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
#!/usr/bin/env python3
import argparse
from contextlib import contextmanager
from typing import Any, Dict, Optional, Tuple, Union, Callable # noqa
from Xlib import X
from Xlib.display import Display
from Xlib.error import XError
from Xlib.xobject.drawable import Window
# Display Vars
disp = Display()
root = disp.screen().root
# Display settings
x = 50 # x position where to puts windows
y = 50 # y position where to puts windows
width = root.get_geometry().width # screen width (eg. 1920)
height = root.get_geometry().height # screen height (eg. 1080)
def main():
# get active window
window = get_active_window()
# execute user selected tiling operation
options[args.mode](window)
# update window
disp.sync()
def tile_left(window: Window):
"""
Tile window to the left part of the screen.
-m tile_left
"""
print(f"Left Tiling Window {window}")
# get window size
win_size = get_window_size(window)
_x = 0
_y = 0
_w = int(width/2)
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling left windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def tile_right(window: Window):
"""
Tile window to the right part of the screen.
-m tile_right
"""
print(f"Tiling Right Window {window}")
# get window size
win_size = get_window_size(window)
_x = int(width/2)
_y = 0
_w = int(width / 2) - 10 # 10 = border width
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling right windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def tile_leftcenter(window: Window):
"""
Tile window to the left-center (2/3) part of the screen.
-m tile_leftcenter
"""
print(f"Tiling LeftCenter Window {window}")
# get window size
win_size = get_window_size(window)
_x = 0
_y = 0
_w = int(width / 3)*2
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling LeftCenter windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def tile_rightcenter(window: Window):
"""
Tile window to the right-center (2/3) part of the screen.
-m tile_rightcenter
"""
print(f"Tiling RightCenter Window {window}")
# get window size
win_size = get_window_size(window)
_x = int(width / 3)
_y = 0
_w = int(width / 3)*2
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling RightCenter windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def tile_leftleft(window: Window):
"""
Tile window to the left-left (1/3) part of the screen.
-m tile_leftleft
"""
print(f"Tiling RightCenter Window {window}")
# get window size
win_size = get_window_size(window)
_x = 0
_y = 0
_w = int(width / 3)
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling RightCenter windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def tile_rightright(window: Window):
"""
Tile window to the right-right (1/3) part of the screen.
-m tile_rightright
"""
print(f"Tiling RightCenter Window {window}")
# get window size
win_size = get_window_size(window)
_x = int(width / 3) * 2
_y = 0
_w = int(width / 3)
_h = usable_height
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling RightCenter windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def centering_window(window: Window):
"""
Centers the active window.
-m center
Used with --noresize the active window is not resized when centered
"""
print(f"Centering Window {window}")
# get window size
win_size = get_window_size(window)
if args.noresize:
# centering windows without resize dims
print('\t with NoResize mode')
_x = int((width - win_size.width) / 2)
_y = int((height - win_size.height) / 2)
_w = win_size.width
_h = win_size.height
else:
# centering windows and resize dims
print('\t with Resize mode')
_x = x
_y = y
_w = width - (x * 2)
_h = height - (y * 2)
print(f"New Window pos: ({_x}, {_y}), New Window size: ({_w}, {_h})")
# tiling center windows
window.configure(
# where to put the window
x=_x,
y=_y,
# new window dims
width=_w,
height=_h,
# other stuff
border_width=0,
stack_mode=X.Above
)
def get_window_size(window: Window):
"""Returns window size object"""
# get window dims
win_size = window.get_geometry()
print(f"Window pos: ({win_size.x}, {win_size.y}) Window size: ({win_size.width}, {win_size.height})")
return win_size
def calculate_usable_height():
prop1 = root.get_full_property(disp.get_atom("_NET_WORKAREA"), X.AnyPropertyType).value[3]
prop2 = root.get_full_property(disp.get_atom("_NET_WORKAREA"), X.AnyPropertyType).value[1]
res = prop1 - prop2
return res if res > 0 else res*(-1)
def get_active_window():
"""Wrapper for getting the active window object. Returns the WindowObject of the active window."""
# Prepare the property names we use so they can be fed into X11 APIs
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')
NET_WM_NAME = disp.intern_atom('_NET_WM_NAME') # UTF-8
WM_NAME = disp.intern_atom('WM_NAME') # Legacy encoding
last_seen = {'xid': None, 'title': None} # type: Dict[str, Any]
@contextmanager
def window_obj(win_id: Optional[int]) -> Window:
"""Simplify dealing with BadWindow (make it either valid or None)"""
window_obj = None
if win_id:
try:
window_obj = disp.create_resource_object('window', win_id)
except XError:
pass
yield window_obj
def _get_active_window() -> Tuple[Optional[int], bool]:
"""Return a (window_id, focus_has_changed) tuple for the active window."""
response = root.get_full_property(NET_ACTIVE_WINDOW,
X.AnyPropertyType)
if not response:
return None, False
win_id = response.value[0]
focus_changed = (win_id != last_seen['xid'])
if focus_changed:
with window_obj(last_seen['xid']) as old_win:
if old_win:
old_win.change_attributes(event_mask=X.NoEventMask)
last_seen['xid'] = win_id
with window_obj(win_id) as new_win:
if new_win:
new_win.change_attributes(event_mask=X.PropertyChangeMask)
return win_id, focus_changed
# get active window and return Window Object
win_id, _ = _get_active_window()
with window_obj(win_id) as window:
return window
if __name__ == '__main__':
# define selectable options
# type: Dict[str, Callable[[Window], None]]
options = {
'center': centering_window, # center window
'tile_left': tile_left, # tiling window to the left
'tile_right': tile_right, # tiling window to the right
'tile_leftcenter': tile_leftcenter,
'tile_rightcenter': tile_rightcenter,
'tile_leftleft': tile_leftleft,
'tile_rightright': tile_rightright,
}
# init argument parser
parser = argparse.ArgumentParser(description='My awesome python tiling assistant !')
# arguments
parser.add_argument(
'-m',
'--mode',
type=str,
choices=options.keys(),
required=True,
help='Choose how to tile the active window'
)
parser.add_argument(
'--noresize',
action='store_true',
help='Center window without resize it'
)
args = parser.parse_args()
usable_height = calculate_usable_height()
main()