From 3dd23937974dea9a2c9205431e3cf2275290f162 Mon Sep 17 00:00:00 2001 From: robotanarchy Date: Mon, 21 Dec 2015 18:57:26 +0100 Subject: [PATCH 1/5] remove unused execinfo.h include from debug_log.c --- sway/debug_log.c | 1 - 1 file changed, 1 deletion(-) diff --git a/sway/debug_log.c b/sway/debug_log.c index 8b30ed45..6fd6422f 100644 --- a/sway/debug_log.c +++ b/sway/debug_log.c @@ -10,7 +10,6 @@ #include #include #include -#include #include "workspace.h" extern log_importance_t v; From 94cac7a0149a8c1c48f33b0a8140edec5581ce64 Mon Sep 17 00:00:00 2001 From: robotanarchy Date: Mon, 21 Dec 2015 19:01:17 +0100 Subject: [PATCH 2/5] use CMake's FindBacktrace for backtrace feature detection --- CMakeLists.txt | 8 ++++++++ common/CMakeLists.txt | 1 + common/log.c | 5 ++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cd9c67b..cabdc468 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,14 @@ find_package(Pango) find_package(GdkPixbuf) find_package(PAM) +find_package(Backtrace) +if(Backtrace_FOUND) + include_directories(${Backtrace_INCLUDE_DIRS}) + target_link_libraries(${Backtrace_LIBRARIES}) + add_definitions(-DSWAY_Backtrace_FOUND=1) + set(SWAY_Backtrace_HEADER "${Backtrace_HEADER}") +endif() + include(FeatureSummary) include(Manpage) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a40f096d..95617e15 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(sway-common + ${SWAY_Backtrace_HEADER} ipc-client.c list.c log.c diff --git a/common/log.c b/common/log.c index 02aac4c1..f9242bf4 100644 --- a/common/log.c +++ b/common/log.c @@ -10,7 +10,6 @@ #include #include #include -#include int colored = 1; log_importance_t loglevel_default = L_ERROR; @@ -137,6 +136,7 @@ bool _sway_assert(bool condition, const char* format, ...) { } void error_handler(int sig) { +#if SWAY_Backtrace_FOUND int i; int max_lines = 20; void *array[max_lines]; @@ -155,5 +155,8 @@ void error_handler(int sig) { for (i = 0; (size_t)i < bt_len; i++) { sway_log(L_ERROR, "Backtrace: %s", bt[i]); } +#else + sway_log(L_ERROR, "Error: Signal %d.", sig); +#endif exit(1); } From 03c041dd30315125b72699a90085718ebdc13248 Mon Sep 17 00:00:00 2001 From: robotanarchy Date: Mon, 21 Dec 2015 19:09:39 +0100 Subject: [PATCH 3/5] add -fPIC flag (position independent code) like in wlc Linking fails otherwise: Linking C executable ../bin/sway /usr/bin/ld: CMakeFiles/sway.dir/commands.c.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC CMakeFiles/sway.dir/commands.c.o: error adding symbols: Bad value collect2: error: ld returned 1 exit status sway/CMakeFiles/sway.dir/build.make:442: recipe for target 'bin/sway' failed --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index cabdc468..bb13ef96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ add_definitions(-DFALLBACK_CONFIG_DIR=\"${FALLBACK_CONFIG_DIR}\") set(CMAKE_C_FLAGS "-g") set(CMAKE_C_STANDARD 99) set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) add_definitions( -D_GNU_SOURCE From 470e59b291b7c5e416f5c43bf14dedb8c379b25e Mon Sep 17 00:00:00 2001 From: robotanarchy Date: Tue, 22 Dec 2015 00:32:41 +0100 Subject: [PATCH 4/5] fix backtrace detection in CMake works on arch (glibc) and void linux (tested with musl libc) now --- CMakeLists.txt | 4 ++-- common/CMakeLists.txt | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb13ef96..4c349865 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,9 +61,9 @@ find_package(PAM) find_package(Backtrace) if(Backtrace_FOUND) - include_directories(${Backtrace_INCLUDE_DIRS}) - target_link_libraries(${Backtrace_LIBRARIES}) + include_directories("${Backtrace_INCLUDE_DIRS}") add_definitions(-DSWAY_Backtrace_FOUND=1) + set(LINK_LIBRARIES, "${LINK_LIBRARIES} ${Backtrace_LIBRARIES}") set(SWAY_Backtrace_HEADER "${Backtrace_HEADER}") endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 95617e15..38767249 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,5 +1,4 @@ add_library(sway-common - ${SWAY_Backtrace_HEADER} ipc-client.c list.c log.c @@ -7,3 +6,10 @@ add_library(sway-common readline.c stringop.c ) + +if(Backtrace_FOUND) + set_target_properties(sway-common + PROPERTIES + COMPILE_FLAGS "-include ${Backtrace_HEADER}" + ) +endif() From c3e9ee5e43c6d7adf6d5c9b74b39a5170cfe0b02 Mon Sep 17 00:00:00 2001 From: robotanarchy Date: Tue, 22 Dec 2015 00:38:18 +0100 Subject: [PATCH 5/5] replace non-standard qsort_r with qsort I've tried to make as few changes, as possible. Usually the reason for using qsort_r is, that you can pass an extra userdata pointer to the compare function. However, in sway list_sort wrapped qsort_r and always called a wrapper function for comparing, the wrapper function then had the real compare function as argument. The only thing, that the wrapper function does, is dereferencing the 'left' and 'right' function arguments before passing them to the real compare function. I have renamed list_sort to list_qsort to avoid confusion (so nobody tries to use list_qsort like list_sort) and removed the wrapper functionality. Now the dereferencing must be done in the compare function, that gets passed. Some compare functions were used in both list_sort and list_seq_find. To make the difference clear, I've added a '_qsort' suffix to the compare functions, that are intended to be used with the new list_qsort. (In other words: list_qsort is not compatible anymore with list_seq_find). - Changed and renamed function (it isn't used anywhere but in commands.c, and only for sorting): compare_set -> compare_set_qsort - New wrapper functions: sway_binding_cmp_qsort (for sway_binding_cmp) sway_mouse_binding_cmp_qsort (for sway_mouse_binding_cmp) --- common/list.c | 10 ++-------- include/config.h | 2 ++ include/list.h | 5 +++-- sway/commands.c | 12 ++++++------ sway/config.c | 8 ++++++++ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/common/list.c b/common/list.c index d6f6f2ea..850c8569 100644 --- a/common/list.c +++ b/common/list.c @@ -50,14 +50,8 @@ void list_cat(list_t *list, list_t *source) { } } -// pass the pointer of the object we care about to the comparison function -static int list_cmp(const void *l, const void *r, void *_cmp) { - int (*cmp)(const void *, const void *) = _cmp; - return cmp(*(void**)l, *(void**)r); -} - -void list_sort(list_t *list, int compare(const void *left, const void *right)) { - qsort_r(list->items, list->length, sizeof(void *), list_cmp, compare); +void list_qsort(list_t* list, int compare(const void *left, const void *right)) { + qsort(list->items, list->length, sizeof(void *), compare); } int list_seq_find(list_t *list, int compare(const void *item, const void *data), const void *data) { diff --git a/include/config.h b/include/config.h index b97acb57..a915fbed 100644 --- a/include/config.h +++ b/include/config.h @@ -179,10 +179,12 @@ void free_output_config(struct output_config *oc); int workspace_output_cmp_workspace(const void *a, const void *b); int sway_binding_cmp(const void *a, const void *b); +int sway_binding_cmp_qsort(const void *a, const void *b); int sway_binding_cmp_keys(const void *a, const void *b); void free_sway_binding(struct sway_binding *sb); int sway_mouse_binding_cmp(const void *a, const void *b); +int sway_mouse_binding_cmp_qsort(const void *a, const void *b); int sway_mouse_binding_cmp_buttons(const void *a, const void *b); void free_sway_mouse_binding(struct sway_mouse_binding *smb); diff --git a/include/list.h b/include/list.h index 90d0ad36..d18d3f54 100644 --- a/include/list.h +++ b/include/list.h @@ -13,8 +13,9 @@ void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); void list_cat(list_t *list, list_t *source); -// See qsort -void list_sort(list_t *list, int compare(const void *left, const void *right)); +// See qsort. Remember to use *_qsort functions as compare functions, +// because they dereference the left and right arguments first! +void list_qsort(list_t *list, int compare(const void *left, const void *right)); // Return index for first item in list that returns 0 for given compare // function or -1 if none matches. int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to); diff --git a/sway/commands.c b/sway/commands.c index 3d882a7b..f6d9b947 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -219,7 +219,7 @@ static struct cmd_results *cmd_bindsym(int argc, char **argv) { } binding->order = binding_order++; list_add(mode->bindings, binding); - list_sort(mode->bindings, sway_binding_cmp); + list_qsort(mode->bindings, sway_binding_cmp_qsort); sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -1255,9 +1255,9 @@ static struct cmd_results *cmd_scratchpad(int argc, char **argv) { } // sort in order of longest->shortest -static int compare_set(const void *_l, const void *_r) { - struct sway_variable const *l = _l; - struct sway_variable const *r = _r; +static int compare_set_qsort(const void *_l, const void *_r) { + struct sway_variable const *l = *(void **)_l; + struct sway_variable const *r = *(void **)_r; return strlen(r->name) - strlen(l->name); } @@ -1284,7 +1284,7 @@ static struct cmd_results *cmd_set(int argc, char **argv) { var = malloc(sizeof(struct sway_variable)); var->name = strdup(argv[0]); list_add(config->symbols, var); - list_sort(config->symbols, compare_set); + list_qsort(config->symbols, compare_set_qsort); } var->value = join_args(argv + 1, argc - 1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -1620,7 +1620,7 @@ static struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { list_del(bar->bindings, i); } list_add(bar->bindings, binding); - list_sort(bar->bindings, sway_mouse_binding_cmp); + list_qsort(bar->bindings, sway_mouse_binding_cmp_qsort); sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/config.c b/sway/config.c index 4b1bf01a..e86eda53 100644 --- a/sway/config.c +++ b/sway/config.c @@ -642,6 +642,10 @@ int sway_binding_cmp(const void *a, const void *b) { return lenient_strcmp(binda->command, bindb->command); } +int sway_binding_cmp_qsort(const void *a, const void *b) { + return sway_binding_cmp(*(void **)a, *(void **)b); +} + void free_sway_binding(struct sway_binding *binding) { if (binding->keys) { for (int i = 0; i < binding->keys->length; i++) { @@ -675,6 +679,10 @@ int sway_mouse_binding_cmp(const void *a, const void *b) { return lenient_strcmp(binda->command, bindb->command); } +int sway_mouse_binding_cmp_qsort(const void *a, const void *b) { + return sway_mouse_binding_cmp(*(void **)a, *(void **)b); +} + void free_sway_mouse_binding(struct sway_mouse_binding *binding) { if (binding->command) { free(binding->command);