added query for swaysock through pidof

This commit is contained in:
mazunki 2022-03-18 04:16:40 +01:00
parent 440d0bc22d
commit 08cd8ee5cb

View file

@ -87,6 +87,24 @@ void detect_proprietary(int allow_unsupported_gpu) {
fclose(f); 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) { void run_as_ipc_client(char *command, char *socket_path) {
int socketfd = ipc_open_socket(socket_path); int socketfd = ipc_open_socket(socket_path);
uint32_t len = strlen(command); uint32_t len = strlen(command);
@ -102,6 +120,7 @@ static void log_env(void) {
"LD_PRELOAD", "LD_PRELOAD",
"PATH", "PATH",
"SWAYSOCK", "SWAYSOCK",
"XDG_RUNTIME_DIR",
}; };
for (size_t i = 0; i < sizeof(log_vars) / sizeof(char *); ++i) { for (size_t i = 0; i < sizeof(log_vars) / sizeof(char *); ++i) {
char *value = getenv(log_vars[i]); char *value = getenv(log_vars[i]);
@ -307,7 +326,20 @@ int main(int argc, char **argv) {
case 'p': ; // --get-socketpath case 'p': ; // --get-socketpath
if (getenv("SWAYSOCK")) { if (getenv("SWAYSOCK")) {
printf("%s\n", 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 { } else {
fprintf(stderr, "sway socket not detected.\n"); fprintf(stderr, "sway socket not detected.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);