mirror of
https://github.com/swaywm/sway.git
synced 2026-04-22 06:46:27 -04:00
added query for swaysock through pidof
This commit is contained in:
parent
440d0bc22d
commit
08cd8ee5cb
1 changed files with 33 additions and 1 deletions
34
sway/main.c
34
sway/main.c
|
|
@ -87,6 +87,24 @@ void detect_proprietary(int allow_unsupported_gpu) {
|
|||
fclose(f);
|
||||
}
|
||||
|
||||
int query_for_swaypid(char *xdg_runtime_dir) {
|
||||
if (xdg_runtime_dir != NULL) {
|
||||
char pidline[1024];
|
||||
char *pid;
|
||||
FILE *fp = popen("pidof sway", "r");
|
||||
fgets(pidline, 1024, fp);
|
||||
pid = strtok(pidline, " ");
|
||||
int sway_pid = atoi(pid);
|
||||
if (strtok(NULL, " ") != NULL) {
|
||||
return -1;
|
||||
} else {
|
||||
return sway_pid;
|
||||
}
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
};
|
||||
}
|
||||
|
||||
void run_as_ipc_client(char *command, char *socket_path) {
|
||||
int socketfd = ipc_open_socket(socket_path);
|
||||
uint32_t len = strlen(command);
|
||||
|
|
@ -102,6 +120,7 @@ static void log_env(void) {
|
|||
"LD_PRELOAD",
|
||||
"PATH",
|
||||
"SWAYSOCK",
|
||||
"XDG_RUNTIME_DIR",
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(log_vars) / sizeof(char *); ++i) {
|
||||
char *value = getenv(log_vars[i]);
|
||||
|
|
@ -307,7 +326,20 @@ int main(int argc, char **argv) {
|
|||
case 'p': ; // --get-socketpath
|
||||
if (getenv("SWAYSOCK")) {
|
||||
printf("%s\n", getenv("SWAYSOCK"));
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (getenv("XDG_RUNTIME_DIR") && getuid()) {
|
||||
int swaypid = query_for_swaypid(getenv("XDG_RUNTIME_DIR"));
|
||||
char *swaysock = {0};
|
||||
if (swaypid > 0) {
|
||||
sprintf(swaysock, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid);
|
||||
if (access(swaysock, F_OK) != -1) {
|
||||
printf("%s\n", swaysock);
|
||||
} else {
|
||||
fprintf(stderr, "sway socket not detected.\n"); // found a pid, but no socket file
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Found more than one sway instance running.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "sway socket not detected.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue