2017-03-11 04:41:24 +00:00
|
|
|
#define _XOPEN_SOURCE 500
|
2015-11-26 17:41:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-11-26 19:31:29 +00:00
|
|
|
#include <string.h>
|
2017-03-11 04:41:24 +00:00
|
|
|
#include <strings.h>
|
2015-11-26 19:31:29 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdint.h>
|
2015-11-26 20:06:41 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/socket.h>
|
2017-04-03 11:27:25 +00:00
|
|
|
#include <ctype.h>
|
2015-11-26 20:06:41 +00:00
|
|
|
#include <unistd.h>
|
2016-07-31 18:45:53 +00:00
|
|
|
#include <json-c/json.h>
|
2015-11-26 19:31:29 +00:00
|
|
|
#include "stringop.h"
|
2015-11-27 14:50:04 +00:00
|
|
|
#include "ipc-client.h"
|
2015-11-26 19:31:29 +00:00
|
|
|
#include "readline.h"
|
|
|
|
#include "log.h"
|
2015-11-26 17:41:24 +00:00
|
|
|
|
2016-02-26 08:08:05 +00:00
|
|
|
void sway_terminate(int exit_code) {
|
|
|
|
exit(exit_code);
|
2015-11-26 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 11:27:25 +00:00
|
|
|
static void pretty_print_cmd(json_object *r) {
|
|
|
|
bool _success;
|
|
|
|
json_object *success;
|
|
|
|
if (!json_object_object_get_ex(r, "success", &success)) {
|
|
|
|
_success = true;
|
|
|
|
} else {
|
|
|
|
_success = json_object_get_boolean(success);
|
|
|
|
}
|
|
|
|
if (!_success) {
|
|
|
|
json_object *error;
|
|
|
|
if (!json_object_object_get_ex(r, "error", &error)) {
|
|
|
|
printf("An unknkown error occured");
|
|
|
|
} else {
|
|
|
|
printf("Error: %s\n", json_object_get_string(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print_workspace(json_object *w) {
|
|
|
|
json_object *name, *rect, *visible, *output, *urgent, *layout, *focused;
|
|
|
|
json_object_object_get_ex(w, "name", &name);
|
|
|
|
json_object_object_get_ex(w, "rect", &rect);
|
|
|
|
json_object_object_get_ex(w, "visible", &visible);
|
|
|
|
json_object_object_get_ex(w, "output", &output);
|
|
|
|
json_object_object_get_ex(w, "urgent", &urgent);
|
|
|
|
json_object_object_get_ex(w, "layout", &layout);
|
|
|
|
json_object_object_get_ex(w, "focused", &focused);
|
|
|
|
printf(
|
|
|
|
"Workspace %s%s%s%s\n"
|
|
|
|
" Output: %s\n"
|
|
|
|
" Layout: %s\n\n",
|
|
|
|
json_object_get_string(name),
|
|
|
|
json_object_get_boolean(focused) ? " (focused)" : "",
|
|
|
|
!json_object_get_boolean(visible) ? " (off-screen)" : "",
|
|
|
|
json_object_get_boolean(urgent) ? " (urgent)" : "",
|
|
|
|
json_object_get_string(output),
|
|
|
|
json_object_get_string(layout)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print_input(json_object *i) {
|
|
|
|
json_object *id, *name, *size, *caps;
|
|
|
|
json_object_object_get_ex(i, "identifier", &id);
|
|
|
|
json_object_object_get_ex(i, "name", &name);
|
|
|
|
json_object_object_get_ex(i, "size", &size);
|
|
|
|
json_object_object_get_ex(i, "capabilities", &caps);
|
|
|
|
|
|
|
|
printf( "Input device %s\n Type: ", json_object_get_string(name));
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *a;
|
|
|
|
const char *b;
|
|
|
|
} cap_names[] = {
|
|
|
|
{ "keyboard", "Keyboard" },
|
|
|
|
{ "pointer", "Mouse" },
|
|
|
|
{ "touch", "Touch" },
|
|
|
|
{ "tablet_tool", "Tablet tool" },
|
|
|
|
{ "tablet_pad", "Tablet pad" },
|
|
|
|
{ "gesture", "Gesture" },
|
|
|
|
{ "switch", "Switch" },
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t len = json_object_array_length(caps);
|
|
|
|
if (len == 0) {
|
|
|
|
printf("Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object *cap;
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
cap = json_object_array_get_idx(caps, i);
|
|
|
|
const char *cap_s = json_object_get_string(cap);
|
|
|
|
const char *_name = NULL;
|
|
|
|
for (size_t j = 0; j < sizeof(cap_names) / sizeof(cap_names[0]); ++i) {
|
|
|
|
if (strcmp(cap_names[i].a, cap_s) == 0) {
|
|
|
|
_name = cap_names[i].b;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s%s", _name ? _name : cap_s, len > 1 && i != len - 1 ? ", " : "");
|
|
|
|
}
|
|
|
|
printf("\n Sway ID: %s\n", json_object_get_string(id));
|
|
|
|
if (size) {
|
|
|
|
json_object *width, *height;
|
|
|
|
json_object_object_get_ex(size, "width", &width);
|
|
|
|
json_object_object_get_ex(size, "height", &height);
|
|
|
|
printf(" Size: %lfmm x %lfmm\n",
|
|
|
|
json_object_get_double(width), json_object_get_double(height));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print_output(json_object *o) {
|
|
|
|
json_object *name, *rect, *focused, *active, *ws, *scale;
|
|
|
|
json_object_object_get_ex(o, "name", &name);
|
|
|
|
json_object_object_get_ex(o, "rect", &rect);
|
|
|
|
json_object_object_get_ex(o, "focused", &focused);
|
|
|
|
json_object_object_get_ex(o, "active", &active);
|
|
|
|
json_object_object_get_ex(o, "current_workspace", &ws);
|
|
|
|
json_object_object_get_ex(o, "scale", &scale);
|
|
|
|
json_object *x, *y, *width, *height;
|
|
|
|
json_object_object_get_ex(rect, "x", &x);
|
|
|
|
json_object_object_get_ex(rect, "y", &y);
|
|
|
|
json_object_object_get_ex(rect, "width", &width);
|
|
|
|
json_object_object_get_ex(rect, "height", &height);
|
|
|
|
printf(
|
|
|
|
"Output %s%s%s\n"
|
|
|
|
" Geometry: %dx%d @ %d,%d\n"
|
|
|
|
" Scale factor: %dx\n"
|
|
|
|
" Workspace: %s\n\n",
|
|
|
|
json_object_get_string(name),
|
|
|
|
json_object_get_boolean(focused) ? " (focused)" : "",
|
|
|
|
!json_object_get_boolean(active) ? " (inactive)" : "",
|
|
|
|
json_object_get_int(width), json_object_get_int(height),
|
|
|
|
json_object_get_int(x), json_object_get_int(y),
|
|
|
|
json_object_get_int(scale),
|
|
|
|
json_object_get_string(ws)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print_version(json_object *v) {
|
|
|
|
json_object *ver;
|
|
|
|
json_object_object_get_ex(v, "human_readable", &ver);
|
|
|
|
printf("sway version %s\n", json_object_get_string(ver));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print(int type, json_object *resp) {
|
|
|
|
if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES &&
|
|
|
|
type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS &&
|
|
|
|
type != IPC_GET_VERSION) {
|
|
|
|
printf("%s\n", json_object_to_json_string_ext(resp,
|
|
|
|
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == IPC_GET_VERSION) {
|
|
|
|
pretty_print_version(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object *obj;
|
|
|
|
size_t len = json_object_array_length(resp);
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
obj = json_object_array_get_idx(resp, i);
|
|
|
|
switch (type) {
|
|
|
|
case IPC_COMMAND:
|
|
|
|
pretty_print_cmd(obj);
|
|
|
|
break;
|
|
|
|
case IPC_GET_WORKSPACES:
|
|
|
|
pretty_print_workspace(obj);
|
|
|
|
break;
|
|
|
|
case IPC_GET_INPUTS:
|
|
|
|
pretty_print_input(obj);
|
|
|
|
break;
|
|
|
|
case IPC_GET_OUTPUTS:
|
|
|
|
pretty_print_output(obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:31:29 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
static int quiet = 0;
|
2017-04-03 11:27:25 +00:00
|
|
|
static int raw = 0;
|
2015-11-26 19:31:29 +00:00
|
|
|
char *socket_path = NULL;
|
|
|
|
char *cmdtype = NULL;
|
|
|
|
|
2015-11-26 20:06:41 +00:00
|
|
|
init_log(L_INFO);
|
|
|
|
|
2015-11-26 19:31:29 +00:00
|
|
|
static struct option long_options[] = {
|
2015-11-28 14:18:46 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2015-11-28 14:35:44 +00:00
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
2017-04-03 11:27:25 +00:00
|
|
|
{"raw", no_argument, NULL, 'r'},
|
2015-11-26 19:31:29 +00:00
|
|
|
{"socket", required_argument, NULL, 's'},
|
|
|
|
{"type", required_argument, NULL, 't'},
|
2017-04-03 11:27:25 +00:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
2015-11-26 19:31:29 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2015-11-28 14:09:19 +00:00
|
|
|
const char *usage =
|
|
|
|
"Usage: swaymsg [options] [message]\n"
|
|
|
|
"\n"
|
2015-11-28 14:18:46 +00:00
|
|
|
" -h, --help Show help message and quit.\n"
|
2015-11-28 14:09:19 +00:00
|
|
|
" -q, --quiet Be quiet.\n"
|
2017-04-03 11:27:25 +00:00
|
|
|
" -r, --raw Use raw output even if using a tty\n"
|
2015-11-28 14:09:19 +00:00
|
|
|
" -s, --socket <socket> Use the specified socket.\n"
|
2017-04-03 11:27:25 +00:00
|
|
|
" -t, --type <type> Specify the message type.\n"
|
|
|
|
" -v, --version Show the version number and quit.\n";
|
|
|
|
|
|
|
|
raw = !isatty(STDOUT_FILENO);
|
2015-11-28 14:09:19 +00:00
|
|
|
|
2015-11-26 19:31:29 +00:00
|
|
|
int c;
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
2017-04-03 11:27:25 +00:00
|
|
|
c = getopt_long(argc, argv, "hqrs:t:v", long_options, &option_index);
|
2015-11-26 19:31:29 +00:00
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
2015-11-28 14:35:44 +00:00
|
|
|
case 'q': // Quiet
|
|
|
|
quiet = 1;
|
2015-11-26 19:31:29 +00:00
|
|
|
break;
|
2017-04-03 11:27:25 +00:00
|
|
|
case 'r': // Raw
|
|
|
|
raw = 1;
|
|
|
|
break;
|
2015-11-26 19:31:29 +00:00
|
|
|
case 's': // Socket
|
|
|
|
socket_path = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 't': // Type
|
|
|
|
cmdtype = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
2017-04-26 09:29:30 +00:00
|
|
|
fprintf(stdout, "sway version " SWAY_VERSION "\n");
|
2015-11-28 13:47:44 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-11-26 19:31:29 +00:00
|
|
|
break;
|
2015-11-28 14:09:19 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s", usage);
|
|
|
|
exit(EXIT_FAILURE);
|
2015-11-26 19:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmdtype) {
|
2015-11-26 20:06:41 +00:00
|
|
|
cmdtype = strdup("command");
|
2015-11-26 19:31:29 +00:00
|
|
|
}
|
|
|
|
if (!socket_path) {
|
|
|
|
socket_path = get_socketpath();
|
|
|
|
if (!socket_path) {
|
|
|
|
sway_abort("Unable to retrieve socket path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:42:24 +00:00
|
|
|
uint32_t type = IPC_COMMAND;
|
2015-11-26 19:31:29 +00:00
|
|
|
|
|
|
|
if (strcasecmp(cmdtype, "command") == 0) {
|
|
|
|
type = IPC_COMMAND;
|
|
|
|
} else if (strcasecmp(cmdtype, "get_workspaces") == 0) {
|
|
|
|
type = IPC_GET_WORKSPACES;
|
2016-01-17 10:53:37 +00:00
|
|
|
} else if (strcasecmp(cmdtype, "get_inputs") == 0) {
|
|
|
|
type = IPC_GET_INPUTS;
|
2015-11-26 19:31:29 +00:00
|
|
|
} 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 14:42:24 +00:00
|
|
|
} else {
|
|
|
|
sway_abort("Unknown message type %s", cmdtype);
|
2015-11-26 19:31:29 +00:00
|
|
|
}
|
2015-11-26 20:06:41 +00:00
|
|
|
free(cmdtype);
|
2015-11-26 19:31:29 +00:00
|
|
|
|
|
|
|
char *command = strdup("");
|
|
|
|
if (optind < argc) {
|
|
|
|
command = join_args(argv + optind, argc - optind);
|
|
|
|
}
|
|
|
|
|
2016-07-31 18:45:53 +00:00
|
|
|
int ret = 0;
|
2015-11-27 15:10:29 +00:00
|
|
|
int socketfd = ipc_open_socket(socket_path);
|
|
|
|
uint32_t len = strlen(command);
|
|
|
|
char *resp = ipc_single_command(socketfd, type, command, &len);
|
2015-11-26 20:06:41 +00:00
|
|
|
if (!quiet) {
|
2016-07-31 18:45:53 +00:00
|
|
|
// pretty print the json
|
|
|
|
json_object *obj = json_tokener_parse(resp);
|
|
|
|
|
|
|
|
if (obj == NULL) {
|
|
|
|
fprintf(stderr, "ERROR: Could not parse json response from ipc. This is a bug in sway.");
|
|
|
|
printf("%s\n", resp);
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
2017-04-03 11:27:25 +00:00
|
|
|
if (raw) {
|
|
|
|
printf("%s\n", json_object_to_json_string_ext(obj,
|
|
|
|
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
|
|
|
|
} else {
|
|
|
|
pretty_print(type, obj);
|
|
|
|
}
|
2016-07-31 18:45:53 +00:00
|
|
|
free(obj);
|
|
|
|
}
|
2015-11-26 20:06:41 +00:00
|
|
|
}
|
2015-11-27 15:10:29 +00:00
|
|
|
close(socketfd);
|
2015-11-26 20:06:41 +00:00
|
|
|
|
|
|
|
free(command);
|
|
|
|
free(resp);
|
2015-11-26 19:31:29 +00:00
|
|
|
free(socket_path);
|
2016-07-31 18:45:53 +00:00
|
|
|
return ret;
|
2015-11-26 17:41:24 +00:00
|
|
|
}
|