swayc_t *workspace_by_number(const char* name) { struct workspace_by_number_data wbnd = {0, "1234567890", name}; wbnd.len = strspn(name, wbnd.cset); if (wbnd.len <= 0) { return NULL; } return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); }
swayc_t *workspace_by_name(const char* name) { if (strcmp(name, "prev") == 0) { return workspace_prev(); } else if (strcmp(name, "prev_on_output") == 0) { return workspace_output_prev(); } else if (strcmp(name, "next") == 0) { return workspace_next(); } else if (strcmp(name, "next_on_output") == 0) { return workspace_output_next(); } else if (strcmp(name, "current") == 0) { return swayc_active_workspace(); } else { return swayc_by_test(&root_container, _workspace_by_name, (void *) name); } }
void ipc_client_handle_command(struct ipc_client *client) { if (!sway_assert(client != NULL, "client != NULL")) { return; } char *buf = malloc(client->payload_length + 1); if (!buf) { sway_log_errno(L_INFO, "Out of memory"); ipc_client_disconnect(client); return; } if (client->payload_length > 0) { ssize_t received = recv(client->fd, buf, client->payload_length, 0); if (received == -1) { sway_log_errno(L_INFO, "Unable to receive payload from IPC client"); ipc_client_disconnect(client); free(buf); return; } } buf[client->payload_length] = '\0'; const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }"; switch (client->current_command) { case IPC_COMMAND: { if (!(config->ipc_policy & IPC_FEATURE_COMMAND)) { goto exit_denied; } struct cmd_results *results = handle_command(buf, CONTEXT_IPC); const char *json = cmd_results_to_json(results); char reply[256]; int length = snprintf(reply, sizeof(reply), "%s", json); ipc_send_reply(client, reply, (uint32_t) length); free_cmd_results(results); goto exit_cleanup; } case IPC_SUBSCRIBE: { struct json_object *request = json_tokener_parse(buf); if (request == NULL) { ipc_send_reply(client, "{\"success\": false}", 18); sway_log_errno(L_INFO, "Failed to read request"); goto exit_cleanup; } // parse requested event types for (int i = 0; i < json_object_array_length(request); i++) { const char *event_type = json_object_get_string(json_object_array_get_idx(request, i)); if (strcmp(event_type, "workspace") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE); } else if (strcmp(event_type, "barconfig_update") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE); } else if (strcmp(event_type, "mode") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_MODE); } else if (strcmp(event_type, "window") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_WINDOW); } else if (strcmp(event_type, "modifier") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER); } else if (strcmp(event_type, "binding") == 0) { client->subscribed_events |= event_mask(IPC_EVENT_BINDING); } else { ipc_send_reply(client, "{\"success\": false}", 18); json_object_put(request); sway_log_errno(L_INFO, "Failed to parse request"); goto exit_cleanup; } } json_object_put(request); ipc_send_reply(client, "{\"success\": true}", 17); goto exit_cleanup; } case IPC_GET_WORKSPACES: { if (!(config->ipc_policy & IPC_FEATURE_GET_WORKSPACES)) { goto exit_denied; } json_object *workspaces = json_object_new_array(); container_map(&root_container, ipc_get_workspaces_callback, workspaces); const char *json_string = json_object_to_json_string(workspaces); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); json_object_put(workspaces); // free goto exit_cleanup; } case IPC_GET_INPUTS: { if (!(config->ipc_policy & IPC_FEATURE_GET_INPUTS)) { goto exit_denied; } json_object *inputs = json_object_new_array(); if (input_devices) { for(int i=0; i<input_devices->length; i++) { struct libinput_device *device = input_devices->items[i]; char* identifier = libinput_dev_unique_id(device); json_object *device_object = json_object_new_object(); json_object_object_add(device_object, "identifier", json_object_new_string(identifier)); json_object_array_add(inputs, device_object); free(identifier); } } const char *json_string = json_object_to_json_string(inputs); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); json_object_put(inputs); goto exit_cleanup; } case IPC_GET_OUTPUTS: { if (!(config->ipc_policy & IPC_FEATURE_GET_OUTPUTS)) { goto exit_denied; } json_object *outputs = json_object_new_array(); container_map(&root_container, ipc_get_outputs_callback, outputs); const char *json_string = json_object_to_json_string(outputs); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); json_object_put(outputs); // free goto exit_cleanup; } case IPC_GET_TREE: { if (!(config->ipc_policy & IPC_FEATURE_GET_TREE)) { goto exit_denied; } json_object *tree = ipc_json_describe_container_recursive(&root_container); const char *json_string = json_object_to_json_string(tree); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); json_object_put(tree); goto exit_cleanup; } case IPC_GET_VERSION: { json_object *version = ipc_json_get_version(); const char *json_string = json_object_to_json_string(version); ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); json_object_put(version); // free goto exit_cleanup; } case IPC_SWAY_GET_PIXELS: { char response_header[9]; memset(response_header, 0, sizeof(response_header)); json_object *obj = json_tokener_parse(buf); json_object *o, *x, *y, *w, *h; json_object_object_get_ex(obj, "output", &o); json_object_object_get_ex(obj, "x", &x); json_object_object_get_ex(obj, "y", &y); json_object_object_get_ex(obj, "w", &w); json_object_object_get_ex(obj, "h", &h); struct wlc_geometry g = { .origin = { .x = json_object_get_int(x), .y = json_object_get_int(y) }, .size = { .w = json_object_get_int(w), .h = json_object_get_int(h) } }; swayc_t *output = swayc_by_test(&root_container, output_by_name_test, (void *)json_object_get_string(o)); json_object_put(obj); if (!output) { sway_log(L_ERROR, "IPC GET_PIXELS request with unknown output name"); ipc_send_reply(client, response_header, sizeof(response_header)); goto exit_cleanup; } struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request)); req->client = client; req->output = output->handle; req->geo = g; list_add(ipc_get_pixel_requests, req); wlc_output_schedule_render(output->handle); goto exit_cleanup; } case IPC_GET_BAR_CONFIG: { if (!(config->ipc_policy & IPC_FEATURE_GET_BAR_CONFIG)) { goto exit_denied; } if (!buf[0]) { // Send list of configured bar IDs json_object *bars = json_object_new_array(); int i; for (i = 0; i < config->bars->length; ++i) { struct bar_config *bar = config->bars->items[i]; json_object_array_add(bars, json_object_new_string(bar->id)); } const char *json_string = json_object_to_json_string(bars); ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); json_object_put(bars); // free } else { // Send particular bar's details struct bar_config *bar = NULL; int i; for (i = 0; i < config->bars->length; ++i) { bar = config->bars->items[i]; if (strcmp(buf, bar->id) == 0) { break; } bar = NULL; } if (!bar) { const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }"; ipc_send_reply(client, error, (uint32_t)strlen(error)); goto exit_cleanup; } json_object *json = ipc_json_describe_bar_config(bar); const char *json_string = json_object_to_json_string(json); ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); json_object_put(json); // free } goto exit_cleanup; } default: sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); goto exit_cleanup; }