Merge pull request #137 from minus7/json

added json-c lib and implemented IPC get_version
This commit is contained in:
Drew DeVault 2015-08-26 18:48:50 -04:00
commit eb53f173c5
4 changed files with 77 additions and 72 deletions

17
CMake/FindJsonC.cmake Normal file
View file

@ -0,0 +1,17 @@
# - Find json-c
# Find the json-c libraries
#
# This module defines the following variables:
# JSONC_FOUND - True if JSONC is found
# JSONC_LIBRARIES - JSONC libraries
# JSONC_INCLUDE_DIRS - JSONC include directories
#
find_package(PkgConfig)
pkg_check_modules(PC_JSONC QUIET JSONC)
find_path(JSONC_INCLUDE_DIRS NAMES json-c/json.h HINTS ${PC_JSONC_INCLUDE_DIRS})
find_library(JSONC_LIBRARIES NAMES json-c HINTS ${PC_JSONC_LIBRARY_DIRS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(JSONC DEFAULT_MSG JSONC_LIBRARIES JSONC_INCLUDE_DIRS)
mark_as_advanced(JSONC_LIBRARIES JSONC_INCLUDE_DIRS)

View file

@ -22,12 +22,14 @@ find_package(XKBCommon REQUIRED)
find_package(WLC REQUIRED) find_package(WLC REQUIRED)
find_package(A2X REQUIRED) find_package(A2X REQUIRED)
find_package(PCRE REQUIRED) find_package(PCRE REQUIRED)
find_package(JsonC REQUIRED)
FILE(GLOB sources ${PROJECT_SOURCE_DIR}/sway/*.c) FILE(GLOB sources ${PROJECT_SOURCE_DIR}/sway/*.c)
include_directories( include_directories(
${WLC_INCLUDE_DIRS} ${WLC_INCLUDE_DIRS}
${PCRE_INCLUDE_DIRS} ${PCRE_INCLUDE_DIRS}
${JSONC_INCLUDE_DIRS}
include/ include/
) )
@ -39,6 +41,7 @@ target_link_libraries(sway
${WLC_LIBRARIES} ${WLC_LIBRARIES}
${XKBCOMMON_LIBRARIES} ${XKBCOMMON_LIBRARIES}
${PCRE_LIBRARIES} ${PCRE_LIBRARIES}
${JSONC_LIBRARIES}
) )
INSTALL( INSTALL(

View file

@ -152,7 +152,7 @@ swayc_t *new_workspace(swayc_t *output, const char *name) {
workspace->width = output->width; workspace->width = output->width;
workspace->height = output->height; workspace->height = output->height;
workspace->name = strdup(name); workspace->name = strdup(name);
workspace->visible = true; workspace->visible = false;
workspace->floating = create_list(); workspace->floating = create_list();
add_child(output, workspace); add_child(output, workspace);
@ -503,18 +503,17 @@ bool swayc_is_active(swayc_t *view) {
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
if (container) { if (container) {
f(container, data);
int i; int i;
if (container->children) { if (container->children) {
for (i = 0; i < container->children->length; ++i) { for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
f(child, data);
container_map(child, f, data); container_map(child, f, data);
} }
} }
if (container->floating) { if (container->floating) {
for (i = 0; i < container->floating->length; ++i) { for (i = 0; i < container->floating->length; ++i) {
swayc_t *child = container->floating->items[i]; swayc_t *child = container->floating->items[i];
f(child, data);
container_map(child, f, data); container_map(child, f, data);
} }
} }

View file

@ -12,6 +12,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include <json-c/json.h>
#include "ipc.h" #include "ipc.h"
#include "log.h" #include "log.h"
#include "config.h" #include "config.h"
@ -40,11 +41,9 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
void ipc_client_disconnect(struct ipc_client *client); void ipc_client_disconnect(struct ipc_client *client);
void ipc_client_handle_command(struct ipc_client *client); void ipc_client_handle_command(struct ipc_client *client);
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length);
void ipc_get_workspaces_callback(swayc_t *container, void *data); void ipc_get_workspaces_callback(swayc_t *workspace, void *data);
void ipc_get_outputs_callback(swayc_t *container, void *data); void ipc_get_outputs_callback(swayc_t *container, void *data);
char *json_list(list_t *items);
void ipc_init(void) { void ipc_init(void) {
ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (ipc_socket == -1) { if (ipc_socket == -1) {
@ -208,22 +207,32 @@ void ipc_client_handle_command(struct ipc_client *client) {
} }
case IPC_GET_WORKSPACES: case IPC_GET_WORKSPACES:
{ {
list_t *workspaces = create_list(); json_object *workspaces = json_object_new_array();
container_map(&root_container, ipc_get_workspaces_callback, workspaces); container_map(&root_container, ipc_get_workspaces_callback, workspaces);
char *json = json_list(workspaces); const char *json_string = json_object_to_json_string(workspaces);
free_flat_list(workspaces); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
ipc_send_reply(client, json, strlen(json)); json_object_put(workspaces); // free
free(json);
break; break;
} }
case IPC_GET_OUTPUTS: case IPC_GET_OUTPUTS:
{ {
list_t *outputs = create_list(); json_object *outputs = json_object_new_array();
container_map(&root_container, ipc_get_outputs_callback, outputs); container_map(&root_container, ipc_get_outputs_callback, outputs);
char *json = json_list(outputs); const char *json_string = json_object_to_json_string(outputs);
free_flat_list(outputs); ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
ipc_send_reply(client, json, strlen(json)); json_object_put(outputs); // free
free(json); break;
}
case IPC_GET_VERSION:
{
json_object *json = json_object_new_object();
json_object_object_add(json, "human_readable", json_object_new_string(SWAY_GIT_VERSION));
json_object_object_add(json, "major", json_object_new_int(0));
json_object_object_add(json, "minor", json_object_new_int(0));
json_object_object_add(json, "patch", json_object_new_int(1));
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
break; break;
} }
default: default:
@ -259,67 +268,44 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
return true; return true;
} }
char *json_list(list_t *items) { void ipc_get_workspaces_callback(swayc_t *workspace, void *data) {
char *json_elements = join_list(items, ","); if (workspace->type == C_WORKSPACE) {
size_t len = strlen(json_elements); int num = isdigit(workspace->name[0]) ? atoi(workspace->name) : -1;
char *json = malloc(len + 3); json_object *object = json_object_new_object();
json[0] = '['; json_object *rect = json_object_new_object();
memcpy(json + 1, json_elements, len); json_object_object_add(rect, "x", json_object_new_int((int32_t) workspace->x));
json[len+1] = ']'; json_object_object_add(rect, "y", json_object_new_int((int32_t) workspace->y));
json[len+2] = '\0'; json_object_object_add(rect, "width", json_object_new_int((int32_t) workspace->width));
free(json_elements); json_object_object_add(rect, "height", json_object_new_int((int32_t) workspace->height));
return json;
}
void ipc_get_workspaces_callback(swayc_t *container, void *data) { json_object_object_add(object, "num", json_object_new_int(num));
if (container->type == C_WORKSPACE) { json_object_object_add(object, "name", json_object_new_string(workspace->name));
char *json = malloc(512); // Output should usually be around 180 chars json_object_object_add(object, "visible", json_object_new_boolean(workspace->visible));
int num = isdigit(container->name[0]) ? atoi(container->name) : -1; bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace;
// TODO: escape the name (quotation marks, unicode) json_object_object_add(object, "focused", json_object_new_boolean(focused));
sprintf(json, json_object_object_add(object, "rect", rect);
"{" json_object_object_add(object, "output", json_object_new_string(workspace->parent->name));
"\"num\":%d," json_object_object_add(object, "urgent", json_object_new_boolean(false));
"\"name\":\"%s\","
"\"visible\":%s," json_object_array_add((json_object *)data, object);
"\"focused\":%s,"
"\"rect\":{"
"\"x\":%d,"
"\"y\":%d,"
"\"width\":%d,"
"\"height\":%d"
"},"
"\"output\":\"%s\","
"\"urgent\":%s"
"}",
num, container->name, container->visible ? "true" : "false", container->is_focused ? "true" : "false",
(int)container->x, (int)container->y, (int)container->width, (int)container->height,
container->parent->name, "false" // TODO: urgent hint
);
list_add((list_t *)data, json);
} }
} }
void ipc_get_outputs_callback(swayc_t *container, void *data) { void ipc_get_outputs_callback(swayc_t *container, void *data) {
if (container->type == C_OUTPUT) { if (container->type == C_OUTPUT) {
char *json = malloc(512); // Output should usually be around 130 chars json_object *object = json_object_new_object();
// TODO: escape the name (quotation marks, unicode) json_object *rect = json_object_new_object();
sprintf(json, json_object_object_add(rect, "x", json_object_new_int((int32_t) container->x));
"{" json_object_object_add(rect, "y", json_object_new_int((int32_t) container->y));
"\"name\":\"%s\"," json_object_object_add(rect, "width", json_object_new_int((int32_t) container->width));
"\"active\":%s," json_object_object_add(rect, "height", json_object_new_int((int32_t) container->height));
"\"primary\":%s,"
"\"rect\":{" json_object_object_add(object, "name", json_object_new_string(container->name));
"\"x\":%d," json_object_object_add(object, "active", json_object_new_boolean(true));
"\"y\":%d," json_object_object_add(object, "primary", json_object_new_boolean(false));
"\"width\":%d," json_object_object_add(object, "rect", rect);
"\"height\":%d" json_object_object_add(object, "current_workspace", container->focused ? json_object_new_string(container->focused->name) : NULL);
"},"
"\"current_workspace\":\"%s\"" json_object_array_add((json_object *)data, object);
"}",
container->name, "true", "false", // TODO: active, primary
(int)container->x, (int)container->y, (int)container->width, (int)container->height,
container->focused ? container->focused->name : ""
);
list_add((list_t *)data, json);
} }
} }