Skip to content

Commit

Permalink
Add bounds support for Linux (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
arciisine authored and sindresorhus committed Jan 10, 2019
1 parent d346d51 commit 140f469
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions lib/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const childProcess = require('child_process');

const execFile = util.promisify(childProcess.execFile);
const xpropBin = 'xprop';
const xwininfoBin = 'xwininfo';
const xpropActiveArgs = ['-root', '\t$0', '_NET_ACTIVE_WINDOW'];
const xpropDetailsArgs = ['-id'];

const parseLinux = linuxData => {
const {stdout, activeWindowId} = linuxData;

const processOutput = output => {
const result = {};
for (const row of stdout.trim().split('\n')) {
for (const row of output.trim().split('\n')) {
if (row.includes('=')) {
const [key, value] = row.split('=');
result[key.trim()] = value.trim();
Expand All @@ -20,6 +19,12 @@ const parseLinux = linuxData => {
result[key.trim()] = value.trim();
}
}
return result;
};

const parseLinux = ({stdout, boundsStdout, activeWindowId}) => {
const result = processOutput(stdout);
const bounds = processOutput(boundsStdout);

const windowIdProperty = 'WM_CLIENT_LEADER(WINDOW)';
const resultKeys = Object.keys(result);
Expand All @@ -32,6 +37,12 @@ const parseLinux = linuxData => {
owner: {
name: JSON.parse(result['WM_CLASS(STRING)'].split(',').pop()),
processId: parseInt(result['_NET_WM_PID(CARDINAL)'], 10)
},
bounds: {
x: parseInt(bounds['Absolute upper-left X'], 10),
y: parseInt(bounds['Absolute upper-left Y'], 10),
width: parseInt(bounds.Width, 10),
height: parseInt(bounds.Height, 10)
}
};
};
Expand All @@ -41,10 +52,15 @@ const getActiveWindowId = activeWindowIdStdout => parseInt(activeWindowIdStdout.
module.exports = async () => {
const {stdout: activeWindowIdStdout} = await execFile(xpropBin, xpropActiveArgs);
const activeWindowId = getActiveWindowId(activeWindowIdStdout);
const {stdout} = await execFile(xpropBin, xpropDetailsArgs.concat(activeWindowId));

const [{stdout}, {stdout: boundsStdout}] = await Promise.all([
execFile(xpropBin, xpropDetailsArgs.concat([activeWindowId])),
execFile(xwininfoBin, xpropDetailsArgs.concat([activeWindowId]))
]);

return parseLinux({
activeWindowId,
boundsStdout,
stdout
});
};
Expand All @@ -53,9 +69,11 @@ module.exports.sync = () => {
const activeWindowIdStdout = childProcess.execFileSync(xpropBin, xpropActiveArgs, {encoding: 'utf8'});
const activeWindowId = getActiveWindowId(activeWindowIdStdout);
const stdout = childProcess.execFileSync(xpropBin, xpropDetailsArgs.concat(activeWindowId), {encoding: 'utf8'});
const boundsStdout = childProcess.execFileSync(xwininfoBin, xpropDetailsArgs.concat([activeWindowId]), {encoding: 'utf8'});

return parseLinux({
activeWindowId,
boundsStdout,
stdout
});
};
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Returns an `Object` with the result.

- `title` *(string)* - Window title
- `id` *(number)* - Window identifier
- `bounds` *(Object)* - Window position and size *(macOS only)*
- `bounds` *(Object)* - Window position and size
- `x` *(number)*
- `y` *(number)*
- `width` *(number)*
Expand Down

0 comments on commit 140f469

Please sign in to comment.