Skip to content
Open
Show file tree
Hide file tree
Changes from 17 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
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 @@ -81,7 +82,7 @@ function killX11() {
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
167 changes: 154 additions & 13 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,14 +214,23 @@ 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
return _waitForAllAppsToStart(savedWindowList);
})
.then((updatedCurrentWindowList: WinObj[]) => {
_updateWindowIds(savedWindowList, updatedCurrentWindowList);
return _restoreWindowPositions(savedWindowList);
})
.then(() => {
Expand Down Expand Up @@ -461,12 +471,141 @@ async function _startSessionPrograms(
})
.map(win => {
win.instancesStarted += 1;
return startProgram(win.executableFile, win.desktopFilePath);
return startProgram(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken this would lead toisAllowInputArgs being false to have no effect?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long to answer,
The code would start the programs with arguments, in case there were input arguments in the json file, if the isAllowInputArgs was set to false.
The effect isAllowInputArgs being set to false would be running _startSessionPrograms instead of _startAndWaitPrograms.
I believe I did that way because the input arguments themselves were not a big change on the code, that could lead to instabilities (like _startAndWaitPrograms).
But, now that you said, I agree that makes much more sense to remove them. I will just pass a empty string as an argument then, so the code will behave exactly as before.

win.executableFile,
win.desktopFilePath,
win.executableArgs
);
});

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)
var activeWindows = await getActiveWindowListFlow();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer using 'letorconst`.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, will change them.

var 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, []);

var windowsNotStarted = windowList.filter(win => win.windowId === null);
var windowStarted = windowList.filter(win => win.windowId !== null);
var 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[]) {
var windowsToStart = [];
for (var 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[]
) {
var 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 (var 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 @@ -554,10 +693,10 @@ async function _restoreWindowPositions(
): Promise<void> {
const promises = [];
let last_desktop_nr = 0;

// Sort the window objects based on which workspace they are locate,
// so the windows can be moved workspace by workspace
// This is needed because the window manager just creates an aditional workspace when
// This is needed because the window manager just creates an aditional workspace when
// the previous one has some window on it.
savedWindowList = savedWindowList.concat().sort((a, b) => {
return a.wmCurrentDesktopNr - b.wmCurrentDesktopNr;
Expand All @@ -566,11 +705,13 @@ 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
if ( (win.wmCurrentDesktopNr !== last_desktop_nr) || (win === savedWindowList.slice(-1)[0])) {
if (
win.wmCurrentDesktopNr != last_desktop_nr ||
win == savedWindowList.slice(-1)[0]
) {
for (const promise of promises) {
try {
await promise;
Expand All @@ -579,7 +720,7 @@ async function _restoreWindowPositions(
}
}
last_desktop_nr = win.wmCurrentDesktopNr;
promises.length = 0
promises.length = 0;
}
}
}
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