Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
764c2c4
Added execution arguments for .desktop programs
RodrigoATorres Apr 8, 2020
9eff5df
Adding possibility to add arguments to regular executable files
RodrigoATorres Apr 22, 2020
3f764a0
Added funcionality to guarantee that the windows are open and moved i…
RodrigoATorres Apr 22, 2020
f831f63
Fixing arguments for executable files
RodrigoATorres Apr 22, 2020
ea43efa
Merge branch 'feature/exec_args'
RodrigoATorres Apr 22, 2020
38056e4
Fixing bug when there are no args for executable files
RodrigoATorres Apr 27, 2020
ee22ad3
Added flag --allowInputArgs to make possible to run lwsm with origini…
RodrigoATorres Apr 27, 2020
30abb05
Running prettier to index.ts (should have done it before)
RodrigoATorres Apr 27, 2020
13b2166
Added POLL_ALL_MAX_TIMEOUT_ALLOW_ARGS to configs
RodrigoATorres Apr 27, 2020
c555b58
Bug fixed on return promisses from restoreSession
RodrigoATorres Apr 27, 2020
89a23c7
Merge branch 'master' into feature/exec_args
RodrigoATorres Apr 27, 2020
0fba482
Merge remote-tracking branch 'upstream/master'
RodrigoATorres Apr 27, 2020
55e71f8
Merging
RodrigoATorres Apr 27, 2020
3f56d70
Adjusting startProgram to allow multiple input arguments for executab…
RodrigoATorres Apr 28, 2020
f64de0a
Merge remote-tracking branch 'upstream/master'
RodrigoATorres Apr 28, 2020
d10d030
Merging master
RodrigoATorres Apr 28, 2020
d437643
Merging
RodrigoATorres May 13, 2020
79c14e7
Removed executable arguments when flag is off, replaced var declarati…
RodrigoATorres Sep 7, 2020
9280827
Merging master
RodrigoATorres Sep 7, 2020
2c9b961
Merge branch 'master' into feature/exec_args
RodrigoATorres Sep 7, 2020
0bf45c9
Empty executable args paramenters automaticlly added to json when sav…
RodrigoATorres Sep 7, 2020
0ab548f
Updated README to include instructions on how to use --allowInputArgs…
RodrigoATorres Sep 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lwsm restore my-session
lwsm restore --closeAllOpenWindows
```


### List saved sessions:
```
lwsm list
Expand All @@ -64,6 +65,60 @@ lwsm rename oldName newName
### Adjusting the configuration:
You can manually edit the config file present at `~/.config/lwsm/config.json` and the session files in `~/.config/lwsm/sessionData/[sessionName].json`.

### Adding input arguments to programs when restoring a session:
Making use of the "--allowInputArgs" flag, it is possible to restore sessions passing input parameters to the programs.
The parameters have to be added manually to the saved .json files.

For google chrome you can add:
```
"executableArgs": "--new-window gmail.com google.com"
```

For firefox:
```
"executableArgs": "--new-window gmail.com"
```
or (if you want to open more than one tab):
```
"executableArgs": " gmail.com google.com"
```

For nautilus:
```
"executableArgs": "/home/user/Desktop"
```

For Vscode:
```
"executableArgs": "/home/user/Desktop"
```

The json would have a format similar to this:
```
{
"windowId": "0x2c0000b",
...
"simpleName": "Google-chrome",
"executableFile": "google-chrome.desktop",
"desktopFilePath": "/usr/share/applications/google-chrome.desktop",
"executableArgs": "--new-window gmail.com"
},
{
"windowId": "0x2e00001",
...
"simpleName": "Code",
"executableFile": "code.desktop",
"desktopFilePath": "/usr/share/applications/code.desktop",
"executableArgs": "/path/to/you/code"
},
```

After edditing the json, you can call lwsm with the following command:
```
# restore the session from ~/.config/lwsm/sessionData/my-session.json
lwsm restore my-session --allowInputArgs
```

