This file is indexed.

/usr/share/gnome-shell/extensions/pixel-saver@deadalnix.me/decoration.js is in gnome-shell-extension-pixelsaver 1.10+git20161217-49f47bf-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
385
386
387
388
389
390
391
392
393
394
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop;
const Meta = imports.gi.Meta;
const Util = imports.misc.util;

function LOG(message) {
	// log("[pixel-saver]: " + message);
}

function WARN(message) {
	log("[pixel-saver]: " + message);
}

/**
 * Guesses the X ID of a window.
 *
 * It is often in the window's title, being `"0x%x %10s".format(XID, window.title)`.
 * (See `mutter/src/core/window-props.c`).
 *
 * If we couldn't find it there, we use `win`'s actor, `win.get_compositor_private()`.
 * The actor's `x-window` property is the X ID of the window *actor*'s frame
 * (as opposed to the window itself).
 *
 * However, the child window of the window actor is the window itself, so by
 * using `xwininfo -children -id [actor's XID]` we can attempt to deduce the
 * window's X ID.
 *
 * It is not always foolproof, but works good enough for now.
 *
 * @param {Meta.Window} win - the window to guess the XID of. You wil get better
 * success if the window's actor (`win.get_compositor_private()`) exists.
 */
function guessWindowXID(win) {
	// We cache the result so we don't need to redetect.
	if (win._pixelSaverWindowID) {
		return win._pixelSaverWindowID;
	}
	
	/**
	 * If window title has non-utf8 characters, get_description() complains
	 * "Failed to convert UTF-8 string to JS string: Invalid byte sequence in conversion input",
	 * event though get_title() works.
	 */
	try {
		let m = win.get_description().match(/0x[0-9a-f]+/);
		if (m && m[0]) {
			return win._pixelSaverWindowID = m[0];
		}
	} catch (err) { }
	
	// use xwininfo, take first child.
	let act = win.get_compositor_private();
	let xwindow = act && act['x-window'];
	if (xwindow) {
		let xwininfo = GLib.spawn_command_line_sync('xwininfo -children -id 0x%x'.format(xwindow));
		if (xwininfo[0]) {
			let str = xwininfo[1].toString();
			
			/**
			 * The X ID of the window is the one preceding the target window's title.
			 * This is to handle cases where the window has no frame and so
			 * act['x-window'] is actually the X ID we want, not the child.
			 */
			let regexp = new RegExp('(0x[0-9a-f]+) +"%s"'.format(win.title));
			let m = str.match(regexp);
			if (m && m[1]) {
				return win._pixelSaverWindowID = m[1];
			}
			
			// Otherwise, just grab the child and hope for the best
			m = str.split(/child(?:ren)?:/)[1].match(/0x[0-9a-f]+/);
			if (m && m[0]) {
				return win._pixelSaverWindowID = m[0];
			}
		}
	}
	
	// Try enumerating all available windows and match the title. Note that this
	// may be necessary if the title contains special characters and `x-window`
	// is not available.
	let result = GLib.spawn_command_line_sync('xprop -root _NET_CLIENT_LIST');
	LOG('xprop -root _NET_CLIENT_LIST')
	if (result[0]) {
		let str = result[1].toString();
		
		// Get the list of window IDs.
		let windowList = str.match(/0x[0-9a-f]+/g);
		
		// For each window ID, check if the title matches the desired title.
		for (var i = 0; i < windowList.length; ++i) {
			let cmd = 'xprop -id "' + windowList[i] + '" _NET_WM_NAME _PIXEL_SAVER_ORIGINAL_STATE';
			let result = GLib.spawn_command_line_sync(cmd);
			LOG(cmd);
			
			if (result[0]) {
				let output = result[1].toString();
				let isManaged = output.indexOf("_PIXEL_SAVER_ORIGINAL_STATE(CARDINAL)") > -1;
				if (isManaged) {
					continue;
				}

				let title = output.match(/_NET_WM_NAME(\(\w+\))? = "(([^\\"]|\\"|\\\\)*)"/);
				LOG("Title of XID %s is \"%s\".".format(windowList[i], title[2]));

				// Is this our guy?
				if (title && title[2] == win.title) {
					return windowList[i];
				}
			}
		}
	}

	// debugging for when people find bugs..
	WARN("Could not find XID for window with title %s".format(win.title));
	return null;
}

