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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
|
/* DING: Desktop Icons New Generation for GNOME Shell
*
* Copyright (C) 2019 Sergio Costas (rastersoft@gmail.com)
* Based on code original (C) Carlos Soriano
*
* 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, version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
'use strict';
import Clutter from 'gi://Clutter'
import GLib from 'gi://GLib'
import Gio from 'gi://Gio'
import Meta from 'gi://Meta'
import St from 'gi://St'
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js'
import * as EmulateX11 from './emulateX11WindowType.js';
import * as VisibleArea from './visibleArea.js';
import * as GnomeShellOverride from './gnomeShellOverride.js';
const Clipboard = St.Clipboard.get_default();
const CLIPBOARD_TYPE = St.ClipboardType.CLIPBOARD;
export default class DING extends Extension {
constructor(metadata) {
super(metadata);
this.DesktopIconsUsableArea = null;
this.data = {};
this.data.isEnabled = false;
this.data.launchDesktopId = 0;
this.data.currentProcess = null;
this.data.dbusTimeoutId = 0;
this.data.switchWorkspaceId = 0;
this.data.GnomeShellOverride = null;
/* The constructor of the EmulateX11 class only initializes some
* internal properties, but nothing else. In fact, it has its own
* enable() and disable() methods. That's why it could have been
* created here, in init(). But since the rule seems to be NO CLASS
* CREATION IN INIT UNDER NO CIRCUMSTANCES...
*/
this.data.x11Manager = null;
this.data.visibleArea = null;
/* Ensures that there aren't "rogue" processes.
* This is a safeguard measure for the case of Gnome Shell being
* relaunched (for example, under X11, with Alt+F2 and R), to kill
* any old DING instance. That's why it must be here, in init(),
* and not in enable() or disable() (disable already guarantees that
* the current instance is killed).
*/
this.doKillAllOldDesktopProcesses();
}
enable() {
if (!this.data.GnomeShellOverride) {
this.data.GnomeShellOverride = new GnomeShellOverride.GnomeShellOverride();
}
if (!this.data.x11Manager) {
this.data.x11Manager = new EmulateX11.EmulateX11WindowType();
}
if (!this.DesktopIconsUsableArea) {
this.DesktopIconsUsableArea = new VisibleArea.VisibleArea();
this.data.visibleArea = this.DesktopIconsUsableArea;
}
// If the desktop is still starting up, we wait until it is ready
if (Main.layoutManager._startingUp) {
this.data.startupPreparedId = Main.layoutManager.connect('startup-complete', () => this.innerEnable());
} else {
this.data.startupPreparedId = null;
this.innerEnable();
}
}
disable() {
this.DesktopIconsUsableArea = null;
this.data.isEnabled = false;
this.killCurrentProcess();
this.data.GnomeShellOverride.disable();
this.data.x11Manager.disable();
this.data.visibleArea.disable();
if (this.data.doCopyId) {
this.data.doCopy.disconnect(this.data.doCopyId);
this.data.doCopyId = 0;
this.data.doCopy = undefined;
}
if (this.data.switchWorkspaceId) {
global.window_manager.disconnect(this.data.switchWorkspaceId);
this.data.switchWorkspaceId = 0;
}
if (this.data.doCutId) {
this.data.doCut.disconnect(this.data.doCutId);
this.data.doCutId = 0;
this.data.doCut = undefined;
}
if (this.data.disableTimerId) {
this.data.disableTimer.disconnect(this.data.disableTimerId);
this.data.disableTimerId = 0;
this.data.disableTimer = undefined;
}
this.data.desktopGeometry = undefined;
// disconnect signals only if connected
if (this.data.dbusConnectionGroupId) {
this.data.dbusConnection.unexport_action_group(this.data.dbusConnectionGroupId);
this.data.dbusConnectionGroupId = 0;
this.data.dbusConnection = undefined;
}
if (this.data.dbusConnectionId) {
Gio.bus_unown_name(this.data.dbusConnectionId);
this.data.dbusConnectionId = 0;
}
this.data.actionGroup = undefined;
if (this.data.visibleAreaId) {
this.data.visibleArea.disconnect(this.data.visibleAreaId);
this.data.visibleAreaId = 0;
}
if (this.data.startupPreparedId) {
Main.layoutManager.disconnect(this.data.startupPreparedId);
this.data.startupPreparedId = 0;
}
if (this.data.monitorsChangedId) {
Main.layoutManager.disconnect(this.data.monitorsChangedId);
this.data.monitorsChangedId = 0;
}
if (this.data.workareasChangedId) {
global.display.disconnect(this.data.workareasChangedId);
this.data.workareasChangedId = 0;
}
if (this.data.sizeChangedId) {
global.window_manager.disconnect(this.data.sizeChangedId);
this.data.sizeChangedId = 0;
}
if (this.data.dbusTimeoutId) {
GLib.source_remove(this.data.dbusTimeoutId);
this.data.dbusTimeoutId = 0;
}
}
/**
* The true code that configures everything and launches the desktop program
*/
innerEnable() {
if (this.data.startupPreparedId !== null) {
Main.layoutManager.disconnect(this.data.startupPreparedId);
this.data.startupPreparedId = null;
}
this.data.GnomeShellOverride.enable();
// under X11 we don't need to cheat, so only do all this under wayland
if (Meta.is_wayland_compositor()) {
this.data.x11Manager.enable();
} else {
this.data.switchWorkspaceId = global.window_manager.connect('switch-workspace', () => {
let windows = global.display.get_tab_list(Meta.TabList.NORMAL_ALL, global.workspace_manager.get_active_workspace());
windows = global.display.sort_windows_by_stacking(windows);
if (windows.length) {
let topWindow = windows[windows.length - 1];
topWindow.focus(Clutter.CURRENT_TIME);
}
});
}
/*
* If the desktop geometry changes (because a new monitor has been added, for example),
* we kill the desktop program. It will be relaunched automatically with the new geometry,
* thus adapting to it on-the-fly.
*/
this.data.monitorsChangedId = Main.layoutManager.connect('monitors-changed', () => this.updateDesktopGeometry());
/*
* Any change in the workareas must be detected too, for example if the used size
* changes.
*/
this.data.workareasChangedId = global.display.connect('workareas-changed', () => this.updateDesktopGeometry());
/*
* This callback allows to detect a change in the working area (like when changing the Scale value)
*/
this.data.visibleAreaId = this.data.visibleArea.connect('updated-usable-area', () => this.updateDesktopGeometry());
this.data.isEnabled = true;
if (this.data.launchDesktopId) {
GLib.source_remove(this.data.launchDesktopId);
}
/*
* Due to a problem in the Clipboard API in Gtk3, it is not possible to do the CUT/COPY operation from
* dynamic languages like Javascript, because one of the methods needed is marked as NOT INTROSPECTABLE
*
* https://discourse.gnome.org/t/missing-gtk-clipboard-set-with-data-in-gtk-3/6920
*
* The right solution is to migrate DING to Gtk4, where the whole API is available, but that is a very
* big task, so in the meantime, we take advantage of the fact that the St API, in Gnome Shell, can put
* binary contents in the clipboard, so we use DBus to notify that we want to do a CUT or a COPY operation,
* passing the URIs as parameters, and delegate that to the DING Gnome Shell extension. This is easily done
* with a GLib.SimpleAction.
*/
this.data.dbusConnectionId = Gio.bus_own_name(Gio.BusType.SESSION, 'com.rastersoft.dingextension', Gio.BusNameOwnerFlags.NONE, null, (connection, name) => {
this.data.dbusConnection = connection;
this.data.doCopy = new Gio.SimpleAction({
name: 'doCopy',
parameter_type: new GLib.VariantType('as'),
});
this.data.doCut = new Gio.SimpleAction({
name: 'doCut',
parameter_type: new GLib.VariantType('as'),
});
this.data.disableTimer = new Gio.SimpleAction({
name: 'disableTimer',
});
this.data.desktopGeometry = Gio.SimpleAction.new_stateful('desktopGeometry', new GLib.VariantType('av'), this.getDesktopGeometry());
this.data.desktopGeometry.set_enabled(true);
this.data.doCopyId = this.data.doCopy.connect('activate', (action, parameters) => this.manageCutCopy(action, parameters));
this.data.doCutId = this.data.doCut.connect('activate', (action, parameters) => this.manageCutCopy(action, parameters));
this.data.disableTimerId = this.data.disableTimer.connect('activate', () => {
if (this.data.currentProcess && this.data.currentProcess.subprocess) {
this.data.currentProcess.cancel_timer();
}
});
this.data.actionGroup = new Gio.SimpleActionGroup();
this.data.actionGroup.add_action(this.data.doCopy);
this.data.actionGroup.add_action(this.data.doCut);
this.data.actionGroup.add_action(this.data.disableTimer);
this.data.actionGroup.add_action(this.data.desktopGeometry);
this.data.dbusConnectionGroupId = this.data.dbusConnection.export_action_group(
'/com/rastersoft/dingextension/control',
this.data.actionGroup
);
this.launchDesktop();
}, null);
}
/*
* Before Gnome Shell 40, St API couldn't access binary data in the clipboard, only text data. Also, the
* original Desktop Icons was a pure extension, so it was limited to what Clutter and St offered. That was
* the reason why Nautilus accepted a text format for CUT and COPY operations in the form
*
* x-special/nautilus-clipboard
* OPERATION
* FILE_URI
* [FILE_URI]
* [...]
*
* In Gnome Shell 40, St was enhanced and now it supports binary data; that's why Nautilus migrated to a
* binary format identified by the atom 'x-special/gnome-copied-files', where the CUT or COPY operation is
* shared.
*
*/
/**
*
* @param action
* @param parameters
*/
manageCutCopy(action, parameters) {
let content = '';
if (action.name == 'doCut') {
content += 'cut\n';
} else {
content += 'copy\n';
}
let first = true;
for (let file of parameters.recursiveUnpack()) {
if (!first) {
content += '\n';
}
first = false;
content += file;
}
let obj = new TextEncoder();
Clipboard.set_content(CLIPBOARD_TYPE, 'x-special/gnome-copied-files', new GLib.Bytes(obj.encode(content)));
}
/**
* Kills the current desktop program
*/
killCurrentProcess() {
if (this.data.launchDesktopId) {
GLib.source_remove(this.data.launchDesktopId);
this.data.launchDesktopId = 0;
}
// kill the desktop program. It will be reloaded automatically.
if (this.data.currentProcess && this.data.currentProcess.subprocess) {
this.data.currentProcess.cancel_timer();
this.data.currentProcess.cancellable.cancel();
this.data.currentProcess.subprocess.send_signal(15);
}
this.data.currentProcess = null;
this.data.x11Manager.setWaylandClient(null);
}
/**
*
*/
updateDesktopGeometry() {
if (this.data.actionGroup && (Main.layoutManager.monitors.length != 0)) {
this.data.actionGroup.change_action_state('desktopGeometry', this.getDesktopGeometry());
this.data.x11Manager.refreshWindowsPosition();
}
}
/**
*
*/
getDesktopGeometry() {
let desktopList = [];
let ws = global.workspace_manager.get_workspace_by_index(0);
for (let monitorIndex = 0; monitorIndex < Main.layoutManager.monitors.length; monitorIndex++) {
let area = this.data.visibleArea.getMonitorGeometry(ws, monitorIndex);
let desktopListElement = new GLib.Variant('a{sd}', {
'x': area.x,
'y': area.y,
'width': area.width,
'height': area.height,
'zoom': area.scale,
'marginTop': area.marginTop,
'marginBottom': area.marginBottom,
'marginLeft': area.marginLeft,
'marginRight': area.marginRight,
monitorIndex,
'primaryMonitor': Main.layoutManager.primaryIndex,
});
desktopList.push(desktopListElement);
}
return new GLib.Variant('av', desktopList);
}
/**
* This function checks all the processes in the system and kills those
* that are a desktop manager from the current user (but not others).
* This allows to avoid having several ones in case gnome shell resets,
* or other odd cases. It requires the /proc virtual filesystem, but
* doesn't fail if it doesn't exist.
*/
/**
*
*/
doKillAllOldDesktopProcesses() {
let procFolder = Gio.File.new_for_path('/proc');
if (!procFolder.query_exists(null)) {
return;
}
let fileEnum = procFolder.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NONE, null);
let info;
while ((info = fileEnum.next_file(null))) {
let filename = info.get_name();
if (!filename) {
break;
}
let processPath = GLib.build_filenamev(['/proc', filename, 'cmdline']);
let processUser = Gio.File.new_for_path(processPath);
if (!processUser.query_exists(null)) {
continue;
}
let [binaryData, etag] = processUser.load_bytes(null);
let contents = '';
let readData = binaryData.get_data();
for (let i = 0; i < readData.length; i++) {
if (readData[i] < 32) {
contents += ' ';
} else {
contents += String.fromCharCode(readData[i]);
}
}
let path = `gjs ${GLib.build_filenamev([this.path, 'app', 'ding.js'])}`;
if (contents.startsWith(path)) {
let proc = new Gio.Subprocess({argv: ['/bin/kill', filename]});
proc.init(null);
proc.wait(null);
}
}
}
/**
*
* @param reloadTime
*/
doRelaunch(reloadTime) {
this.data.currentProcess = null;
this.data.x11Manager.setWaylandClient(null);
if (this.data.isEnabled) {
if (this.data.launchDesktopId) {
GLib.source_remove(this.data.launchDesktopId);
}
this.data.launchDesktopId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, reloadTime, () => {
this.data.launchDesktopId = 0;
this.launchDesktop();
return false;
});
}
}
/**
* Launches the desktop program, passing to it the current desktop geometry for each monitor
* and the path where it is stored. It also monitors it, to relaunch it in case it dies or is
* killed. Finally, it reads STDOUT and STDERR and redirects them to the journal, to help to
* debug it.
*/
launchDesktop() {
console.log('Launching DING process');
let argv = [];
argv.push(GLib.build_filenamev([this.path, 'app', 'ding.js']));
// Specify that it must work as true desktop
argv.push('-E');
// The path. Allows the program to find translations, settings and modules.
argv.push('-P');
argv.push(GLib.build_filenamev([this.path, 'app']));
this.data.currentProcess = new LaunchSubprocess(0, 'DING');
this.data.currentProcess.set_cwd(GLib.get_home_dir());
if (this.data.currentProcess.spawnv(argv) === null) {
this.doRelaunch(1000);
return;
}
this.data.x11Manager.setWaylandClient(this.data.currentProcess);
this.data.launchTime = GLib.get_monotonic_time();
/*
* If the desktop process dies, wait 100ms and relaunch it, unless the exit status is different than
* zero, in which case it will wait one second. This is done this way to avoid relaunching the desktop
* too fast if it has a bug that makes it fail continuously, avoiding filling the journal too fast.
*/
this.data.currentProcess.subprocess.wait_async(null, (obj, res) => {
let delta = GLib.get_monotonic_time() - this.data.launchTime;
if (delta < 1000000) {
// If the process is dying over and over again, ensure that it isn't respawn faster than once per second
var reloadTime = 1000;
} else {
// but if the process just died after having run for at least one second, reload it ASAP
var reloadTime = 1;
}
obj.wait_finish(res);
if (!this.data.currentProcess || obj !== this.data.currentProcess.subprocess) {
return;
}
if (obj.get_if_exited()) {
obj.get_exit_status();
}
this.doRelaunch(reloadTime);
});
}
}
/**
* This class encapsulates the code to launch a subprocess that can detect whether a window belongs to it
* It only accepts to do it under Wayland, because under X11 there is no need to do these tricks
*
* It is compatible with https://gitlab.gnome.org/GNOME/mutter/merge_requests/754 to simplify the code
*
* @param {int} flags Flags for the SubprocessLauncher class
* @param {string} process_id An string id for the debug output
*/
class LaunchSubprocess {
constructor(flags, process_id) {
this._process_id = process_id;
this.cancellable = new Gio.Cancellable();
this._launcher = new Gio.SubprocessLauncher({flags: flags | Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_MERGE});
if (Meta.is_wayland_compositor()) {
try {
this._waylandClient = Meta.WaylandClient.new(this._launcher);
} catch (e) {
this._waylandClient = Meta.WaylandClient.new(global.context,
this._launcher);
}
}
this.subprocess = null;
this.process_running = false;
this._launch_timer = 0;
this._waiting_for_windows = 0;
}
spawnv(argv) {
try {
if (Meta.is_wayland_compositor()) {
this.subprocess = this._waylandClient.spawnv(global.display, argv);
} else {
this.subprocess = this._launcher.spawnv(argv);
}
} catch (e) {
this.subprocess = null;
console.log(`Error while trying to launch DING process: ${e.message}\n${e.stack}`);
}
// This is for GLib 2.68 or greater
if (this._launcher.close) {
this._launcher.close();
}
this._launcher = null;
if (this.subprocess) {
/*
* It reads STDOUT and STDERR and sends it to the journal using console.log(). This allows to
* have any error from the desktop app in the same journal than other extensions. Every line from
* the desktop program is prepended with the "process_id" parameter sent in the constructor.
*/
this._dataInputStream = Gio.DataInputStream.new(this.subprocess.get_stdout_pipe());
this.read_output();
this.subprocess.wait_async(this.cancellable, () => {
this.process_running = false;
this._dataInputStream = null;
this.cancellable = null;
if (this._launch_timer != 0) {
GLib.source_remove(this._launch_timer);
this._launch_timer = 0;
this._waiting_for_windows = 0;
}
});
this.process_running = true;
if (Meta.is_wayland_compositor() && (Main.layoutManager.monitors.length != 0)) {
// This ensures that, if the DING window isn't detected in three seconds
// after launch, the desktop will be killed and, thus, relaunched again.
this._waiting_for_windows = Main.layoutManager.monitors.length;
this._launch_timer = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 3000, () => {
this._launch_timer = 0;
this.subprocess.force_exit();
return false;
});
}
}
return this.subprocess;
}
cancel_timer() {
if (this._launch_timer != 0) {
GLib.source_remove(this._launch_timer);
this._launch_timer = 0;
this._waiting_for_windows = 0;
}
}
set_cwd(cwd) {
this._launcher.set_cwd(cwd);
}
read_output() {
if (!this._dataInputStream) {
return;
}
this._dataInputStream.read_line_async(GLib.PRIORITY_DEFAULT, this.cancellable, (object, res) => {
try {
const [output, length] = object.read_line_finish_utf8(res);
if (length) {
print(`${this._process_id}: ${output}`);
}
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
return;
}
console.error(e, `${this._process_id}_Error`);
}
this.read_output();
});
}
/**
* Queries whether the passed window belongs to the launched subprocess or not.
*
* @param {MetaWindow} window The window to check.
*/
query_window_belongs_to(window) {
if (!Meta.is_wayland_compositor()) {
return false;
}
if (!this.process_running) {
return false;
}
try {
let ownsWindow = this._waylandClient.owns_window(window);
if (ownsWindow && (this._launch_timer != 0) && (this._waiting_for_windows != 0)) {
console.log(`Received notification for window. ${this._waiting_for_windows - 1} notifications remaining.`);
this._waiting_for_windows--;
if (this._waiting_for_windows == 0) {
GLib.source_remove(this._launch_timer);
this._launch_timer = 0;
}
}
return ownsWindow;
} catch (error) {
console.log(`Exception error: ${error.message}\n${error.stack}`);
return false;
}
}
show_in_window_list(window) {
if (Meta.is_wayland_compositor() && this.process_running) {
this._waylandClient.show_in_window_list(window);
}
}
hide_from_window_list(window) {
if (Meta.is_wayland_compositor() && this.process_running) {
this._waylandClient.hide_from_window_list(window);
}
}
};
|