cleanup + add timeouts for pid_workspace list

This commit is contained in:
Zandr Martin 2016-06-11 12:43:34 -05:00
parent 66caee645c
commit 2298143d09
No known key found for this signature in database
GPG key ID: AA2BB8EF77F7BBDC
5 changed files with 65 additions and 24 deletions

View file

@ -555,7 +555,7 @@ static struct cmd_results *cmd_exec_always(int argc, char **argv) {
struct pid_workspace *pw = malloc(sizeof(struct pid_workspace));
pw->pid = child;
pw->workspace = strdup(ws->name);
list_add(config->pid_workspaces, pw);
pid_workspace_add(pw);
// TODO: keep track of this pid and open the corresponding view on the current workspace
// blocked pending feature in wlc
} else {

View file

@ -89,12 +89,57 @@ static void free_workspace_output(struct workspace_output *wo) {
free(wo);
}
static void pid_workspace_cleanup() {
struct timespec ts;
struct pid_workspace *pw = NULL;
clock_gettime(CLOCK_MONOTONIC, &ts);
// work backwards through list and remove any entries
// older than PID_WORKSPACE_TIMEOUT
for (int i = config->pid_workspaces->length - 1; i > -1; i--) {
pw = config->pid_workspaces->items[i];
if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) {
list_del(config->pid_workspaces, i);
}
}
}
// de-dupe pid_workspaces to ensure pid uniqueness
void pid_workspace_add(struct pid_workspace *pw) {
struct pid_workspace *list_pw = NULL;
struct timespec ts;
time_t *now = malloc(sizeof(time_t));
pid_workspace_cleanup();
// add current time to pw
clock_gettime(CLOCK_MONOTONIC, &ts);
*now = ts.tv_sec;
pw->time_added = now;
// work backwards through list and delete any entries that
// have the same pid as that in our new pid_workspace
for (int i = config->pid_workspaces->length - 1; i > -1; i--) {
list_pw = config->pid_workspaces->items[i];
if (pw->pid == list_pw->pid) {
list_del(config->pid_workspaces, i);
}
}
list_add(config->pid_workspaces, pw);
}
void free_pid_workspace(struct pid_workspace *pw) {
if (!pw) {
return;
}
free(pw->pid);
free(pw->workspace);
free(pw->time_added);
free(pw);
}

View file

@ -322,7 +322,7 @@ swayc_t *workspace_for_pid(pid_t pid) {
// sway_log(L_DEBUG, "all pid_workspaces");
// for (int k = 0; k < config->pid_workspaces->length; k++) {
// pw = config->pid_workspaces->items[k];
// sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace);
// sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added);
// }
do {
@ -352,13 +352,12 @@ swayc_t *workspace_for_pid(pid_t pid) {
ws = workspace_by_name(pw->workspace);
if (!ws) {
sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace);
sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid);
ws = workspace_create(pw->workspace);
}
list_del(config->pid_workspaces, i);
}
free_pid_workspace(pw);
return ws;
}