-
Notifications
You must be signed in to change notification settings - Fork 1
/
pywin.py
executable file
·347 lines (285 loc) · 12.4 KB
/
pywin.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
#!/usr/bin/env python
# coding: utf8
#
# Copyright 2018 B.Jacob
#
# 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.
#
import argparse
import gi
from Xlib import X, display # python-xlib
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
gi.require_version("Wnck", "3.0")
from gi.repository import Wnck
import json
import os
from argparse import RawTextHelpFormatter
screens = [
{"name": "screen1", "x": 0, "y": 0, "width": 2560, "height": 1440 - 32},
{"name": "screen2", "x": 2560, "y": 0, "width": 2560, "height": 1440},
{"name": "screen3", "x": 2560 + 2560, "y": 0, "width": 1280, "height": 1024}
]
# switch off to use screens defined above
autoDiscoverScreens = True
storageFile = "/tmp/pywin.json"
choices = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center']
def placeMouseOver(rect):
d = display.Display()
s = d.screen()
root = s.root
x = int(rect[0] + rect[2] / 2)
y = int(rect[1] + rect[3] / 2)
if verbose:
print("new win pos:" + str(rect))
print ("new x,y" + str(x) + "," + str(y))
root.warp_pointer(x, y)
d.sync()
def discoverScreens():
# TODO discover taskbars on any side of the screen
# (e.g. make a window fullscreen and take coords?)
screens = []
display = Gdk.Display.get_default()
for x in range(0, display.get_n_monitors()):
screenDim = display.get_monitor(x).get_geometry()
if screenDim is None:
continue
name = "screen-" + str(x + 1)
screens.append({"name": name, "x": screenDim.x, "y": screenDim.y,
"width": screenDim.width, "height": screenDim.height})
return screens
def boxIntersects(leftA, topA, rightA, bottomA, leftB, topB, rightB, bottomB):
return not (leftA > rightB or
rightA < leftB or
topA > bottomB or
bottomA < topB)
def findBounds(x, y, width, height):
if verbose:
print("Find best matching screen for window:")
bestMatch = screens[0] # always use a default
match = -1000
for screen in screens:
# screens without intersections must not be relevant here
if not boxIntersects(x, y, x + width, y + height, screen['x'], screen['y'], screen['x'] + screen['width'],
screen['y'] + screen['height']):
if verbose:
print(" No intersection with: " + str(screen["name"]))
continue
x1 = max(x, screen['x'])
y1 = max(y, screen['y'])
x2 = min(x + width, screen['x'] + screen['width'])
y2 = min(y + height, screen['y'] + screen['height'])
sizeWindow = width * height
sizeOnScreen = (x2 - x1) * (y2 - y1)
percentOnScreen = sizeOnScreen * 100 / sizeWindow
if verbose:
print(" " + str(percentOnScreen) + "% are on: " + str(screen["name"]))
if match < percentOnScreen:
# a window belongs to the screen that renders most parts of it
match = percentOnScreen
bestMatch = screen
# always return screen 1
if verbose:
print(" Using: " + str(bestMatch["name"]) + " as best match")
return bestMatch
def read_args():
parser = argparse.ArgumentParser(description='Window-Placement/Window-tiling', formatter_class=RawTextHelpFormatter)
parser.add_argument('-p', '--pos', dest='position', metavar="position", required=True, choices=choices,
help='Direction to place window. Using abbreviations for directions like n=north ne=northeast and so on. Use one of:' +
(','.join(choices)))
parser.add_argument('-f', '--factor', dest='factor', metavar="factor", type=int, default=2,
help='default scale factor ')
parser.add_argument('-s', '--stateful', dest='stateful', action='store_true',
help='remember window sizing by storing some data under: ' + storageFile)
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help='print some debugging output')
parser.add_argument('-o', '--horizontal', dest='hori', action='store_true',
help='scale only horizontal')
parser.add_argument('-e', '--vertically', dest='vert', action='store_true',
help='scale only vertical')
parser.add_argument('-c', '--with-cursor', dest='mouse', action='store_true', default=False,
help='place mouse cursor over moved window. Subsequent invocations should address same window'
' if system activates windows on hover')
parser.add_argument('-m', '--my-factors', dest='myfactor', metavar="scale-factors", default="1,1.334,1.5,2,3,4",
help='Comma delimited list of scale-factors to use. e.g. \"1,1.5,2,3\" This requires stateful option.')
args = parser.parse_args()
return args
def calcNewPos(screen, position, factor, geometryRaw, currentPos, verticallyOnly=False, horizontalOnly=False):
gravity = Wnck.WindowGravity.NORTHWEST
if position == 'n':
y = screen["y"]
x = screen["x"]
width = screen["width"]
height = screen["height"] / factor
if position == 'ne':
y = screen["y"]
x = screen["x"] + screen["width"] - screen["width"] / factor
if not horizontalOnly and not verticallyOnly:
width = screen["width"] / factor
height = screen["height"] / factor
if horizontalOnly:
width = screen["width"] / factor
height = currentPos.heightp
if verticallyOnly:
width = currentPos.widthp
height = screen["height"] / factor
x = currentPos.xp
if position == 'e':
y = screen["y"]
x = screen["x"] + screen["width"] - screen["width"] / factor
width = screen["width"] / factor
height = screen["height"]
if position == 'se':
y = screen["y"] + screen["height"] - screen["height"] / factor
x = screen["x"] + screen["width"] - screen["width"] / factor
if not horizontalOnly and not verticallyOnly:
width = screen["width"] / factor
height = screen["height"] / factor
if horizontalOnly:
width = screen["width"] / factor
height = currentPos.heightp
y = currentPos.yp
if verticallyOnly:
x = currentPos.xp
width = currentPos.widthp
height = screen["height"] / factor
if position == 's':
y = screen["y"] + screen["height"] - screen["height"] / factor
x = screen["x"]
width = screen["width"]
height = screen["height"] / factor
if position == 'sw':
y = screen["y"] + screen["height"] - screen["height"] / factor
x = screen["x"]
if not horizontalOnly and not verticallyOnly:
width = screen["width"] / factor
height = screen["height"] / factor
if horizontalOnly:
width = screen["width"] / factor
height = currentPos.heightp
y = currentPos.yp
if verticallyOnly:
width = currentPos.widthp
height = screen["height"] / factor
if position == 'w':
y = screen["y"]
x = screen["x"]
width = screen["width"] / factor
height = screen["height"]
if position == 'nw':
y = screen["y"]
x = screen["x"]
if not horizontalOnly and not verticallyOnly:
width = screen["width"] / factor
height = screen["height"] / factor
if horizontalOnly:
width = screen["width"] / factor
height = currentPos.heightp
if verticallyOnly:
width = currentPos.widthp
height = screen["height"] / factor
if position == 'center':
# active.maximize()
# sys.exit(0)
width = screen["width"] / factor
height = screen["height"]
y = screen["y"]
x = ((screen["x"] + (screen["width"] / 2)) - width / 2)
return (x, y, width, height, gravity)
def get_window_id(window):
# read the id of the window
xid = window.get_xid()
return xid
if autoDiscoverScreens:
# read screens and their config from gtk
screens = discoverScreens()
# taskbar on screen 1
screens[0]["height"] = screens[0]["height"] - 32
screen = Wnck.Screen.get_default()
# while gtk.events_pending(): #do something magic
# gtk.main_iteration()
# read parameters
args = read_args()
verbose = args.verbose
factor = args.factor
position = args.position
stateful = args.stateful
factors = []
for elem in args.myfactor.split(","):
factors.append(float(elem))
# get current window
screen.force_update() # recommended by documenation
active = screen.get_active_window()
# read dimensions of window
currentPos = (winX, winY, winW, winH) = active.get_geometry()
# find out the screen the active window lives on
screen = findBounds(max(0, winX), max(0, winY), winW, winH)
flags = Wnck.WindowMoveResizeMask.X | \
Wnck.WindowMoveResizeMask.Y | \
Wnck.WindowMoveResizeMask.WIDTH | \
Wnck.WindowMoveResizeMask.HEIGHT
active.unmaximize()
#
# Note: set_geometry position seems to works relative to current window
#
geometryRaw = active.get_client_window_geometry()
if verbose:
print("Geometry raw:" + str(geometryRaw))
print("current Pos: " + str(currentPos))
if (stateful):
if verbose:
print("Decide next scale factor" + storageFile)
data = {}
if os.path.isfile(storageFile):
with open(storageFile) as json_data:
data = json.load(json_data)
id = get_window_id(active)
if str(id) in data:
currentFactor = float(data[str(id)])
else:
currentFactor = 1
if currentFactor in factors:
idx = factors.index(currentFactor)
else:
idx = 0
# now we select the index of the next scale factors
idx = (idx + 1) % len(factors)
# and now lookup the next factor
newFactor = factors[idx]
if verbose:
print(" Next scale-factor for window with id:" + str(id) + " is:" + str(newFactor))
# and remember the factor
data[str(id)] = str(newFactor)
# store info
if verbose:
print(" Write state to file:" + str(storageFile))
with open(storageFile, 'w') as outfile:
json.dump(data, outfile)
if verbose:
print("factor" + str(factor))
print(position)
print(screen)
newPos = calcNewPos(screen, position, newFactor, geometryRaw, currentPos, args.vert, args.hori)
else:
newPos = calcNewPos(screen, position, factor, geometryRaw, currentPos, args.vert, args.hori)
if verbose:
print("New Geometry calculated " + str(newPos))
# find difference of window decoration and take it into the calculation
# find difference of window decoration and use it for calculation
correctureY = geometryRaw[1] - currentPos[1]
correctureX = geometryRaw[0] - currentPos[0]
active.set_geometry(gravity=newPos[4],
geometry_mask=flags,
x=int(newPos[0] - correctureX),
y=int(newPos[1] - correctureY),
width=int(newPos[2]),
height=int(newPos[3]))
if args.mouse:
# keep the active window in foreground otherwise mouse might focus an other window
Wnck.Window.make_above(active) # - always on top on
Wnck.Window.unmake_above(active) # - always on top off
placeMouseOver((int(newPos[0] - correctureX), int(newPos[1] - correctureY), int(newPos[2]), int(newPos[3])))