### Command-line completion:
```
# Automatically install command-line completion
Expand Down
3 changes: 2 additions & 1 deletion cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const savePrompts = {
};

const isCloseAllWinBefore = process.argv.indexOf('--closeAllOpenWindows') > -1;
const isAllowInputArgs = process.argv.indexOf('--allowInputArgs') > -1;
const action = process.argv[2];
let sessionName;
if (process.argv[3] && !process.argv[3].match(/^--/)) {
Expand All @@ -91,7 +92,7 @@ console.log(`lwsm ${version} --> ${action} ${new Date()}`);
if (action === 'save') {
base.saveSession(sessionName, savePrompts).then(killX11);
} else if (action === 'restore') {
base.restoreSession(sessionName, isCloseAllWinBefore).then(killX11);
base.restoreSession(sessionName, isCloseAllWinBefore, isAllowInputArgs).then(killX11);
} else if (action === 'remove') {
base.removeSession(sessionName).then(killX11);
} else if (action === 'resetCfg') {
Expand Down
1 change: 1 addition & 0 deletions src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const DEFAULT_CFG = {
GIVE_X11_TIME_TIMEOUT: 80,
POLL_ALL_APPS_STARTED_INTERVAL: 2000,
POLL_ALL_MAX_TIMEOUT: 120000,
POLL_ALL_MAX_TIMEOUT_ALLOW_ARGS: 1200000,
SAVE_SESSION_IN_PRETTY_FORMAT: true,
WM_CLASS_AND_EXECUTABLE_FILE_MAP: {
"gnome-terminal-server.Gnome-terminal": "gnome-terminal",
Expand Down
150 changes: 146 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ function saveSessionForDisplayToDb(

function restoreSession(
sessionName: string,
isCloseAllOpenWindows: boolean
isCloseAllOpenWindows: boolean,
isAllowInputArgs: boolean
): Promise<any> {
const sessionToHandle = sessionName || "DEFAULT";

Expand Down Expand Up @@ -213,7 +214,21 @@ function restoreSession(
return getActiveWindowListFlow();
})
.then(currentWindowList => {
return _startSessionPrograms(savedWindowList, currentWindowList);
if (isAllowInputArgs) {
return _startAndWaitPrograms(savedWindowList);
} else {
return _startSessionPrograms(savedWindowList, currentWindowList)
.then(() => {
// gets current window list by itself and returns the updated variant
return _waitForAllAppsToStart(savedWindowList);
})
.then((updatedCurrentWindowList: WinObj[]) => {
return _updateWindowIds(
savedWindowList,
updatedCurrentWindowList
);
});
}
})
.then(() => {
// gets current window list by itself and returns the updated variant
Expand Down Expand Up @@ -417,6 +432,7 @@ function _guessFilePath(win: WinObj, inputHandler): Promise<string | unknown> {
`\n Alternative guessing approach for "${win.simpleName}" SUCCESS -> ${ent[0]}`
);
win.executableFile = ent[0];
win.executableArgs = "";
fulfill(win.executableFile);
}
});
Expand All @@ -425,9 +441,11 @@ function _guessFilePath(win: WinObj, inputHandler): Promise<string | unknown> {
.then(input => {
if (_isDesktopFile(win.executableFile)) {
win.desktopFilePath = input;
win.executableArgs = "";
fulfill(win.desktopFilePath);
} else {
win.executableFile = input;
win.executableArgs = "";
fulfill(win.executableFile);
}
})
Expand Down Expand Up @@ -469,12 +487,137 @@ async function _startSessionPrograms(
})
.map(win => {
win.instancesStarted += 1;
return startProgram(win.executableFile, win.desktopFilePath);
return startProgram(win.executableFile, win.desktopFilePath, "");
});

await Promise.all(promises);
}

// This function is necessary to make possible sending custom arguments to applications to start,
// making sure it will keep track of which windows correspond to which instance of the applications
async function _startAndWaitPrograms(windowList: WinObj[]) {
// Clear the windowIds from the window objects
windowList.forEach(win => {
win.windowId = null;
win.windowIdDec = null;
});

// Get the windowIds of all Ids that were previously opened in order to
// avoid these windows from being used isntead of the newly created windows
// (necessary for windows with custom input arguments)
let activeWindows = await getActiveWindowListFlow();
let blackWinIdList = activeWindows.map(win => win.windowId);

// Match all the previously opened windows with the windows that do not have
// custom arguments (in this case, no blacklist needed)
await _matchWindows(windowList, false, []);

let windowsNotStarted = windowList.filter(win => win.windowId === null);
let windowStarted = windowList.filter(win => win.windowId !== null);
let windowsToStart = _getWindowsToStart(windowsNotStarted, windowStarted);

let totalTimeWaited = 0;
// Runs the loop until all windows have been opened or time limit is over
while (windowList.find(win => win.windowId === null)) {
totalTimeWaited += CFG.POLL_ALL_APPS_STARTED_INTERVAL;
if (totalTimeWaited > CFG.POLL_ALL_MAX_TIMEOUT_ALLOW_ARGS) {
console.error("POLL_ALL_MAX_TIMEOUT_ALLOW_ARGS reached");
windowList
.filter(win => win.windowId === null)
.forEach(e => {
console.log("Unable to start: ", e.wmClassName);
});
break;
}

let promises = windowsToStart.map(win => {
startProgram(win.executableFile, win.desktopFilePath, win.executableArgs);
});
await Promise.all(promises);

// Try to match newly started windows, the black list is needed to avoid matching
// windows that have custom arguments with windows oppened before running lwsm
await _matchWindows(windowList, true, blackWinIdList);
// Update the lists with the windows not to be started yet, the windows to be started next
// and the ones that already have the command to start sent
windowsNotStarted = windowsNotStarted.filter(
winNotStarted =>
!windowsToStart.find(winToStart => winToStart === winNotStarted)
);
windowStarted = windowStarted.concat(windowsToStart);
windowsToStart = _getWindowsToStart(windowsNotStarted, windowStarted);
}

windowList.forEach(win => {
win.windowIdDec = parseInt(win.windowId, 16);
});
}

// Get, from windowList, all the windows that can be started without the risk of
// mixing same application windows with different arguments. The others will have to wait for the
// previous windows to start, before the command can be issued
function _getWindowsToStart(windowList: WinObj[], windowsStarted: WinObj[]) {
let windowsToStart = [];
for (let win of windowList) {
let shouldAdd = true;
// If another instance of the same application with different input arguments
// is in the list of windows to start, then the current one should not be added
windowsToStart.forEach(winToRun => {
if (
win.executableFile === winToRun.executableFile &&
win.executableArgs !== winToRun.executableArgs
) {
shouldAdd = false;
}
});
// If another instance of the same application is in the list of windows that
// have been sent command to start, but still don't have a windowId, the current one should not be added
if (
windowsStarted.find(
winStarted =>
winStarted.windowId === null &&
winStarted.wmClassName === win.wmClassName
)
) {
shouldAdd = false;
}
if (!win.windowId && shouldAdd) {
windowsToStart.push(win);
}
}
return windowsToStart;
}

// Match the windows that have been opened already to the windows on windowList
async function _matchWindows(
windowList: WinObj[],
includeExecsWithArgs: boolean,
blackWinIdList: String[]
) {
let activeWindows = await getActiveWindowListFlow();
// Remove the active windows that already have a window assigned on windowList
// and the ones that are on the black list
activeWindows = activeWindows.filter(
actWin => !windowList.find(win => win.windowId === actWin.windowId)
);
activeWindows = activeWindows.filter(
actWin => !blackWinIdList.find(windowId => windowId === actWin.windowId)
);

for (let win of windowList.filter(win => win.windowId === null)) {
let activeWindowMatch = activeWindows.find(
actWin => actWin.wmClassName == win.wmClassName
);
if (
activeWindowMatch &&
(!win.executableArgs || win.executableArgs === "" || includeExecsWithArgs)
) {
win.windowId = activeWindowMatch.windowId;
activeWindows = activeWindows.filter(e => e !== activeWindowMatch);
}
}
}

function _getNumberOfInstancesToRun(
windowToMatch: WinObj,
windowList: WinObj[]
Expand Down Expand Up @@ -576,7 +719,6 @@ async function _restoreWindowPositions(
for (const win of savedWindowList) {
promises.push(restoreWindowPosition(win));
promises.push(moveToWorkspace(win.windowId, win.wmCurrentDesktopNr));

// The promises are not executed until the last item is reached or
// the desktop_nr is different from the previous entry and which case
// the app waits for those to finish before continuing the process
Expand Down
1 change: 1 addition & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface WinObj extends WinObjIdOnly {
height: number;
simpleName: string;
executableFile: string;
executableArgs: string;
desktopFilePath?: string;
instancesStarted?: number;
}
21 changes: 17 additions & 4 deletions src/otherCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export async function getAdditionalMetaDataForWin(
// TODO prettify args structure
export function startProgram(
executableFile: string,
desktopFilePath: string
desktopFilePath: string,
executableArgs: string
): Promise<void> {
IS_DEBUG &&
console.log("DEBUG: startProgram():", executableFile, desktopFilePath);
Expand All @@ -88,14 +89,26 @@ export function startProgram(
let args = [];
if (desktopFilePath) {
cmd = `awk`;
args.push(
'/^Exec=/ {sub("^Exec=", ""); gsub(" ?%[cDdFfikmNnUuv]", ""); exit system($0)}'
);

if (executableArgs) {
args.push(
`/^Exec=/ {sub("^Exec=", ""); gsub(" ?%[cDdFfikmNnUuv]", " ${executableArgs}"); exit system($0)}`
);
} else {
args.push(
'/^Exec=/ {sub("^Exec=", ""); gsub(" ?%[cDdFfikmNnUuv]", ""); exit system($0)}'
);
}

args.push(desktopFilePath);
} else {
const parsedCmd = parseCmdArgs(executableFile);
cmd = parsedCmd[0];
args = parsedCmd[1];
if (executableArgs) {
var executableArgsArray = executableArgs.split(" ");
args = args.concat(executableArgsArray);
}
}

return new Promise(fulfill => {
Expand Down