Skip to content

Commit

Permalink
Close by rules: compatible with Wayland
Browse files Browse the repository at this point in the history
  • Loading branch information
nlpsuge committed Jun 27, 2022
1 parent 2fac1b4 commit 23ae35c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ sudo dnf install ydotool

#Check the permission of `/dev/uinput`, if it's `crw-rw----+`, you can skip step 2
# 2. Get permission to access to `/dev/uinput` as the normal user
sudo touch /etc/udev/rules.d/60-awsm-ydotool-uinput.rules
sudo echo '# See:
# https://github.com/ValveSoftware/steam-devices/blob/master/60-steam-input.rules
# https://github.com/ReimuNotMoe/ydotool/issues/25
Expand Down
71 changes: 53 additions & 18 deletions closeSession.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { Shell, Gio, GLib } = imports.gi;
const { Meta, Shell, Gio, GLib } = imports.gi;

const Main = imports.ui.main;
const Scripting = imports.ui.scripting;
Expand Down Expand Up @@ -186,6 +186,31 @@ var CloseSession = class {
}

_activateAndFocusWindow(app) {
let windows;
if (Meta.is_wayland_compositor()) {
windows = this._sortWindowsOnWayland(app);
} else {
windows = this._sortWindowsOnX11(app);
}

const topLevelWindow = windows[windows.length - 1];
if (topLevelWindow) {
this._log.info(`Activating the running window ${topLevelWindow.get_title()} of ${app.get_name()}`);
Main.activateWindow(topLevelWindow);
}
}

_sortWindowsOnWayland(app) {
const windows = app.get_windows();
windows.sort((w1, w2) => {
const windowStableSequence1 = w1.get_stable_sequence();
const windowStableSequence2 = w2.get_stable_sequence();
return this._compareWindowStableSequence(windowStableSequence1, windowStableSequence2);
});
return windows;
}

_sortWindowsOnX11(app) {
const savedWindowsMappingJsonStr = this._settings.get_string('windows-mapping');
const savedWindowsMapping = new Map(JSON.parse(savedWindowsMappingJsonStr));

Expand All @@ -196,31 +221,41 @@ var CloseSession = class {
windows.sort((w1, w2) => {
const xid1 = w1.get_description();
const value1 = xidObj[xid1];
const windowStableSequence1 = value1.windowStableSequence;
let windowStableSequence1;
if (value1) {
windowStableSequence1 = value1.windowStableSequence;
} else {
windowStableSequence1 = w1.get_stable_sequence();
this._log.warn(`Mapping for this xid ${xid1} and stable sequence does not exist, use sequence ${windowStableSequence1} instead. app name: ${app.get_name()}, window title: ${w1.get_title()}`);
}

const xid2 = w2.get_description();
const value2 = xidObj[xid2];
const windowStableSequence2 = value2.windowStableSequence;

const diff = windowStableSequence1 - windowStableSequence2;
if (diff === 0) {
return 0;
let windowStableSequence2;
if (value2) {
windowStableSequence2 = value2.windowStableSequence;
} else {
windowStableSequence2 = w2.get_stable_sequence();
this._log.warn(`Mapping for this xid ${xid2} and stable sequence does not exist, use sequence ${windowStableSequence2} instead. app name: ${app.get_name()}, window title: ${w2.get_title()}`);
}

if (diff > 0) {
return 1;
}
return this._compareWindowStableSequence(windowStableSequence1, windowStableSequence2);
});
return windows;
}

if (diff < 0) {
return -1;
}
_compareWindowStableSequence(windowStableSequence1, windowStableSequence2) {
const diff = windowStableSequence1 - windowStableSequence2;
if (diff === 0) {
return 0;
}

});
if (diff > 0) {
return 1;
}

const topLevelWindow = windows[windows.length - 1];
if (topLevelWindow) {
this._log.info(`Activating the running window ${topLevelWindow.get_title()} of ${app.get_name()}`);
Main.activateWindow(topLevelWindow);
if (diff < 0) {
return -1;
}
}

Expand Down
5 changes: 5 additions & 0 deletions openWindowsInfoTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const EndSessionDialogProxy = Gio.DBusProxy.makeProxyWrapper(EndSessionDialogIfa
var OpenWindowsInfoTracker = class {

constructor() {
// Only track windows on X11
if (Meta.is_wayland_compositor()) {
return;
}

this._windowTracker = Shell.WindowTracker.get_default();
this._defaultAppSystem = Shell.AppSystem.get_default();

Expand Down
4 changes: 4 additions & 0 deletions utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var Log = class {
log(`[INFO ][Another window session manager] ${logContent}`);
}

warn(logContent) {
log(`[WARNING ][Another window session manager] ${logContent}`);
}

destroy() {
if (this._prefsUtils) {
this._prefsUtils.destroy();
Expand Down

0 comments on commit 23ae35c

Please sign in to comment.