aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/util.c28
-rw-r--r--include/config.h5
-rw-r--r--sway/commands.c2
-rw-r--r--sway/config.c45
-rw-r--r--sway/workspace.c5
5 files changed, 63 insertions, 22 deletions
diff --git a/common/util.c b/common/util.c
index e760443a..12cb7470 100644
--- a/common/util.c
+++ b/common/util.c
@@ -74,30 +74,22 @@ pid_t get_parent_pid(pid_t child) {
char file_name[100];
char *buffer = NULL;
char *token = NULL;
- const char sep[2] = " ";
+ const char *sep = " ";
FILE *stat = NULL;
- sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child);
-
sprintf(file_name, "/proc/%d/stat", child);
- if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) {
- return -1;
- }
-
- fclose(stat);
+ if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) {
+ fclose(stat);
- sway_log(L_DEBUG, "buffer string is %s", buffer);
+ token = strtok(buffer, sep); // pid
+ token = strtok(NULL, sep); // executable name
+ token = strtok(NULL, sep); // state
+ token = strtok(NULL, sep); // parent pid
- token = strtok(buffer, sep);
-
- for (int i = 0; i < 3; i++) {
- token = strtok(NULL, sep);
+ parent = strtol(token, NULL, 10);
+ return (parent == child) ? -1 : parent;
}
- parent = strtol(token, NULL, 10);
-
- sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child);
-
- return (parent == child) ? -1 : parent;
+ return -1;
}
diff --git a/include/config.h b/include/config.h
index 35797ac2..bf278ddb 100644
--- a/include/config.h
+++ b/include/config.h
@@ -1,11 +1,14 @@
#ifndef _SWAY_CONFIG_H
#define _SWAY_CONFIG_H
+#define PID_WORKSPACE_TIMEOUT 60
+
#include <libinput.h>
#include <stdint.h>
#include <wlc/geometry.h>
#include <wlc/wlc.h>
#include <xkbcommon/xkbcommon.h>
+#include <time.h>
#include "wayland-desktop-shell-server-protocol.h"
#include "list.h"
#include "layout.h"
@@ -95,8 +98,10 @@ struct workspace_output {
struct pid_workspace {
pid_t *pid;
char *workspace;
+ time_t *time_added;
};
+void pid_workspace_add(struct pid_workspace *pw);
void free_pid_workspace(struct pid_workspace *pw);
struct bar_config {
diff --git a/sway/commands.c b/sway/commands.c
index 3a6b2af5..5e84ea9a 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -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 {
diff --git a/sway/config.c b/sway/config.c
index 07b1f2f7..819a70ce 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -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);
}
diff --git a/sway/workspace.c b/sway/workspace.c
index 0c5c70a3..5319aec4 100644
--- a/sway/workspace.c
+++ b/sway/workspace.c
@@ -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;
}