Skip to content

Commit

Permalink
Adaptions for API change in json-c v0.13
Browse files Browse the repository at this point in the history
Lift restriction on json-c <= 0.12.1

ipc-server: Acquire ownership of referenced json_object properly

When adding a referenced json_object with an unknown lifetime to
another json_object, it must be done with a wrapped call to
json_object_get() to acquire the ownership of that json_object.
  • Loading branch information
besser82 authored and ddevault committed Dec 17, 2017
1 parent d0bd0ed commit c08c805
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (LD_LIBRARY_PATH)
add_definitions(-D_LD_LIBRARY_PATH="${LD_LIBRARY_PATH}")
endif()

find_package(JsonC 0.12.1 REQUIRED)
find_package(JsonC REQUIRED)
find_package(PCRE REQUIRED)
find_package(WLC REQUIRED)
find_package(Wayland REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion README.de.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Abhängigkeiten:
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.el.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ To username μου στο Freenode είναι kon14 και θα με βρείτ
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Installez les dépendances :
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.it.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Installa queste dipendenze:
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Swayは沢山のディストリビューションで提供されています。"
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Install dependencies:
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Antes de iniciar a compilação, instale as dependências:
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Sway доступен во многих дистрибутивах и наход
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
2 changes: 1 addition & 1 deletion README.uk.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Sway доступний у багатьох дистрибутивах Linux (а
* libcap
* asciidoc
* pcre
* json-c <= 0.12.1
* json-c
* pango
* cairo
* gdk-pixbuf2 *
Expand Down
16 changes: 16 additions & 0 deletions include/sway_json_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _SWAY_JSON_HELPER_H
#define _SWAY_JSON_HELPER_H

#include <json-c/json.h>

// Macros for checking a specific version.
#define JSON_C_VERSION_013 (13 << 8)

// json-c v0.13 uses size_t for array_list_length().
#if defined(JSON_C_VERSION_NUM) && JSON_C_VERSION_NUM >= JSON_C_VERSION_013
typedef size_t json_ar_len_t;
#else
typedef int json_ar_len_t;
#endif

#endif // _SWAY_JSON_HELPER_H
2 changes: 1 addition & 1 deletion include/swaygrab/json.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <json-c/json.h>
#include "sway_json_helper.h"
#include "wlc/wlc.h"

void init_json_tree(int socketfd);
Expand Down
7 changes: 4 additions & 3 deletions sway/ipc-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <json-c/json.h>
#include <list.h>
#include <libinput.h>
#ifdef __linux__
Expand All @@ -25,6 +24,7 @@ struct ucred {
gid_t gid;
};
#endif
#include "sway_json_helper.h"
#include "sway/ipc-json.h"
#include "sway/ipc-server.h"
#include "sway/security.h"
Expand Down Expand Up @@ -724,7 +724,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
}

// parse requested event types
for (int i = 0; i < json_object_array_length(request); i++) {
for (json_ar_len_t 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);
Expand Down Expand Up @@ -1126,7 +1126,8 @@ static void ipc_event_binding(json_object *sb_obj) {
sway_log(L_DEBUG, "Sending binding::run event");
json_object *obj = json_object_new_object();
json_object_object_add(obj, "change", json_object_new_string("run"));
json_object_object_add(obj, "binding", sb_obj);
// sb_obj gets owned by the temporary json_object, too.
json_object_object_add(obj, "binding", json_object_get(sb_obj));

const char *json_string = json_object_to_json_string(obj);
ipc_send_event(json_string, IPC_EVENT_BINDING);
Expand Down
5 changes: 2 additions & 3 deletions swaybar/status_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <json-c/json.h>

#include "sway_json_helper.h"
#include "swaybar/config.h"
#include "swaybar/status_line.h"
#include "log.h"
Expand Down Expand Up @@ -70,8 +70,7 @@ static void parse_json(struct bar *bar, const char *text) {

bar->status->block_line = create_list();

int i;
for (i = 0; i < json_object_array_length(results); ++i) {
for (json_ar_len_t i = 0; i < json_object_array_length(results); ++i) {
json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
json_object *name, *instance, *separator, *separator_block_width;
json_object *background, *border, *border_top, *border_bottom;
Expand Down
9 changes: 4 additions & 5 deletions swaygrab/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ static json_object *get_focused_container_r(json_object *c) {
} else {
json_object *nodes, *node, *child;
json_object_object_get_ex(c, "nodes", &nodes);
int i;
for (i = 0; i < json_object_array_length(nodes); i++) {
for (json_ar_len_t i = 0; i < json_object_array_length(nodes); i++) {
node = json_object_array_get_idx(nodes, i);

if ((child = get_focused_container_r(node))) {
Expand All @@ -60,7 +59,7 @@ static json_object *get_focused_container_r(json_object *c) {
}

json_object_object_get_ex(c, "floating_nodes", &nodes);
for (i = 0; i < json_object_array_length(nodes); i++) {
for (json_ar_len_t i = 0; i < json_object_array_length(nodes); i++) {
node = json_object_array_get_idx(nodes, i);

if ((child = get_focused_container_r(node))) {
Expand All @@ -83,7 +82,7 @@ char *get_focused_output() {
if (!outputs) {
sway_abort("Unabled to get focused output. No nodes in tree.");
}
for (int i = 0; i < json_object_array_length(outputs); i++) {
for (json_ar_len_t i = 0; i < json_object_array_length(outputs); i++) {
output = json_object_array_get_idx(outputs, i);

if (get_focused_container_r(output)) {
Expand Down Expand Up @@ -131,7 +130,7 @@ json_object *get_output_container(const char *output) {
json_object *outputs, *json_output, *name;
json_object_object_get_ex(tree, "nodes", &outputs);

for (int i = 0; i < json_object_array_length(outputs); i++) {
for (json_ar_len_t i = 0; i < json_object_array_length(outputs); i++) {
json_output = json_object_array_get_idx(outputs, i);
json_object_object_get_ex(json_output, "name", &name);

Expand Down
4 changes: 2 additions & 2 deletions swaylock/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-names.h>
#include <security/pam_appl.h>
#include <json-c/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -14,6 +13,7 @@
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include "sway_json_helper.h"
#include "client/window.h"
#include "client/registry.h"
#include "client/cairo.h"
Expand Down Expand Up @@ -583,7 +583,7 @@ int main(int argc, char **argv) {

for (i = 0; i < registry->outputs->length; ++i) {
if (displays_paths[i * 2] != NULL) {
for (int j = 0;; ++j) {
for (json_ar_len_t j = 0;; ++j) {
if (j >= json_object_array_length(json_outputs)) {
sway_log(L_ERROR, "%s is not an extant output", displays_paths[i * 2]);
exit(EXIT_FAILURE);
Expand Down
4 changes: 2 additions & 2 deletions swaymsg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <sys/socket.h>
#include <ctype.h>
#include <unistd.h>
#include <json-c/json.h>
#include "sway_json_helper.h"
#include "stringop.h"
#include "ipc-client.h"
#include "readline.h"
Expand Down Expand Up @@ -149,7 +149,7 @@ static void pretty_print_version(json_object *v) {
static void pretty_print_clipboard(json_object *v) {
if (success(v, true)) {
if (json_object_is_type(v, json_type_array)) {
for (int i = 0; i < json_object_array_length(v); ++i) {
for (json_ar_len_t i = 0; i < json_object_array_length(v); ++i) {
json_object *o = json_object_array_get_idx(v, i);
printf("%s\n", json_object_get_string(o));
}
Expand Down

0 comments on commit c08c805

Please sign in to comment.