const WindowState = {
	DEFAULT: 'default',
	HIDE_TITLEBAR: 'hide_titlebar',
	UNDECORATED: 'undecorated',
	UNKNOWN: 'unknown'
}

/**
 * Get the value of _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED before
 * pixel saver did its magic.
 * 
 * @param {Meta.Window} win - the window to check the property
 */
function getOriginalState(win) {
	if (win._pixelSaverOriginalState !== undefined) {
		return win._pixelSaverOriginalState;
	}
	
	if (!win.decorated) {
		return win._pixelSaverOriginalState = WindowState.UNDECORATED;
	}
	
	let id = guessWindowXID(win);
	let cmd = 'xprop -id ' + id;
	LOG(cmd);
	
	let xprops = GLib.spawn_command_line_sync(cmd);
	if (!xprops[0]) {
		WARN("xprop failed for " + win.title + " with id " + id);
		return win._pixelSaverOriginalState = State.UNKNOWN;
	}
	
	let str = xprops[1].toString();
	let m = str.match(/^_PIXEL_SAVER_ORIGINAL_STATE\(CARDINAL\) = ([0-9]+)$/m);
	if (m) {
		return win._pixelSaverOriginalState = !!m[1]
			? WindowState.HIDE_TITLEBAR
			: WindowState.DEFAULT;
	}
	
	m = str.match(/^_GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED(\(CARDINAL\))? = ([0-9]+)$/m);
	if (m) {
		let state = !!m[1];
		cmd = ['xprop', '-id', id,
		      '-f', '_PIXEL_SAVER_ORIGINAL_STATE', '32c',
		      '-set', '_PIXEL_SAVER_ORIGINAL_STATE',
		      (state ? '0x1' : '0x0')];
		LOG(cmd.join(' '));
		Util.spawn(cmd);
		return win._pixelSaverOriginalState = state
			? WindowState.HIDE_TITLEBAR
			: WindowState.DEFAULT;
	}
	
	WARN("Can't find original state for " + win.title + " with id " + id);
	
	// GTK uses the _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED atom to indicate that the
	// title bar should be hidden when maximized. If we can't find this atom, the
	// window uses the default behavior
	return win._pixelSaverOriginalState = WindowState.DEFAULT;
}

/**
 * Tells the window manager to hide the titlebar on maximised windows.
 *
 * Does this by setting the _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED hint - means
 * I can do it once and forget about it, rather than tracking maximize/unmaximize
 * events.
 *
 * **Caveat**: doesn't work with Ubuntu's Ambiance and Radiance window themes -
 * my guess is they don't respect or implement this property.
 * 
 * I don't know how to read the inital value, so I'm not sure how to resore it.
 *
 * @param {Meta.Window} win - window to set the HIDE_TITLEBAR_WHEN_MAXIMIZED property of.
 * @param {boolean} hide - whether to hide the titlebar or not.
 */
function setHideTitlebar(win, hide) {
	LOG('setHideTitlebar: ' + win.get_title() + ': ' + hide);
	
	// Make sure we save the state before altering it.
	getOriginalState(win);
	
	/**
	 * Undecorate with xprop. Use _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED.
	 * See (eg) mutter/src/window-props.c
	 */
	let cmd = ['xprop', '-id', guessWindowXID(win),
	           '-f', '_GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED', '32c',
	           '-set', '_GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED',
	           (hide ? '0x1' : '0x0')];
	LOG(cmd.join(' '));
	
	// Run xprop
	[success, pid] = GLib.spawn_async(
		null,
		cmd,
		null,
		GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
		null);
	
	// After xprop completes, unmaximize and remaximize any window
	// that is already maximized. It seems that setting the xprop on
	// a window that is already maximized doesn't actually take
	// effect immediately but it needs a focuse change or other
	// action to force a relayout. Doing unmaximize and maximize
	// here seems to be an uninvasive way to handle this. This needs
	// to happen _after_ xprop completes.
	GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, function () {
		const MAXIMIZED = Meta.MaximizeFlags.BOTH;
		let flags = win.get_maximized();
		if (flags == MAXIMIZED) {
			win.unmaximize(MAXIMIZED);
			win.maximize(MAXIMIZED);
		}
	});
}

