sway/swaymsg/main.c

106 lines
2.3 KiB
C
Raw Normal View History

2015-11-26 18:41:24 +01:00
#include <stdio.h>
#include <stdlib.h>
2015-11-26 20:31:29 +01:00
#include <string.h>
#include <getopt.h>
#include <stdint.h>
2015-11-26 21:06:41 +01:00
#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>
2015-11-26 20:31:29 +01:00
#include "stringop.h"
#include "ipc-client.h"
2015-11-26 20:31:29 +01:00
#include "readline.h"
#include "log.h"
2015-11-26 18:41:24 +01:00
void sway_terminate(void) {
exit(1);
}
2015-11-26 20:31:29 +01:00
int main(int argc, char **argv) {
static int quiet = 0;
char *socket_path = NULL;
char *cmdtype = NULL;
2015-11-26 21:06:41 +01:00
init_log(L_INFO);
2015-11-26 20:31:29 +01:00
static struct option long_options[] = {
{"quiet", no_argument, &quiet, 'q'},
{"version", no_argument, NULL, 'v'},
{"socket", required_argument, NULL, 's'},
{"type", required_argument, NULL, 't'},
{0, 0, 0, 0}
};
int c;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "qvs:t:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 0: // Flag
break;
case 's': // Socket
socket_path = strdup(optarg);
break;
case 't': // Type
cmdtype = strdup(optarg);
break;
case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
fprintf(stdout, "version not detected\n");
#endif
exit(0);
break;
}
}
if (!cmdtype) {
2015-11-26 21:06:41 +01:00
cmdtype = strdup("command");
2015-11-26 20:31:29 +01:00
}
if (!socket_path) {
socket_path = get_socketpath();
if (!socket_path) {
sway_abort("Unable to retrieve socket path");
}
}
2015-11-27 15:42:24 +01:00
uint32_t type = IPC_COMMAND;
2015-11-26 20:31:29 +01:00
if (strcasecmp(cmdtype, "command") == 0) {
type = IPC_COMMAND;
} else if (strcasecmp(cmdtype, "get_workspaces") == 0) {
type = IPC_GET_WORKSPACES;
} else if (strcasecmp(cmdtype, "get_outputs") == 0) {
type = IPC_GET_OUTPUTS;
} else if (strcasecmp(cmdtype, "get_tree") == 0) {
type = IPC_GET_TREE;
} else if (strcasecmp(cmdtype, "get_marks") == 0) {
type = IPC_GET_MARKS;
} else if (strcasecmp(cmdtype, "get_bar_config") == 0) {
type = IPC_GET_BAR_CONFIG;
} else if (strcasecmp(cmdtype, "get_version") == 0) {
type = IPC_GET_VERSION;
2015-11-27 15:42:24 +01:00
} else {
sway_abort("Unknown message type %s", cmdtype);
2015-11-26 20:31:29 +01:00
}
2015-11-26 21:06:41 +01:00
free(cmdtype);
2015-11-26 20:31:29 +01:00
char *command = strdup("");
if (optind < argc) {
command = join_args(argv + optind, argc - optind);
}
char *resp = ipc_single_command(socket_path, type, command, strlen(command));
2015-11-26 21:06:41 +01:00
if (!quiet) {
printf("%s", resp);
}
free(command);
free(resp);
2015-11-26 20:31:29 +01:00
free(socket_path);
2015-11-26 18:41:24 +01:00
return 0;
}