simplify yajl related code
Specifically, put the version dependent code in some macros, and put that plus the `y' and `ystr' macros in a separate file, `yajl_utils.h'.
This commit is contained in:
parent
dece12bf18
commit
b67eedf71a
|
@ -0,0 +1,31 @@
|
||||||
|
/*
|
||||||
|
* vim:ts=4:sw=4:expandtab
|
||||||
|
*
|
||||||
|
* i3 - an improved dynamic tiling window manager
|
||||||
|
* © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
|
||||||
|
*
|
||||||
|
* yajl_utils.h
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef I3_YAJL_UTILS_H
|
||||||
|
#define I3_YAJL_UTILS_H
|
||||||
|
|
||||||
|
#include <yajl/yajl_gen.h>
|
||||||
|
#include <yajl/yajl_parse.h>
|
||||||
|
#include <yajl/yajl_version.h>
|
||||||
|
|
||||||
|
/* Shorter names for all those yajl_gen_* functions */
|
||||||
|
#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__)
|
||||||
|
#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str))
|
||||||
|
|
||||||
|
#if YAJL_MAJOR >= 2
|
||||||
|
#define ygenalloc() yajl_gen_alloc(NULL)
|
||||||
|
#define yalloc(callbacks, client) yajl_alloc(callbacks, NULL, client)
|
||||||
|
typedef size_t ylength;
|
||||||
|
#else
|
||||||
|
#define ygenalloc() yajl_gen_alloc(NULL, NULL);
|
||||||
|
#define yalloc(callbacks, client) yajl_alloc(callbacks, NULL, NULL, client)
|
||||||
|
typedef unsigned int ylength;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
103
src/ipc.c
103
src/ipc.c
|
@ -10,6 +10,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include "all.h"
|
#include "all.h"
|
||||||
|
#include "yajl_utils.h"
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
@ -18,14 +19,9 @@
|
||||||
#include <ev.h>
|
#include <ev.h>
|
||||||
#include <yajl/yajl_gen.h>
|
#include <yajl/yajl_gen.h>
|
||||||
#include <yajl/yajl_parse.h>
|
#include <yajl/yajl_parse.h>
|
||||||
#include <yajl/yajl_version.h>
|
|
||||||
|
|
||||||
char *current_socketpath = NULL;
|
char *current_socketpath = NULL;
|
||||||
|
|
||||||
/* Shorter names for all those yajl_gen_* functions */
|
|
||||||
#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__)
|
|
||||||
#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str))
|
|
||||||
|
|
||||||
TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
|
TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -128,11 +124,7 @@ IPC_HANDLER(command) {
|
||||||
tree_render();
|
tree_render();
|
||||||
|
|
||||||
const unsigned char *reply;
|
const unsigned char *reply;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
yajl_gen_get_buf(command_output->json_gen, &reply, &length);
|
yajl_gen_get_buf(command_output->json_gen, &reply, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
|
||||||
|
@ -367,20 +359,12 @@ void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
|
||||||
|
|
||||||
IPC_HANDLER(tree) {
|
IPC_HANDLER(tree) {
|
||||||
setlocale(LC_NUMERIC, "C");
|
setlocale(LC_NUMERIC, "C");
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
dump_node(gen, croot, false);
|
dump_node(gen, croot, false);
|
||||||
setlocale(LC_NUMERIC, "");
|
setlocale(LC_NUMERIC, "");
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
|
||||||
|
@ -394,11 +378,7 @@ IPC_HANDLER(tree) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
IPC_HANDLER(get_workspaces) {
|
IPC_HANDLER(get_workspaces) {
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
y(array_open);
|
y(array_open);
|
||||||
|
|
||||||
Con *focused_ws = con_get_workspace(focused);
|
Con *focused_ws = con_get_workspace(focused);
|
||||||
|
@ -451,11 +431,7 @@ IPC_HANDLER(get_workspaces) {
|
||||||
y(array_close);
|
y(array_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
|
||||||
|
@ -468,11 +444,7 @@ IPC_HANDLER(get_workspaces) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
IPC_HANDLER(get_outputs) {
|
IPC_HANDLER(get_outputs) {
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
y(array_open);
|
y(array_open);
|
||||||
|
|
||||||
Output *output;
|
Output *output;
|
||||||
|
@ -512,11 +484,7 @@ IPC_HANDLER(get_outputs) {
|
||||||
y(array_close);
|
y(array_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
|
||||||
|
@ -529,11 +497,7 @@ IPC_HANDLER(get_outputs) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
IPC_HANDLER(get_marks) {
|
IPC_HANDLER(get_marks) {
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
y(array_open);
|
y(array_open);
|
||||||
|
|
||||||
Con *con;
|
Con *con;
|
||||||
|
@ -544,11 +508,7 @@ IPC_HANDLER(get_marks) {
|
||||||
y(array_close);
|
y(array_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
|
||||||
|
@ -560,11 +520,7 @@ IPC_HANDLER(get_marks) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
IPC_HANDLER(get_version) {
|
IPC_HANDLER(get_version) {
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
y(map_open);
|
y(map_open);
|
||||||
|
|
||||||
ystr("major");
|
ystr("major");
|
||||||
|
@ -582,11 +538,7 @@ IPC_HANDLER(get_version) {
|
||||||
y(map_close);
|
y(map_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
|
||||||
|
@ -599,11 +551,7 @@ IPC_HANDLER(get_version) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
IPC_HANDLER(get_bar_config) {
|
IPC_HANDLER(get_bar_config) {
|
||||||
#if YAJL_MAJOR >= 2
|
yajl_gen gen = ygenalloc();
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL);
|
|
||||||
#else
|
|
||||||
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* If no ID was passed, we return a JSON array with all IDs */
|
/* If no ID was passed, we return a JSON array with all IDs */
|
||||||
if (message_size == 0) {
|
if (message_size == 0) {
|
||||||
|
@ -615,11 +563,7 @@ IPC_HANDLER(get_bar_config) {
|
||||||
y(array_close);
|
y(array_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
|
||||||
|
@ -753,11 +697,7 @@ IPC_HANDLER(get_bar_config) {
|
||||||
y(map_close);
|
y(map_close);
|
||||||
|
|
||||||
const unsigned char *payload;
|
const unsigned char *payload;
|
||||||
#if YAJL_MAJOR >= 2
|
ylength length;
|
||||||
size_t length;
|
|
||||||
#else
|
|
||||||
unsigned int length;
|
|
||||||
#endif
|
|
||||||
y(get_buf, &payload, &length);
|
y(get_buf, &payload, &length);
|
||||||
|
|
||||||
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
|
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
|
||||||
|
@ -768,13 +708,8 @@ IPC_HANDLER(get_bar_config) {
|
||||||
* Callback for the YAJL parser (will be called when a string is parsed).
|
* Callback for the YAJL parser (will be called when a string is parsed).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#if YAJL_MAJOR < 2
|
|
||||||
static int add_subscription(void *extra, const unsigned char *s,
|
static int add_subscription(void *extra, const unsigned char *s,
|
||||||
unsigned int len) {
|
ylength len) {
|
||||||
#else
|
|
||||||
static int add_subscription(void *extra, const unsigned char *s,
|
|
||||||
size_t len) {
|
|
||||||
#endif
|
|
||||||
ipc_client *client = extra;
|
ipc_client *client = extra;
|
||||||
|
|
||||||
DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
|
DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
|
||||||
|
@ -824,11 +759,7 @@ IPC_HANDLER(subscribe) {
|
||||||
memset(&callbacks, 0, sizeof(yajl_callbacks));
|
memset(&callbacks, 0, sizeof(yajl_callbacks));
|
||||||
callbacks.yajl_string = add_subscription;
|
callbacks.yajl_string = add_subscription;
|
||||||
|
|
||||||
#if YAJL_MAJOR >= 2
|
p = yalloc(&callbacks, (void*)client);
|
||||||
p = yajl_alloc(&callbacks, NULL, (void*)client);
|
|
||||||
#else
|
|
||||||
p = yajl_alloc(&callbacks, NULL, NULL, (void*)client);
|
|
||||||
#endif
|
|
||||||
stat = yajl_parse(p, (const unsigned char*)message, message_size);
|
stat = yajl_parse(p, (const unsigned char*)message, message_size);
|
||||||
if (stat != yajl_status_ok) {
|
if (stat != yajl_status_ok) {
|
||||||
unsigned char *err;
|
unsigned char *err;
|
||||||
|
|
Loading…
Reference in New Issue