-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-gnome-desktop-session.py
executable file
·254 lines (231 loc) · 8.35 KB
/
restore-gnome-desktop-session.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
#!/usr/bin/python
#
# `restore-gnome-desktop-session.py`
#
# This script is intended to restore the virtual desktop(s) as saved in the
# session configuration file (default: `~/.gnome-desktop-session.json`).
#
# Author: Karl Wilbur <[email protected]>
#
#
import json as simplejson;
import argparse, time, subprocess, sys;
try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
__args = []
__session_data = ''
__displays = []
__workspaces = []
def args():
global __args
if not __args:
__args = parse_invocation_args()
return __args
def center_on_display(width, height, display):
if args().verbose > 0:
print('center_on_display()')
d_left = int(display.get('left'))
d_right = int(display.get('right'))
left = (abs(d_right - d_left - int(width)) / 2) + d_left
d_top = int(display.get('top'))
d_bottom = int(display.get('bottom'))
if args().verbose > 2:
print('display dimensions (left, top, right, bottom): '+str(d_left)+','+str(d_top)+','+str(d_right)+','+str(d_bottom))
top = (abs(d_bottom - d_top - int(height)) / 2) + d_top
if args().verbose > 2:
print('window dimensions (width, height): '+str(width)+','+str(height))
if args().verbose > 2:
print('window position (top, left): '+str(top)+','+str(left))
return [top, left]
def displays():
global __displays
if not __displays:
__displays = session_data()['displays']
return __displays
def execute_process(cmd):
if args().verbose > 0:
print('execute_process(' + ' '.join(cmd) + ')')
# return subprocess.Popen(cmd)
return subprocess.Popen(cmd, stdout=DEVNULL, stderr=subprocess.STDOUT)
def find_new_windows(current_windows):
if args().verbose > 1:
print('find_new_windows()')
new_windows = get_current_window_list()
return list_diff(new_windows, current_windows)
def get_current_window_list():
if args().verbose > 1:
print('get_current_window_list()')
proc_output = get_process_output(['wmctrl', '-l'])
current_windows = []
for w in proc_output.splitlines():
window_id = str(w.split()[0])
if args().verbose > 3:
print('window: ' + window_id)
current_windows.append(window_id)
return current_windows
def get_display_by_position(position):
for display in displays():
if display.get('position') == position:
return display
def get_process_output(cmd):
if args().verbose > 0:
print('get_process_output(' + ' '.join(cmd) + ')')
try:
return subprocess.check_output(cmd).decode('utf-8').strip()
except (subprocess.CalledProcessError, TypeError):
pass
def get_window_geometry(window_id, app):
if args().verbose > 1:
print('get_window_geometry() for window_id '+window_id)
# window = get_process_output(['wmctrl', '-lG', '|', 'grep', window_id])
proc_output = get_process_output(['wmctrl', '-lG'])
if args().verbose > 2:
print( u' '.join(('Raw proc_output: ', proc_output)).encode('utf-8').strip())
for line in proc_output.splitlines():
if line.find(window_id) == 0:
window = line
if args().verbose > 2:
print( u' '.join(('Process window: ', window)).encode('utf-8').strip())
window = window.split()
if args().verbose > 2:
print('Windows gemometry (left,top,width,height): '+window[2]+','+window[3]+','+window[4]+','+window[5])
gravity = 0
left = int(window[2])
top = int(window[3])
width = int(window[4])
height = int(window[5])
if app.get('size'):
width, height = app.get('size').split('x')
if app.get('position'):
left, top = app.get('position').split('x')
if app.get('display'):
display = get_display_by_position(app.get('display'))
if app.get('position'):
# If we have a display and position, set that position
# relative to that display
left = int(left) + int(display.get('left'))
top = int(top) + int(display.get('top'))
else:
# If no position, center on the display
top, left = center_on_display(width, height, display)
if args().verbose > 1:
print('positioning window (left,top,width,height): '+str(left)+','+str(top)+','+str(width)+','+str(height))
return [str(gravity), str(left), str(top), str(width), str(height)]
def list_diff(first, second):
global args
if args().verbose > 1:
print('list_diff()')
second = set(second)
return [item for item in first if item not in second]
def parse_invocation_args():
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
'--version',
action='version',
version='%(prog)s 0.1.0'
)
parser.add_argument(
'--verbose',
'-v',
help='increase message verbosity; stackable',
action='count'
)
parser.add_argument(
'-s',
'--session-file',
action='store',
nargs='?',
type=argparse.FileType('r'),
help='the session file from which to restore (default: session.json)',
default=os.path.expanduser('~/.gnome-desktop-session.json')
)
return parser.parse_args()
def session_data():
global __session_data
if not __session_data:
__session_data = simplejson.load(args().session_file)
return __session_data
def set_up_workspace(workspace, workspace_index):
if args().verbose > 0:
print('Workspace Name: ' + workspace['name'])
print('Workspace Index: {}'.format(workspace_index))
apps = workspace.get('apps')
if apps:
for app in apps:
cmd = app.get('command')
cmd_args = app.get('args', [])
app_command = app_command_with_args(app)
if args().verbose > 0:
print('App Command: ' + app_command)
current_windows = get_current_window_list()
execute_process( [cmd] + cmd_args )
new_windows = wait_for_new_windows(current_windows,app)
if len(new_windows) > 1:
print('Multiple windows found for command.')
print(' command: ' + app_command)
print(' windows: {}'.format(new_windows))
for w in new_windows:
if args().verbose > 1:
print('Window ID: ' + w)
# Set attributes
execute_process(['wmctrl', '-i', '-r', w, '-b', ('remove','add')[app.get('sticky', False)] + ',sticky'])
execute_process(['wmctrl', '-i', '-r', w, '-b', ('remove','add')[app.get('maximized', False)] + ',maximized_vert,maximized_horz'])
execute_process(['wmctrl', '-i', '-r', w, '-b', ('remove','add')[app.get('fullscreen', False)] + ',fullscreen'])
# Set position
if app.get('position') or app.get('size') or app.get('display'):
execute_process(['wmctrl', '-i', '-r', w, '-e', ','.join(get_window_geometry(w,app))])
# Move to correct workspace
execute_process(['wmctrl', '-i', '-r', w, '-t', str(workspace_index)])
if args().verbose > 1:
print('')
if args().verbose > 0:
print('')
def app_command_with_args(app):
return app.get('command') + ' ' + ' '.join(app.get('args', []))
def wait_for_new_windows(current_windows,app):
if args().verbose > 1:
print('wait_for_new_windows()')
print('Current windows: {}'.format(current_windows))
new_windows = []
sleep_cycles = 0
if app.get('run_in_background'):
if args().verbose > 1:
print('App runs in background, no new windows expected.')
return new_windows # No new windows for this app
time.sleep(app.get('startup_delay',4)) # wait for window to open
new_windows = find_new_windows(current_windows)
while len(new_windows) == 0:
sleep_cycles += 1
if sleep_cycles >= 5:
print('Max sleep cycles reaching waiting for new windows.')
print('Command: {}'.format(app_command_with_args(app)))
print('Current windows: {}'.format(current_windows))
print('New windows: {}'.format(new_windows))
sys.exit()
if args().verbose > 1:
print('starting another sleep cycle, waiting for new windows')
time.sleep(2)
new_windows = find_new_windows(current_windows)
return new_windows
def workspaces():
global __workspaces
if not __workspaces:
__workspaces = session_data()['workspaces']
return __workspaces
### MAIN
# Process each defined workspace
print('Loading session from '+args().session_file.name)
# Wait for any other starting processes to finish before processing the session
time.sleep(10)
workspace_index = 0
for _index, workspace in enumerate(workspaces()):
workspace_index += 1
if workspace.get('disabled'):
continue
set_up_workspace(workspace, workspace_index)
# Switch back to the first workspace
subprocess.Popen(['wmctrl', '-s', '0'])
print('Done')