Skip to content

Commit

Permalink
Added a fallback method for Linux window name tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
kmteras committed Dec 31, 2019
1 parent 3a1175b commit 994d355
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/models/heartbeat_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ProcessModel from "@/models/process_model";
import WindowModel from "@/models/window_model";
import {powerMonitor} from "electron"
import Settings from "@/services/settings";
import getActiveWindow from "@/util/active_window_linux";


export default class HeartbeatModel {
Expand All @@ -12,7 +13,7 @@ export default class HeartbeatModel {
window: WindowModel;
idle: boolean;

constructor(time: bigint) {
constructor() {
this.time = Math.floor(new Date().getTime() / 1000);
const windowInfo = activeWin.sync();

Expand All @@ -22,7 +23,23 @@ export default class HeartbeatModel {
this.process = new ProcessModel(windowInfo.owner.path, windowInfo.owner.name);
this.window = new WindowModel(windowInfo.title, this.process);
} else {
throw new Error("Could not get active window");
/**
* On Linux some X11 windows do not have a PID attached to them. (Process created with another process?)
* Module active-win fails on that.
* We can still get the active window but it will associated with a process.
*/

if (process.platform === 'linux') {
try {
const windowName = getActiveWindow();
this.process = new ProcessModel("/unknown/unknown", "unknown");
this.window = new WindowModel(windowName, this.process);
} catch (_) {
throw new Error("Could not get a active window");
}
} else {
throw new Error("Could not get active window");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Heartbeat {

start() {
try {
this.heartbeat(new HeartbeatModel(BigInt(0))).then();
this.heartbeat(new HeartbeatModel()).then();
} catch (e) {
// Tough shit, cant really do anything - not a severe problem
log.debug(e)
Expand Down
1 change: 1 addition & 0 deletions src/services/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class Processes {

if (result.path.search(regex) < 0) {
log.warn(`Could not regex process name from ${result.path}`);
result.name = result.path;
continue;
}

Expand Down
16 changes: 16 additions & 0 deletions src/util/active_window_linux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as child from 'child_process';

const xpropIdCommand = "xprop -root 32x '\\t$0' _NET_ACTIVE_WINDOW | cut -f 2";
const xpropNamePropCommand = `xprop -id $(${xpropIdCommand}) WM_NAME`;


export default function getActiveWindow(): string {
const returnString = child.execSync(xpropNamePropCommand).toString();
const row = returnString.split("=");
const namePart = row.pop();
if (namePart != undefined) {
return JSON.parse(namePart);
} else {
throw new Error(`Could not find name from ${returnString}`);
}
}

0 comments on commit 994d355

Please sign in to comment.