-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·189 lines (166 loc) · 7.13 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
#!/usr/bin/python3
from xcffib import connect, xproto
from xcffib.xproto import MotionNotifyEvent, ConfigWindow, GetPropertyType
from ewmh import EWMH
from sys import argv, exit
import pynput
from pynput.mouse import Button
from time import sleep
import cProfile
def get_mouse_position(mouse):
def on_move(x, y):
return False
with pynput.mouse.Listener(on_move=on_move) as listener:
listener.join()
return mouse.position
def map_to_virtual(mouse_position, original_position, screen_size,
virtual_size):
ratio = (virtual_size[0] / screen_size[0],
virtual_size[1] / screen_size[1])
size_difference = (screen_size[0] - virtual_size[0],
screen_size[1] - virtual_size[1])
mouse_delta = (mouse_position[0] - screen_size[0] / 2,
mouse_position[1] - screen_size[1] / 2)
return (-round(original_position[0] + mouse_delta[0] * ratio[0] +
size_difference[0]),
-round(original_position[1] + mouse_delta[1] * ratio[1] +
size_difference[1]))
def add_new_window(connection, window, root_id, window_data, mouse_position,
screen_size, virtual_size):
extents = get_frame_extents(connection, window.id, window_data)
geometry = get_window_geometry(connection, window.id, root_id, extents)
window_data[window.id] = {}
window_data[window.id]["original_position"] = map_to_virtual(
mouse_position, geometry[0], screen_size, virtual_size)
window_data[window.id]["types"] = ewmh.getWmWindowType(window, str=True)
window_data[window.id]["extents"] = extents
window_data[window.id]["position"] = geometry[0]
window_data[window.id]["size"] = geometry[1]
def move_windows(connection, mouse_position, windows, screen_size,
virtual_size, root_id, window_data):
global dragged_window
for window in windows:
if window.id not in window_data:
add_new_window(connection, window, root_id, window_data,
mouse_position, screen_size, virtual_size)
window_types = window_data[window.id]["types"]
if "_NET_WM_WINDOW_TYPE_NORMAL" in window_types:
extents = window_data[window.id]["extents"]
position = window_data[window.id]["position"]
size = window_data[window.id]["size"]
dragging_window = check_dragging_window(mouse_position, position,
size, extents, window.id)
if dragging_window:
dragged_window = window.id
else:
if window.id == dragged_window:
geometry = get_window_geometry(connection, window.id,
root_id, extents)
window_data[
window.id]["original_position"] = map_to_virtual(
mouse_position, geometry[0], screen_size,
virtual_size)
window_data[window.id]["position"] = geometry[0]
window_data[window.id]["size"] = geometry[1]
dragged_window = None
new_coords = map_to_virtual(
mouse_position,
window_data[window.id]["original_position"], screen_size,
virtual_size)
set_window_geometry(connection, window.id, new_coords[0],
new_coords[1])
window_data[window.id]["position"] = new_coords
def get_window_geometry(connection, window_id, root_id, extents):
a = connection.core.TranslateCoordinates(window_id, root_id, 0, 0).reply()
b = connection.core.GetGeometry(window_id).reply()
position = (a.dst_x - b.x, a.dst_y - b.y)
size = (b.width, b.height)
return (position, size)
def set_window_geometry(connection,
window_id,
x=None,
y=None,
width=None,
height=None):
properties = 0
values = []
if x is not None:
properties = properties | ConfigWindow.X
values.append(x)
if y is not None:
properties = properties | ConfigWindow.Y
values.append(y)
if width is not None:
properties = properties | ConfigWindow.Width
values.append(width)
if height is not None:
properties = properties | ConfigWindow.Height
values.append(height)
connection.core.ConfigureWindow(window_id, properties, values)
def get_frame_extents(connection, window_id, window_data):
_NET_FRAME_EXTENTS = connection.core.InternAtom(
True, len("_NET_FRAME_EXTENTS"), "_NET_FRAME_EXTENTS").reply().atom
extent_bytes = connection.core.GetProperty(False, window_id,
_NET_FRAME_EXTENTS,
GetPropertyType.Any, 0,
2**32 - 1).reply().value
results = []
for i in range(4):
results.append(
int.from_bytes(b''.join(extent_bytes[i * 4:i * 4 + 1]), "little"))
extents = results
return results
def handle_click(x, y, button, pressed):
global lmb_pressed
if button == Button.left:
lmb_pressed = pressed
def cursor_on_titlebar(mouse_position, position, size, extents):
result = position[0] <= mouse_position[0] <= position[0] + size[
0] and position[1] <= mouse_position[1] <= position[1] + extents[2]
return result
def check_dragging_window(mouse_position, position, size, extents, window_id):
global dragged_window
global lmb_pressed
result = cursor_on_titlebar(
mouse_position, position, size,
extents) and lmb_pressed or window_id == dragged_window and lmb_pressed
return result
ewmh = EWMH()
connection = connect()
root_id = connection.setup.roots[0].root
mouse = pynput.mouse.Controller()
screen_geometry = connection.core.GetGeometry(
ewmh.display.screen().root.id).reply()
screen_size = (screen_geometry.width, screen_geometry.height)
screen_center = (round(screen_size[0] / 2), round(screen_size[1] / 2))
mouse.position = screen_center
lmb_pressed = None
mouse_listener = pynput.mouse.Listener(on_click=handle_click)
mouse_listener.start()
virtual_size = (int(argv[-2]), int(argv[-1]))
original_positions = {}
windows = None
while windows is None:
try:
windows = ewmh.getClientList()
break
except TypeError:
continue
mouse_position = mouse.position
last_position = mouse_position
window_data = {}
for window in windows:
add_new_window(connection, window, root_id, window_data, mouse_position,
screen_size, virtual_size)
dragged_window = None
extents = None
while True:
sleep(1 / 60)
mouse_position = connection.core.QueryPointer(root_id).reply()
mouse_position = (mouse_position.root_x, mouse_position.root_y)
if mouse_position == last_position:
continue
windows = ewmh.getClientList()
move_windows(connection, mouse_position, windows, screen_size,
virtual_size, root_id, window_data)
last_position = mouse_position