-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactive_screen.py
executable file
·223 lines (186 loc) · 7.08 KB
/
active_screen.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
#!/usr/bin/env python
import wnck
import gtk
import keybinder
import appindicator
#import pdb
class WindowState(object):
_windows = {}
def set_windowstate(self, window, newstate):
#print newstate
self._windows[window] = newstate
def get_windowstate(self, window):
try:
state = self._windows[window]
except KeyError:
state = None
return state
class Productivity(object):
def active_window(self, windowlist):
for window in windowlist:
if window.is_active():
return window
return False
def move_right(self, window):
if window.is_maximized():
window.unmaximize()
new_width = self.width1 / 2
new_height = self.height1 / 2
newY = 0
if self._window_screen == 1 and self.ws.get_windowstate(window.get_xid()) == 'div-right-scr-1':
self._window_screen = 2
self.move_left(window)
return
if self._window_screen == 1:
new_width = self.width1 / 2
new_height = self.height1
newX = new_width
self.ws.set_windowstate(window.get_xid(), 'div-right-scr-1')
else:
new_width = self.width2 / 2
new_height = self.height2
newX = new_width + self.width1
self.ws.set_windowstate(window.get_xid(), 'div-right-scr-2')
window.set_geometry(0, 255, newX, newY, new_width, new_height)
def move_left(self, window):
if window.is_maximized():
window.unmaximize()
if self._window_screen == 2 and self.ws.get_windowstate(window.get_xid()) == 'div-left-scr-2':
self._window_screen = 1
self.move_right(window)
return
if self._window_screen == 1:
new_width = self.width1 / 2
new_height = self.height1
newX = 0
newY = 0
self.ws.set_windowstate(window.get_xid(), 'div-left-scr-1')
else:
new_width = self.width2 / 2
new_height = self.height2
newX = self.width1
newY = 0
self.ws.set_windowstate(window.get_xid(), 'div-left-scr-2')
window.set_geometry(0, 255, newX, newY, new_width, new_height)
def move_down(self, window):
if window.is_maximized():
window.unmaximize()
else:
if self._window_screen == 1:
new_width = self.width1
new_height = self.height1 / 2
newX = 0
newY = new_height
self.ws.set_windowstate(window.get_xid(), 'div-down-scr-2')
else:
new_width = self.width2
new_height = self.height2 / 2
newX = new_height + self.width1
newY = new_height
self.ws.set_windowstate(window.get_xid(), 'div-down-scr-2')
window.set_geometry(0, 255, newX, newY, new_width, new_height)
def move_up(self, window):
msg = ('div-down-scr-%s' % (str(self._window_screen)),)
if self.ws.get_windowstate(window.get_xid()) in msg:
if self._window_screen == 1:
new_width = self.width1
new_height = self.height1 / 2
newX = 0
newY = 0
self.ws.set_windowstate(window.get_xid(), 'div-up-scr-1')
else:
new_width = self.width2
new_height = self.height2 / 2
newX = self.width1
newY = new_height
self.ws.set_windowstate(window.get_xid(), 'div-up-scr-2')
window.set_geometry(0, 255, newX, newY, new_width, new_height)
else:
window.maximize()
def detect_screens(self):
import commands
(exitcode, commandoutput) = commands.getstatusoutput("xrandr | grep ' connected'")
windows = {}
incr = 1
#FUGLY! REFACTOR CANDIDATE!
for entry in commandoutput.split("\n"):
windows[incr] = {}
first_occ = entry.split('x', 1)
lst_of_words = first_occ[0].split(' ')
for word in lst_of_words:
if word.isdigit():
windows[incr]['width'] = int(word)
windows[incr]['height'] = int(first_occ[1].split('+')[0])
incr = incr + 1
return windows
def __init__(self):
screens = self.detect_screens()
for i in range(0, len(screens)):
n = i + 1
name = 'screen%s_dimension' % (str(n))
scr_w = 'width%s' % (str(n))
scr_h = 'height%s' % (str(n))
# magic to create: screenX_dimension (width, height) tuples
setattr(self, name, (screens[n]['width'], screens[n]['height']))
setattr(self, scr_w, screens[n]['width'])
setattr(self, scr_h, screens[n]['height'])
self._window_screen = 0
self.screen = wnck.screen_get_default()
self.ws = WindowState()
def silent_shutdown(self):
self.screen = None
if 'wnck_shutdown' in dir(wnck):
wnck.wnck_shutdown()
def main(self, direction):
self.screen.force_update()
windows = self.screen.get_windows()
if len(windows) == 0:
#print "No windows found"
return
window = self.active_window(windows)
if window is False:
#print 'Couldnt find active window in: %s' % windows
return
#state = self.ws.get_windowstate(window.get_xid())
# screen 1 or 2?
(wX, wY, wWidth, wHeight) = window.get_client_window_geometry()
#print "Window now:\nx: %s, y: %s, width: %s, height: %s" % (wX, wY, wWidth, wHeight)
if wX < self.width1:
#print 'Active window is on screen 1'
self._window_screen = 1
else:
#print 'Active window is on screen 2'
self._window_screen = 2
move_method = getattr(self, 'move_' + direction)
#print 'Moving window to: %s' % direction
move_method(window)
if __name__ == '__main__':
MagicKey = '<Super><Alt>'
x = Productivity()
#x.init()
keystr_right = MagicKey + "Right"
keystr_left = MagicKey + "Left"
keystr_left = MagicKey + "Down"
keystr_left = MagicKey + "Up"
keybinder.bind(keystr_right, x.main, "right")
keybinder.bind(keystr_left, x.main, "left")
keybinder.bind(keystr_left, x.main, "down")
keybinder.bind(keystr_left, x.main, "up")
status_bar = appindicator.Indicator("Some indicator",
"Message?",
appindicator.CATEGORY_APPLICATION_STATUS)
status_bar.set_status(appindicator.STATUS_ACTIVE)
status_bar.set_attention_icon("indicator-messages-new")
some_menu = gtk.Menu()
for i in range(3):
rnd_txt = "Test menu item #%s" % i
some_element = gtk.MenuItem(rnd_txt)
#some_element.connect("activate", callback, rnd_txt)
some_menu.append(some_element)
some_element.show()
status_bar.set_menu(some_menu)
try:
gtk.main()
except KeyboardInterrupt, e:
x.silent_shutdown()
exit()