-
Notifications
You must be signed in to change notification settings - Fork 1
/
stiler.py
executable file
·385 lines (309 loc) · 10.1 KB
/
stiler.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
#!/usr/bin/python
############################################################################
# Copyright (c) 2009 unohu <[email protected]> #
# #
# Permission to use, copy, modify, and/or distribute this software for any #
# purpose with or without fee is hereby granted, provided that the above #
# copyright notice and this permission notice appear in all copies. #
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES #
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF #
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES #
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN #
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF #
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #
# #
############################################################################
import sys
import os
import commands
import pickle
import ConfigParser
def initconfig():
rcfile=os.getenv('HOME')+"/.stilerrc"
if not os.path.exists(rcfile):
cfg=open(rcfile,'w')
cfg.write("""#Tweak these values
[default]
BottomPadding = 0
TopPadding = 0
LeftPadding = 0
RightPadding = 0
WinTitle = 21
WinBorder = 1
MwFactor = 0.65
TempFile = /tmp/tile_winlist
""")
cfg.close()
config=ConfigParser.RawConfigParser()
config.read(rcfile)
return config
def get_screen_size():
s = commands.getoutput('''xdpyinfo | grep 'dimension' | awk -F: '{ print $2 }' | awk '{ print $1 }' ''')
(x,y) = s.split('x')
return (int(x), int(y))
(resx, resy) = get_screen_size()
def initialize():
desk_output = commands.getoutput("wmctrl -d").split("\n")
desk_list = [line.split()[0] for line in desk_output]
current = filter(lambda x: x.split()[1] == "*" , desk_output)[0].split()
desktop = current[0]
width = current[8].split("x")[0]
height = current[8].split("x")[1]
orig_x = current[7].split(",")[0]
orig_y = current[7].split(",")[1]
(resx, resy) = get_screen_size()
win_output = commands.getoutput("wmctrl -lG").split("\n")
win_list = {}
win_filtered = []
for win in win_output:
w = win.split()
x = int(w[2])
y = int(w[3])
if x < 0 or x >= resx:
continue
if y < 0 or y >= resy:
continue
if w[7] == '<unknown>':
continue
if w[6] == 'N/A':
continue
if w[7] == 'x-nautilus-desktop':
continue
win_filtered.append(win)
for desk in desk_list:
win_list[desk] = map(lambda y: hex(int(y.split()[0],16)) , filter(lambda x: x.split()[1] == desk, win_filtered ))
return (desktop,orig_x,orig_y,width,height,win_list)
def get_active_window():
return str(hex(int(commands.getoutput("xdotool getactivewindow 2>/dev/null").split()[0])))
def store(object,file):
with open(file, 'w') as f:
pickle.dump(object,f)
f.close()
def retrieve(file):
try:
with open(file,'r+') as f:
obj = pickle.load(f)
f.close()
return(obj)
except:
f = open(file,'w')
f.close
dict = {}
return (dict)
# Get all global variables
Config = initconfig()
BottomPadding = Config.getint("default","BottomPadding")
TopPadding = Config.getint("default","TopPadding")
LeftPadding = Config.getint("default","LeftPadding")
RightPadding = Config.getint("default","RightPadding")
WinTitle = Config.getint("default","WinTitle")
WinBorder = Config.getint("default","WinBorder")
MwFactor = Config.getfloat("default","MwFactor")
TempFile = Config.get("default","TempFile")
(Desktop,OrigXstr,OrigYstr,MaxWidthStr,MaxHeightStr,WinList) = initialize()
MaxWidth = int(MaxWidthStr) - LeftPadding - RightPadding
MaxHeight = int(MaxHeightStr) - TopPadding - BottomPadding
OrigX = int(OrigXstr) + LeftPadding
OrigY = int(OrigYstr) + TopPadding
OldWinList = retrieve(TempFile)
def get_simple_tile(wincount):
rows = wincount - 1
layout = []
if rows == 0:
layout.append((OrigX,OrigY,MaxWidth,MaxHeight-WinTitle-WinBorder))
return layout
else:
layout.append((OrigX,OrigY,int(MaxWidth*MwFactor),MaxHeight-WinTitle-WinBorder))
x=OrigX + int((MaxWidth*MwFactor)+(2*WinBorder))
width=int((MaxWidth*(1-MwFactor))-2*WinBorder)
height=int(MaxHeight/rows - WinTitle-WinBorder)
for n in range(0,rows):
y= OrigY+int((MaxHeight/rows)*(n))
layout.append((x,y,width,height))
return layout
def get_vertical_tile(wincount):
layout = []
y = OrigY
width = int(MaxWidth/wincount)
height = MaxHeight - WinTitle - WinBorder
for n in range(0,wincount):
x= OrigX + n * width
layout.append((x,y,width,height))
return layout
def get_horiz_tile(wincount):
layout = []
x = OrigX
height = int(MaxHeight/wincount - WinTitle - WinBorder)
width = MaxWidth
for n in range(0,wincount):
y= OrigY + int((MaxHeight/wincount)*(n))
layout.append((x,y,width,height))
return layout
def get_max_all(wincount):
layout = []
x = OrigX
y = OrigY
height = MaxHeight - WinTitle - WinBorder
width = MaxWidth
for n in range(0,wincount):
layout.append((x,y,width,height))
return layout
def move_active(PosX,PosY,Width,Height):
command = " wmctrl -r :ACTIVE: -e 0," + str(PosX) + "," + str(PosY)+ "," + str(Width) + "," + str(Height)
os.system(command)
def unmaximize_one(windowid):
command = " wmctrl -i -r " + windowid + " -bremove,maximized_vert,maximized_horz"
os.system(command)
def maximize_one(windowid):
command = " wmctrl -i -r " + windowid + " -badd,maximized_vert,maximized_horz"
os.system(command)
def move_window(windowid,PosX,PosY,Width,Height):
# Unmaximize window
unmaximize_one(windowid)
# Now move it
command = " wmctrl -i -r " + windowid + " -e 0," + str(PosX) + "," + str(PosY)+ "," + str(Width) + "," + str(Height)
os.system(command)
command = "wmctrl -i -r " + windowid + " -b remove,hidden,shaded"
os.system(command)
def raise_window(windowid):
if windowid == ":ACTIVE:":
command = "wmctrl -a :ACTIVE: "
else:
command = "wmctrl -i -a " + windowid
os.system(command)
def left():
Width=MaxWidth/2-1
Height=MaxHeight - WinTitle -WinBorder
PosX=LeftPadding
PosY=TopPadding
move_active(PosX,PosY,Width,Height)
raise_window(":ACTIVE:")
def right():
Width=MaxWidth/2-1
Height=MaxHeight - WinTitle - WinBorder
PosX=MaxWidth/2
PosY=TopPadding
move_active(PosX,PosY,Width,Height)
raise_window(":ACTIVE:")
def compare_win_list(newlist,oldlist):
templist = []
for window in oldlist:
if newlist.count(window) != 0:
templist.append(window)
for window in newlist:
if oldlist.count(window) == 0:
templist.append(window)
return templist
def create_win_list():
Windows = WinList[Desktop]
if OldWinList == {}:
pass
else:
OldWindows = OldWinList[Desktop]
if Windows == OldWindows:
pass
else:
Windows = compare_win_list(Windows,OldWindows)
return Windows
def arrange(layout,windows):
for win , lay in zip(windows,layout):
move_window(win,lay[0],lay[1],lay[2],lay[3])
WinList[Desktop]=windows
store(WinList,TempFile)
def simple():
Windows = create_win_list()
arrange(get_simple_tile(len(Windows)),Windows)
def swap():
winlist = create_win_list()
if len(winlist) == 1:
# only one window, maximize it
maximize_active()
return
active = get_active_window()
winlist.remove(active)
winlist.insert(0,active)
arrange(get_simple_tile(len(winlist)),winlist)
def vertical():
winlist = create_win_list()
active = get_active_window()
winlist.remove(active)
winlist.insert(0,active)
arrange(get_vertical_tile(len(winlist)),winlist)
def horiz():
winlist = create_win_list()
active = get_active_window()
winlist.remove(active)
winlist.insert(0,active)
arrange(get_horiz_tile(len(winlist)),winlist)
def cycle():
winlist = create_win_list()
winlist.insert(0,winlist[len(winlist)-1])
winlist = winlist[:-1]
arrange(get_simple_tile(len(winlist)),winlist)
def focus_next():
Windows = create_win_list()
# we need to find which window is the active one
# then pick the next one
# for now just pick the second window always
active = get_active_window()
found = 0
for win in Windows:
if found == 1:
raise_window(win)
return
if win == active:
found = 1
raise_window(Windows[0])
def focus_prev():
Windows = create_win_list()
active = get_active_window()
prev = Windows[len(Windows)-1]
for win in Windows:
if win == active:
raise_window(prev)
prev = win
def maximize_active():
os.system("wmctrl -r :ACTIVE: -badd,maximized_vert,maximized_horz")
def maximize():
Width=MaxWidth
Height=MaxHeight - WinTitle -WinBorder
PosX=LeftPadding
PosY=TopPadding
move_active(PosX,PosY,Width,Height)
raise_window(":ACTIVE:")
def old_max_all():
winlist = create_win_list()
active = get_active_window()
winlist.remove(active)
winlist.insert(0,active)
arrange(get_max_all(len(winlist)),winlist)
# new max_all(), use real 'maximize'
# this way unity can remove the title bar
def max_all():
winlist = create_win_list()
for win in winlist:
maximize_one(win)
if sys.argv[1] == "left":
left()
elif sys.argv[1] == "right":
right()
elif sys.argv[1] == "simple":
simple()
elif sys.argv[1] == "vertical":
vertical()
elif sys.argv[1] == "horizontal":
horiz()
elif sys.argv[1] == "swap":
swap()
elif sys.argv[1] == "cycle":
cycle()
elif sys.argv[1] == "maximize":
maximize()
elif sys.argv[1] == "next":
focus_next()
elif sys.argv[1] == "prev":
focus_prev()
elif sys.argv[1] == "max_all":
max_all()