/**** Callbacks ****/
/**
 * Callback when a window is added in any of the workspaces.
 * This includes a window switching to another workspace.
 *
 * If it is a window we already know about, we do nothing.
 *
 * Otherwise, we activate the hide title on maximize feature.
 *
 * @param {Meta.Window} win - the window that was added.
 *
 * @see undecorate
 */
function onWindowAdded(ws, win, retry) {
	if (win.window_type === Meta.WindowType.DESKTOP) {
		return false;
	}
	
	// If the window is simply switching workspaces, it will trigger a
	// window-added signal. We don't want to reprocess it then because we already
	// have.
	if (win._pixelSaverOriginalState !== undefined) {
		return false;
	}
	
	/**
	 * Newly-created windows are added to the workspace before
	 * the compositor knows about them: get_compositor_private() is null.
	 * Additionally things like .get_maximized() aren't properly done yet.
	 * (see workspace.js _doAddWindow)
	 */
	if (!win.get_compositor_private()) {
		retry = (retry !== undefined) ? retry : 0;
		if (retry > 3) {
			return false;
		}
		
		Mainloop.idle_add(function () {
			onWindowAdded(ws, win, retry + 1);
			return false;
		});
		return false;
	}
	
	retry = 3;
	Mainloop.idle_add(function () {
		let id = guessWindowXID(win);
		if (!id) {
			if (--retry) {
				return true;
			}
			
			WARN("Finding XID for window %s failed".format(win.title));
			return false;
		}
		
		LOG('onWindowAdded: ' + win.get_title());
		setHideTitlebar(win, true);
		return false;
	});
	
	return false;
}

let workspaces = [];

/**
 * Callback whenever the number of workspaces changes.
 *
 * We ensure that we are listening to the 'window-added' signal on each of
 * the workspaces.
 *
 * @see onWindowAdded
 */
function onChangeNWorkspaces() {
	cleanWorkspaces();
	
	let i = global.screen.n_workspaces;
	while (i--) {
		let ws = global.screen.get_workspace_by_index(i);
		workspaces.push(ws);
		// we need to add a Mainloop.idle_add, or else in onWindowAdded the
		// window's maximized state is not correct yet.
		ws._pixelSaverWindowAddedId = ws.connect('window-added', function (ws, win) {
			Mainloop.idle_add(function () { return onWindowAdded(ws, win); });
		});
	}
	
	return false;
}

/**
 * Utilities
 */
function cleanWorkspaces() {
	// disconnect window-added from workspaces
	workspaces.forEach(function(ws) {
		ws.disconnect(ws._pixelSaverWindowAddedId);
		delete ws._pixelSaverWindowAddedId;
	});
	
	workspaces = [];
}

function forEachWindow(callback) {
	global.get_window_actors()
		.map(function (w) { return w.meta_window; })
		.filter(function(w) { return w.window_type !== Meta.WindowType.DESKTOP; })
		.forEach(callback);
}

/**
 * Subextension hooks
 */
function init() {}

let changeWorkspaceID = 0;
function enable() {
	// Connect events
	changeWorkspaceID = global.screen.connect('notify::n-workspaces', onChangeNWorkspaces);
	
	/**
	 * Go through already-maximised windows & undecorate.
	 * This needs a delay as the window list is not yet loaded
	 * when the extension is loaded.
	 * Also, connect up the 'window-added' event.
	 * Note that we do not connect this before the onMaximise loop
	 * because when one restarts the gnome-shell, window-added gets
	 * fired for every currently-existing window, and then
	 * these windows will have onMaximise called twice on them.
	 */
	Mainloop.idle_add(function () {
		forEachWindow(function(win) {
			onWindowAdded(null, win);
		});
		
		onChangeNWorkspaces();
		return false;
	});
}

function disable() {
	if (changeWorkspaceID) {
		global.screen.disconnect(changeWorkspaceID);
		changeWorkspaceID = 0;
	}
	
	cleanWorkspaces();
	
	forEachWindow(function(win) {
		let state = getOriginalState(win);
		LOG('stopUndecorating: ' + win.title + ' original=' + state);
		if (state == WindowState.DEFAULT) {
			setHideTitlebar(win, false);
		}
		
		delete win._pixelSaverOriginalState;
	});
}