diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-29 16:51:36 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:11:08 -0400 |
commit | 6836074fed83255438960fdc9597532d8bcae4bd (patch) | |
tree | afc1f5292e934ef012800c6575f7fb8e0e303eee /sway/ipc-server.c | |
parent | Fixed laggy focused output boolean (diff) | |
download | sway-6836074fed83255438960fdc9597532d8bcae4bd.tar.gz sway-6836074fed83255438960fdc9597532d8bcae4bd.tar.zst sway-6836074fed83255438960fdc9597532d8bcae4bd.zip |
Implement enough IPC for swaybar to work
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r-- | sway/ipc-server.c | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 156de380..408ed432 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -21,6 +21,7 @@ | |||
21 | #include "sway/ipc-server.h" | 21 | #include "sway/ipc-server.h" |
22 | #include "sway/server.h" | 22 | #include "sway/server.h" |
23 | #include "sway/input/input-manager.h" | 23 | #include "sway/input/input-manager.h" |
24 | #include "sway/input/seat.h" | ||
24 | #include "list.h" | 25 | #include "list.h" |
25 | #include "log.h" | 26 | #include "log.h" |
26 | 27 | ||
@@ -279,6 +280,31 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event) | |||
279 | } | 280 | } |
280 | } | 281 | } |
281 | 282 | ||
283 | void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) { | ||
284 | wlr_log(L_DEBUG, "Sending workspace::%s event", change); | ||
285 | json_object *obj = json_object_new_object(); | ||
286 | json_object_object_add(obj, "change", json_object_new_string(change)); | ||
287 | if (strcmp("focus", change) == 0) { | ||
288 | if (old) { | ||
289 | json_object_object_add(obj, "old", | ||
290 | ipc_json_describe_container_recursive(old)); | ||
291 | } else { | ||
292 | json_object_object_add(obj, "old", NULL); | ||
293 | } | ||
294 | } | ||
295 | |||
296 | if (new) { | ||
297 | json_object_object_add(obj, "current", | ||
298 | ipc_json_describe_container_recursive(new)); | ||
299 | } else { | ||
300 | json_object_object_add(obj, "current", NULL); | ||
301 | } | ||
302 | |||
303 | const char *json_string = json_object_to_json_string(obj); | ||
304 | ipc_send_event(json_string, IPC_EVENT_WORKSPACE); | ||
305 | json_object_put(obj); | ||
306 | } | ||
307 | |||
282 | void ipc_event_window(swayc_t *window, const char *change) { | 308 | void ipc_event_window(swayc_t *window, const char *change) { |
283 | wlr_log(L_DEBUG, "Sending window::%s event", change); | 309 | wlr_log(L_DEBUG, "Sending window::%s event", change); |
284 | json_object *obj = json_object_new_object(); | 310 | json_object *obj = json_object_new_object(); |
@@ -357,6 +383,25 @@ void ipc_client_disconnect(struct ipc_client *client) { | |||
357 | free(client); | 383 | free(client); |
358 | } | 384 | } |
359 | 385 | ||
386 | static void ipc_get_workspaces_callback(swayc_t *workspace, void *data) { | ||
387 | if (workspace->type == C_WORKSPACE) { | ||
388 | json_object *workspace_json = ipc_json_describe_container(workspace); | ||
389 | // override the default focused indicator because | ||
390 | // it's set differently for the get_workspaces reply | ||
391 | struct sway_seat *seat = | ||
392 | sway_input_manager_get_default_seat(input_manager); | ||
393 | swayc_t *focused_ws = sway_seat_get_focus(seat); | ||
394 | if (focused_ws->type != C_WORKSPACE) { | ||
395 | focused_ws = swayc_parent_by_type(focused_ws, C_WORKSPACE); | ||
396 | } | ||
397 | bool focused = workspace == focused_ws; | ||
398 | json_object_object_del(workspace_json, "focused"); | ||
399 | json_object_object_add(workspace_json, "focused", | ||
400 | json_object_new_boolean(focused)); | ||
401 | json_object_array_add((json_object *)data, workspace_json); | ||
402 | } | ||
403 | } | ||
404 | |||
360 | void ipc_client_handle_command(struct ipc_client *client) { | 405 | void ipc_client_handle_command(struct ipc_client *client) { |
361 | if (!sway_assert(client != NULL, "client != NULL")) { | 406 | if (!sway_assert(client != NULL, "client != NULL")) { |
362 | return; | 407 | return; |
@@ -412,6 +457,16 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
412 | goto exit_cleanup; | 457 | goto exit_cleanup; |
413 | } | 458 | } |
414 | 459 | ||
460 | case IPC_GET_WORKSPACES: | ||
461 | { | ||
462 | json_object *workspaces = json_object_new_array(); | ||
463 | container_map(&root_container, ipc_get_workspaces_callback, workspaces); | ||
464 | const char *json_string = json_object_to_json_string(workspaces); | ||
465 | ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); | ||
466 | json_object_put(workspaces); // free | ||
467 | goto exit_cleanup; | ||
468 | } | ||
469 | |||
415 | case IPC_SUBSCRIBE: | 470 | case IPC_SUBSCRIBE: |
416 | { | 471 | { |
417 | // TODO: Check if they're permitted to use these events | 472 | // TODO: Check if they're permitted to use these events |
@@ -446,7 +501,6 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
446 | } | 501 | } |
447 | 502 | ||
448 | json_object_put(request); | 503 | json_object_put(request); |
449 | |||
450 | ipc_send_reply(client, "{\"success\": true}", 17); | 504 | ipc_send_reply(client, "{\"success\": true}", 17); |
451 | goto exit_cleanup; | 505 | goto exit_cleanup; |
452 | } | 506 | } |
@@ -483,6 +537,43 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
483 | goto exit_cleanup; | 537 | goto exit_cleanup; |
484 | } | 538 | } |
485 | 539 | ||
540 | case IPC_GET_BAR_CONFIG: | ||
541 | { | ||
542 | if (!buf[0]) { | ||
543 | // Send list of configured bar IDs | ||
544 | json_object *bars = json_object_new_array(); | ||
545 | int i; | ||
546 | for (i = 0; i < config->bars->length; ++i) { | ||
547 | struct bar_config *bar = config->bars->items[i]; | ||
548 | json_object_array_add(bars, json_object_new_string(bar->id)); | ||
549 | } | ||
550 | const char *json_string = json_object_to_json_string(bars); | ||
551 | ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); | ||
552 | json_object_put(bars); // free | ||
553 | } else { | ||
554 | // Send particular bar's details | ||
555 | struct bar_config *bar = NULL; | ||
556 | int i; | ||
557 | for (i = 0; i < config->bars->length; ++i) { | ||
558 | bar = config->bars->items[i]; | ||
559 | if (strcmp(buf, bar->id) == 0) { | ||
560 | break; | ||
561 | } | ||
562 | bar = NULL; | ||
563 | } | ||
564 | if (!bar) { | ||
565 | const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }"; | ||
566 | ipc_send_reply(client, error, (uint32_t)strlen(error)); | ||
567 | goto exit_cleanup; | ||
568 | } | ||
569 | json_object *json = ipc_json_describe_bar_config(bar); | ||
570 | const char *json_string = json_object_to_json_string(json); | ||
571 | ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); | ||
572 | json_object_put(json); // free | ||
573 | } | ||
574 | goto exit_cleanup; | ||
575 | } | ||
576 | |||
486 | default: | 577 | default: |
487 | wlr_log(L_INFO, "Unknown IPC command type %i", client->current_command); | 578 | wlr_log(L_INFO, "Unknown IPC command type %i", client->current_command); |
488 | goto exit_cleanup; | 579 | goto exit_cleanup; |