From d8926f957b5de49c17566426460a42164cd34089 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Mon, 25 Jan 2021 17:56:03 +0100 Subject: [PATCH] #775 some cleanup --- src/display.c | 17 ++++++++++------- src/event_signal.c | 4 ++-- src/event_signal.h | 7 ------- src/misc/helpers.h | 15 +++++++++++++-- src/rule.c | 32 ++++++++++++++++---------------- src/rule.h | 7 ------- src/view.c | 27 +++++++++++++++------------ 7 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/display.c b/src/display.c index 6766ee88..6b6701a7 100644 --- a/src/display.c +++ b/src/display.c @@ -35,11 +35,12 @@ void display_serialize(FILE *rsp, uint32_t did) int count; uint64_t *space_list = display_space_list(did, &count); if (space_list) { + int first_mci = space_manager_mission_control_index(space_list[0]); for (int i = 0; i < count; ++i) { if (i < count - 1) { - bytes_written = snprintf(cursor, buffer_size, "%d, ", space_manager_mission_control_index(space_list[i])); + bytes_written = snprintf(cursor, buffer_size, "%d, ", first_mci + i); } else { - bytes_written = snprintf(cursor, buffer_size, "%d", space_manager_mission_control_index(space_list[i])); + bytes_written = snprintf(cursor, buffer_size, "%d", first_mci + i); } cursor += bytes_written; @@ -53,12 +54,14 @@ void display_serialize(FILE *rsp, uint32_t did) "\t\"id\":%d,\n" "\t\"uuid\":\"%s\",\n" "\t\"index\":%d,\n" - "\t\"spaces\":[%s],\n" - "\t\"frame\":{\n\t\t\"x\":%.4f,\n\t\t\"y\":%.4f,\n\t\t\"w\":%.4f,\n\t\t\"h\":%.4f\n\t}\n" + "\t\"frame\":{\n\t\t\"x\":%.4f,\n\t\t\"y\":%.4f,\n\t\t\"w\":%.4f,\n\t\t\"h\":%.4f\n\t},\n" + "\t\"spaces\":[%s]\n" "}", - did, uuid ? uuid : "", display_arrangement(did), buffer, - frame.origin.x, frame.origin.y, - frame.size.width, frame.size.height); + did, + uuid ? uuid : "", + display_arrangement(did), + frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, + buffer); } CFStringRef display_uuid(uint32_t did) diff --git a/src/event_signal.c b/src/event_signal.c index ecc1a549..8ed4f829 100644 --- a/src/event_signal.c +++ b/src/event_signal.c @@ -325,7 +325,7 @@ static void event_signal_serialize(FILE *rsp, struct signal *signal, enum signal "\t\"label\":\"%s\",\n" "\t\"app\":\"%s\",\n" "\t\"title\":\"%s\",\n" - "\t\"active\":\"%d\",\n" + "\t\"active\":%s,\n" "\t\"event\":\"%s\",\n" "\t\"action\":\"%s\"\n" "}", @@ -333,7 +333,7 @@ static void event_signal_serialize(FILE *rsp, struct signal *signal, enum signal signal->label ? signal->label : "", signal->app ? signal->app : "", signal->title ? signal->title : "", - signal_prop[signal->active], + json_optional_bool(signal->active), signal_type_str[type], escaped_action ? escaped_action : signal->command ? signal->command : ""); } diff --git a/src/event_signal.h b/src/event_signal.h index ce12bc84..7e804d6d 100644 --- a/src/event_signal.h +++ b/src/event_signal.h @@ -83,13 +83,6 @@ static const char *signal_type_str[] = #define SIGNAL_PROP_YES 1 #define SIGNAL_PROP_NO 2 -static int signal_prop[] = -{ - [SIGNAL_PROP_UD] = -1, - [SIGNAL_PROP_NO] = 0, - [SIGNAL_PROP_YES] = 1, -}; - struct event_signal { enum signal_type type; diff --git a/src/misc/helpers.h b/src/misc/helpers.h index bfa49e58..12dc33a8 100644 --- a/src/misc/helpers.h +++ b/src/misc/helpers.h @@ -3,6 +3,19 @@ const CFStringRef kAXEnhancedUserInterface = CFSTR("AXEnhancedUserInterface"); +static inline char *json_optional_bool(int value) +{ + if (value == 0) return "null"; + if (value == 1) return "true"; + + return "false"; +} + +static inline char *json_bool(bool value) +{ + return value ? "true" : "false"; +} + static const char *bool_str[] = { "off", "on" }; static const char *layer_str[] = @@ -145,7 +158,6 @@ static inline char *ts_cfstring_copy(CFStringRef string) { CFIndex num_bytes = CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8); char *result = ts_alloc(num_bytes + 1); - if (!result) return NULL; if (!CFStringGetCString(string, result, num_bytes + 1, kCFStringEncodingUTF8)) { result = NULL; @@ -172,7 +184,6 @@ static inline char *ts_string_copy(char *s) { int length = strlen(s); char *result = ts_alloc(length + 1); - if (!result) return NULL; memcpy(result, s, length); result[length] = '\0'; diff --git a/src/rule.c b/src/rule.c index aeaada0d..5ea995d8 100644 --- a/src/rule.c +++ b/src/rule.c @@ -11,32 +11,32 @@ void rule_serialize(FILE *rsp, struct rule *rule, int index) "\t\"label\":\"%s\",\n" "\t\"app\":\"%s\",\n" "\t\"title\":\"%s\",\n" - "\t\"display_id\":%d,\n" - "\t\"space_id\":%lld,\n" - "\t\"follow_space\":%d,\n" + "\t\"display\":%d,\n" + "\t\"space\":%d,\n" + "\t\"follow_space\":%s,\n" "\t\"opacity\":%.4f,\n" - "\t\"manage\":%d,\n" - "\t\"sticky\":%d,\n" - "\t\"mouse_follows_focus\":%d,\n" + "\t\"manage\":%s,\n" + "\t\"sticky\":%s,\n" + "\t\"mouse_follows_focus\":%s,\n" "\t\"layer\":\"%s\",\n" - "\t\"border\":%d,\n" - "\t\"native-fullscreen\":%d,\n" + "\t\"border\":%s,\n" + "\t\"native-fullscreen\":%s,\n" "\t\"grid\":\"%d:%d:%d:%d:%d:%d\"\n" "}", index, rule->label ? rule->label : "", rule->app ? rule->app : "", rule->title ? rule->title : "", - rule->did, - rule->sid, - rule->follow_space, + display_arrangement(rule->did), + space_manager_mission_control_index(rule->sid), + json_bool(rule->follow_space), rule->alpha, - rule_prop[rule->manage], - rule_prop[rule->sticky], - rule_prop[rule->mff], + json_optional_bool(rule->manage), + json_optional_bool(rule->sticky), + json_optional_bool(rule->mff), layer_str[rule->layer], - rule_prop[rule->border], - rule_prop[rule->fullscreen], + json_optional_bool(rule->border), + json_optional_bool(rule->fullscreen), rule->grid[0], rule->grid[1], rule->grid[2], rule->grid[3], rule->grid[4], rule->grid[5]); diff --git a/src/rule.h b/src/rule.h index f685533c..37c7ab21 100644 --- a/src/rule.h +++ b/src/rule.h @@ -5,13 +5,6 @@ #define RULE_PROP_ON 1 #define RULE_PROP_OFF 2 -static int rule_prop[] = -{ - [RULE_PROP_UD] = -1, - [RULE_PROP_OFF] = 0, - [RULE_PROP_ON] = 1, -}; - struct rule { char *label; diff --git a/src/view.c b/src/view.c index 6a2108fe..92244eab 100644 --- a/src/view.c +++ b/src/view.c @@ -724,6 +724,7 @@ void view_flush(struct view *view) void view_serialize(FILE *rsp, struct view *view) { + char *uuid = ts_cfstring_copy(view->suuid); int buffer_size = MAXLEN; size_t bytes_written = 0; char buffer[MAXLEN] = {}; @@ -761,28 +762,30 @@ void view_serialize(FILE *rsp, struct view *view) fprintf(rsp, "{\n" "\t\"id\":%lld,\n" - "\t\"label\":\"%s\",\n" + "\t\"uuid\":\"%s\",\n" "\t\"index\":%d,\n" + "\t\"label\":\"%s\",\n" + "\t\"type\":\"%s\",\n" "\t\"display\":%d,\n" "\t\"windows\":[%s],\n" - "\t\"type\":\"%s\",\n" - "\t\"visible\":%d,\n" - "\t\"focused\":%d,\n" - "\t\"native-fullscreen\":%d,\n" "\t\"first-window\":%d,\n" - "\t\"last-window\":%d\n" + "\t\"last-window\":%d,\n" + "\t\"has-focus\":%s,\n" + "\t\"is-visible\":%s,\n" + "\t\"is-native-fullscreen\":%s\n" "}", view->sid, - space_label ? space_label->label : "", + uuid ? uuid : "", space_manager_mission_control_index(view->sid), + space_label ? space_label->label : "", + view_type_str[view->layout], display_arrangement(space_display_id(view->sid)), buffer, - view_type_str[view->layout], - space_is_visible(view->sid), - view->sid == g_space_manager.current_space_id, - space_is_fullscreen(view->sid), first_leaf ? first_leaf->window_order[0] : 0, - last_leaf ? last_leaf->window_order[0] : 0); + last_leaf ? last_leaf->window_order[0] : 0, + json_bool(space_is_visible(view->sid)), + json_bool(view->sid == g_space_manager.current_space_id), + json_bool(space_is_fullscreen(view->sid))); } void view_update(struct view *view)