summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin+git@gmail.com>2016-06-11 12:43:34 -0500
committerLibravatar Zandr Martin <zandrmartin+git@gmail.com>2016-06-11 12:43:34 -0500
commit2298143d09ce8810d9772f95e1cb605fb6b08536 (patch)
treed1b4fb33848d09c71602005c57919a903ce3d72d
parentMerge branch 'master' into assign-command (diff)
downloadsway-2298143d09ce8810d9772f95e1cb605fb6b08536.tar.gz
sway-2298143d09ce8810d9772f95e1cb605fb6b08536.tar.zst
sway-2298143d09ce8810d9772f95e1cb605fb6b08536.zip
cleanup + add timeouts for pid_workspace list
-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) {
74 char file_name[100]; 74 char file_name[100];
75 char *buffer = NULL; 75 char *buffer = NULL;
76 char *token = NULL; 76 char *token = NULL;
77 const char sep[2] = " "; 77 const char *sep = " ";
78 FILE *stat = NULL; 78 FILE *stat = NULL;
79 79
80 sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child);
81
82 sprintf(file_name, "/proc/%d/stat", child); 80 sprintf(file_name, "/proc/%d/stat", child);
83 81
84 if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { 82 if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) {
85 return -1; 83 fclose(stat);
86 }
87
88 fclose(stat);
89 84
90 sway_log(L_DEBUG, "buffer string is %s", buffer); 85 token = strtok(buffer, sep); // pid
86 token = strtok(NULL, sep); // executable name
87 token = strtok(NULL, sep); // state
88 token = strtok(NULL, sep); // parent pid
91 89
92 token = strtok(buffer, sep); 90 parent = strtol(token, NULL, 10);
93 91 return (parent == child) ? -1 : parent;
94 for (int i = 0; i < 3; i++) {
95 token = strtok(NULL, sep);
96 } 92 }
97 93
98 parent = strtol(token, NULL, 10); 94 return -1;
99
100 sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child);
101
102 return (parent == child) ? -1 : parent;
103} 95}
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 @@
1#ifndef _SWAY_CONFIG_H 1#ifndef _SWAY_CONFIG_H
2#define _SWAY_CONFIG_H 2#define _SWAY_CONFIG_H
3 3
4#define PID_WORKSPACE_TIMEOUT 60
5
4#include <libinput.h> 6#include <libinput.h>
5#include <stdint.h> 7#include <stdint.h>
6#include <wlc/geometry.h> 8#include <wlc/geometry.h>
7#include <wlc/wlc.h> 9#include <wlc/wlc.h>
8#include <xkbcommon/xkbcommon.h> 10#include <xkbcommon/xkbcommon.h>
11#include <time.h>
9#include "wayland-desktop-shell-server-protocol.h" 12#include "wayland-desktop-shell-server-protocol.h"
10#include "list.h" 13#include "list.h"
11#include "layout.h" 14#include "layout.h"
@@ -95,8 +98,10 @@ struct workspace_output {
95struct pid_workspace { 98struct pid_workspace {
96 pid_t *pid; 99 pid_t *pid;
97 char *workspace; 100 char *workspace;
101 time_t *time_added;
98}; 102};
99 103
104void pid_workspace_add(struct pid_workspace *pw);
100void free_pid_workspace(struct pid_workspace *pw); 105void free_pid_workspace(struct pid_workspace *pw);
101 106
102struct bar_config { 107struct 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) {
555 struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); 555 struct pid_workspace *pw = malloc(sizeof(struct pid_workspace));
556 pw->pid = child; 556 pw->pid = child;
557 pw->workspace = strdup(ws->name); 557 pw->workspace = strdup(ws->name);
558 list_add(config->pid_workspaces, pw); 558 pid_workspace_add(pw);
559 // TODO: keep track of this pid and open the corresponding view on the current workspace 559 // TODO: keep track of this pid and open the corresponding view on the current workspace
560 // blocked pending feature in wlc 560 // blocked pending feature in wlc
561 } else { 561 } 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) {
89 free(wo); 89 free(wo);
90} 90}
91 91
92static void pid_workspace_cleanup() {
93 struct timespec ts;
94 struct pid_workspace *pw = NULL;
95
96 clock_gettime(CLOCK_MONOTONIC, &ts);
97
98 // work backwards through list and remove any entries
99 // older than PID_WORKSPACE_TIMEOUT
100 for (int i = config->pid_workspaces->length - 1; i > -1; i--) {
101 pw = config->pid_workspaces->items[i];
102
103 if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) {
104 list_del(config->pid_workspaces, i);
105 }
106 }
107}
108
109// de-dupe pid_workspaces to ensure pid uniqueness
110void pid_workspace_add(struct pid_workspace *pw) {
111 struct pid_workspace *list_pw = NULL;
112 struct timespec ts;
113 time_t *now = malloc(sizeof(time_t));
114
115 pid_workspace_cleanup();
116
117 // add current time to pw
118 clock_gettime(CLOCK_MONOTONIC, &ts);
119 *now = ts.tv_sec;
120
121 pw->time_added = now;
122
123 // work backwards through list and delete any entries that
124 // have the same pid as that in our new pid_workspace
125 for (int i = config->pid_workspaces->length - 1; i > -1; i--) {
126 list_pw = config->pid_workspaces->items[i];
127
128 if (pw->pid == list_pw->pid) {
129 list_del(config->pid_workspaces, i);
130 }
131 }
132
133 list_add(config->pid_workspaces, pw);
134}
135
92void free_pid_workspace(struct pid_workspace *pw) { 136void free_pid_workspace(struct pid_workspace *pw) {
93 if (!pw) { 137 if (!pw) {
94 return; 138 return;
95 } 139 }
96 free(pw->pid); 140 free(pw->pid);
97 free(pw->workspace); 141 free(pw->workspace);
142 free(pw->time_added);
98 free(pw); 143 free(pw);
99} 144}
100 145
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) {
322 // sway_log(L_DEBUG, "all pid_workspaces"); 322 // sway_log(L_DEBUG, "all pid_workspaces");
323 // for (int k = 0; k < config->pid_workspaces->length; k++) { 323 // for (int k = 0; k < config->pid_workspaces->length; k++) {
324 // pw = config->pid_workspaces->items[k]; 324 // pw = config->pid_workspaces->items[k];
325 // sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); 325 // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added);
326 // } 326 // }
327 327
328 do { 328 do {
@@ -352,13 +352,12 @@ swayc_t *workspace_for_pid(pid_t pid) {
352 ws = workspace_by_name(pw->workspace); 352 ws = workspace_by_name(pw->workspace);
353 353
354 if (!ws) { 354 if (!ws) {
355 sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); 355 sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid);
356 ws = workspace_create(pw->workspace); 356 ws = workspace_create(pw->workspace);
357 } 357 }
358 358
359 list_del(config->pid_workspaces, i); 359 list_del(config->pid_workspaces, i);
360 } 360 }
361 361
362 free_pid_workspace(pw);
363 return ws; 362 return ws;
364} 363}