-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
231 lines (185 loc) · 7.63 KB
/
client.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
#
# Copyright (C) 2012 stephane duverger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from xcb.xproto import *
from utils import Geometry, change_property, debug, configure_window, map_window, stack_window, set_input_focus
import runtime
class Client:
def __init__(self, con, window, parent, workspace, geometry=None, ignored=False):
self.__con = con
self.id = window
self.parent = parent
self.__min_w = 20
self.__min_h = 20
if geometry is None:
self.geo_virt = Geometry(0,0, self.__min_w, self.__min_w, 1)
self.geo_want = Geometry(0,0, self.__min_w, self.__min_w, 1)
else:
self.geo_virt = geometry.copy()
self.geo_want = geometry.copy()
self.geo_unmax = None
self.border_color = runtime.passive_color
self.tiled = False
if ignored:
self.never_tiled = False
else:
self.never_tiled = True
self.__set_workspace(workspace)
self.__setup()
def __set_workspace(self, workspace):
self.workspace = workspace
change_property(PropMode.Replace, self.id, runtime.fp_wm_atoms["_FP_WM_WORKSPACE"], Atom.STRING, 8,
len(self.workspace.name), self.workspace.name)
def __setup(self):
mask = EventMask.EnterWindow|EventMask.PropertyChange|EventMask.FocusChange
self.__con.core.ChangeWindowAttributes(self.id, CW.BorderPixel|CW.EventMask, [self.border_color,mask])
def relative_geometry(self):
return self.geo_virt.copy()
def absolute_geometry(self):
return Geometry(self.geo_virt.x+self.workspace.screen.x, self.geo_virt.y+self.workspace.screen.y,
self.geo_virt.w, self.geo_virt.h, self.geo_virt.b)
def located_into(self, workspace):
geo_abs = self.absolute_geometry()
screen = workspace.screen
if geo_abs.x >= screen.x and geo_abs.x < screen.x+screen.width:
return True
return False
def move(self, dx, dy):
self.geo_virt.x += dx
self.geo_virt.y += dy
self.real_configure_notify()
def resize(self, up, left, dx, dy):
if up:
my = min(dy, self.geo_virt.h - self.__min_h)
ry = -my
else:
my = 0
ry = max(dy, self.__min_h - self.geo_virt.h)
if left:
mx = min(dx, self.geo_virt.w - self.__min_w)
rx = -mx
else:
mx = 0
rx = max(dx, self.__min_w - self.geo_virt.w)
if rx == 0 and ry == 0:
return
if mx != 0 or my != 0:
self.move(mx, my)
self.geo_virt.w += rx
self.geo_virt.h += ry
self.real_configure_notify()
def focus(self, ignore_follow=False):
self.border_color = runtime.focused_color
self.update_border_color()
set_input_focus(self.id, self.geo_virt, ignore_follow)
def unfocus(self):
self.border_color = runtime.passive_color
self.update_border_color()
def update_border_color(self):
self.__con.core.ChangeWindowAttributes(self.id, CW.BorderPixel, [self.border_color])
def reparent(self, who, wm_state):
self.parent = who
if self.workspace.screen is None:
geo = self.geo_virt
else:
geo = self.absolute_geometry()
self.__con.core.ReparentWindow(self.id, self.parent, geo.x, geo.y)
change_property(PropMode.Replace, self.id, runtime.wm_atoms["WM_STATE"], Atom.CARDINAL, 32, 1, wm_state)
def map(self):
map_window(self.id, 1)
def tile(self):
if self.never_tiled:
self.never_tiled = False
if not self.tiled:
self.tiled = True
self.stack_below()
def untile(self):
if self.tiled:
self.tiled = False
def __stack(self, how):
stack_window(self.id, how)
def stack_above(self):
self.__stack(StackMode.Above)
def stack_below(self):
self.__stack(StackMode.Below)
def toggle_maximize(self):
if self.geo_unmax is None:
self.maximize()
else:
self.unmaximize()
self.real_configure_notify()
def maximize(self):
self.geo_unmax = self.relative_geometry()
self.geo_virt.x = 0
self.geo_virt.y = 0
self.geo_virt.w = self.workspace.screen.width - 2*self.geo_virt.b
self.geo_virt.h = self.workspace.screen.height - 2*self.geo_virt.b
self.stack_above()
def unmaximize(self):
self.geo_virt.x = self.geo_unmax.x
self.geo_virt.y = self.geo_unmax.y
self.geo_virt.w = self.geo_unmax.w
self.geo_virt.h = self.geo_unmax.h
self.geo_unmax = None
if self.tiled:
self.stack_below()
def attach(self, workspace, teleport):
if not teleport:
geo_abs = self.absolute_geometry()
self.geo_virt.x = geo_abs.x - workspace.screen.x
self.geo_virt.y = geo_abs.y - workspace.screen.y
self.__set_workspace(workspace)
def send_config_window(self, geo):
configure_window(self.id, geo)
def real_configure_notify(self):
geo_abs = self.absolute_geometry()
if not self.tiled and self.workspace.screen.gap is not None:
if self.workspace.screen.gap.top:
geo_abs.y -= self.workspace.screen.gap.h
self.send_config_window(geo_abs)
def synthetic_configure_notify(self):
debug("s_configure: x %d y %d w %d h %d\n" % (self.geo_want.x, self.geo_want.y, self.geo_want.w, self.geo_want.h))
event = pack("=B3xIIIHHHHHBx", 22, self.id, self.id, 0,
self.geo_want.x, self.geo_want.y,
self.geo_want.w, self.geo_want.h, self.geo_want.b, 0)
self.__con.core.SendEvent(False, self.id, EventMask.StructureNotify, event)
def moveresize(self):
if self.geo_want.x != self.geo_virt.x:
self.geo_virt.x = self.geo_want.x
if self.geo_want.y != self.geo_virt.y:
self.geo_virt.y = self.geo_want.y
if self.geo_want.w != self.geo_virt.w:
self.geo_virt.w = self.geo_want.w
if self.geo_want.h != self.geo_virt.h:
self.geo_virt.h = self.geo_want.h
self.real_configure_notify()
def configure(self, event):
if not self.never_tiled:
return
if event.value_mask & ConfigWindow.X:
self.geo_want.x = event.x
if event.value_mask & ConfigWindow.Y:
self.geo_want.y = event.y
if event.value_mask & ConfigWindow.Width:
self.geo_want.w = event.width
if event.value_mask & ConfigWindow.Height:
self.geo_want.h = event.height
if event.value_mask & ConfigWindow.BorderWidth:
self.geo_want.b = event.border_width
if not self.tiled and event.value_mask & (ConfigWindow.X|ConfigWindow.Y|ConfigWindow.Width|ConfigWindow.Height):
return self.moveresize()
return self.synthetic_configure_notify()