Beispiel #1
0
void ipc_client_handle_command(struct ipc_client *client) {
	if (!sway_assert(client != NULL, "client != NULL")) {
		return;
	}

	char buf[client->payload_length + 1];
	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);
			return;
		}
	}

	switch (client->current_command) {
	case IPC_COMMAND:
	{
		buf[client->payload_length] = '\0';
		bool success = handle_command(config, buf);
		char reply[64];
		int length = snprintf(reply, sizeof(reply), "{\"success\":%s}", success ? "true" : "false");
		ipc_send_reply(client, reply, (uint32_t) length);
		break;
	}
	case IPC_GET_WORKSPACES:
	{
		list_t *workspaces = create_list();
		container_map(&root_container, ipc_get_workspaces_callback, workspaces);
		char *json = json_list(workspaces);
		free_flat_list(workspaces);
		ipc_send_reply(client, json, strlen(json));
		free(json);
		break;
	}
	case IPC_GET_OUTPUTS:
	{
		list_t *outputs = create_list();
		container_map(&root_container, ipc_get_outputs_callback, outputs);
		char *json = json_list(outputs);
		free_flat_list(outputs);
		ipc_send_reply(client, json, strlen(json));
		free(json);
		break;
	}
	default:
		sway_log(L_INFO, "Unknown IPC command type %i", client->current_command);
		ipc_client_disconnect(client);
		break;
	}

	client->payload_length = 0;
}
Beispiel #2
0
static json_t *
json_option(const struct nh_option_desc *option)
{
    int i;
    json_t *jopt, *joptval, *joptdesc, *jobj;
    struct nh_autopickup_rule *r;

    switch (option->type) {
    case OPTTYPE_BOOL:
        joptval = json_integer(option->value.b);
        joptdesc = json_object();
        break;

    case OPTTYPE_INT:
        joptval = json_integer(option->value.i);
        joptdesc =
            json_pack("{si,si}", "max", option->i.max, "min", option->i.min);
        break;

    case OPTTYPE_ENUM:
        joptval = json_integer(option->value.e);
        joptdesc = json_list(option->e.choices, option->e.numchoices);
        break;

    case OPTTYPE_STRING:
        joptval = json_string(option->value.s);
        joptdesc = json_integer(option->s.maxlen);
        break;

    case OPTTYPE_AUTOPICKUP_RULES:
        joptdesc = json_list(option->a.classes, option->a.numclasses);
        joptval = json_array();
        for (i = 0; option->value.ar && i < option->value.ar->num_rules; i++) {
            r = &option->value.ar->rules[i];
            jobj =
                json_pack("{ss,si,si,si}", "pattern", r->pattern, "oclass",
                          r->oclass, "buc", r->buc, "action", r->action);
            json_array_append_new(joptval, jobj);
        }
        break;

    default:
        joptdesc = json_string("<error: no description for option>");
        joptval = json_string("<error>");
        break;
    }

    jopt =
        json_pack("{ss,ss,si,so,so}", "name", option->name, "helptxt",
                  option->helptxt, "type", option->type, "value", joptval,
                  "desc", joptdesc);
    return jopt;
}
Beispiel #3
0
int main(void)
{
	myputs(xxx);
	_print("yyy");
	json_key("yyy");

	const char *list = "xxx, yyy, zzz";
	json_list(LIST);

}