From d404c4ab284d4055c3a60ce45b38d9e2cec549c6 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Tue, 9 May 2017 21:16:14 +1200 Subject: [PATCH 01/17] New list interface. --- CMakeLists.txt | 2 +- common/list.c | 237 +++++++++++++++++++++++++++++-------------------- include/list.h | 121 ++++++++++++++++++++----- 3 files changed, 242 insertions(+), 118 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ed458301..b5ed026fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.1.0) project(sway C) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") -set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD 11) set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) diff --git a/common/list.c b/common/list.c index 39cc10e19..134dbf12b 100644 --- a/common/list.c +++ b/common/list.c @@ -1,134 +1,177 @@ #include "list.h" -#include + #include +#include #include -list_t *create_list(void) { - list_t *list = malloc(sizeof(list_t)); - if (!list) { +list_t *list_new(size_t memb_size, size_t capacity) { + if (capacity == 0) + capacity = 8; + + list_t *l = malloc(sizeof(*l) + memb_size * capacity); + if (!l) { return NULL; } - list->capacity = 10; - list->length = 0; - list->items = malloc(sizeof(void*) * list->capacity); - return list; + + l->capacity = capacity; + l->length = 0; + l->memb_size = memb_size; + + return l; } -static void list_resize(list_t *list) { - if (list->length == list->capacity) { - list->capacity += 10; - list->items = realloc(list->items, sizeof(void*) * list->capacity); +static bool resize(list_t **list) { + list_t *l = *list; + + if (l->length < l->capacity) { + return true; } + + size_t new_cap = l->capacity * 2; + l = realloc(l, sizeof(*l) + l->memb_size * new_cap); + if (!l) { + return false; + } + + *list = l; + l->capacity = new_cap; + return true; } -void list_free(list_t *list) { - if (list == NULL) { +void list_add(list_t **list, const void *data) { + if (!data || !list || !*list || !resize(list)) { return; } - free(list->items); - free(list); + list_t *l = *list; + + memcpy(l->data + l->memb_size * l->length, data, l->memb_size); + ++l->length; } -void list_foreach(list_t *list, void (*callback)(void *item)) { - if (list == NULL || callback == NULL) { +void list_insert(list_t **list, size_t index, const void *data) { + if (!data || !list || !*list || index > (*list)->length || !resize(list)) { return; } - for (int i = 0; i < list->length; i++) { - callback(list->items[i]); + list_t *l = *list; + + size_t size = l->memb_size; + unsigned char (*array)[size] = (void *)l->data; + memmove(&array[index + 1], &array[index], size * (l->length - index)); + memcpy(&array[index], data, size); + ++l->length; +} + +void list_delete(list_t *list, size_t index) { + if (!list || index >= list->length) { + return; + } + + size_t size = list->memb_size; + unsigned char (*array)[size] = (void *)list->data; + + memmove(&array[index], &array[index + 1], size * (list->length - index)); + --list->length; +} + +void list_swap(list_t *list, size_t i1, size_t i2) { + if (!list || i1 >= list->length || i2 >= list->length) { + return; + } + + size_t size = list->memb_size; + unsigned char (*array)[size] = (void *)list->data; + + unsigned char tmp[size]; + memcpy(tmp, &array[i1], size); + memcpy(&array[i1], &array[i2], size); + memcpy(&array[i2], tmp, size); +} + +void *list_get(list_t *list, size_t index) { + if (!list || index >= list->length) { + return NULL; + } + + return list->data + list->memb_size * index; +} + +void list_qsort(list_t *list, int compare(const void *, const void *)) { + if (!list || !compare) { + return; + } + + qsort(list->data, list->length, list->memb_size, compare); +} + +void list_isort(list_t *list, int compare(const void *, const void *)) { + if (!list || !compare) { + return; + } + + size_t size = list->memb_size; + unsigned char (*array)[size] = (void *)list->data; + + for (size_t i = 1; i < list->length; ++i) { + unsigned char tmp[size]; + memcpy(tmp, &array[i], size); + + ssize_t j = i - 1; + while (j >= 0 && compare(&array[j], tmp) > 0) { + memcpy(&array[j + 1], &array[j], size); + --j; + } + + memcpy(array[j + 1], tmp, size); } } -void list_add(list_t *list, void *item) { - list_resize(list); - list->items[list->length++] = item; -} +ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), + const void *key, void *ret) { + if (!list || !compare || !key) { + return -1; + } -void list_insert(list_t *list, int index, void *item) { - list_resize(list); - memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); - list->length++; - list->items[index] = item; -} - -void list_del(list_t *list, int index) { - list->length--; - memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); -} - -void list_cat(list_t *list, list_t *source) { - int i; - for (i = 0; i < source->length; ++i) { - list_add(list, source->items[i]); + const unsigned char *ptr = bsearch(key, list->data, list->length, list->memb_size, compare); + if (!ptr) { + return -1; + } else { + if (ret) { + memcpy(ret, ptr, list->memb_size); + } + return (ptr - list->data) / list->memb_size; } } -void list_qsort(list_t *list, int compare(const void *left, const void *right)) { - qsort(list->items, list->length, sizeof(void *), compare); -} +ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), + const void *key, void *ret) { + if (!list || !compare || !key) { + return -1; + } -int list_seq_find(list_t *list, int compare(const void *item, const void *data), const void *data) { - for (int i = 0; i < list->length; i++) { - void *item = list->items[i]; - if (compare(item, data) == 0) { + size_t size = list->memb_size; + unsigned char (*array)[size] = (void *)list->data; + + for (size_t i = 0; i < list->length; ++i) { + if (compare(&array[i], key) == 0) { + if (ret) { + memcpy(ret, &array[i], size); + } return i; } } + return -1; } -void list_swap(list_t *list, int src, int dest) { - void *tmp = list->items[src]; - list->items[src] = list->items[dest]; - list->items[dest] = tmp; -} - -static void list_rotate(list_t *list, int from, int to) { - void *tmp = list->items[to]; - - while (to > from) { - list->items[to] = list->items[to - 1]; - to--; - } - - list->items[from] = tmp; -} - -static void list_inplace_merge(list_t *list, int left, int last, int mid, int compare(const void *a, const void *b)) { - int right = mid + 1; - - if (compare(&list->items[mid], &list->items[right]) <= 0) { +void list_foreach_cb(list_t *list, void callback(void *)) { + if (!list || !callback) { return; } - while (left <= mid && right <= last) { - if (compare(&list->items[left], &list->items[right]) <= 0) { - left++; - } else { - list_rotate(list, left, right); - left++; - mid++; - right++; - } - } -} + size_t size = list->memb_size; + unsigned char (*array)[size] = (void *)list->data; -static void list_inplace_sort(list_t *list, int first, int last, int compare(const void *a, const void *b)) { - if (first >= last) { - return; - } else if ((last - first) == 1) { - if (compare(&list->items[first], &list->items[last]) > 0) { - list_swap(list, first, last); - } - } else { - int mid = (int)((last + first) / 2); - list_inplace_sort(list, first, mid, compare); - list_inplace_sort(list, mid + 1, last, compare); - list_inplace_merge(list, first, last, mid, compare); - } -} - -void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { - if (list->length > 1) { - list_inplace_sort(list, 0, list->length - 1, compare); + for (size_t i = 0; i < list->length; ++i) { + callback(&array[i]); } } diff --git a/include/list.h b/include/list.h index 7eead4acc..087804c91 100644 --- a/include/list.h +++ b/include/list.h @@ -1,27 +1,108 @@ #ifndef _SWAY_LIST_H #define _SWAY_LIST_H +#include +#include +#include + typedef struct { - int capacity; - int length; - void **items; + size_t capacity; + size_t length; + size_t memb_size; + alignas(max_align_t) unsigned char data[]; } list_t; -list_t *create_list(void); -void list_free(list_t *list); -void list_foreach(list_t *list, void (*callback)(void* item)); -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. 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); -// stable sort since qsort is not guaranteed to be stable -void list_stable_sort(list_t *list, int compare(const void *a, const void *b)); -// swap two elements in a list -void list_swap(list_t *list, int src, int dest); +/* + * Creates a new list with an inital capacity. + * If capacity is zero, some default value is used instead. + * + * memb_size must be the size of the type stored in this list. + * If an element is added to this list which is not the same type + * as the elements of the list, the behavior is undefined. + * + * This list should be freed with the stdlib free() function. + */ +list_t *list_new(size_t memb_size, size_t capacity); + +/* + * Adds an element to the end of the list. + */ +void list_add(list_t **list, const void *data); + +/* + * Adds an element at an arbitrary position in the list, moving + * the elements past index to make space. + * index must be less than or equal to the length of the list. + */ +void list_insert(list_t **list, size_t index, const void *data); + +/* + * Deletes the element at an arbitrary position in the list, + * moving the elements past index into the space. + * index must be less than the length of the list. + */ +void list_delete(list_t *list, size_t index); + +/* + * Swaps the elements at i1 and i2. + * i1 and i2 must both be less than the length of the list. + */ +void list_swap(list_t *list, size_t i1, size_t i2); + +/* + * Gets the element of the list at the index. + * index must be less than the length of the list. + */ +void *list_get(list_t *list, size_t index); + +/* + * Sorts the list using the stdlib qsort() function. + */ +void list_qsort(list_t *list, int compare(const void *, const void *)); + +/* + * Sorts the list using insertion sort. + * This should be used if you need a stable sort, and your list is + * short and/or nearly sorted. + */ +void list_isort(list_t *list, int compare(const void *, const void *)); + +/* + * Returns the index of key in the list, using the stdlib bsearch() function, + * or -1 if it was not found. If ret is not null, the found element will be + * copied into it. + * The list must be sorted. + */ +ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), + const void *key, void *ret); + +/* + * Returns the index of the key in the list, using a linear search, + * or -1 if it was not found. If ret is not null, the found element will be + * copied into it. + */ +ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), + const void *key, void *ret); + +/* + * Calls a function on every item in the list. + */ +void list_foreach_cb(list_t *list, void callback(void *)); + +/* + * This is for loop helper, iterating over every element in list. + * iter must be a pointer to the list element's type. + * You should not add, remove, or sort elements from the list while inside this loop. + * + * Example: + * char **ptr; + * list_foreach(list_of_strings, ptr) { + * printf("%s\n", *ptr); + * } + */ +#define list_foreach(list, iter) \ + for (iter = (void *)list->data; \ + (unsigned char *)iter < list->data + list->memb_size * list->length; \ + ++iter) + #endif From 464bc5ff8e1e915a3cc6d454140042b53bbd2579 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 01:50:54 +1200 Subject: [PATCH 02/17] Changed to heap-based implementation. --- CMakeLists.txt | 2 +- common/list.c | 83 ++++++++++++++++++++++++++++++++------------------ include/list.h | 33 ++++++++++---------- 3 files changed, 71 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5ed026fd..9ed458301 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.1.0) project(sway C) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") -set(CMAKE_C_STANDARD 11) +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) diff --git a/common/list.c b/common/list.c index 134dbf12b..cf86ac953 100644 --- a/common/list.c +++ b/common/list.c @@ -8,7 +8,7 @@ list_t *list_new(size_t memb_size, size_t capacity) { if (capacity == 0) capacity = 8; - list_t *l = malloc(sizeof(*l) + memb_size * capacity); + list_t *l = malloc(sizeof(*l)); if (!l) { return NULL; } @@ -17,48 +17,62 @@ list_t *list_new(size_t memb_size, size_t capacity) { l->length = 0; l->memb_size = memb_size; + l->data = malloc(memb_size * capacity); + if (!l->data) { + free(l); + return NULL; + } + return l; } -static bool resize(list_t **list) { - list_t *l = *list; +void list_free(list_t *list) { + if (!list) { + return; + } - if (l->length < l->capacity) { + free(list->data); + free(list); +} + +static bool resize(list_t *list) { + if (list->length < list->capacity) { return true; } - size_t new_cap = l->capacity * 2; - l = realloc(l, sizeof(*l) + l->memb_size * new_cap); - if (!l) { + size_t new_cap = list->capacity * 2; + void *data = realloc(list->data, list->memb_size * new_cap); + if (!data) { return false; } - *list = l; - l->capacity = new_cap; + list->data = data; + list->capacity = new_cap; return true; } -void list_add(list_t **list, const void *data) { - if (!data || !list || !*list || !resize(list)) { +void list_add(list_t *list, const void *data) { + if (!data || !list || !resize(list)) { return; } - list_t *l = *list; - memcpy(l->data + l->memb_size * l->length, data, l->memb_size); - ++l->length; + size_t size = list->memb_size; + unsigned char (*array)[size] = list->data; + + memcpy(&array[list->length], data, size); + ++list->length; } -void list_insert(list_t **list, size_t index, const void *data) { - if (!data || !list || !*list || index > (*list)->length || !resize(list)) { +void list_insert(list_t *list, size_t index, const void *data) { + if (!data || !list || index > list->length || !resize(list)) { return; } - list_t *l = *list; - size_t size = l->memb_size; - unsigned char (*array)[size] = (void *)l->data; - memmove(&array[index + 1], &array[index], size * (l->length - index)); + size_t size = list->memb_size; + unsigned char (*array)[size] = list->data; + memmove(&array[index + 1], &array[index], size * (list->length - index)); memcpy(&array[index], data, size); - ++l->length; + ++list->length; } void list_delete(list_t *list, size_t index) { @@ -67,7 +81,7 @@ void list_delete(list_t *list, size_t index) { } size_t size = list->memb_size; - unsigned char (*array)[size] = (void *)list->data; + unsigned char (*array)[size] = list->data; memmove(&array[index], &array[index + 1], size * (list->length - index)); --list->length; @@ -79,7 +93,7 @@ void list_swap(list_t *list, size_t i1, size_t i2) { } size_t size = list->memb_size; - unsigned char (*array)[size] = (void *)list->data; + unsigned char (*array)[size] = list->data; unsigned char tmp[size]; memcpy(tmp, &array[i1], size); @@ -92,7 +106,10 @@ void *list_get(list_t *list, size_t index) { return NULL; } - return list->data + list->memb_size * index; + size_t size = list->memb_size; + unsigned char (*array)[size] = list->data; + + return &array[index]; } void list_qsort(list_t *list, int compare(const void *, const void *)) { @@ -109,7 +126,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } size_t size = list->memb_size; - unsigned char (*array)[size] = (void *)list->data; + unsigned char (*array)[size] = list->data; for (size_t i = 1; i < list->length; ++i) { unsigned char tmp[size]; @@ -138,7 +155,7 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) if (ret) { memcpy(ret, ptr, list->memb_size); } - return (ptr - list->data) / list->memb_size; + return (ptr - (unsigned char *)list->data) / list->memb_size; } } @@ -149,7 +166,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) } size_t size = list->memb_size; - unsigned char (*array)[size] = (void *)list->data; + unsigned char (*array)[size] = list->data; for (size_t i = 0; i < list->length; ++i) { if (compare(&array[i], key) == 0) { @@ -163,15 +180,23 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) return -1; } -void list_foreach_cb(list_t *list, void callback(void *)) { +void list_foreach(list_t *list, void callback(void *)) { if (!list || !callback) { return; } size_t size = list->memb_size; - unsigned char (*array)[size] = (void *)list->data; + unsigned char (*array)[size] = list->data; for (size_t i = 0; i < list->length; ++i) { callback(&array[i]); } } + +void *list_end(list_t *list) { + if (!list) { + return NULL; + } + + return (unsigned char *)list->data + list->memb_size * list->length; +} diff --git a/include/list.h b/include/list.h index 087804c91..fd5015137 100644 --- a/include/list.h +++ b/include/list.h @@ -1,7 +1,6 @@ #ifndef _SWAY_LIST_H #define _SWAY_LIST_H -#include #include #include @@ -9,7 +8,7 @@ typedef struct { size_t capacity; size_t length; size_t memb_size; - alignas(max_align_t) unsigned char data[]; + void *data; } list_t; /* @@ -19,22 +18,25 @@ typedef struct { * memb_size must be the size of the type stored in this list. * If an element is added to this list which is not the same type * as the elements of the list, the behavior is undefined. - * - * This list should be freed with the stdlib free() function. */ list_t *list_new(size_t memb_size, size_t capacity); +/* + * Frees a list. + */ +void list_free(list_t *list); + /* * Adds an element to the end of the list. */ -void list_add(list_t **list, const void *data); +void list_add(list_t *list, const void *data); /* * Adds an element at an arbitrary position in the list, moving * the elements past index to make space. * index must be less than or equal to the length of the list. */ -void list_insert(list_t **list, size_t index, const void *data); +void list_insert(list_t *list, size_t index, const void *data); /* * Deletes the element at an arbitrary position in the list, @@ -87,22 +89,19 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) /* * Calls a function on every item in the list. */ -void list_foreach_cb(list_t *list, void callback(void *)); +void list_foreach(list_t *list, void callback(void *)); /* - * This is for loop helper, iterating over every element in list. - * iter must be a pointer to the list element's type. - * You should not add, remove, or sort elements from the list while inside this loop. + * Returns a pointer to just past the end of the list. + * This can be used to write for loops more easily. * - * Example: - * char **ptr; - * list_foreach(list_of_strings, ptr) { + * Example (for a list of char *): + * char **end = list_end(list); + * for (char **ptr = list->items; ptr < end; ++ptr) { * printf("%s\n", *ptr); * } */ -#define list_foreach(list, iter) \ - for (iter = (void *)list->data; \ - (unsigned char *)iter < list->data + list->memb_size * list->length; \ - ++iter) +// Consider making this inline or __attribute__((pure))__ +void *list_end(list_t *list); #endif From 4e27b1e72605268fecd365dfbe06eabb4ba6e84d Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 12:53:48 +1200 Subject: [PATCH 03/17] Added sway_asserts. --- common/list.c | 31 ++++++++++++++++++------------- include/list.h | 6 +++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/common/list.c b/common/list.c index cf86ac953..962ed6318 100644 --- a/common/list.c +++ b/common/list.c @@ -1,4 +1,5 @@ #include "list.h" +#include "log.h" #include #include @@ -52,7 +53,7 @@ static bool resize(list_t *list) { } void list_add(list_t *list, const void *data) { - if (!data || !list || !resize(list)) { + if (!sway_assert(list && data, "Invalid argument") || !resize(list)) { return; } @@ -64,7 +65,9 @@ void list_add(list_t *list, const void *data) { } void list_insert(list_t *list, size_t index, const void *data) { - if (!data || !list || index > list->length || !resize(list)) { + if (!sway_assert(list && data && index <= list->length, "Invalid argument") || + !resize(list)) { + return; } @@ -76,7 +79,7 @@ void list_insert(list_t *list, size_t index, const void *data) { } void list_delete(list_t *list, size_t index) { - if (!list || index >= list->length) { + if (!sway_assert(list && index < list->length, "Invalid argument")) { return; } @@ -88,7 +91,7 @@ void list_delete(list_t *list, size_t index) { } void list_swap(list_t *list, size_t i1, size_t i2) { - if (!list || i1 >= list->length || i2 >= list->length) { + if (!sway_assert(list && i1 < list->length && i2 < list->length, "Invalid argument")) { return; } @@ -102,7 +105,7 @@ void list_swap(list_t *list, size_t i1, size_t i2) { } void *list_get(list_t *list, size_t index) { - if (!list || index >= list->length) { + if (!sway_assert(list && index < list->length, "Invalid argument")) { return NULL; } @@ -113,7 +116,7 @@ void *list_get(list_t *list, size_t index) { } void list_qsort(list_t *list, int compare(const void *, const void *)) { - if (!list || !compare) { + if (!sway_assert(list && compare, "Invalid argument")) { return; } @@ -121,7 +124,7 @@ void list_qsort(list_t *list, int compare(const void *, const void *)) { } void list_isort(list_t *list, int compare(const void *, const void *)) { - if (!list || !compare) { + if (!sway_assert(list && compare, "Invalid argument")) { return; } @@ -143,8 +146,9 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), - const void *key, void *ret) { - if (!list || !compare || !key) { + const void *key, void *ret) { + + if (!sway_assert(list && compare && key, "Invalid argument")) { return -1; } @@ -160,8 +164,9 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) } ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), - const void *key, void *ret) { - if (!list || !compare || !key) { + const void *key, void *ret) { + + if (!sway_assert(list && compare && key, "Invalid argument")) { return -1; } @@ -181,7 +186,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) } void list_foreach(list_t *list, void callback(void *)) { - if (!list || !callback) { + if (!sway_assert(list && callback, "Invalid argument")) { return; } @@ -194,7 +199,7 @@ void list_foreach(list_t *list, void callback(void *)) { } void *list_end(list_t *list) { - if (!list) { + if (!sway_assert(list, "Invalid argument")) { return NULL; } diff --git a/include/list.h b/include/list.h index fd5015137..52197bb6a 100644 --- a/include/list.h +++ b/include/list.h @@ -23,6 +23,7 @@ list_t *list_new(size_t memb_size, size_t capacity); /* * Frees a list. + * If list is null, no action is taken. */ void list_free(list_t *list); @@ -76,7 +77,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)); * The list must be sorted. */ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), - const void *key, void *ret); + const void *key, void *ret); /* * Returns the index of the key in the list, using a linear search, @@ -84,7 +85,7 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) * copied into it. */ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), - const void *key, void *ret); + const void *key, void *ret); /* * Calls a function on every item in the list. @@ -101,7 +102,6 @@ void list_foreach(list_t *list, void callback(void *)); * printf("%s\n", *ptr); * } */ -// Consider making this inline or __attribute__((pure))__ void *list_end(list_t *list); #endif From 8429e871f84a5cf11e8c207ceafb02b755aa0e3a Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 13:11:49 +1200 Subject: [PATCH 04/17] Added list shrinking. --- common/list.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/list.c b/common/list.c index 962ed6318..3fd4fad93 100644 --- a/common/list.c +++ b/common/list.c @@ -88,6 +88,21 @@ void list_delete(list_t *list, size_t index) { memmove(&array[index], &array[index + 1], size * (list->length - index)); --list->length; + + /* We shrink very sparse lists, but only down to a certain size. + * The choice of >= 8 is somewhat arbitrary, but leaves a minimum + * size of 4 elements. + */ + if (list->length <= list->capacity / 4 && list->capacity >= 8) { + size_t new_cap = list->capacity / 2; + void *data = realloc(list->data, list->memb_size * new_cap); + if (!data) { + return; + } + + list->data = data; + list->capacity = new_cap; + } } void list_swap(list_t *list, size_t i1, size_t i2) { From 95737e38b810d5a13a0f21362bbb6312d7046501 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 13:18:20 +1200 Subject: [PATCH 05/17] Renamed 'data' to 'items', to be more consistent with the old API. --- common/list.c | 59 +++++++++++++++++++++++++------------------------- include/list.h | 2 +- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/common/list.c b/common/list.c index 3fd4fad93..74852a426 100644 --- a/common/list.c +++ b/common/list.c @@ -6,25 +6,26 @@ #include list_t *list_new(size_t memb_size, size_t capacity) { - if (capacity == 0) + if (capacity == 0) { capacity = 8; + } - list_t *l = malloc(sizeof(*l)); - if (!l) { + list_t *list = malloc(sizeof(*list)); + if (!list) { return NULL; } - l->capacity = capacity; - l->length = 0; - l->memb_size = memb_size; + list->capacity = capacity; + list->length = 0; + list->memb_size = memb_size; - l->data = malloc(memb_size * capacity); - if (!l->data) { - free(l); + list->items = malloc(memb_size * capacity); + if (!list->items) { + free(list); return NULL; } - return l; + return list; } void list_free(list_t *list) { @@ -32,7 +33,7 @@ void list_free(list_t *list) { return; } - free(list->data); + free(list->items); free(list); } @@ -42,12 +43,12 @@ static bool resize(list_t *list) { } size_t new_cap = list->capacity * 2; - void *data = realloc(list->data, list->memb_size * new_cap); - if (!data) { + void *items = realloc(list->items, list->memb_size * new_cap); + if (!items) { return false; } - list->data = data; + list->items = items; list->capacity = new_cap; return true; } @@ -58,7 +59,7 @@ void list_add(list_t *list, const void *data) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; memcpy(&array[list->length], data, size); ++list->length; @@ -72,7 +73,7 @@ void list_insert(list_t *list, size_t index, const void *data) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; memmove(&array[index + 1], &array[index], size * (list->length - index)); memcpy(&array[index], data, size); ++list->length; @@ -84,7 +85,7 @@ void list_delete(list_t *list, size_t index) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; memmove(&array[index], &array[index + 1], size * (list->length - index)); --list->length; @@ -95,12 +96,12 @@ void list_delete(list_t *list, size_t index) { */ if (list->length <= list->capacity / 4 && list->capacity >= 8) { size_t new_cap = list->capacity / 2; - void *data = realloc(list->data, list->memb_size * new_cap); - if (!data) { + void *items = realloc(list->items, list->memb_size * new_cap); + if (!items) { return; } - list->data = data; + list->items = items; list->capacity = new_cap; } } @@ -111,7 +112,7 @@ void list_swap(list_t *list, size_t i1, size_t i2) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; unsigned char tmp[size]; memcpy(tmp, &array[i1], size); @@ -125,7 +126,7 @@ void *list_get(list_t *list, size_t index) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; return &array[index]; } @@ -135,7 +136,7 @@ void list_qsort(list_t *list, int compare(const void *, const void *)) { return; } - qsort(list->data, list->length, list->memb_size, compare); + qsort(list->items, list->length, list->memb_size, compare); } void list_isort(list_t *list, int compare(const void *, const void *)) { @@ -144,7 +145,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; for (size_t i = 1; i < list->length; ++i) { unsigned char tmp[size]; @@ -167,14 +168,14 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) return -1; } - const unsigned char *ptr = bsearch(key, list->data, list->length, list->memb_size, compare); + const unsigned char *ptr = bsearch(key, list->items, list->length, list->memb_size, compare); if (!ptr) { return -1; } else { if (ret) { memcpy(ret, ptr, list->memb_size); } - return (ptr - (unsigned char *)list->data) / list->memb_size; + return (ptr - (unsigned char *)list->items) / list->memb_size; } } @@ -186,7 +187,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { if (compare(&array[i], key) == 0) { @@ -206,7 +207,7 @@ void list_foreach(list_t *list, void callback(void *)) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->data; + unsigned char (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { callback(&array[i]); @@ -218,5 +219,5 @@ void *list_end(list_t *list) { return NULL; } - return (unsigned char *)list->data + list->memb_size * list->length; + return (unsigned char *)list->items + list->memb_size * list->length; } diff --git a/include/list.h b/include/list.h index 52197bb6a..2b0155ce7 100644 --- a/include/list.h +++ b/include/list.h @@ -8,7 +8,7 @@ typedef struct { size_t capacity; size_t length; size_t memb_size; - void *data; + void *items; } list_t; /* From 1fd8d33f2f1ecde3220ec5c2713c91b4e596ab6b Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 13:21:12 +1200 Subject: [PATCH 06/17] Changed unsigned char to uint8_t --- common/list.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/common/list.c b/common/list.c index 74852a426..ab8e4d023 100644 --- a/common/list.c +++ b/common/list.c @@ -1,8 +1,9 @@ #include "list.h" #include "log.h" -#include #include +#include +#include #include list_t *list_new(size_t memb_size, size_t capacity) { @@ -59,7 +60,7 @@ void list_add(list_t *list, const void *data) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; memcpy(&array[list->length], data, size); ++list->length; @@ -73,7 +74,7 @@ void list_insert(list_t *list, size_t index, const void *data) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; memmove(&array[index + 1], &array[index], size * (list->length - index)); memcpy(&array[index], data, size); ++list->length; @@ -85,7 +86,7 @@ void list_delete(list_t *list, size_t index) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; memmove(&array[index], &array[index + 1], size * (list->length - index)); --list->length; @@ -112,9 +113,9 @@ void list_swap(list_t *list, size_t i1, size_t i2) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; - unsigned char tmp[size]; + uint8_t tmp[size]; memcpy(tmp, &array[i1], size); memcpy(&array[i1], &array[i2], size); memcpy(&array[i2], tmp, size); @@ -126,7 +127,7 @@ void *list_get(list_t *list, size_t index) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; return &array[index]; } @@ -145,10 +146,10 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; for (size_t i = 1; i < list->length; ++i) { - unsigned char tmp[size]; + uint8_t tmp[size]; memcpy(tmp, &array[i], size); ssize_t j = i - 1; @@ -168,14 +169,14 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) return -1; } - const unsigned char *ptr = bsearch(key, list->items, list->length, list->memb_size, compare); + const uint8_t *ptr = bsearch(key, list->items, list->length, list->memb_size, compare); if (!ptr) { return -1; } else { if (ret) { memcpy(ret, ptr, list->memb_size); } - return (ptr - (unsigned char *)list->items) / list->memb_size; + return (ptr - (uint8_t *)list->items) / list->memb_size; } } @@ -187,7 +188,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { if (compare(&array[i], key) == 0) { @@ -207,7 +208,7 @@ void list_foreach(list_t *list, void callback(void *)) { } size_t size = list->memb_size; - unsigned char (*array)[size] = list->items; + uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { callback(&array[i]); @@ -219,5 +220,5 @@ void *list_end(list_t *list) { return NULL; } - return (unsigned char *)list->items + list->memb_size * list->length; + return (uint8_t *)list->items + list->memb_size * list->length; } From 9538e36728ec0839c0a73682702893b9dcadda59 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 13:38:49 +1200 Subject: [PATCH 07/17] Added argument names to the function pointers. --- common/list.c | 6 +++--- include/list.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/list.c b/common/list.c index ab8e4d023..41bbb7eb1 100644 --- a/common/list.c +++ b/common/list.c @@ -162,7 +162,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } } -ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), +ssize_t list_bsearch(const list_t *list, int compare(const void *key, const void *item), const void *key, void *ret) { if (!sway_assert(list && compare && key, "Invalid argument")) { @@ -180,7 +180,7 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) } } -ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), +ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void *item), const void *key, void *ret) { if (!sway_assert(list && compare && key, "Invalid argument")) { @@ -202,7 +202,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *) return -1; } -void list_foreach(list_t *list, void callback(void *)) { +void list_foreach(list_t *list, void callback(void *item)) { if (!sway_assert(list && callback, "Invalid argument")) { return; } diff --git a/include/list.h b/include/list.h index 2b0155ce7..15b695bfa 100644 --- a/include/list.h +++ b/include/list.h @@ -61,14 +61,14 @@ void *list_get(list_t *list, size_t index); /* * Sorts the list using the stdlib qsort() function. */ -void list_qsort(list_t *list, int compare(const void *, const void *)); +void list_qsort(list_t *list, int compare(const void *left, const void *right)); /* * Sorts the list using insertion sort. * This should be used if you need a stable sort, and your list is * short and/or nearly sorted. */ -void list_isort(list_t *list, int compare(const void *, const void *)); +void list_isort(list_t *list, int compare(const void *left, const void *right)); /* * Returns the index of key in the list, using the stdlib bsearch() function, @@ -76,7 +76,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)); * copied into it. * The list must be sorted. */ -ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *), +ssize_t list_bsearch(const list_t *list, int compare(const void *key, const void *item), const void *key, void *ret); /* @@ -84,13 +84,13 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *) * or -1 if it was not found. If ret is not null, the found element will be * copied into it. */ -ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *), +ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void *item), const void *key, void *ret); /* * Calls a function on every item in the list. */ -void list_foreach(list_t *list, void callback(void *)); +void list_foreach(list_t *list, void callback(void *item)); /* * Returns a pointer to just past the end of the list. From 46a53704ada9b277ade7d97eccf784f89bb7926e Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 10 May 2017 13:50:18 +1200 Subject: [PATCH 08/17] Small fix: arguments passed in wrong order. --- common/list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/list.c b/common/list.c index 41bbb7eb1..3d83faab4 100644 --- a/common/list.c +++ b/common/list.c @@ -191,7 +191,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { - if (compare(&array[i], key) == 0) { + if (compare(key, &array[i]) == 0) { if (ret) { memcpy(ret, &array[i], size); } From a2896f33a45d488f4717ef5d5129f5fade80a713 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 00:22:49 +1200 Subject: [PATCH 09/17] Got it to build. --- common/list.c | 55 ++++++-- common/stringop.c | 27 ++-- include/list.h | 33 ++++- include/sway/config.h | 2 +- include/sway/criteria.h | 4 +- include/sway/layout.h | 2 +- sway/border.c | 22 ++-- sway/commands.c | 33 +++-- sway/commands/assign.c | 6 +- sway/commands/bar.c | 7 +- sway/commands/bar/bindsym.c | 8 +- sway/commands/bar/hidden_state.c | 6 +- sway/commands/bar/id.c | 5 +- sway/commands/bar/mode.c | 6 +- sway/commands/bar/modifier.c | 7 +- sway/commands/bar/output.c | 17 +-- sway/commands/bind.c | 48 +++---- sway/commands/floating_mod.c | 5 +- sway/commands/focus.c | 25 ++-- sway/commands/for_window.c | 6 +- sway/commands/gaps.c | 9 +- sway/commands/ipc.c | 2 +- sway/commands/layout.c | 7 +- sway/commands/mark.c | 35 ++--- sway/commands/mode.c | 9 +- sway/commands/move.c | 8 +- sway/commands/no_focus.c | 6 +- sway/commands/output.c | 13 +- sway/commands/resize.c | 30 +++-- sway/commands/scratchpad.c | 4 +- sway/commands/set.c | 7 +- sway/commands/unmark.c | 11 +- sway/commands/workspace.c | 10 +- sway/config.c | 212 +++++++++++++++---------------- sway/container.c | 144 ++++++++++----------- sway/criteria.c | 51 ++++---- sway/debug_log.c | 6 +- sway/extensions.c | 31 ++--- sway/focus.c | 2 +- sway/handlers.c | 120 ++++++++--------- sway/ipc-json.c | 29 ++--- sway/ipc-server.c | 56 ++++---- sway/layout.c | 201 ++++++++++++++--------------- sway/main.c | 2 +- sway/output.c | 22 ++-- sway/security.c | 26 ++-- sway/workspace.c | 66 +++++----- swaybar/bar.c | 52 ++++---- swaybar/config.c | 2 +- swaybar/ipc.c | 33 +++-- swaybar/render.c | 10 +- swaybar/status_line.c | 18 ++- swaybg/main.c | 22 ++-- swaylock/main.c | 35 +++-- wayland/registry.c | 4 +- 55 files changed, 822 insertions(+), 797 deletions(-) diff --git a/common/list.c b/common/list.c index 3d83faab4..16b5629a0 100644 --- a/common/list.c +++ b/common/list.c @@ -66,6 +66,14 @@ void list_add(list_t *list, const void *data) { ++list->length; } +void *list_alloc(list_t *list) { + if (!sway_assert(list, "Invalid argument") || !resize(list)) { + return NULL; + } + + return (uint8_t *)list->items + list->memb_size * list->length++; +} + void list_insert(list_t *list, size_t index, const void *data) { if (!sway_assert(list && data && index <= list->length, "Invalid argument") || !resize(list)) { @@ -80,17 +88,7 @@ void list_insert(list_t *list, size_t index, const void *data) { ++list->length; } -void list_delete(list_t *list, size_t index) { - if (!sway_assert(list && index < list->length, "Invalid argument")) { - return; - } - - size_t size = list->memb_size; - uint8_t (*array)[size] = list->items; - - memmove(&array[index], &array[index + 1], size * (list->length - index)); - --list->length; - +static void shrink(list_t *list) { /* We shrink very sparse lists, but only down to a certain size. * The choice of >= 8 is somewhat arbitrary, but leaves a minimum * size of 4 elements. @@ -107,6 +105,28 @@ void list_delete(list_t *list, size_t index) { } } +void list_remove(list_t *list) { + if (!sway_assert(list, "Invalid argument") || list->length == 0) { + return; + } + + --list->length; + shrink(list); +} + +void list_delete(list_t *list, size_t index) { + if (!sway_assert(list && index < list->length, "Invalid argument")) { + return; + } + + size_t size = list->memb_size; + uint8_t (*array)[size] = list->items; + + memmove(&array[index], &array[index + 1], size * (list->length - index)); + --list->length; + shrink(list); +} + void list_swap(list_t *list, size_t i1, size_t i2) { if (!sway_assert(list && i1 < list->length && i2 < list->length, "Invalid argument")) { return; @@ -215,6 +235,15 @@ void list_foreach(list_t *list, void callback(void *item)) { } } +void list_free_with(list_t *list, void callback(void *item)) { + if (!sway_assert(callback, "Invalid argument") || !list) { + return; + } + + list_foreach(list, callback); + list_free(list); +} + void *list_end(list_t *list) { if (!sway_assert(list, "Invalid argument")) { return NULL; @@ -222,3 +251,7 @@ void *list_end(list_t *list) { return (uint8_t *)list->items + list->memb_size * list->length; } + +void list_elem_free(void *item) { + free(*(void **)item); +} diff --git a/common/stringop.c b/common/stringop.c index 99e9636d8..167562256 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -69,14 +69,14 @@ int lenient_strcmp(char *a, char *b) { } list_t *split_string(const char *str, const char *delims) { - list_t *res = create_list(); + list_t *res = list_new(sizeof(char *), 0); char *copy = strdup(str); char *token; token = strtok(copy, delims); while(token) { token = strdup(token); - list_add(res, token); + list_add(res, &token); token = strtok(NULL, delims); } free(copy); @@ -84,11 +84,7 @@ list_t *split_string(const char *str, const char *delims) { } void free_flat_list(list_t *list) { - int i; - for (i = 0; i < list->length; ++i) { - free(list->items[i]); - } - list_free(list); + list_free_with(list, list_elem_free); } char **split_args(const char *start, int *argc) { @@ -315,22 +311,25 @@ char *join_list(list_t *list, char *separator) { len += (list->length - 1) * sep_len; } - for (int i = 0; i < list->length; i++) { - len += strlen(list->items[i]); + for (size_t i = 0; i < list->length; i++) { + char *item = *(char **)list_get(list, i); + len += strlen(item); } char *res = malloc(len); - char *p = res + strlen(list->items[0]); - strcpy(res, list->items[0]); + char *item = *(char **)list_get(list, 0); + char *p = res + strlen(item); + strcpy(res, item); - for (int i = 1; i < list->length; i++) { + for (size_t i = 1; i < list->length; i++) { + item = *(char **)list_get(list, i); if (sep_len) { memcpy(p, separator, sep_len); p += sep_len; } - strcpy(p, list->items[i]); - p += strlen(list->items[i]); + strcpy(p, item); + p += strlen(item); } *p = '\0'; diff --git a/include/list.h b/include/list.h index 15b695bfa..ee9143f71 100644 --- a/include/list.h +++ b/include/list.h @@ -28,10 +28,41 @@ list_t *list_new(size_t memb_size, size_t capacity); void list_free(list_t *list); /* - * Adds an element to the end of the list. + * Frees a list, calling a function on each element before doing so. + * If list is null, no action is taken. + * DO NOT pass free as the callback. Use list_elem_free if + * you want to do that. + */ +void list_free_with(list_t *list, void callback(void *item)); + +/* + * This is a convinience function designed to be used with + * list_free_with or list_foreach. + * It calls the stdlib free function on each element. + * We can't pass in free directly, as each pointer needs to be + * dereferenced first. + * This should only be used on lists of pointers that were + * allocated using malloc (or similar). + */ +void list_elem_free(void *item); + +/* + * Adds an element at the end of the list. */ void list_add(list_t *list, const void *data); +/* + * Adds an uninitialized element at the end of the list, + * and returns a pointer to it. + */ +void *list_alloc(list_t *list); + +/* + * Deletes the last element of the list. + * If the list is empty, no action is taken. + */ +void list_remove(list_t *list); + /* * Adds an element at an arbitrary position in the list, moving * the elements past index to make space. diff --git a/include/sway/config.h b/include/sway/config.h index 35f8d5f7e..28f31678e 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -344,7 +344,7 @@ char *do_var_replacement(char *str); struct cmd_results *check_security_config(); -int input_identifier_cmp(const void *item, const void *data); +int input_identifier_cmp(const void *key, const void *item); void merge_input_config(struct input_config *dst, struct input_config *src); void apply_input_config(struct input_config *ic, struct libinput_device *dev); void free_input_config(struct input_config *ic); diff --git a/include/sway/criteria.h b/include/sway/criteria.h index c5ed9857e..debc09dde 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -22,7 +22,7 @@ struct criteria { char *cmdlist; }; -int criteria_cmp(const void *item, const void *data); +int criteria_cmp(const void *key, const void *item); void free_criteria(struct criteria *crit); // Pouplate list with crit_tokens extracted from criteria string, returns error @@ -39,4 +39,6 @@ list_t *container_for(list_t *tokens); // Returns true if any criteria in the given list matches this container bool criteria_any(swayc_t *cont, list_t *criteria); +struct crit_token; + #endif diff --git a/include/sway/layout.h b/include/sway/layout.h index f0791588a..c45c008ae 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -24,7 +24,7 @@ void add_child(swayc_t *parent, swayc_t *child); // Adds child to parent at index, if parent has no focus, it is set to child // parent must be of type C_WORKSPACE or C_CONTAINER -void insert_child(swayc_t *parent, swayc_t *child, int index); +void insert_child(swayc_t *parent, swayc_t *child, size_t index); // Adds child as floating window to ws, if there is no focus it is set to child. // ws must be of type C_WORKSPACE diff --git a/sway/border.c b/sway/border.c index 10ad92c2d..73516381a 100644 --- a/sway/border.c +++ b/sway/border.c @@ -198,7 +198,7 @@ static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, int total_len = 0; for(int i = view->marks->length - 1; i >= 0; --i) { - char *mark = (char *)view->marks->items[i]; + char *mark = *(char **)list_get(view->marks, i); if (*mark != '_') { int width, height; get_text_size(cr, config->font, &width, &height, 1, false, "[%s]", mark); @@ -271,10 +271,9 @@ static char *generate_container_title(swayc_t *container) { } snprintf(name, len, "sway: %c[", layout); - int i; - for (i = 0; i < container->children->length; ++i) { + for (size_t i = 0; i < container->children->length; ++i) { prev_name = name; - swayc_t* child = container->children->items[i]; + swayc_t* child = *(swayc_t **)list_get(container->children, i); const char *title = NULL; if (child->type == C_VIEW) { title = child->app_id ? child->app_id : @@ -330,9 +329,8 @@ void update_tabbed_stacked_titlebars(swayc_t *c, cairo_t *cr, struct wlc_geometr return; } - int i; - for (i = 0; i < c->children->length; ++i) { - swayc_t *child = c->children->items[i]; + for (size_t i = 0; i < c->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(c->children, i); update_tabbed_stacked_titlebars(child, cr, g, focused, focused_inactive); } } else { @@ -402,9 +400,8 @@ static void update_view_border(swayc_t *view) { } // generate container titles - int i; - for (i = 0; i < p->children->length; ++i) { - swayc_t *child = p->children->items[i]; + for (size_t i = 0; i < p->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(p->children, i); if (child->type == C_CONTAINER) { generate_container_title(child); } @@ -471,8 +468,9 @@ void update_container_border(swayc_t *container) { update_view_border(container); return; } else { - for (int i = 0; i < container->children->length; ++i) { - update_container_border(container->children->items[i]); + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + update_container_border(item); } } } diff --git a/sway/commands.c b/sway/commands.c index 509fd1a82..ffade4cb5 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -102,16 +102,15 @@ void hide_view_in_scratchpad(swayc_t *sp_view) { } void input_cmd_apply(struct input_config *input) { - int i; - i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier); + struct input_config *ic; + ssize_t i = list_lsearch(config->input_configs, input_identifier_cmp, input->identifier, &ic); if (i >= 0) { // merge existing config - struct input_config *ic = config->input_configs->items[i]; merge_input_config(ic, input); free_input_config(input); input = ic; } else { - list_add(config->input_configs, input); + list_add(config->input_configs, &input); } current_input_config = input; @@ -121,8 +120,8 @@ void input_cmd_apply(struct input_config *input) { // this is during startup then there will be no container and config // will be applied during normal "new input" event from wlc. struct libinput_device *device = NULL; - for (int i = 0; i < input_devices->length; ++i) { - device = input_devices->items[i]; + for (size_t i = 0; i < input_devices->length; ++i) { + device = *(struct libinput_device **)list_get(input_devices, i); char* dev_identifier = libinput_dev_unique_id(device); if (!dev_identifier) { break; @@ -138,15 +137,15 @@ void input_cmd_apply(struct input_config *input) { } void remove_view_from_scratchpad(swayc_t *view) { - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == view) { + for (size_t i = 0; i < scratchpad->length; i++) { + swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + if (item == view) { if (sp_index == 0) { sp_index = scratchpad->length - 1; } else { sp_index--; } - list_del(scratchpad, sp_index); + list_delete(scratchpad, sp_index); sp_view = NULL; } } @@ -384,7 +383,7 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { char *criteria_string = argsep(&head, "]"); if (head) { ++head; - list_t *tokens = create_list(); + list_t *tokens = list_new(sizeof(struct crit_token *), 0); char *error; if ((error = extract_crit_tokens(tokens, criteria_string))) { @@ -448,14 +447,14 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { free_argv(argc, argv); goto cleanup; } - int i = 0; + size_t i = 0; do { if (!containers) { current_container = get_focused_container(&root_container); } else if (containers->length == 0) { break; } else { - current_container = (swayc_t *)containers->items[i]; + current_container = *(swayc_t **)list_get(containers, i); } sway_log(L_INFO, "Running on container '%s'", current_container->name); @@ -483,7 +482,7 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { cleanup: free(exec); if (containers) { - free(containers); + list_free(containers); } if (!results) { results = cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -594,8 +593,8 @@ struct cmd_results *config_commands_command(char *exec) { } struct command_policy *policy = NULL; - for (int i = 0; i < config->command_policies->length; ++i) { - struct command_policy *p = config->command_policies->items[i]; + for (size_t i = 0; i < config->command_policies->length; ++i) { + struct command_policy *p = *(struct command_policy **)list_get(config->command_policies, i); if (strcmp(p->command, cmd) == 0) { policy = p; break; @@ -606,7 +605,7 @@ struct cmd_results *config_commands_command(char *exec) { if (!policy) { sway_abort("Unable to allocate security policy"); } - list_add(config->command_policies, policy); + list_add(config->command_policies, &policy); } policy->context = context; diff --git a/sway/commands/assign.c b/sway/commands/assign.c index ec262bb8b..baeebc9c7 100644 --- a/sway/commands/assign.c +++ b/sway/commands/assign.c @@ -36,7 +36,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) { } crit->crit_raw = strdup(criteria); crit->cmdlist = cmdlist; - crit->tokens = create_list(); + crit->tokens = list_new(sizeof(struct crit_token *), 0); char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); if (err_str) { @@ -46,12 +46,12 @@ struct cmd_results *cmd_assign(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { + } else if (list_lsearch(config->criteria, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "assign: Duplicate, skipping."); free_criteria(crit); } else { sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); - list_add(config->criteria, crit); + list_add(config->criteria, &crit); } return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 04745a6ef..611ca9ac7 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c @@ -38,13 +38,12 @@ struct cmd_results *cmd_bar(int argc, char **argv) { } // set bar id - int i; - for (i = 0; i < config->bars->length; ++i) { - if (bar == config->bars->items[i]) { + for (size_t i = 0; i < config->bars->length; ++i) { + if (bar == *(struct bar_config **)list_get(config->bars, i)) { const int len = 5 + numlen(i); // "bar-" + i + \0 bar->id = malloc(len * sizeof(char)); if (bar->id) { - snprintf(bar->id, len, "bar-%d", i); + snprintf(bar->id, len, "bar-%zu", i); } else { return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID"); } diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index 5f90b51a8..83c7ec38c 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c @@ -33,14 +33,14 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { binding->command = join_args(argv + 1, argc - 1); struct bar_config *bar = config->current_bar; - int i = list_seq_find(bar->bindings, sway_mouse_binding_cmp_buttons, binding); + struct sway_mouse_binding *dup; + ssize_t i = list_lsearch(bar->bindings, sway_mouse_binding_cmp_buttons, binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); - struct sway_mouse_binding *dup = bar->bindings->items[i]; free_sway_mouse_binding(dup); - list_del(bar->bindings, i); + list_delete(bar->bindings, i); } - list_add(bar->bindings, binding); + list_add(bar->bindings, &binding); 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); diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c index 0b49aa6b6..dfc10ef3e 100644 --- a/sway/commands/bar/hidden_state.c +++ b/sway/commands/bar/hidden_state.c @@ -58,10 +58,8 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { id = argv[1]; } - int i; - struct bar_config *bar; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); if (id && strcmp(id, bar->id) == 0) { return bar_set_hidden_state(bar, state); } diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c index 1221ebf65..0f686225b 100644 --- a/sway/commands/bar/id.c +++ b/sway/commands/bar/id.c @@ -13,9 +13,8 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) { const char *oldname = config->current_bar->id; // check if id is used by a previously defined bar - int i; - for (i = 0; i < config->bars->length; ++i) { - struct bar_config *find = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + struct bar_config *find = *(struct bar_config **)list_get(config->bars, i); if (strcmp(name, find->id) == 0 && config->current_bar != find) { return cmd_results_new(CMD_FAILURE, "id", "Id '%s' already defined for another bar. Id unchanged (%s).", diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c index 36816b939..39fe61f7b 100644 --- a/sway/commands/bar/mode.c +++ b/sway/commands/bar/mode.c @@ -63,10 +63,8 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) { id = argv[1]; } - int i; - struct bar_config *bar; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); if (id && strcmp(id, bar->id) == 0) { return bar_set_mode(bar, mode); } diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c index 153d87e6f..7abec0f1e 100644 --- a/sway/commands/bar/modifier.c +++ b/sway/commands/bar/modifier.c @@ -17,14 +17,15 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) { uint32_t mod = 0; list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { + for (size_t i = 0; i < split->length; ++i) { uint32_t tmp_mod; - if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) { + char *item = *(char **)list_get(split, i); + if ((tmp_mod = get_modifier_mask_by_name(item)) > 0) { mod |= tmp_mod; continue; } else { free_flat_list(split); - return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]); + return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", item); } } free_flat_list(split); diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c index a5710bc0b..9aa938810 100644 --- a/sway/commands/bar/output.c +++ b/sway/commands/bar/output.c @@ -17,23 +17,23 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { const char *output = argv[0]; list_t *outputs = config->current_bar->outputs; if (!outputs) { - outputs = create_list(); + outputs = list_new(sizeof(char *), 0); config->current_bar->outputs = outputs; } - int i; int add_output = 1; if (strcmp("*", output) == 0) { // remove all previous defined outputs and replace with '*' - for (i = 0; i < outputs->length; ++i) { - free(outputs->items[i]); - list_del(outputs, i); + for (size_t i = 0; i < outputs->length; ++i) { + char *item = *(char **)list_get(outputs, i); + free(item); + list_delete(outputs, i); } } else { // only add output if not already defined with either the same // name or as '*' - for (i = 0; i < outputs->length; ++i) { - const char *find = outputs->items[i]; + for (size_t i = 0; i < outputs->length; ++i) { + const char *find = *(char **)list_get(outputs, i); if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { add_output = 0; break; @@ -42,7 +42,8 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { } if (add_output) { - list_add(outputs, strdup(output)); + char *ptr = strdup(output); + list_add(outputs, &ptr); sway_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output); } diff --git a/sway/commands/bind.c b/sway/commands/bind.c index af5a01e52..2f504bc34 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -22,7 +22,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); } - binding->keys = create_list(); + binding->keys = list_new(sizeof(xkb_keysym_t *), 0); binding->modifiers = 0; binding->release = false; binding->bindcode = false; @@ -44,27 +44,28 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { binding->command = join_args(argv + 1, argc - 1); list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { + for (size_t i = 0; i < split->length; ++i) { // Check for a modifier key uint32_t mod; - if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + char *item = *(char **)list_get(split, i); + if ((mod = get_modifier_mask_by_name(item)) > 0) { binding->modifiers |= mod; continue; } // Check for xkb key - xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], + xkb_keysym_t sym = xkb_keysym_from_name(item, XKB_KEYSYM_CASE_INSENSITIVE); // Check for mouse binding - if (strncasecmp(split->items[i], "button", strlen("button")) == 0 && - strlen(split->items[i]) == strlen("button0")) { - sym = ((char *)split->items[i])[strlen("button")] - '1' + M_LEFT_CLICK; + if (strncasecmp(item, "button", strlen("button")) == 0 && + strlen(item) == strlen("button0")) { + sym = ((char *)item)[strlen("button")] - '1' + M_LEFT_CLICK; } if (!sym) { free_sway_binding(binding); free_flat_list(split); return cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", - (char *)split->items[i]); + item); } xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); if (!key) { @@ -74,20 +75,20 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { "Unable to allocate binding"); } *key = sym; - list_add(binding->keys, key); + list_add(binding->keys, &key); } free_flat_list(split); struct sway_mode *mode = config->current_mode; - int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); + struct sway_binding *dup; + ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]); - struct sway_binding *dup = mode->bindings->items[i]; free_sway_binding(dup); - list_del(mode->bindings, i); + list_delete(mode->bindings, i); } binding->order = binding_order++; - list_add(mode->bindings, binding); + list_add(mode->bindings, &binding); list_qsort(mode->bindings, sway_binding_cmp_qsort); sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); @@ -105,7 +106,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); } - binding->keys = create_list(); + binding->keys = list_new(sizeof(xkb_keycode_t *), 0); binding->modifiers = 0; binding->release = false; binding->bindcode = true; @@ -127,41 +128,42 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { binding->command = join_args(argv + 1, argc - 1); list_t *split = split_string(argv[0], "+"); - for (int i = 0; i < split->length; ++i) { + for (size_t i = 0; i < split->length; ++i) { // Check for a modifier key uint32_t mod; - if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { + char *item = *(char **)list_get(split, i); + if ((mod = get_modifier_mask_by_name(item)) > 0) { binding->modifiers |= mod; continue; } // parse keycode - xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10); + xkb_keycode_t keycode = (int)strtol(item, NULL, 10); if (!xkb_keycode_is_legal_ext(keycode)) { - error = cmd_results_new(CMD_INVALID, "bindcode", "Invalid keycode '%s'", (char *)split->items[i]); + error = cmd_results_new(CMD_INVALID, "bindcode", "Invalid keycode '%s'", item); free_sway_binding(binding); list_free(split); return error; } xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); *key = keycode - 8; - list_add(binding->keys, key); + list_add(binding->keys, &key); } free_flat_list(split); struct sway_mode *mode = config->current_mode; - int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding); + struct sway_binding *dup; + ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, binding, &dup); if (i > -1) { - struct sway_binding *dup = mode->bindings->items[i]; if (dup->bindcode) { sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]); } else { sway_log(L_DEBUG, "bindcode - '%s' already exists as bindsym, overwriting", argv[0]); } free_sway_binding(dup); - list_del(mode->bindings, i); + list_delete(mode->bindings, i); } binding->order = binding_order++; - list_add(mode->bindings, binding); + list_add(mode->bindings, &binding); list_qsort(mode->bindings, sway_binding_cmp_qsort); sway_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command); diff --git a/sway/commands/floating_mod.c b/sway/commands/floating_mod.c index b8e81ab92..c34af0ac9 100644 --- a/sway/commands/floating_mod.c +++ b/sway/commands/floating_mod.c @@ -12,13 +12,12 @@ struct cmd_results *cmd_floating_mod(int argc, char **argv) { if ((error = checkarg(argc, "floating_modifier", EXPECTED_AT_LEAST, 1))) { return error; } - int i; list_t *split = split_string(argv[0], "+"); config->floating_mod = 0; // set modifier keys - for (i = 0; i < split->length; ++i) { - config->floating_mod |= get_modifier_mask_by_name(split->items[i]); + for (size_t i = 0; i < split->length; ++i) { + config->floating_mod |= get_modifier_mask_by_name(*(char **)list_get(split, i)); } free_flat_list(split); if (!config->floating_mod) { diff --git a/sway/commands/focus.c b/sway/commands/focus.c index defaba291..dfdb8df1f 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -36,8 +36,8 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) { return error; } - static int floating_toggled_index = 0; - static int tiled_toggled_index = 0; + static size_t floating_toggled_index = 0; + static size_t tiled_toggled_index = 0; if (strcasecmp(argv[0], "left") == 0) { move_focus(MOVE_LEFT); } else if (strcasecmp(argv[0], "right") == 0) { @@ -55,37 +55,40 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } else if (strcasecmp(argv[0], "prev") == 0) { move_focus(MOVE_PREV); } else if (strcasecmp(argv[0], "mode_toggle") == 0) { - int i; swayc_t *workspace = swayc_active_workspace(); swayc_t *focused = get_focused_view(workspace); if (focused->is_floating) { if (workspace->children->length > 0) { - for (i = 0;i < workspace->floating->length; i++) { - if (workspace->floating->items[i] == focused) { + for (size_t i = 0;i < workspace->floating->length; i++) { + swayc_t *item = *(swayc_t **)list_get(workspace->floating, i); + if (item == focused) { floating_toggled_index = i; break; } } if (workspace->children->length > tiled_toggled_index) { - set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index])); + swayc_t *item = *(swayc_t **)list_get(workspace->children, tiled_toggled_index); + set_focused_container(get_focused_view(item)); } else { - set_focused_container(get_focused_view(workspace->children->items[0])); + swayc_t *item = *(swayc_t **)list_get(workspace->children, 0); + set_focused_container(get_focused_view(item)); tiled_toggled_index = 0; } } } else { if (workspace->floating->length > 0) { - for (i = 0;i < workspace->children->length; i++) { - if (workspace->children->items[i] == focused) { + for (size_t i = 0;i < workspace->children->length; i++) { + swayc_t *item = *(swayc_t **)list_get(workspace->children, i); + if (item == focused) { tiled_toggled_index = i; break; } } if (workspace->floating->length > floating_toggled_index) { - swayc_t *floating = workspace->floating->items[floating_toggled_index]; + swayc_t *floating = *(swayc_t **)list_get(workspace->floating, floating_toggled_index); set_focused_container(get_focused_view(floating)); } else { - swayc_t *floating = workspace->floating->items[workspace->floating->length - 1]; + swayc_t *floating = *(swayc_t **)list_get(workspace->floating, workspace->floating->length - 1); set_focused_container(get_focused_view(floating)); tiled_toggled_index = workspace->floating->length - 1; } diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c index d1fd1641a..49f2daa4a 100644 --- a/sway/commands/for_window.c +++ b/sway/commands/for_window.c @@ -20,7 +20,7 @@ struct cmd_results *cmd_for_window(int argc, char **argv) { } crit->crit_raw = strdup(criteria); crit->cmdlist = cmdlist; - crit->tokens = create_list(); + crit->tokens = list_new(sizeof(struct crit_token *), 0); char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); if (err_str) { @@ -30,12 +30,12 @@ struct cmd_results *cmd_for_window(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { + } else if (list_lsearch(config->criteria, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "for_window: Duplicate, skipping."); free_criteria(crit); } else { sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); - list_add(config->criteria, crit); + list_add(config->criteria, &crit); } return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c index 0a48592da..5c554f7e8 100644 --- a/sway/commands/gaps.c +++ b/sway/commands/gaps.c @@ -133,11 +133,10 @@ struct cmd_results *cmd_gaps(int argc, char **argv) { arrange_windows(cont->parent, -1, -1); } else if (inout == OUTER) { //resize all workspace. - int i,j; - for (i = 0; i < root_container.children->length; ++i) { - swayc_t *op = root_container.children->items[i]; - for (j = 0; j < op->children->length; ++j) { - swayc_t *ws = op->children->items[j]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *op = *(swayc_t **)list_get(root_container.children, i); + for (size_t j = 0; j < op->children->length; ++j) { + swayc_t *ws = *(swayc_t **)list_get(op->children, j); if (method == SET) { ws->gaps = amount; } else if ((ws->gaps += amount) < 0) { diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c index f0b3035af..eeb67fdef 100644 --- a/sway/commands/ipc.c +++ b/sway/commands/ipc.c @@ -37,7 +37,7 @@ struct cmd_results *cmd_ipc(int argc, char **argv) { } current_policy = alloc_ipc_policy(program); - list_add(config->ipc_policies, current_policy); + list_add(config->ipc_policies, ¤t_policy); free(program); return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL); diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 57a865654..83f87df5a 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -154,10 +154,11 @@ static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char ** } if (inc) { for (int i = container->nb_master; - i >= 0 && i < container->children->length + i >= 0 && (size_t)i < container->children->length && i != (int)container->nb_master + inc;) { - ((swayc_t *)container->children->items[i])->height = -1; - ((swayc_t *)container->children->items[i])->width = -1; + swayc_t *item = *(swayc_t **)list_get(container->children, i); + item->height = -1; + item->width = -1; i += inc > 0 ? 1 : -1; } container->nb_master += inc; diff --git a/sway/commands/mark.c b/sway/commands/mark.c index c1d959df5..de51604c9 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -8,9 +8,13 @@ static void find_marks_callback(swayc_t *container, void *_mark) { char *mark = (char *)_mark; - int index; - if (container->marks && ((index = list_seq_find(container->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1)) { - list_del(container->marks, index); + if (!container->marks) { + return; + } + + ssize_t index = list_lsearch(container->marks, (int (*)(const void *, const void *))strcmp, mark, NULL); + if (index != -1) { + list_delete(container->marks, index); } } @@ -45,11 +49,12 @@ struct cmd_results *cmd_mark(int argc, char **argv) { if (view->marks) { if (add) { - int index; - if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + ssize_t index; + char *item; + if ((index = list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { if (toggle) { - free(view->marks->items[index]); - list_del(view->marks, index); + free(item); + list_delete(view->marks, index); if (0 == view->marks->length) { list_free(view->marks); @@ -58,26 +63,26 @@ struct cmd_results *cmd_mark(int argc, char **argv) { } free(mark); } else { - list_add(view->marks, mark); + list_add(view->marks, &mark); } } else { - if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) { + if (toggle && list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, NULL) != -1) { // Delete the list - list_foreach(view->marks, free); + list_foreach(view->marks, list_elem_free); list_free(view->marks); view->marks = NULL; } else { // Delete and replace with a new list - list_foreach(view->marks, free); + list_foreach(view->marks, list_elem_free); list_free(view->marks); - view->marks = create_list(); - list_add(view->marks, mark); + view->marks = list_new(sizeof(char *), 0); + list_add(view->marks, &mark); } } } else { - view->marks = create_list(); - list_add(view->marks, mark); + view->marks = list_new(sizeof(char *), 0); + list_add(view->marks, &mark); } } else { return cmd_results_new(CMD_FAILURE, "mark", diff --git a/sway/commands/mode.c b/sway/commands/mode.c index d2985c54d..2d9de6c56 100644 --- a/sway/commands/mode.c +++ b/sway/commands/mode.c @@ -22,9 +22,8 @@ struct cmd_results *cmd_mode(int argc, char **argv) { } struct sway_mode *mode = NULL; // Find mode - int i, len = config->modes->length; - for (i = 0; i < len; ++i) { - struct sway_mode *find = config->modes->items[i]; + for (size_t i = 0; i < config->modes->length; ++i) { + struct sway_mode *find = *(struct sway_mode **)list_get(config->modes, i); if (strcasecmp(find->name, mode_name) == 0) { mode = find; break; @@ -37,8 +36,8 @@ struct cmd_results *cmd_mode(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode"); } mode->name = strdup(mode_name); - mode->bindings = create_list(); - list_add(config->modes, mode); + mode->bindings = list_new(sizeof(struct sway_mode *), 0); + list_add(config->modes, &mode); } if (!mode) { error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name); diff --git a/sway/commands/move.c b/sway/commands/move.c index a38687c1d..f4bf38946 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -126,15 +126,15 @@ struct cmd_results *cmd_move(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views."); } swayc_t *view = current_container; - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == view) { + for (size_t i = 0; i < scratchpad->length; i++) { + swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + if (item == view) { hide_view_in_scratchpad(view); sp_view = NULL; return cmd_results_new(CMD_SUCCESS, NULL, NULL); } } - list_add(scratchpad, view); + list_add(scratchpad, &view); if (!view->is_floating) { destroy_container(remove_child(view)); } else { diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c index b3b88e5aa..dcecc3c4e 100644 --- a/sway/commands/no_focus.c +++ b/sway/commands/no_focus.c @@ -19,7 +19,7 @@ struct cmd_results *cmd_no_focus(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "no_focus", "Unable to allocate criteria"); } crit->crit_raw = strdup(criteria); - crit->tokens = create_list(); + crit->tokens = list_new(sizeof(struct crit_token *), 0); crit->cmdlist = NULL; char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); @@ -30,12 +30,12 @@ struct cmd_results *cmd_no_focus(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_seq_find(config->no_focus, criteria_cmp, crit) != -1) { + } else if (list_lsearch(config->no_focus, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); free_criteria(crit); } else { sway_log(L_DEBUG, "no_focus: '%s' added", crit->crit_raw); - list_add(config->no_focus, crit); + list_add(config->no_focus, &crit); } return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/output.c b/sway/commands/output.c index e5d4b3170..d9ab5d0f2 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -38,8 +38,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { // TODO: atoi doesn't handle invalid numbers - int i; - for (i = 1; i < argc; ++i) { + for (int i = 1; i < argc; ++i) { const char *command = argv[i]; if (strcasecmp(command, "disable") == 0) { @@ -157,15 +156,15 @@ struct cmd_results *cmd_output(int argc, char **argv) { } } - i = list_seq_find(config->output_configs, output_name_cmp, name); + struct output_config *oc; + ssize_t i = list_lsearch(config->output_configs, output_name_cmp, name, &oc); if (i >= 0) { // merge existing config - struct output_config *oc = config->output_configs->items[i]; merge_output_config(oc, output); free_output_config(output); output = oc; } else { - list_add(config->output_configs, output); + list_add(config->output_configs, &output); } sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)", @@ -178,8 +177,8 @@ struct cmd_results *cmd_output(int argc, char **argv) { // this is during startup then there will be no container and config // will be applied during normal "new output" event from wlc. swayc_t *cont = NULL; - for (int i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + cont = *(swayc_t **)list_get(root_container.children, i); if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { apply_output_config(output, cont); diff --git a/sway/commands/resize.c b/sway/commands/resize.c index ef52bb077..c86487dc9 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -118,12 +118,12 @@ static bool resize_tiled(int amount, bool use_width) { // 2. Ensure that the resize operation will not make one of the resized containers drop // below the "sane" size threshold. bool valid = true; - swayc_t *focused = parent->children->items[idx_focused]; - int start = use_major ? 0 : auto_group_start_index(parent, idx_focused); - int end = use_major ? parent->children->length : auto_group_end_index(parent, idx_focused); - sway_log(L_DEBUG, "Check children of container %p [%d,%d[", container, start, end); - for (int i = start; i < end; ) { - swayc_t *sibling = parent->children->items[i]; + swayc_t *focused = *(swayc_t **)list_get(parent->children, idx_focused); + size_t start = use_major ? 0 : (size_t)auto_group_start_index(parent, idx_focused); + size_t end = use_major ? parent->children->length : (size_t)auto_group_end_index(parent, idx_focused); + sway_log(L_DEBUG, "Check children of container %p [%zu,%zu[", container, start, end); + for (size_t i = start; i < end; ) { + swayc_t *sibling = *(swayc_t **)list_get(parent->children, i); double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; @@ -142,14 +142,14 @@ static bool resize_tiled(int amount, bool use_width) { sway_log(L_DEBUG, "Container size no longer sane"); break; } - i = use_major ? auto_group_end_index(parent, i) : (i + 1); - sway_log(L_DEBUG, "+++++ check %i", i); + i = use_major ? (size_t)auto_group_end_index(parent, i) : (i + 1); + sway_log(L_DEBUG, "+++++ check %zu", i); } // 3. Apply the size change if (valid) { - for (int i = start; i < end; ) { - int next_i = use_major ? auto_group_end_index(parent, i) : (i + 1); - swayc_t *sibling = parent->children->items[i]; + for (size_t i = start; i < end; ) { + int next_i = use_major ? (size_t)auto_group_end_index(parent, i) : (i + 1); + swayc_t *sibling = *(swayc_t **)list_get(parent->children, i); double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; @@ -162,7 +162,8 @@ static bool resize_tiled(int amount, bool use_width) { sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); if (use_major) { for (int j = i; j < next_i; ++j) { - recursive_resize(parent->children->items[j], pixels, + swayc_t *item = *(swayc_t **)list_get(parent->children, j); + recursive_resize(item, pixels, use_width ? (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : (is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP)); @@ -176,9 +177,10 @@ static bool resize_tiled(int amount, bool use_width) { } else { if (use_major) { for (int j = i; j < next_i; ++j) { - recursive_resize(parent->children->items[j], pixels / 2, + swayc_t *item = *(swayc_t **)list_get(parent->children, j); + recursive_resize(item, pixels / 2, use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); - recursive_resize(parent->children->items[j], pixels / 2, + recursive_resize(item, pixels / 2, use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM); } } else { diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c index dec32f518..ba04f5dc3 100644 --- a/sway/commands/scratchpad.c +++ b/sway/commands/scratchpad.c @@ -7,10 +7,10 @@ #include "sway/layout.h" static swayc_t *fetch_view_from_scratchpad() { - if (sp_index >= scratchpad->length) { + if ((size_t)sp_index >= scratchpad->length) { sp_index = 0; } - swayc_t *view = scratchpad->items[sp_index++]; + swayc_t *view = *(swayc_t **)list_get(scratchpad, sp_index++); if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { wlc_view_set_output(view->handle, swayc_active_output()->handle); diff --git a/sway/commands/set.c b/sway/commands/set.c index 1d6bce04e..670004e88 100644 --- a/sway/commands/set.c +++ b/sway/commands/set.c @@ -37,9 +37,8 @@ struct cmd_results *cmd_set(int argc, char **argv) { struct sway_variable *var = NULL; // Find old variable if it exists - int i; - for (i = 0; i < config->symbols->length; ++i) { - var = config->symbols->items[i]; + for (size_t i = 0; i < config->symbols->length; ++i) { + var = *(struct sway_variable **)list_get(config->symbols, i); if (strcmp(var->name, argv[0]) == 0) { break; } @@ -53,7 +52,7 @@ struct cmd_results *cmd_set(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable"); } var->name = strdup(argv[0]); - list_add(config->symbols, var); + list_add(config->symbols, &var); list_qsort(config->symbols, compare_set_qsort); } var->value = join_args(argv + 1, argc - 1); diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index ac2132613..35d46a744 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -10,10 +10,11 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { if (view->marks) { if (argc) { char *mark = join_args(argv, argc); - int index; - if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { - free(view->marks->items[index]); - list_del(view->marks, index); + char *item; + ssize_t index; + if ((index = list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { + free(item); + list_delete(view->marks, index); if (view->marks->length == 0) { list_free(view->marks); @@ -22,7 +23,7 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { } free(mark); } else { - list_foreach(view->marks, free); + list_foreach(view->marks, list_elem_free); list_free(view->marks); view->marks = NULL; } diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index a78397466..f79ec9443 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -34,14 +34,14 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { } wso->workspace = join_args(argv, argc - 2); wso->output = strdup(argv[output_location + 1]); - int i = -1; - if ((i = list_seq_find(config->workspace_outputs, workspace_output_cmp_workspace, wso)) != -1) { - struct workspace_output *old = config->workspace_outputs->items[i]; + ssize_t i = -1; + struct workspace_output *old; + if ((i = list_lsearch(config->workspace_outputs, workspace_output_cmp_workspace, wso, &old)) != -1) { free(old); // workspaces can only be assigned to a single output - list_del(config->workspace_outputs, i); + list_delete(config->workspace_outputs, i); } sway_log(L_DEBUG, "Assigning workspace %s to output %s", wso->workspace, wso->output); - list_add(config->workspace_outputs, wso); + list_add(config->workspace_outputs, &wso); } else { if (config->reading || !config->active) { return cmd_results_new(CMD_DEFER, "workspace", NULL); diff --git a/sway/config.c b/sway/config.c index 19b1882f4..d301deea7 100644 --- a/sway/config.c +++ b/sway/config.c @@ -55,9 +55,8 @@ static void free_mode(struct sway_mode *mode) { return; } free(mode->name); - int i; - for (i = 0; mode->bindings && i < mode->bindings->length; ++i) { - free_binding(mode->bindings->items[i]); + for (size_t i = 0; mode->bindings && i < mode->bindings->length; ++i) { + free_binding(*(struct sway_binding **)list_get(mode->bindings, i)); } list_free(mode->bindings); free(mode); @@ -72,9 +71,8 @@ static void free_bar(struct bar_config *bar) { free(bar->status_command); free(bar->font); free(bar->separator_symbol); - int i; - for (i = 0; bar->bindings && i < bar->bindings->length; ++i) { - free_sway_mouse_binding(bar->bindings->items[i]); + for (size_t i = 0; bar->bindings && i < bar->bindings->length; ++i) { + free_sway_mouse_binding(*(struct sway_mouse_binding **)list_get(bar->bindings, i)); } list_free(bar->bindings); @@ -144,12 +142,12 @@ static void pid_workspace_cleanup() { // work backwards through list and remove any entries // older than PID_WORKSPACE_TIMEOUT - for (int i = config->pid_workspaces->length - 1; i > -1; i--) { - pw = config->pid_workspaces->items[i]; + for (ssize_t i = config->pid_workspaces->length - 1; i > -1; i--) { + pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { - free_pid_workspace(config->pid_workspaces->items[i]); - list_del(config->pid_workspaces, i); + free_pid_workspace(pw); + list_delete(config->pid_workspaces, i); } } } @@ -174,16 +172,16 @@ void pid_workspace_add(struct pid_workspace *pw) { // work backwards through list and delete any entries that // have the same pid as that in our new pid_workspace - for (int i = config->pid_workspaces->length - 1; i > -1; i--) { - list_pw = config->pid_workspaces->items[i]; + for (ssize_t i = config->pid_workspaces->length - 1; i > -1; i--) { + list_pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); if (pw->pid == list_pw->pid) { - free_pid_workspace(config->pid_workspaces->items[i]); - list_del(config->pid_workspaces, i); + free_pid_workspace(list_pw); + list_delete(config->pid_workspaces, i); } } - list_add(config->pid_workspaces, pw); + list_add(config->pid_workspaces, &pw); } void free_pid_workspace(struct pid_workspace *pw) { @@ -216,61 +214,61 @@ void free_config(struct sway_config *config) { if (!config) { return; } - int i; + size_t i; for (i = 0; config->symbols && i < config->symbols->length; ++i) { - free_variable(config->symbols->items[i]); + free_variable(*(struct sway_variable **)list_get(config->symbols, i)); } list_free(config->symbols); for (i = 0; config->modes && i < config->modes->length; ++i) { - free_mode(config->modes->items[i]); + free_mode(*(struct sway_mode **)list_get(config->modes, i)); } list_free(config->modes); for (i = 0; config->bars && i < config->bars->length; ++i) { - free_bar(config->bars->items[i]); + free_bar(*(struct bar_config **)list_get(config->bars, i)); } list_free(config->bars); free_flat_list(config->cmd_queue); for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { - free_workspace_output(config->workspace_outputs->items[i]); + free_workspace_output(*(struct workspace_output **)list_get(config->workspace_outputs, i)); } list_free(config->workspace_outputs); for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { - free_pid_workspace(config->pid_workspaces->items[i]); + free_pid_workspace(*(struct pid_workspace **)list_get(config->pid_workspaces, i)); } list_free(config->pid_workspaces); for (i = 0; config->criteria && i < config->criteria->length; ++i) { - free_criteria(config->criteria->items[i]); + free_criteria(*(struct criteria **)list_get(config->criteria, i)); } list_free(config->criteria); for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { - free_criteria(config->no_focus->items[i]); + free_criteria(*(struct criteria **)list_get(config->no_focus, i)); } list_free(config->no_focus); for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { - free_input_config(config->input_configs->items[i]); + free_input_config(*(struct input_config **)list_get(config->input_configs, i)); } list_free(config->input_configs); for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { - free_output_config(config->output_configs->items[i]); + free_output_config(*(struct output_config **)list_get(config->output_configs, i)); } list_free(config->output_configs); for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { - free_command_policy(config->command_policies->items[i]); + free_command_policy(*(struct command_policy **)list_get(config->command_policies, i)); } list_free(config->command_policies); for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { - free_feature_policy(config->feature_policies->items[i]); + free_feature_policy(*(struct feature_policy **)list_get(config->feature_policies, i)); } list_free(config->feature_policies); @@ -290,23 +288,23 @@ static bool file_exists(const char *path) { } static void config_defaults(struct sway_config *config) { - if (!(config->symbols = create_list())) goto cleanup; - if (!(config->modes = create_list())) goto cleanup; - if (!(config->bars = create_list())) goto cleanup; - if (!(config->workspace_outputs = create_list())) goto cleanup; - if (!(config->pid_workspaces = create_list())) goto cleanup; - if (!(config->criteria = create_list())) goto cleanup; - if (!(config->no_focus = create_list())) goto cleanup; - if (!(config->input_configs = create_list())) goto cleanup; - if (!(config->output_configs = create_list())) goto cleanup; + if (!(config->symbols = list_new(sizeof(struct sway_variable *), 0))) goto cleanup; + if (!(config->modes = list_new(sizeof(struct sway_mode *), 0))) goto cleanup; + if (!(config->bars = list_new(sizeof(struct bar_config *), 0))) goto cleanup; + if (!(config->workspace_outputs = list_new(sizeof(struct workspace_output *), 0))) goto cleanup; + if (!(config->pid_workspaces = list_new(sizeof(struct pid_workspace *), 0))) goto cleanup; + if (!(config->criteria = list_new(sizeof(struct criteria *), 0))) goto cleanup; + if (!(config->no_focus = list_new(sizeof(struct criterua *), 0))) goto cleanup; + if (!(config->input_configs = list_new(sizeof(struct list_config *), 0))) goto cleanup; + if (!(config->output_configs = list_new(sizeof(struct output_config *), 0))) goto cleanup; - if (!(config->cmd_queue = create_list())) goto cleanup; + if (!(config->cmd_queue = list_new(sizeof(char *), 0))) goto cleanup; if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup; if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; strcpy(config->current_mode->name, "default"); - if (!(config->current_mode->bindings = create_list())) goto cleanup; - list_add(config->modes, config->current_mode); + if (!(config->current_mode->bindings = list_new(sizeof(struct sway_binding *), 0))) goto cleanup; + list_add(config->modes, &config->current_mode); config->floating_mod = 0; config->dragging_key = M_LEFT_CLICK; @@ -342,9 +340,9 @@ static void config_defaults(struct sway_config *config) { config->gaps_inner = 0; config->gaps_outer = 0; - if (!(config->active_bar_modifiers = create_list())) goto cleanup; + if (!(config->active_bar_modifiers = list_new(sizeof(uint32_t *), 0))) goto cleanup; - if (!(config->config_chain = create_list())) goto cleanup; + if (!(config->config_chain = list_new(sizeof(char *), 0))) goto cleanup; config->current_config = NULL; // borders @@ -388,9 +386,9 @@ static void config_defaults(struct sway_config *config) { config->border_colors.background = 0xFFFFFFFF; // Security - if (!(config->command_policies = create_list())) goto cleanup; - if (!(config->feature_policies = create_list())) goto cleanup; - if (!(config->ipc_policies = create_list())) goto cleanup; + if (!(config->command_policies = list_new(sizeof(struct command_policy *), 0))) goto cleanup; + if (!(config->feature_policies = list_new(sizeof(struct feature_policy *), 0))) goto cleanup; + if (!(config->ipc_policies = list_new(sizeof(struct ipc_policy *), 0))) goto cleanup; return; cleanup: @@ -407,15 +405,13 @@ static int compare_modifiers(const void *left, const void *right) { void update_active_bar_modifiers() { if (config->active_bar_modifiers->length > 0) { list_free(config->active_bar_modifiers); - config->active_bar_modifiers = create_list(); + config->active_bar_modifiers = list_new(sizeof(uint32_t *), 0); } - struct bar_config *bar; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { - if (list_seq_find(config->active_bar_modifiers, compare_modifiers, &bar->modifier) < 0) { + if (list_lsearch(config->active_bar_modifiers, compare_modifiers, &bar->modifier, NULL) < 0) { list_add(config->active_bar_modifiers, &bar->modifier); } } @@ -524,7 +520,7 @@ bool load_main_config(const char *file, bool is_active) { } config->current_config = path; - list_add(config->config_chain, path); + list_add(config->config_chain, &path); config->reading = true; @@ -535,7 +531,7 @@ bool load_main_config(const char *file, bool is_active) { sway_log(L_ERROR, "%s does not exist, sway will have no security configuration" " and will probably be broken", SYSCONFDIR "/sway/security.d"); } else { - list_t *secconfigs = create_list(); + list_t *secconfigs = list_new(sizeof(char *), 0); char *base = SYSCONFDIR "/sway/security.d/"; struct dirent *ent = readdir(dir); struct stat s; @@ -545,15 +541,15 @@ bool load_main_config(const char *file, bool is_active) { strcat(_path, ent->d_name); lstat(_path, &s); if (S_ISREG(s.st_mode)) { - list_add(secconfigs, _path); + list_add(secconfigs, &_path); } ent = readdir(dir); } closedir(dir); list_qsort(secconfigs, qstrcmp); - for (int i = 0; i < secconfigs->length; ++i) { - char *_path = secconfigs->items[i]; + for (size_t i = 0; i < secconfigs->length; ++i) { + char *_path = *(char **)list_get(secconfigs, i); if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) { sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path); success = false; @@ -608,9 +604,8 @@ static bool load_include_config(const char *path, const char *parent_dir, struct } // check if config has already been included - int j; - for (j = 0; j < config->config_chain->length; ++j) { - char *old_path = config->config_chain->items[j]; + for (size_t j = 0; j < config->config_chain->length; ++j) { + char *old_path = *(char **)list_get(config->config_chain, j); if (strcmp(real_path, old_path) == 0) { sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path); free(real_path); @@ -619,13 +614,13 @@ static bool load_include_config(const char *path, const char *parent_dir, struct } config->current_config = real_path; - list_add(config->config_chain, real_path); - int index = config->config_chain->length - 1; + list_add(config->config_chain, &real_path); + size_t index = config->config_chain->length - 1; if (!load_config(real_path, config)) { free(real_path); config->current_config = parent_config; - list_del(config->config_chain, index); + list_delete(config->config_chain, index); return false; } @@ -715,7 +710,8 @@ bool read_config(FILE *file, struct sway_config *config) { case CMD_DEFER: sway_log(L_DEBUG, "Defferring command `%s'", line); - list_add(config->cmd_queue, strdup(line)); + char *ptr = strdup(line); + list_add(config->cmd_queue, &ptr); break; case CMD_BLOCK_MODE: @@ -778,7 +774,7 @@ bool read_config(FILE *file, struct sway_config *config) { switch(block) { case CMD_BLOCK_MODE: sway_log(L_DEBUG, "End of mode block"); - config->current_mode = config->modes->items[0]; + config->current_mode = *(struct sway_mode **)list_get(config->modes, 0); block = CMD_BLOCK_END; break; @@ -829,10 +825,10 @@ bool read_config(FILE *file, struct sway_config *config) { return success; } -int input_identifier_cmp(const void *item, const void *data) { - const struct input_config *ic = item; - const char *identifier = data; - return strcmp(ic->identifier, identifier); +int input_identifier_cmp(const void *key, const void *item) { + const struct input_config *const *ic = item; + const char *identifier = key; + return strcmp((*ic)->identifier, identifier); } int output_name_cmp(const void *item, const void *data) { @@ -1003,10 +999,8 @@ void terminate_swaybg(pid_t pid) { } static bool active_output(const char *name) { - int i; - swayc_t *cont = NULL; - for (i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *cont = *(swayc_t **)list_get(root_container.children, i); if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { return true; } @@ -1017,16 +1011,14 @@ static bool active_output(const char *name) { void load_swaybars() { // Check for bars - list_t *bars = create_list(); + list_t *bars = list_new(sizeof(struct bar_config *), 0); struct bar_config *bar = NULL; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + bar = *(struct bar_config **)list_get(config->bars, i); bool apply = false; if (bar->outputs) { - int j; - for (j = 0; j < bar->outputs->length; ++j) { - char *o = bar->outputs->items[j]; + for (size_t j = 0; j < bar->outputs->length; ++j) { + char *o = *(char **)list_get(bar->outputs, j); if (!strcmp(o, "*") || active_output(o)) { apply = true; break; @@ -1036,12 +1028,12 @@ void load_swaybars() { apply = true; } if (apply) { - list_add(bars, bar); + list_add(bars, &bar); } } - for (i = 0; i < bars->length; ++i) { - bar = bars->items[i]; + for (size_t i = 0; i < bars->length; ++i) { + bar = *(struct bar_config **)list_get(bars, i); if (bar->pid != 0) { terminate_swaybar(bar->pid); } @@ -1130,8 +1122,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { output->y = oc->y; } else { int x = 0; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (c->type == C_OUTPUT) { if (c->width + c->x > x) { x = c->width + c->x; @@ -1143,17 +1135,18 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { if (!oc || !oc->background) { // Look for a * config for background - int i = list_seq_find(config->output_configs, output_name_cmp, "*"); - if (i >= 0) { - oc = config->output_configs->items[i]; + struct output_config *item; + if (list_lsearch(config->output_configs, output_name_cmp, "*", &item) != -1) { + oc = item; } else { oc = NULL; } } - int output_i; + size_t output_i; for (output_i = 0; output_i < root_container.children->length; ++output_i) { - if (root_container.children->items[output_i] == output) { + swayc_t *item = *(swayc_t **)list_get(root_container.children, output_i); + if (item == output) { break; } } @@ -1163,11 +1156,11 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { terminate_swaybg(output->bg_pid); } - sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); + sway_log(L_DEBUG, "Setting background for output %zu to %s", output_i, oc->background); size_t bufsize = 12; char output_id[bufsize]; - snprintf(output_id, bufsize, "%d", output_i); + snprintf(output_id, bufsize, "%zu", output_i); output_id[bufsize-1] = 0; char *const cmd[] = { @@ -1186,7 +1179,6 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } char *do_var_replacement(char *str) { - int i; char *find = str; while ((find = strchr(find, '$'))) { // Skip if escaped. @@ -1196,9 +1188,10 @@ char *do_var_replacement(char *str) { continue; } } + size_t i; // Find matching variable for (i = 0; i < config->symbols->length; ++i) { - struct sway_variable *var = config->symbols->items[i]; + struct sway_variable *var = *(struct sway_variable **)list_get(config->symbols, i); int vnlen = strlen(var->name); if (strncmp(find, var->name, vnlen) == 0) { int vvlen = strlen(var->value); @@ -1236,8 +1229,8 @@ int workspace_output_cmp_workspace(const void *a, const void *b) { return lenient_strcmp(wsa->workspace, wsb->workspace); } -int sway_binding_cmp_keys(const void *a, const void *b) { - const struct sway_binding *binda = a, *bindb = b; +int sway_binding_cmp_keys(const void *key, const void *item) { + const struct sway_binding *binda = item, *bindb = key; // Count keys pressed for this binding. important so we check long before // short ones. for example mod+a+b before mod+a @@ -1259,16 +1252,16 @@ int sway_binding_cmp_keys(const void *a, const void *b) { return -1; } struct wlc_modifiers no_mods = { 0, 0 }; - for (int i = 0; i < binda->keys->length; i++) { - xkb_keysym_t ka = *(xkb_keysym_t *)binda->keys->items[i], - kb = *(xkb_keysym_t *)bindb->keys->items[i]; + for (size_t i = 0; i < binda->keys->length; i++) { + xkb_keysym_t ka = **(xkb_keysym_t **)list_get(binda->keys, i), + kb = **(xkb_keysym_t **)list_get(bindb->keys, i); if (binda->bindcode) { - uint32_t *keycode = binda->keys->items[i]; + uint32_t *keycode = *(uint32_t **)list_get(binda->keys, i); ka = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); } if (bindb->bindcode) { - uint32_t *keycode = bindb->keys->items[i]; + uint32_t *keycode = *(uint32_t **)list_get(bindb->keys, i); kb = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); } @@ -1297,8 +1290,8 @@ int sway_binding_cmp_qsort(const void *a, const void *b) { void free_sway_binding(struct sway_binding *binding) { if (binding->keys) { - for (int i = 0; i < binding->keys->length; i++) { - free(binding->keys->items[i]); + for (size_t i = 0; i < binding->keys->length; i++) { + free(*(void **)list_get(binding->keys, i)); } list_free(binding->keys); } @@ -1308,8 +1301,8 @@ void free_sway_binding(struct sway_binding *binding) { free(binding); } -int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { - const struct sway_mouse_binding *binda = a, *bindb = b; +int sway_mouse_binding_cmp_buttons(const void *key, const void *item) { + const struct sway_mouse_binding *binda = item, *bindb = key; if (binda->button > bindb->button) { return 1; } @@ -1349,16 +1342,15 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) { new_sb->modifiers = sb->modifiers; new_sb->command = strdup(sb->command); - new_sb->keys = create_list(); - int i; - for (i = 0; i < sb->keys->length; ++i) { + new_sb->keys = list_new(sizeof(xkb_keysym_t *), 0); + for (size_t i = 0; i < sb->keys->length; ++i) { xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); if (!key) { free_sway_binding(new_sb); return NULL; } - *key = *(xkb_keysym_t *)sb->keys->items[i]; - list_add(new_sb->keys, key); + *key = **(xkb_keysym_t **)list_get(sb->keys, i); + list_add(new_sb->keys, &key); } return new_sb; @@ -1375,7 +1367,7 @@ struct bar_config *default_bar_config(void) { bar->modifier = WLC_BIT_MOD_LOGO; bar->outputs = NULL; bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; - if (!(bar->bindings = create_list())) goto cleanup; + if (!(bar->bindings = list_new(sizeof(struct sway_binding *), 0))) goto cleanup; if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p'; sleep 1; done"))) goto cleanup; bar->pango_markup = false; bar->swaybar_command = NULL; @@ -1414,7 +1406,7 @@ struct bar_config *default_bar_config(void) { bar->colors.binding_mode_bg = NULL; bar->colors.binding_mode_text = NULL; - list_add(config->bars, bar); + list_add(config->bars, &bar); return bar; diff --git a/sway/container.c b/sway/container.c index 08aa77a80..f25fccb41 100644 --- a/sway/container.c +++ b/sway/container.c @@ -35,7 +35,7 @@ static swayc_t *new_swayc(enum swayc_types type) { c->nb_master = 1; c->nb_slave_groups = 1; if (type != C_VIEW) { - c->children = create_list(); + c->children = list_new(sizeof(swayc_t *), 0); } return c; } @@ -48,7 +48,7 @@ static void free_swayc(swayc_t *cont) { // remove children until there are no more, free_swayc calls // remove_child, which removes child from this container while (cont->children->length) { - free_swayc(cont->children->items[0]); + free_swayc(*(swayc_t **)list_get(cont->children, 0)); } list_free(cont->children); } @@ -57,7 +57,7 @@ static void free_swayc(swayc_t *cont) { } if (cont->floating) { while (cont->floating->length) { - free_swayc(cont->floating->items[0]); + free_swayc(*(swayc_t **)list_get(cont->floating, 0)); } list_free(cont->floating); } @@ -99,8 +99,8 @@ static void update_root_geometry() { int child_width; int child_height; - for (int i = 0; i < root_container.children->length; ++i) { - child = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + child = *(swayc_t **)list_get(root_container.children, i); child_width = child->width + child->x; child_height = child->height + child->y; if (child_width > width) { @@ -123,24 +123,20 @@ swayc_t *new_output(wlc_handle handle) { output_get_scaled_size(handle, &size); const char *name = wlc_output_get_name(handle); // Find current outputs to see if this already exists - { - int i, len = root_container.children->length; - for (i = 0; i < len; ++i) { - swayc_t *op = root_container.children->items[i]; - const char *op_name = op->name; - if (op_name && name && strcmp(op_name, name) == 0) { - sway_log(L_DEBUG, "restoring output %" PRIuPTR ":%s", handle, op_name); - return op; - } + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *op = *(swayc_t **)list_get(root_container.children, i); + const char *op_name = op->name; + if (op_name && name && strcmp(op_name, name) == 0) { + sway_log(L_DEBUG, "restoring output %" PRIuPTR ":%s", handle, op_name); + return op; } } sway_log(L_DEBUG, "New output %" PRIuPTR ":%s", handle, name); struct output_config *oc = NULL, *all = NULL; - int i; - for (i = 0; i < config->output_configs->length; ++i) { - struct output_config *cur = config->output_configs->items[i]; + for (size_t i = 0; i < config->output_configs->length; ++i) { + struct output_config *cur = *(struct output_config **)list_get(config->output_configs, i); if (strcasecmp(name, cur->name) == 0) { sway_log(L_DEBUG, "Matched output config for %s", name); oc = cur; @@ -168,7 +164,7 @@ swayc_t *new_output(wlc_handle handle) { output->name = name ? strdup(name) : NULL; output->width = size.w; output->height = size.h; - output->unmanaged = create_list(); + output->unmanaged = list_new(sizeof(wlc_handle *), 0); output->bg_pid = 0; apply_output_config(oc, output); @@ -180,8 +176,8 @@ swayc_t *new_output(wlc_handle handle) { swayc_t *ws = NULL; if (name) { - for (i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; + for (size_t i = 0; i < config->workspace_outputs->length; ++i) { + struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); if (strcasecmp(wso->output, name) == 0) { sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); // Check if any other workspaces are using this name @@ -206,7 +202,7 @@ swayc_t *new_output(wlc_handle handle) { ws->is_focused = true; } else { sort_workspaces(output); - set_focused_container(output->children->items[0]); + set_focused_container(*(swayc_t **)list_get(output->children, 0)); } free(ws_name); @@ -231,7 +227,7 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { workspace->height = output->height; workspace->name = !name ? NULL : strdup(name); workspace->visible = false; - workspace->floating = create_list(); + workspace->floating = list_new(sizeof(swayc_t *), 0); add_child(output, workspace); sort_workspaces(output); @@ -265,9 +261,8 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { cont->focused = workspace->focused; workspace->focused = cont; // set all children focu to container - int i; - for (i = 0; i < workspace->children->length; ++i) { - ((swayc_t *)workspace->children->items[i])->parent = cont; + for (size_t i = 0; i < workspace->children->length; ++i) { + (*(swayc_t **)list_get(workspace->children, i))->parent = cont; } // Swap children list_t *tmp_list = workspace->children; @@ -437,16 +432,17 @@ swayc_t *destroy_output(swayc_t *output) { // TODO also check if there will ever be no outputs except for exiting // program if (root_container.children->length > 1) { - int p = root_container.children->items[0] == output; + int p = *(swayc_t **)list_get(root_container.children, 0) == output; // Move workspace from this output to another output + swayc_t *item = *(swayc_t **)list_get(root_container.children, p); while (output->children->length) { - swayc_t *child = output->children->items[0]; + swayc_t *child = *(swayc_t **)list_get(output->children, 0); remove_child(child); - add_child(root_container.children->items[p], child); + add_child(item, child); } - sort_workspaces(root_container.children->items[p]); - update_visibility(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], -1, -1); + sort_workspaces(item); + update_visibility(item); + arrange_windows(item, -1, -1); } } sway_log(L_DEBUG, "OUTPUT: Destroying output '%" PRIuPTR "'", output->handle); @@ -474,23 +470,24 @@ swayc_t *destroy_workspace(swayc_t *workspace) { } else { // Move children to a different workspace on this output swayc_t *new_workspace = NULL; - int i; - for(i = 0; i < output->children->length; i++) { - if(output->children->items[i] != workspace) { + for(size_t i = 0; i < output->children->length; i++) { + new_workspace = *(swayc_t **)list_get(output->children, i); + if (new_workspace != workspace) { break; } } - new_workspace = output->children->items[i]; sway_log(L_DEBUG, "moving children to different workspace '%s' -> '%s'", workspace->name, new_workspace->name); - for(i = 0; i < workspace->children->length; i++) { - move_container_to(workspace->children->items[i], new_workspace); + for (size_t i = 0; i < workspace->children->length; i++) { + swayc_t *item = *(swayc_t **)list_get(workspace->children, i); + move_container_to(item, new_workspace); } - for(i = 0; i < workspace->floating->length; i++) { - move_container_to(workspace->floating->items[i], new_workspace); + for (size_t i = 0; i < workspace->floating->length; i++) { + swayc_t *item = *(swayc_t **)list_get(workspace->floating, i); + move_container_to(item, new_workspace); } } @@ -534,17 +531,16 @@ swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *dat return NULL; } // Special case for checking floating stuff - int i; if (container->type == C_WORKSPACE) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; + for (size_t i = 0; i < container->floating->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->floating, i); if (test(child, data)) { return child; } } } - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->children, i); if (test(child, data)) { return child; } else { @@ -710,8 +706,6 @@ swayc_t *container_under_pointer(void) { struct wlc_point origin; wlc_pointer_get_position(&origin); while (lookup && lookup->type != C_VIEW) { - int i; - int len; // if tabbed/stacked go directly to focused container, otherwise search // children if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) { @@ -720,11 +714,13 @@ swayc_t *container_under_pointer(void) { } // if workspace, search floating if (lookup->type == C_WORKSPACE) { + ssize_t i, len; i = len = lookup->floating->length; bool got_floating = false; while (--i > -1) { - if (pointer_test(lookup->floating->items[i], &origin)) { - lookup = lookup->floating->items[i]; + swayc_t *item = *(swayc_t **)list_get(lookup->floating, i); + if (pointer_test(item, &origin)) { + lookup = item; got_floating = true; break; } @@ -734,10 +730,11 @@ swayc_t *container_under_pointer(void) { } } // search children - len = lookup->children->length; + size_t i, len = lookup->children->length; for (i = 0; i < len; ++i) { - if (pointer_test(lookup->children->items[i], &origin)) { - lookup = lookup->children->items[i]; + swayc_t *item = *(swayc_t **)list_get(lookup->children, i); + if (pointer_test(item, &origin)) { + lookup = item; break; } } @@ -756,8 +753,8 @@ swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), swayc_t *con; if (container->type == C_WORKSPACE) { - for (int i = 0; i < container->floating->length; ++i) { - con = container->floating->items[i]; + for (size_t i = 0; i < container->floating->length; ++i) { + con = *(swayc_t **)list_get(container->floating, i); if (f(con, data)) { return con; } @@ -768,8 +765,8 @@ swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), } } - for (int i = 0; i < container->children->length; ++i) { - con = container->children->items[i]; + for (size_t i = 0; i < container->children->length; ++i) { + con = *(swayc_t **)list_get(container->children, i); if (f(con, data)) { return con; } @@ -837,16 +834,15 @@ int swayc_gap(swayc_t *container) { void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { if (container) { f(container, data); - int i; if (container->children) { - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->children, i); container_map(child, f, data); } } if (container->floating) { - for (i = 0; i < container->floating->length; ++i) { - swayc_t *child = container->floating->items[i]; + for (size_t i = 0; i < container->floating->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->floating, i); container_map(child, f, data); } } @@ -870,15 +866,15 @@ void update_visibility_output(swayc_t *container, wlc_handle output) { // Update visibility for children else { if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->children->items[i], output); + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + update_visibility_output(item, output); } } if (container->floating) { - int i, len = container->floating->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->floating->items[i], output); + for (size_t i = 0; i < container->floating->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(container->floating, i); + update_visibility_output(item, output); } } } @@ -890,9 +886,9 @@ void update_visibility(swayc_t *container) { case C_ROOT: container->visible = true; if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility(container->children->items[i]); + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + update_visibility(item); } } return; @@ -900,9 +896,9 @@ void update_visibility(swayc_t *container) { case C_OUTPUT: container->visible = true; if (container->children) { - int i, len = container->children->length; - for (i = 0; i < len; ++i) { - update_visibility_output(container->children->items[i], container->handle); + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + update_visibility_output(item, container->handle); } } return; @@ -983,8 +979,8 @@ swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT ? L_HORIZ : L_VERT; if (new_major != prev_major) { - for (int i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->children, i); double h = child->height; child->height = child->width; child->width = h; diff --git a/sway/criteria.c b/sway/criteria.c index 1ea8311e4..d5a580e2f 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -52,8 +52,8 @@ static void free_crit_token(struct crit_token *crit) { } static void free_crit_tokens(list_t *crit_tokens) { - for (int i = 0; i < crit_tokens->length; i++) { - free_crit_token(crit_tokens->items[i]); + for (size_t i = 0; i < crit_tokens->length; i++) { + free_crit_token(*(struct crit_token **)list_get(crit_tokens, i)); } list_free(crit_tokens); } @@ -223,13 +223,13 @@ char *extract_crit_tokens(list_t *tokens, const char * const criteria) { goto ect_cleanup; } else if (token->type == CRIT_URGENT || crit_is_focused(value)) { sway_log(L_DEBUG, "%s -> \"%s\"", name, value); - list_add(tokens, token); + list_add(tokens, &token); } else if((error = generate_regex(&token->regex, value))) { free_crit_token(token); goto ect_cleanup; } else { sway_log(L_DEBUG, "%s -> /%s/", name, value); - list_add(tokens, token); + list_add(tokens, &token); } } ect_cleanup: @@ -238,8 +238,10 @@ ect_cleanup: return error; } -static int regex_cmp(const char *item, const pcre *regex) { - return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); +static int regex_cmp(const void *key, const void *item) { + const pcre *regex = key; + const char *str = item; + return pcre_exec(regex, NULL, str, strlen(str), 0, 0, NULL, 0); } // test a single view if it matches list of criteria tokens (all of them). @@ -247,9 +249,9 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (cont->type != C_VIEW) { return false; } - int matches = 0; - for (int i = 0; i < tokens->length; i++) { - struct crit_token *crit = tokens->items[i]; + size_t matches = 0; + for (size_t i = 0; i < tokens->length; i++) { + struct crit_token *crit = *(struct crit_token **)list_get(tokens, i); switch (crit->type) { case CRIT_CLASS: if (!cont->class) { @@ -264,9 +266,9 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { } break; case CRIT_CON_MARK: - if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { + if (crit->regex && cont->marks && (list_lsearch(cont->marks, regex_cmp, crit->regex, NULL) != -1)) { // Make sure it isn't matching the NUL string - if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) { + if ((strcmp(crit->raw, "") == 0) == (list_lsearch(cont->marks, (int (*)(const void *, const void *))strcmp, "", NULL) != -1)) { ++matches; } } @@ -330,15 +332,15 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { return matches == tokens->length; } -int criteria_cmp(const void *a, const void *b) { - if (a == b) { +int criteria_cmp(const void *key, const void *item) { + if (key == item) { return 0; - } else if (!a) { + } else if (!item) { return -1; - } else if (!b) { + } else if (!key) { return 1; } - const struct criteria *crit_a = a, *crit_b = b; + const struct criteria *crit_a = item, *crit_b = key; int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); if (cmp != 0) { return cmp; @@ -360,8 +362,8 @@ void free_criteria(struct criteria *crit) { } bool criteria_any(swayc_t *cont, list_t *criteria) { - for (int i = 0; i < criteria->length; i++) { - struct criteria *bc = criteria->items[i]; + for (size_t i = 0; i < criteria->length; i++) { + struct criteria *bc = *(struct criteria **)list_get(criteria, i); if (criteria_test(cont, bc->tokens)) { return true; } @@ -370,9 +372,9 @@ bool criteria_any(swayc_t *cont, list_t *criteria) { } list_t *criteria_for(swayc_t *cont) { - list_t *criteria = config->criteria, *matches = create_list(); - for (int i = 0; i < criteria->length; i++) { - struct criteria *bc = criteria->items[i]; + list_t *matches = list_new(sizeof(struct criteria *), 0); + for (size_t i = 0; i < config->criteria->length; i++) { + struct criteria *bc = *(struct criteria **)list_get(config->criteria, i); if (criteria_test(cont, bc->tokens)) { list_add(matches, bc); } @@ -385,15 +387,16 @@ struct list_tokens { list_t *tokens; }; -static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { +static void container_match_add(swayc_t *container, void *arg) { + struct list_tokens *list_tokens = arg; if (criteria_test(container, list_tokens->tokens)) { list_add(list_tokens->list, container); } } list_t *container_for(list_t *tokens) { - struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; + struct list_tokens list_tokens = {list_new(sizeof(swayc_t *), 0), tokens}; - container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens); + container_map(&root_container, container_match_add, &list_tokens); return list_tokens.list; } diff --git a/sway/debug_log.c b/sway/debug_log.c index d1eafae8f..b886e34fe 100644 --- a/sway/debug_log.c +++ b/sway/debug_log.c @@ -47,7 +47,7 @@ static void container_log(const swayc_t *c, int depth) { fprintf(stderr, "x:%4.f|y:%4.f|", c->x, c->y); fprintf(stderr, "g:%3d|",c->gaps); fprintf(stderr, "vis:%c|", c->visible?'t':'f'); - fprintf(stderr, "children:%2d|",c->children?c->children->length:0); + fprintf(stderr, "children:%2zu|",c->children?c->children->length:0); fprintf(stderr, "name:%.16s\n", c->name); } void layout_log(const swayc_t *c, int depth) { @@ -59,7 +59,7 @@ void layout_log(const swayc_t *c, int depth) { for (i = 0; i < e; ++i) { fputc('|',stderr); for (d = 0; d < depth; ++d) fputc('-', stderr); - layout_log(c->children->items[i], depth + 1); + layout_log(*(swayc_t **)list_get(c->children, i), depth + 1); } } if (c->type == C_WORKSPACE) { @@ -68,7 +68,7 @@ void layout_log(const swayc_t *c, int depth) { for (i = 0; i < e; ++i) { fputc('|',stderr); for (d = 0; d < depth; ++d) fputc('=', stderr); - layout_log(c->floating->items[i], depth + 1); + layout_log(*(swayc_t **)list_get(c->floating, i), depth + 1); } } } diff --git a/sway/extensions.c b/sway/extensions.c index 96957dbff..38f8db717 100644 --- a/sway/extensions.c +++ b/sway/extensions.c @@ -15,8 +15,8 @@ struct desktop_shell_state desktop_shell; static struct panel_config *find_or_create_panel_config(struct wl_resource *resource) { - for (int i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *conf = desktop_shell.panels->items[i]; + for (size_t i = 0; i < desktop_shell.panels->length; i++) { + struct panel_config *conf = *(struct panel_config **)list_get(desktop_shell.panels, i); if (conf->wl_resource == resource) { sway_log(L_DEBUG, "Found existing panel config for resource %p", resource); return conf; @@ -35,11 +35,10 @@ static struct panel_config *find_or_create_panel_config(struct wl_resource *reso void background_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Background surface killed"); - int i; - for (i = 0; i < desktop_shell.backgrounds->length; ++i) { - struct background_config *config = desktop_shell.backgrounds->items[i]; + for (size_t i = 0; i < desktop_shell.backgrounds->length; ++i) { + struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); if (config->wl_surface_res == resource) { - list_del(desktop_shell.backgrounds, i); + list_delete(desktop_shell.backgrounds, i); break; } } @@ -47,11 +46,10 @@ void background_surface_destructor(struct wl_resource *resource) { void panel_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Panel surface killed"); - int i; - for (i = 0; i < desktop_shell.panels->length; ++i) { - struct panel_config *config = desktop_shell.panels->items[i]; + for (size_t i = 0; i < desktop_shell.panels->length; ++i) { + struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); if (config->wl_surface_res == resource) { - list_del(desktop_shell.panels, i); + list_delete(desktop_shell.panels, i); arrange_windows(&root_container, -1, -1); break; } @@ -60,11 +58,10 @@ void panel_surface_destructor(struct wl_resource *resource) { void lock_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Lock surface killed"); - int i; - for (i = 0; i < desktop_shell.lock_surfaces->length; ++i) { - struct wl_resource *surface = desktop_shell.lock_surfaces->items[i]; + for (size_t i = 0; i < desktop_shell.lock_surfaces->length; ++i) { + struct wl_resource *surface = *(struct wl_resource **)list_get(desktop_shell.lock_surfaces, i); if (surface == resource) { - list_del(desktop_shell.lock_surfaces, i); + list_delete(desktop_shell.lock_surfaces, i); arrange_windows(&root_container, -1, -1); break; } @@ -322,9 +319,9 @@ static void gamma_control_manager_bind(struct wl_client *client, void *data, void register_extensions(void) { wl_global_create(wlc_get_wl_display(), &desktop_shell_interface, 3, NULL, desktop_shell_bind); - desktop_shell.backgrounds = create_list(); - desktop_shell.panels = create_list(); - desktop_shell.lock_surfaces = create_list(); + desktop_shell.backgrounds = list_new(sizeof(struct background_config *), 0); + desktop_shell.panels = list_new(sizeof(struct panel_config *), 0); + desktop_shell.lock_surfaces = list_new(sizeof(struct wl_resource *), 0); desktop_shell.is_locked = false; wl_global_create(wlc_get_wl_display(), &lock_interface, 1, NULL, swaylock_bind); wl_global_create(wlc_get_wl_display(), &gamma_control_manager_interface, 1, diff --git a/sway/focus.c b/sway/focus.c index e9b032f83..955bfe817 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -252,7 +252,7 @@ swayc_t *get_focused_float(swayc_t *ws) { ws = swayc_active_workspace(); } if (ws->floating->length) { - return ws->floating->items[ws->floating->length - 1]; + return *(swayc_t **)list_get(ws->floating, ws->floating->length - 1); } return NULL; } diff --git a/sway/handlers.c b/sway/handlers.c index e1b90a074..b7936942f 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -35,9 +35,8 @@ #define EVENT_HANDLED true static struct panel_config *if_panel_find_config(struct wl_client *client) { - int i; - for (i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = desktop_shell.panels->items[i]; + for (size_t i = 0; i < desktop_shell.panels->length; i++) { + struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); if (config->client == client) { return config; } @@ -46,9 +45,8 @@ static struct panel_config *if_panel_find_config(struct wl_client *client) { } static struct background_config *if_background_find_config(struct wl_client *client) { - int i; - for (i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = desktop_shell.backgrounds->items[i]; + for (size_t i = 0; i < desktop_shell.backgrounds->length; i++) { + struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); if (config->client == client) { return config; } @@ -98,8 +96,8 @@ static void update_panel_geometry(struct panel_config *config) { } static void update_panel_geometries(wlc_handle output) { - for (int i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = desktop_shell.panels->items[i]; + for (size_t i = 0; i < desktop_shell.panels->length; i++) { + struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); if (config->output == output) { update_panel_geometry(config); } @@ -113,8 +111,8 @@ static void update_background_geometry(struct background_config *config) { } static void update_background_geometries(wlc_handle output) { - for (int i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = desktop_shell.backgrounds->items[i]; + for (size_t i = 0; i < desktop_shell.backgrounds->length; i++) { + struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); if (config->output == output) { update_background_geometry(config); } @@ -132,12 +130,11 @@ static bool handle_input_created(struct libinput_device *device) { } sway_log(L_INFO, "Found input device (%s)", identifier); - list_add(input_devices, device); + list_add(input_devices, &device); struct input_config *ic = NULL; - int i; - for (i = 0; i < config->input_configs->length; ++i) { - struct input_config *cur = config->input_configs->items[i]; + for (size_t i = 0; i < config->input_configs->length; ++i) { + struct input_config *cur = *(struct input_config **)list_get(config->input_configs, i); if (strcasecmp(identifier, cur->identifier) == 0) { sway_log(L_DEBUG, "Matched input config for %s", identifier); @@ -157,11 +154,10 @@ static bool handle_input_created(struct libinput_device *device) { } static void handle_input_destroyed(struct libinput_device *device) { - int i; - list_t *list = input_devices; - for (i = 0; i < list->length; ++i) { - if(((struct libinput_device *)list->items[i]) == device) { - list_del(list, i); + for (size_t i = 0; i < input_devices->length; ++i) { + struct libinput_device *item = *(struct libinput_device **)list_get(input_devices, i); + if (item == device) { + list_delete(input_devices, i); break; } } @@ -179,7 +175,7 @@ static bool handle_output_created(wlc_handle output) { // Switch to workspace if we need to if (swayc_active_workspace() == NULL) { - swayc_t *ws = op->children->items[0]; + swayc_t *ws = *(swayc_t **)list_get(op->children, 0); workspace_switch(ws); } @@ -191,21 +187,21 @@ static bool handle_output_created(wlc_handle output) { } static void handle_output_destroyed(wlc_handle output) { - int i; + size_t i; list_t *list = root_container.children; for (i = 0; i < list->length; ++i) { - if (((swayc_t *)list->items[i])->handle == output) { + if ((*(swayc_t **)list_get(list, i))->handle == output) { break; } } if (i < list->length) { - destroy_output(list->items[i]); + destroy_output(*(swayc_t **)list_get(list, i)); } else { return; } if (list->length > 0) { // switch to other outputs active workspace - workspace_switch(((swayc_t *)root_container.children->items[0])->focused); + workspace_switch((*(swayc_t **)list_get(root_container.children, 0))->focused); } } @@ -246,16 +242,16 @@ static void handle_output_focused(wlc_handle output, bool focus) { static void ws_cleanup() { swayc_t *op, *ws; - int i = 0, j; + size_t i = 0, j; if (!root_container.children) return; while (i < root_container.children->length) { - op = root_container.children->items[i++]; + op = *(swayc_t **)list_get(root_container.children, i++); if (!op->children) continue; j = 0; while (j < op->children->length) { - ws = op->children->items[j++]; + ws = *(swayc_t **)list_get(op->children, j++); if (ws->children->length == 0 && ws->floating->length == 0 && ws != op->focused) { if (destroy_workspace(ws)) { j--; @@ -403,7 +399,7 @@ static bool handle_view_created(wlc_handle handle) { // TODO find a better way of doing this // Or to focused container else { - focused = get_focused_container(focused->parent->children->items[0]); + focused = get_focused_container(*(swayc_t **)list_get(focused->parent->children, 0)); } } } @@ -466,8 +462,8 @@ static bool handle_view_created(wlc_handle handle) { arrange_windows(output, -1, -1); // check if it matches for_window in config and execute if so list_t *criteria = criteria_for(newview); - for (int i = 0; i < criteria->length; i++) { - struct criteria *crit = criteria->items[i]; + for (size_t i = 0; i < criteria->length; i++) { + struct criteria *crit = *(struct criteria **)list_get(criteria, i); sway_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'", crit->crit_raw, newview, crit->cmdlist); struct cmd_results *res = handle_command(crit->cmdlist, CONTEXT_CRITERIA); @@ -492,7 +488,7 @@ static bool handle_view_created(wlc_handle handle) { } *h = handle; sway_log(L_DEBUG, "Adding unmanaged window %p to %p", h, output->unmanaged); - list_add(output->unmanaged, h); + list_add(output->unmanaged, &h); wlc_view_set_mask(handle, VISIBLE); } @@ -559,14 +555,12 @@ static void handle_view_destroyed(wlc_handle handle) { ipc_event_window(parent, "close"); } else { // Is it unmanaged? - int i; - for (i = 0; i < root_container.children->length; ++i) { - swayc_t *output = root_container.children->items[i]; - int j; - for (j = 0; j < output->unmanaged->length; ++j) { - wlc_handle *_handle = output->unmanaged->items[j]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *output = *(swayc_t **)list_get(root_container.children, i); + for (size_t j = 0; j < output->unmanaged->length; ++j) { + wlc_handle *_handle = *(wlc_handle **)list_get(output->unmanaged, j); if (*_handle == handle) { - list_del(output->unmanaged, j); + list_delete(output->unmanaged, j); free(_handle); break; } @@ -693,16 +687,15 @@ static void handle_binding_command(struct sway_binding *binding) { } static bool handle_bindsym(struct sway_binding *binding, uint32_t keysym, uint32_t keycode) { - int i; - for (i = 0; i < binding->keys->length; ++i) { + for (size_t i = 0; i < binding->keys->length; ++i) { if (binding->bindcode) { - xkb_keycode_t *key = binding->keys->items[i]; + xkb_keycode_t *key = *(xkb_keycode_t **)list_get(binding->keys, i); if (keycode == *key) { handle_binding_command(binding); return true; } } else { - xkb_keysym_t *key = binding->keys->items[i]; + xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, i); if (keysym == *key) { handle_binding_command(binding); return true; @@ -715,15 +708,14 @@ static bool handle_bindsym(struct sway_binding *binding, uint32_t keysym, uint32 static bool valid_bindsym(struct sway_binding *binding) { bool match = false; - int i; - for (i = 0; i < binding->keys->length; ++i) { + for (size_t i = 0; i < binding->keys->length; ++i) { if (binding->bindcode) { - xkb_keycode_t *key = binding->keys->items[i]; + xkb_keycode_t *key = *(xkb_keycode_t **)list_get(binding->keys, i); if ((match = check_key(0, *key)) == false) { break; } } else { - xkb_keysym_t *key = binding->keys->items[i]; + xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, i); if ((match = check_key(*key, 0)) == false) { break; } @@ -735,7 +727,7 @@ static bool valid_bindsym(struct sway_binding *binding) { static bool handle_bindsym_release(struct sway_binding *binding) { if (binding->keys->length == 1) { - xkb_keysym_t *key = binding->keys->items[0]; + xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, 0); if (check_released_key(*key)) { handle_binding_command(binding); return true; @@ -762,8 +754,6 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier struct wlc_modifiers no_mods = { 0, 0 }; uint32_t sym = tolower(wlc_keyboard_get_keysym_for_key(key, &no_mods)); - int i; - if (state == WLC_KEY_STATE_PRESSED) { press_key(sym, key); } else { // WLC_KEY_STATE_RELEASED @@ -772,8 +762,8 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier // handle bar modifiers pressed/released uint32_t modifier; - for (i = 0; i < config->active_bar_modifiers->length; ++i) { - modifier = *(uint32_t *)config->active_bar_modifiers->items[i]; + for (size_t i = 0; i < config->active_bar_modifiers->length; ++i) { + modifier = **(uint32_t **)list_get(config->active_bar_modifiers, i); switch (modifier_state_changed(modifiers->mods, modifier)) { case MOD_STATE_PRESSED: @@ -788,14 +778,14 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier modifiers_state_update(modifiers->mods); // handle bindings - list_t *candidates = create_list(); - for (i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; + list_t *candidates = list_new(sizeof(struct sway_binding *), 0); + for (size_t i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); if ((modifiers->mods ^ binding->modifiers) == 0) { switch (state) { case WLC_KEY_STATE_PRESSED: { if (!binding->release && valid_bindsym(binding)) { - list_add(candidates, binding); + list_add(candidates, &binding); } } case WLC_KEY_STATE_RELEASED: @@ -808,8 +798,8 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier } } - for (i = 0; i < candidates->length; ++i) { - struct sway_binding *binding = candidates->items[i]; + for (size_t i = 0; i < candidates->length; ++i) { + struct sway_binding *binding = *(struct sway_binding **)list_get(candidates, i); if (state == WLC_KEY_STATE_PRESSED) { if (!binding->release && handle_bindsym(binding, sym, key)) { list_free(candidates); @@ -919,8 +909,8 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w struct sway_mode *mode = config->current_mode; // handle bindings - for (int i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; + for (size_t i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); if ((modifiers->mods ^ binding->modifiers) == 0) { switch (state) { case WLC_BUTTON_STATE_PRESSED: { @@ -1014,10 +1004,10 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w } // Send to front if floating if (pointer->is_floating) { - int i; - for (i = 0; i < pointer->parent->floating->length; i++) { - if (pointer->parent->floating->items[i] == pointer) { - list_del(pointer->parent->floating, i); + for (size_t i = 0; i < pointer->parent->floating->length; i++) { + swayc_t *item = *(swayc_t **)list_get(pointer->parent->floating, i); + if (item == pointer) { + list_delete(pointer->parent->floating, i); list_add(pointer->parent->floating, pointer); break; } @@ -1077,14 +1067,14 @@ static void handle_wlc_ready(void) { // Execute commands until there are none left config->active = true; while (config->cmd_queue->length) { - char *line = config->cmd_queue->items[0]; + char *line = *(char **)list_get(config->cmd_queue, 0); struct cmd_results *res = handle_command(line, CONTEXT_CONFIG); if (res->status != CMD_SUCCESS) { sway_log(L_ERROR, "Error on line '%s': %s", line, res->error); } free_cmd_results(res); free(line); - list_del(config->cmd_queue, 0); + list_delete(config->cmd_queue, 0); } } diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 512144a4a..e2f05a8de 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -145,9 +145,9 @@ static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) // window is in the scratchpad ? changed : none static const char *ipc_json_get_scratchpad_state(swayc_t *c) { - int i; - for (i = 0; i < scratchpad->length; i++) { - if (scratchpad->items[i] == c) { + for (size_t i = 0; i < scratchpad->length; i++) { + swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + if (item == c) { return "changed"; } } @@ -420,9 +420,8 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { // Add outputs if defined if (bar->outputs && bar->outputs->length > 0) { json_object *outputs = json_object_new_array(); - int i; - for (i = 0; i < bar->outputs->length; ++i) { - const char *name = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + const char *name = *(char **)list_get(bar->outputs, i); json_object_array_add(outputs, json_object_new_string(name)); } json_object_object_add(json, "outputs", outputs); @@ -433,30 +432,30 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { json_object *ipc_json_describe_container_recursive(swayc_t *c) { json_object *object = ipc_json_describe_container(c); - int i; json_object *floating = json_object_new_array(); if (c->type != C_VIEW && c->floating && c->floating->length > 0) { - for (i = 0; i < c->floating->length; ++i) { - json_object_array_add(floating, ipc_json_describe_container_recursive(c->floating->items[i])); + for (size_t i = 0; i < c->floating->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(c->floating, i); + json_object_array_add(floating, ipc_json_describe_container_recursive(item)); } } json_object_object_add(object, "floating_nodes", floating); json_object *children = json_object_new_array(); if (c->type != C_VIEW && c->children && c->children->length > 0) { - for (i = 0; i < c->children->length; ++i) { - json_object_array_add(children, ipc_json_describe_container_recursive(c->children->items[i])); + for (size_t i = 0; i < c->children->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(c->children, i); + json_object_array_add(children, ipc_json_describe_container_recursive(item)); } } json_object_object_add(object, "nodes", children); if (c->type == C_ROOT) { json_object *scratchpad_json = json_object_new_array(); - if (scratchpad->length > 0) { - for (i = 0; i < scratchpad->length; ++i) { - json_object_array_add(scratchpad_json, ipc_json_describe_container_recursive(scratchpad->items[i])); - } + for (size_t i = 0; i < scratchpad->length; ++i) { + swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + json_object_array_add(scratchpad_json, ipc_json_describe_container_recursive(item)); } json_object_object_add(object, "scratchpad", scratchpad_json); } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index dca881fa5..41951bbae 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -92,8 +92,8 @@ void ipc_init(void) { setenv("I3SOCK", ipc_sockaddr->sun_path, 1); setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1); - ipc_client_list = create_list(); - ipc_get_pixel_requests = create_list(); + ipc_client_list = list_new(sizeof(struct ipc_client *), 0); + ipc_get_pixel_requests = list_new(sizeof(struct get_pixels_request), 0); ipc_event_source = wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL); } @@ -183,7 +183,7 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) { pid_t pid = get_client_pid(client->fd); client->security_policy = get_ipc_policy_mask(pid); - list_add(ipc_client_list, client); + list_add(ipc_client_list, &client); return 0; } @@ -260,9 +260,9 @@ void ipc_client_disconnect(struct ipc_client *client) { sway_log(L_INFO, "IPC Client %d disconnected", client->fd); wlc_event_source_remove(client->event_source); - int i = 0; - while (i < ipc_client_list->length && ipc_client_list->items[i] != client) i++; - list_del(ipc_client_list, i); + size_t i = 0; + while (i < ipc_client_list->length && *(struct ipc_client **)list_get(ipc_client_list, i) != client) i++; + list_delete(ipc_client_list, i); close(client->fd); free(client); } @@ -280,12 +280,11 @@ void ipc_get_pixels(wlc_handle output) { return; } - list_t *unhandled = create_list(); + list_t *unhandled = list_new(sizeof(struct get_pixels_request *), 0); struct get_pixels_request *req; - int i; - for (i = 0; i < ipc_get_pixel_requests->length; ++i) { - req = ipc_get_pixel_requests->items[i]; + for (size_t i = 0; i < ipc_get_pixel_requests->length; ++i) { + req = *(struct get_pixels_request **)list_get(ipc_get_pixel_requests, i); if (req->output != output) { list_add(unhandled, req); continue; @@ -421,8 +420,8 @@ void ipc_client_handle_command(struct ipc_client *client) { } json_object *inputs = json_object_new_array(); if (input_devices) { - for(int i = 0; ilength; i++) { - struct libinput_device *device = input_devices->items[i]; + for(size_t i = 0; ilength; i++) { + struct libinput_device *device = *(struct libinput_device **)list_get(input_devices, i); json_object_array_add(inputs, ipc_json_describe_input(device)); } } @@ -520,7 +519,7 @@ void ipc_client_handle_command(struct ipc_client *client) { req->client = client; req->output = output->handle; req->geo = g; - list_add(ipc_get_pixel_requests, req); + list_add(ipc_get_pixel_requests, &req); wlc_output_schedule_render(output->handle); goto exit_cleanup; } @@ -533,9 +532,8 @@ void ipc_client_handle_command(struct ipc_client *client) { if (!buf[0]) { // Send list of configured bar IDs json_object *bars = json_object_new_array(); - int i; - for (i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); json_object_array_add(bars, json_object_new_string(bar->id)); } const char *json_string = json_object_to_json_string(bars); @@ -544,9 +542,8 @@ void ipc_client_handle_command(struct ipc_client *client) { } else { // Send particular bar's details struct bar_config *bar = NULL; - int i; - for (i = 0; i < config->bars->length; ++i) { - bar = config->bars->items[i]; + for (size_t i = 0; i < config->bars->length; ++i) { + bar = *(struct bar_config **)list_get(config->bars, i); if (strcmp(buf, bar->id) == 0) { break; } @@ -626,8 +623,8 @@ void ipc_get_outputs_callback(swayc_t *container, void *data) { static void ipc_get_marks_callback(swayc_t *container, void *data) { json_object *object = (json_object *)data; if (container->marks) { - for (int i = 0; i < container->marks->length; ++i) { - char *mark = (char *)container->marks->items[i]; + for (size_t i = 0; i < container->marks->length; ++i) { + char *mark = *(char **)list_get(container->marks, i); json_object_array_add(object, json_object_new_string(mark)); } } @@ -654,10 +651,8 @@ void ipc_send_event(const char *json_string, enum ipc_command_type event) { } } - int i; - struct ipc_client *client; - for (i = 0; i < ipc_client_list->length; i++) { - client = ipc_client_list->items[i]; + for (size_t i = 0; i < ipc_client_list->length; i++) { + struct ipc_client *client = *(struct ipc_client **)list_get(ipc_client_list, i); if (!(client->security_policy & security_mask)) { continue; } @@ -765,9 +760,8 @@ void ipc_event_binding_keyboard(struct sway_binding *sb) { const char *names[10]; int len = get_modifier_names(names, sb->modifiers); - int i; json_object *modifiers = json_object_new_array(); - for (i = 0; i < len; ++i) { + for (int i = 0; i < len; ++i) { json_object_array_add(modifiers, json_object_new_string(names[i])); } @@ -780,8 +774,8 @@ void ipc_event_binding_keyboard(struct sway_binding *sb) { if (sb->bindcode) { // bindcode: populate input_codes uint32_t keycode; - for (i = 0; i < sb->keys->length; ++i) { - keycode = *(uint32_t *)sb->keys->items[i]; + for (size_t i = 0; i < sb->keys->length; ++i) { + keycode = **(uint32_t **)list_get(sb->keys, i); json_object_array_add(input_codes, json_object_new_int(keycode)); if (i == 0) { input_code = keycode; @@ -790,8 +784,8 @@ void ipc_event_binding_keyboard(struct sway_binding *sb) { } else { // bindsym: populate symbols uint32_t keysym; char buffer[64]; - for (i = 0; i < sb->keys->length; ++i) { - keysym = *(uint32_t *)sb->keys->items[i]; + for (size_t i = 0; i < sb->keys->length; ++i) { + keysym = **(uint32_t **)list_get(sb->keys, i); if (xkb_keysym_get_name(keysym, buffer, 64) > 0) { json_object *str = json_object_new_string(buffer); json_object_array_add(symbols, str); diff --git a/sway/layout.c b/sway/layout.c index 69291dafd..b2eab662b 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -27,11 +27,11 @@ void init_layout(void) { root_container.type = C_ROOT; root_container.layout = L_NONE; root_container.name = strdup("root"); - root_container.children = create_list(); + root_container.children = list_new(sizeof(swayc_t *), 0); root_container.handle = -1; root_container.visible = true; current_focus = &root_container; - scratchpad = create_list(); + scratchpad = list_new(sizeof(swayc_t *), 0); } int index_child(const swayc_t *child) { @@ -40,14 +40,16 @@ int index_child(const swayc_t *child) { if (!child->is_floating) { len = parent->children->length; for (i = 0; i < len; ++i) { - if (parent->children->items[i] == child) { + swayc_t *item = *(swayc_t **)list_get(parent->children, i); + if (item == child) { break; } } } else { len = parent->floating->length; for (i = 0; i < len; ++i) { - if (parent->floating->items[i] == child) { + swayc_t *item = *(swayc_t **)list_get(parent->floating, i); + if (item == child) { break; } } @@ -80,14 +82,11 @@ static double *get_width(swayc_t *cont) { return &cont->width; } -void insert_child(swayc_t *parent, swayc_t *child, int index) { +void insert_child(swayc_t *parent, swayc_t *child, size_t index) { if (index > parent->children->length) { index = parent->children->length; } - if (index < 0) { - index = 0; - } - list_insert(parent->children, index, child); + list_insert(parent->children, index, &child); child->parent = parent; if (!parent->focused) { parent->focused = child; @@ -106,21 +105,21 @@ void insert_child(swayc_t *parent, swayc_t *child, int index) { get_maj_dim = get_height; get_min_dim = get_width; } - for (int i = index; i < parent->children->length;) { - int start = auto_group_start_index(parent, i); - int end = auto_group_end_index(parent, i); - swayc_t *first = parent->children->items[start]; + for (size_t i = index; i < parent->children->length;) { + size_t start = auto_group_start_index(parent, i); + size_t end = auto_group_end_index(parent, i); + swayc_t *first = *(swayc_t **)list_get(parent->children, start); if (start + 1 < parent->children->length) { /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(parent->children->items[start + 1]); + *get_maj_dim(first) = *get_maj_dim(*(swayc_t **)list_get(parent->children, start + 1)); } else { /* new group, let the apply_layout handle it */ first->height = first->width = 0; break; } double remaining = *get_min_dim(parent); - for (int j = end - 1; j > start; --j) { - swayc_t *sibling = parent->children->items[j]; + for (size_t j = end - 1; j > start; --j) { + swayc_t *sibling = *(swayc_t **)list_get(parent->children, j); if (sibling == child) { /* the inserted child won't yet have its minor dimension set */ @@ -184,9 +183,9 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { } int i = index_child(child); if (child->is_floating) { - parent->floating->items[i] = new_child; + *(swayc_t **)list_get(parent->floating, i) = new_child; } else { - parent->children->items[i] = new_child; + *(swayc_t **)list_get(parent->children, i) = new_child; } // Set parent and focus for new_child new_child->parent = child->parent; @@ -213,21 +212,23 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child) { } swayc_t *remove_child(swayc_t *child) { - int i; + size_t i; swayc_t *parent = child->parent; if (child->is_floating) { // Special case for floating views for (i = 0; i < parent->floating->length; ++i) { - if (parent->floating->items[i] == child) { - list_del(parent->floating, i); + swayc_t *item = *(swayc_t **)list_get(parent->floating, i); + if (item == child) { + list_delete(parent->floating, i); break; } } i = 0; } else { for (i = 0; i < parent->children->length; ++i) { - if (parent->children->items[i] == child) { - list_del(parent->children, i); + swayc_t *item = *(swayc_t **)list_get(parent->children, i); + if (item == child) { + list_delete(parent->children, i); break; } } @@ -242,27 +243,27 @@ swayc_t *remove_child(swayc_t *child) { get_maj_dim = get_height; get_min_dim = get_width; } - for (int j = parent->children->length - 1; j >= i;) { - int start = auto_group_start_index(parent, j); - int end = auto_group_end_index(parent, j); - swayc_t *first = parent->children->items[start]; + for (size_t j = parent->children->length - 1; j >= i;) { + size_t start = auto_group_start_index(parent, j); + size_t end = auto_group_end_index(parent, j); + swayc_t *first = *(swayc_t **)list_get(parent->children, start); if (i == start) { /* removed element was first child in the current group, use its size along the major axis */ *get_maj_dim(first) = *get_maj_dim(child); } else if (start > i) { /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(parent->children->items[start - 1]); + *get_maj_dim(first) = *get_maj_dim(*(swayc_t **)list_get(parent->children, start - 1)); } if (end != parent->children->length) { double remaining = *get_min_dim(parent); - for (int k = start; k < end - 1; ++k) { - swayc_t *sibling = parent->children->items[k]; + for (size_t k = start; k < end - 1; ++k) { + swayc_t *sibling = *(swayc_t **)list_get(parent->children, k); remaining -= *get_min_dim(sibling); } /* last element of the group gets remaining size, elements that don't change groups keep their ratio */ - *get_min_dim((swayc_t *) parent->children->items[end - 1]) = remaining; + *get_min_dim(*(swayc_t **)list_get(parent->children, end - 1)) = remaining; } /* else last group, let apply_layout handle it */ j = start - 1; } @@ -271,9 +272,9 @@ swayc_t *remove_child(swayc_t *child) { // Set focused to new container if (parent->focused == child) { if (parent->children->length > 0) { - parent->focused = parent->children->items[i ? i-1:0]; + parent->focused = *(swayc_t **)list_get(parent->children, i ? i-1:0); } else if (parent->floating && parent->floating->length) { - parent->focused = parent->floating->items[parent->floating->length - 1]; + parent->focused = *(swayc_t **)list_get(parent->floating, parent->floating->length - 1); } else { parent->focused = NULL; } @@ -297,14 +298,14 @@ void swap_container(swayc_t *a, swayc_t *b) { swayc_t *b_parent = b->parent; // Swap the pointers if (a->is_floating) { - a_parent->floating->items[a_index] = b; + *(swayc_t **)list_get(a_parent->floating, a_index) = b; } else { - a_parent->children->items[a_index] = b; + *(swayc_t **)list_get(a_parent->children, a_index) = b; } if (b->is_floating) { - b_parent->floating->items[b_index] = a; + *(swayc_t **)list_get(b_parent->floating, b_index) = a; } else { - b_parent->children->items[b_index] = a; + *(swayc_t **)list_get(b_parent->children, b_index) = a; } a->parent = b_parent; b->parent = a_parent; @@ -332,14 +333,12 @@ void swap_geometry(swayc_t *a, swayc_t *b) { b->height = h; } -static void swap_children(swayc_t *container, int a, int b) { - if (a >= 0 && b >= 0 && a < container->children->length - && b < container->children->length - && a != b) { - swayc_t *pa = (swayc_t *)container->children->items[a]; - swayc_t *pb = (swayc_t *)container->children->items[b]; - container->children->items[a] = container->children->items[b]; - container->children->items[b] = pa; +static void swap_children(swayc_t *container, size_t a, size_t b) { + if (a < container->children->length && b < container->children->length && a != b) { + swayc_t *pa = *(swayc_t **)list_get(container->children, a); + swayc_t *pb = *(swayc_t **)list_get(container->children, b); + *(swayc_t **)list_get(container->children, a) = *(swayc_t **)list_get(container->children, b); + *(swayc_t **)list_get(container->children, b) = pa; if (is_auto_layout(container->layout)) { size_t ga = auto_group_index(container, a); size_t gb = auto_group_index(container, b); @@ -385,7 +384,7 @@ void move_container(swayc_t *container, enum movement_direction dir, int move_am // swap first child in auto layout with currently focused child if (is_auto_layout(parent->layout)) { int focused_idx = index_child(container); - swayc_t *first = parent->children->items[0]; + swayc_t *first = *(swayc_t **)list_get(parent->children, 0); if (focused_idx > 0) { list_swap(parent->children, 0, focused_idx); swap_geometry(first, container); @@ -432,20 +431,20 @@ void move_container(swayc_t *container, enum movement_direction dir, int move_am diff = dir == MOVE_LEFT || dir == MOVE_UP || dir == MOVE_PREV ? -1 : 1; } int idx = index_child(child); - int desired = idx + diff; + ssize_t desired = idx + diff; if (dir == MOVE_NEXT || dir == MOVE_PREV) { // Next/Prev always wrap. if (desired < 0) { desired += parent->children->length; - } else if (desired >= parent->children->length) { + } else if (desired >= (ssize_t)parent->children->length) { desired = 0; } } // when it has ascended, legal insertion position is 0:len // when it has not, legal insertion position is 0:len-1 - if (desired >= 0 && desired - ascended < parent->children->length) { + if (desired >= 0 && desired - ascended < (ssize_t)parent->children->length) { if (!ascended) { - child = parent->children->items[desired]; + child = *(swayc_t **)list_get(parent->children, desired); // Move container into sibling container if (child->type == C_CONTAINER) { parent = child; @@ -469,7 +468,7 @@ void move_container(swayc_t *container, enum movement_direction dir, int move_am swayc_t *old_parent = remove_child(container); insert_child(parent, container, desired); destroy_container(old_parent); - sway_log(L_DEBUG,"Moving to %p %d", parent, desired); + sway_log(L_DEBUG,"Moving to %p %zd", parent, desired); } break; } @@ -676,9 +675,8 @@ void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { case L_STACKED: if (prev_layout != L_TABBED && prev_layout != L_STACKED) { // cache current geometry for all non-float children - int i; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *child = parent->children->items[i]; + for (size_t i = 0; i < parent->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(parent->children, i); child->cached_geometry.origin.x = child->x; child->cached_geometry.origin.y = child->y; child->cached_geometry.size.w = child->width; @@ -689,9 +687,8 @@ void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { default: if (prev_layout == L_TABBED || prev_layout == L_STACKED) { // recover cached geometry for all non-float children - int i; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *child = parent->children->items[i]; + for (size_t i = 0; i < parent->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(parent->children, i); // only recoverer cached geometry if non-zero if (!wlc_geometry_equals(&child->cached_geometry, &wlc_geometry_zero)) { child->x = child->cached_geometry.origin.x; @@ -831,15 +828,15 @@ void update_geometry(swayc_t *container) { int title_bar_height = config->font_height + 4; //borders + padding if (parent->layout == L_TABBED && parent->children->length > 1) { - int i, x = 0, w, l, r; + int x = 0, w, l, r; l = parent->children->length; w = geometry.size.w / l; r = geometry.size.w % l; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *view = parent->children->items[i]; + for (size_t i = 0; i < parent->children->length; ++i) { + swayc_t *view = *(swayc_t **)list_get(parent->children, i); if (view == container) { x = w * i; - if (i == l - 1) { + if ((ssize_t)i == l - 1) { w += r; } break; @@ -862,9 +859,9 @@ void update_geometry(swayc_t *container) { geometry.size.h -= (border_bottom + title_bar.size.h); container->title_bar_geometry = title_bar; } else if (parent->layout == L_STACKED && parent->children->length > 1) { - int i, y = 0; - for (i = 0; i < parent->children->length; ++i) { - swayc_t *view = parent->children->items[i]; + int y = 0; + for (size_t i = 0; i < parent->children->length; ++i) { + swayc_t *view = *(swayc_t **)list_get(parent->children, i); if (view == container) { y = title_bar_height * i; } @@ -951,7 +948,7 @@ static void apply_auto_layout(swayc_t *container, const double x, const double y bool master_first); static void arrange_windows_r(swayc_t *container, double width, double height) { - int i; + size_t i; if (width == -1 || height == -1) { swayc_log(L_DEBUG, container, "Arranging layout for %p", container); width = container->width; @@ -971,7 +968,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { switch (container->type) { case C_ROOT: for (i = 0; i < container->children->length; ++i) { - swayc_t *output = container->children->items[i]; + swayc_t *output = *(swayc_t **)list_get(container->children, i); sway_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows_r(output, -1, -1); } @@ -988,12 +985,12 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { } // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + swayc_t *child = *(swayc_t **)list_get(container->children, i); arrange_windows_r(child, -1, -1); } // Bring all unmanaged views to the front for (i = 0; i < container->unmanaged->length; ++i) { - wlc_handle *handle = container->unmanaged->items[i]; + wlc_handle *handle = *(wlc_handle **)list_get(container->unmanaged, i); wlc_view_bring_to_front(*handle); } return; @@ -1002,7 +999,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); width = output->width, height = output->height; for (i = 0; i < desktop_shell.panels->length; ++i) { - struct panel_config *config = desktop_shell.panels->items[i]; + struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); if (config->output == output->handle) { struct wlc_size size = *wlc_surface_get_size(config->surface); sway_log(L_DEBUG, "-> Found panel for this workspace: %ux%u, position: %u", size.w, size.h, config->panel_position); @@ -1107,8 +1104,8 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { // Arrage floating layouts for workspaces last if (container->type == C_WORKSPACE) { - for (int i = 0; i < container->floating->length; ++i) { - swayc_t *view = container->floating->items[i]; + for (size_t i = 0; i < container->floating->length; ++i) { + swayc_t *view = *(swayc_t **)list_get(container->floating, i); if (view->type == C_VIEW) { update_geometry(view); sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", @@ -1130,7 +1127,7 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, double scale = 0; // Calculate total width for (int i = start; i < end; ++i) { - double *old_width = &((swayc_t *)container->children->items[i])->width; + double *old_width = &(*(swayc_t **)list_get(container->children, i))->width; if (*old_width <= 0) { if (end - start > 1) { *old_width = width / (end - start - 1); @@ -1148,7 +1145,7 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, sway_log(L_DEBUG, "Arranging %p horizontally", container); swayc_t *focused = NULL; for (int i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + swayc_t *child = *(swayc_t **)list_get(container->children, i); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); @@ -1183,7 +1180,7 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, double scale = 0; // Calculate total height for (i = start; i < end; ++i) { - double *old_height = &((swayc_t *)container->children->items[i])->height; + double *old_height = &(*(swayc_t **)list_get(container->children, i))->height; if (*old_height <= 0) { if (end - start > 1) { *old_height = height / (end - start - 1); @@ -1201,7 +1198,7 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, sway_log(L_DEBUG, "Arranging %p vertically", container); swayc_t *focused = NULL; for (i = start; i < end; ++i) { - swayc_t *child = container->children->items[i]; + swayc_t *child = *(swayc_t **)list_get(container->children, i); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); @@ -1231,10 +1228,9 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, double width, double height) { - int i; swayc_t *focused = NULL; - for (i = 0; i < container->children->length; ++i) { - swayc_t *child = container->children->items[i]; + for (size_t i = 0; i < container->children->length; ++i) { + swayc_t *child = *(swayc_t **)list_get(container->children, i); child->x = x; child->y = y; if (child == container->focused) { @@ -1326,7 +1322,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, for (size_t group = 0; group < nb_groups; ++group) { int idx; if (auto_group_bounds(container, group, &idx, NULL)) { - swayc_t *child = container->children->items[idx]; + swayc_t *child = *(swayc_t **)list_get(container->children, idx); double *dim = group_layout == L_HORIZ ? &child->height : &child->width; if (*dim <= 0) { // New child with uninitialized dimension @@ -1380,9 +1376,8 @@ void arrange_windows(swayc_t *container, double width, double height) { } void arrange_backgrounds(void) { - struct background_config *bg; - for (int i = 0; i < desktop_shell.backgrounds->length; ++i) { - bg = desktop_shell.backgrounds->items[i]; + for (size_t i = 0; i < desktop_shell.backgrounds->length; ++i) { + struct background_config *bg = *(struct background_config **)list_get(desktop_shell.backgrounds, i); wlc_view_send_to_back(bg->handle); } } @@ -1400,10 +1395,10 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_dir switch (dir) { case MOVE_LEFT: // get most right child of new output - return ws->children->items[ws->children->length-1]; + return *(swayc_t **)list_get(ws->children, ws->children->length-1); case MOVE_RIGHT: // get most left child of new output - return ws->children->items[0]; + return *(swayc_t **)list_get(ws->children, 0); case MOVE_UP: case MOVE_DOWN: { @@ -1413,10 +1408,10 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_dir if (parent->layout == L_VERT) { if (dir == MOVE_UP) { // get child furthest down on new output - return parent->children->items[parent->children->length-1]; + return *(swayc_t **)list_get(parent->children, parent->children->length-1); } else if (dir == MOVE_DOWN) { // get child furthest up on new output - return parent->children->items[0]; + return *(swayc_t **)list_get(parent->children, 0); } } return focused_view; @@ -1455,7 +1450,7 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio if (desired < 0) { desired += parent->children->length; } - return parent->children->items[desired]; + return *(swayc_t **)list_get(parent->children, desired); } } @@ -1523,33 +1518,34 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio if (can_move) { if (container->is_floating) { if (desired < 0) { - wrap_candidate = parent->floating->items[parent->floating->length-1]; - } else if (desired >= parent->floating->length){ - wrap_candidate = parent->floating->items[0]; + wrap_candidate = *(swayc_t **)list_get(parent->floating, parent->floating->length-1); + } else if (desired >= (ssize_t)parent->floating->length){ + wrap_candidate = *(swayc_t **)list_get(parent->floating, 0); } else { - wrap_candidate = parent->floating->items[desired]; + wrap_candidate = *(swayc_t **)list_get(parent->floating, desired); } if (wrap_candidate) { wlc_view_bring_to_front(wrap_candidate->handle); } return wrap_candidate; - } else if (desired < 0 || desired >= parent->children->length) { + } else if (desired < 0 || desired >= (ssize_t)parent->children->length) { can_move = false; int len = parent->children->length; if (!wrap_candidate && len > 1) { if (desired < 0) { - wrap_candidate = parent->children->items[len-1]; + wrap_candidate = *(swayc_t **)list_get(parent->children, len-1); } else { - wrap_candidate = parent->children->items[0]; + wrap_candidate = *(swayc_t **)list_get(parent->children, 0); } if (config->force_focus_wrapping) { return wrap_candidate; } } } else { + swayc_t *item = *(swayc_t **)list_get(parent->children, desired); sway_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, - idx, container, dir, desired, parent->children->items[desired]); - return parent->children->items[desired]; + idx, container, dir, desired, item); + return item; } } if (!can_move) { @@ -1568,7 +1564,6 @@ swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) } void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge) { - int i; bool layout_match = true; sway_log(L_DEBUG, "Resizing %p with amount: %f", container, amount); if (edge == WLC_RESIZE_EDGE_LEFT || edge == WLC_RESIZE_EDGE_RIGHT) { @@ -1583,12 +1578,14 @@ void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge ed return; } if (layout_match) { - for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount/container->children->length, edge); + for (size_t i = 0; i < container->children->length; i++) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + recursive_resize(item, amount/container->children->length, edge); } } else { - for (i = 0; i < container->children->length; i++) { - recursive_resize(container->children->items[i], amount, edge); + for (size_t i = 0; i < container->children->length; i++) { + swayc_t *item = *(swayc_t **)list_get(container->children, i); + recursive_resize(item, amount, edge); } } } @@ -1613,8 +1610,6 @@ bool is_auto_layout(enum swayc_layouts layout) { * Return the number of master elements in a container */ static inline size_t auto_master_count(const swayc_t *container) { - sway_assert(container->children->length >= 0, "Container %p has (negative) children %d", - container, container->children->length); return MIN(container->nb_master, (size_t)container->children->length); } diff --git a/sway/main.c b/sway/main.c index b9549b127..290424e68 100644 --- a/sway/main.c +++ b/sway/main.c @@ -343,7 +343,7 @@ int main(int argc, char **argv) { log_env(); detect_proprietary(); - input_devices = create_list(); + input_devices = list_new(sizeof(struct libudev_device *), 0); /* Changing code earlier than this point requires detailed review */ /* (That code runs as root on systems without logind, and wlc_init drops to diff --git a/sway/output.c b/sway/output.c index c0f29c5ad..0ae36ef1c 100644 --- a/sway/output.c +++ b/sway/output.c @@ -37,8 +37,8 @@ swayc_t *output_by_name(const char* name, const struct wlc_point *abs_pos) { output = swayc_opposite_output(MOVE_UP, abs_pos); } } else { - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) { return c; } @@ -58,8 +58,8 @@ swayc_t *swayc_opposite_output(enum movement_direction dir, switch(dir) { case MOVE_LEFT: case MOVE_RIGHT: ; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { if (!opposite) { opposite = c; @@ -73,8 +73,8 @@ swayc_t *swayc_opposite_output(enum movement_direction dir, break; case MOVE_UP: case MOVE_DOWN: ; - for (int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { if (!opposite) { opposite = c; @@ -116,8 +116,8 @@ swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, case MOVE_LEFT: case MOVE_RIGHT: ; double delta_y = 0; - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (c == output || c->type != C_OUTPUT) { continue; } @@ -169,8 +169,8 @@ swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, case MOVE_UP: case MOVE_DOWN: ; double delta_x = 0; - for(int i = 0; i < root_container.children->length; ++i) { - swayc_t *c = root_container.children->items[i]; + for (size_t i = 0; i < root_container.children->length; ++i) { + swayc_t *c = *(swayc_t **)list_get(root_container.children, i); if (c == output || c->type != C_OUTPUT) { continue; } @@ -273,5 +273,5 @@ static int sort_workspace_cmp_qsort(const void *_a, const void *_b) { } void sort_workspaces(swayc_t *output) { - list_stable_sort(output->children, sort_workspace_cmp_qsort); + list_isort(output->children, sort_workspace_cmp_qsort); } diff --git a/sway/security.c b/sway/security.c index 8eab61261..e27a57b88 100644 --- a/sway/security.c +++ b/sway/security.c @@ -48,8 +48,8 @@ struct feature_policy *alloc_feature_policy(const char *program) { if (!validate_ipc_target(program)) { return NULL; } - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = config->feature_policies->items[i]; + for (size_t i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *policy = *(struct feature_policy **)list_get(config->feature_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; break; @@ -76,8 +76,8 @@ struct ipc_policy *alloc_ipc_policy(const char *program) { if (!validate_ipc_target(program)) { return NULL; } - for (int i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = config->ipc_policies->items[i]; + for (size_t i = 0; i < config->ipc_policies->length; ++i) { + struct ipc_policy *policy = *(struct ipc_policy **)list_get(config->ipc_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; break; @@ -143,8 +143,8 @@ static const char *get_pid_exe(pid_t pid) { struct feature_policy *get_feature_policy(const char *name) { struct feature_policy *policy = NULL; - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *p = config->feature_policies->items[i]; + for (size_t i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *p = *(struct feature_policy **)list_get(config->feature_policies, i); if (strcmp(p->program, name) == 0) { policy = p; break; @@ -155,7 +155,7 @@ struct feature_policy *get_feature_policy(const char *name) { if (!policy) { sway_abort("Unable to allocate security policy"); } - list_add(config->feature_policies, policy); + list_add(config->feature_policies, &policy); } return policy; } @@ -164,8 +164,8 @@ uint32_t get_feature_policy_mask(pid_t pid) { uint32_t default_policy = 0; const char *link = get_pid_exe(pid); - for (int i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = config->feature_policies->items[i]; + for (size_t i = 0; i < config->feature_policies->length; ++i) { + struct feature_policy *policy = *(struct feature_policy **)list_get(config->feature_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; } @@ -181,8 +181,8 @@ uint32_t get_ipc_policy_mask(pid_t pid) { uint32_t default_policy = 0; const char *link = get_pid_exe(pid); - for (int i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = config->ipc_policies->items[i]; + for (size_t i = 0; i < config->ipc_policies->length; ++i) { + struct ipc_policy *policy = *(struct ipc_policy **)list_get(config->ipc_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; } @@ -197,8 +197,8 @@ uint32_t get_ipc_policy_mask(pid_t pid) { uint32_t get_command_policy_mask(const char *cmd) { uint32_t default_policy = 0; - for (int i = 0; i < config->command_policies->length; ++i) { - struct command_policy *policy = config->command_policies->items[i]; + for (size_t i = 0; i < config->command_policies->length; ++i) { + struct command_policy *policy = *(struct command_policy **)list_get(config->command_policies, i); if (strcmp(policy->command, "*") == 0) { default_policy = policy->context; } diff --git a/sway/workspace.c b/sway/workspace.c index 29cacce99..37b67c1b8 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -29,9 +29,8 @@ struct workspace_by_number_data { }; static bool workspace_valid_on_output(const char *output_name, const char *ws_name) { - int i; - for (i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; + for (size_t i = 0; i < config->workspace_outputs->length; ++i) { + struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); if (strcasecmp(wso->workspace, ws_name) == 0) { if (strcasecmp(wso->output, output_name) != 0) { return false; @@ -44,7 +43,6 @@ static bool workspace_valid_on_output(const char *output_name, const char *ws_na char *workspace_next_name(const char *output_name) { sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", output_name); - int i; int l = 1; // Scan all workspace bindings to find the next available workspace name, // if none are found/available then default to a number @@ -52,8 +50,8 @@ char *workspace_next_name(const char *output_name) { int order = INT_MAX; char *target = NULL; - for (i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = mode->bindings->items[i]; + for (size_t i = 0; i < mode->bindings->length; ++i) { + struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); char *cmdlist = strdup(binding->command); char *dup = cmdlist; char *name = NULL; @@ -133,15 +131,14 @@ char *workspace_next_name(const char *output_name) { swayc_t *workspace_create(const char* name) { swayc_t *parent; // Search for workspace<->output pair - int i, e = config->workspace_outputs->length; + size_t i, e = config->workspace_outputs->length; for (i = 0; i < e; ++i) { - struct workspace_output *wso = config->workspace_outputs->items[i]; - if (strcasecmp(wso->workspace, name) == 0) - { + struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); + if (strcasecmp(wso->workspace, name) == 0) { // Find output to use if it exists e = root_container.children->length; for (i = 0; i < e; ++i) { - parent = root_container.children->items[i]; + parent = *(swayc_t **)list_get(root_container.children, i); if (strcmp(parent->name, wso->output) == 0) { return new_workspace(parent, name); } @@ -208,10 +205,10 @@ swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { return NULL; } - int i; - for (i = 0; i < output->children->length; i++) { - if (output->children->items[i] == output->focused) { - return output->children->items[wrap(i + (next ? 1 : -1), output->children->length)]; + for (size_t i = 0; i < output->children->length; i++) { + swayc_t *item = *(swayc_t **)list_get(output->children, i); + if (item == output->focused) { + return *(swayc_t **)list_get(output->children, (wrap(i + (next ? 1 : -1), output->children->length))); } } @@ -231,20 +228,21 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { swayc_t *current_output = workspace->parent; int offset = next ? 1 : -1; - int start = next ? 0 : 1; - int end = next ? (current_output->children->length) - 1 : current_output->children->length; - int i; - for (i = start; i < end; i++) { - if (current_output->children->items[i] == workspace) { - return current_output->children->items[i + offset]; + size_t start = next ? 0 : 1; + size_t end = next ? (current_output->children->length) - 1 : current_output->children->length; + for (size_t i = start; i < end; i++) { + swayc_t *item = *(swayc_t **)list_get(current_output->children, i); + if (item == workspace) { + return *(swayc_t **)list_get(current_output->children, i + offset); } } // Given workspace is the first/last on the output, jump to the previous/next output - int num_outputs = root_container.children->length; - for (i = 0; i < num_outputs; i++) { - if (root_container.children->items[i] == current_output) { - swayc_t *next_output = root_container.children->items[wrap(i + offset, num_outputs)]; + size_t num_outputs = root_container.children->length; + for (size_t i = 0; i < num_outputs; i++) { + swayc_t *item = *(swayc_t **)list_get(root_container.children, i); + if (item == current_output) { + swayc_t *next_output = *(swayc_t **)list_get(root_container.children, wrap(i + offset, num_outputs)); return workspace_output_prev_next_impl(next_output, next); } } @@ -294,15 +292,15 @@ bool workspace_switch(swayc_t *workspace) { // move sticky containers if (swayc_parent_by_type(active_ws, C_OUTPUT) == swayc_parent_by_type(workspace, C_OUTPUT)) { // don't change list while traversing it, use intermediate list instead - list_t *stickies = create_list(); - for (int i = 0; i < active_ws->floating->length; i++) { - swayc_t *cont = active_ws->floating->items[i]; + list_t *stickies = list_new(sizeof(swayc_t *), 0); + for (size_t i = 0; i < active_ws->floating->length; i++) { + swayc_t *cont = *(swayc_t **)list_get(active_ws->floating, i); if (cont->sticky) { - list_add(stickies, cont); + list_add(stickies, &cont); } } - for (int i = 0; i < stickies->length; i++) { - swayc_t *cont = stickies->items[i]; + for (size_t i = 0; i < stickies->length; i++) { + swayc_t *cont = *(swayc_t **)list_get(stickies, i); sway_log(L_DEBUG, "Moving sticky container %p to %p:%s", cont, workspace, workspace->name); swayc_t *parent = remove_child(cont); @@ -323,9 +321,9 @@ bool workspace_switch(swayc_t *workspace) { } swayc_t *workspace_for_pid(pid_t pid) { - int i; swayc_t *ws = NULL; struct pid_workspace *pw = NULL; + size_t i; sway_log(L_DEBUG, "looking for workspace for pid %d", pid); @@ -338,7 +336,7 @@ swayc_t *workspace_for_pid(pid_t pid) { do { for (i = 0; i < config->pid_workspaces->length; i++) { - pw = config->pid_workspaces->items[i]; + pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); pid_t *pw_pid = pw->pid; if (pid == *pw_pid) { @@ -367,7 +365,7 @@ swayc_t *workspace_for_pid(pid_t pid) { ws = workspace_create(pw->workspace); } - list_del(config->pid_workspaces, i); + list_delete(config->pid_workspaces, i); } return ws; diff --git a/swaybar/bar.c b/swaybar/bar.c index abde1cc94..90b4c615f 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -19,7 +19,7 @@ static void bar_init(struct bar *bar) { bar->config = init_config(); bar->status = init_status_line(); - bar->outputs = create_list(); + bar->outputs = list_new(sizeof(struct output *), 0); } static void spawn_status_cmd_proc(struct bar *bar) { @@ -55,7 +55,7 @@ struct output *new_output(const char *name) { output->name = strdup(name); output->window = NULL; output->registry = NULL; - output->workspaces = create_list(); + output->workspaces = list_new(sizeof(struct workspace *), 0); return output; } @@ -67,8 +67,8 @@ static void mouse_button_notify(struct window *window, int x, int y, } struct output *clicked_output = NULL; - for (int i = 0; i < swaybar.outputs->length; i++) { - struct output *output = swaybar.outputs->items[i]; + for (size_t i = 0; i < swaybar.outputs->length; i++) { + struct output *output = *(struct output **)list_get(swaybar.outputs, i); if (window == output->window) { clicked_output = output; break; @@ -80,8 +80,8 @@ static void mouse_button_notify(struct window *window, int x, int y, } double button_x = 0.5; - for (int i = 0; i < clicked_output->workspaces->length; i++) { - struct workspace *workspace = clicked_output->workspaces->items[i]; + for (size_t i = 0; i < clicked_output->workspaces->length; i++) { + struct workspace *workspace = *(struct workspace **)list_get(clicked_output->workspaces, i); int button_width, button_height; workspace_button_size(window, workspace->name, &button_width, &button_height); @@ -95,14 +95,14 @@ static void mouse_button_notify(struct window *window, int x, int y, } static void mouse_scroll_notify(struct window *window, enum scroll_direction direction) { + size_t i; sway_log(L_DEBUG, "Mouse wheel scrolled %s", direction == SCROLL_UP ? "up" : "down"); if (!swaybar.config->wrap_scroll) { // Find output this window lives on - int i; struct output *output = NULL; for (i = 0; i < swaybar.outputs->length; ++i) { - output = swaybar.outputs->items[i]; + output = *(struct output **)list_get(swaybar.outputs, i); if (output->window == window) { break; } @@ -112,7 +112,7 @@ static void mouse_scroll_notify(struct window *window, enum scroll_direction dir } int focused = -1; for (i = 0; i < output->workspaces->length; ++i) { - struct workspace *ws = output->workspaces->items[i]; + struct workspace *ws = *(struct workspace **)list_get(output->workspaces, i); if (ws->focused) { focused = i; break; @@ -122,7 +122,7 @@ static void mouse_scroll_notify(struct window *window, enum scroll_direction dir return; } if ((focused == 0 && direction == SCROLL_UP) || - (focused == output->workspaces->length - 1 && direction == SCROLL_DOWN)) { + (focused == (ssize_t)output->workspaces->length - 1 && direction == SCROLL_DOWN)) { // Do not wrap return; } @@ -142,9 +142,8 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { ipc_bar_init(bar, bar_id); - int i; - for (i = 0; i < bar->outputs->length; ++i) { - struct output *bar_output = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + struct output *bar_output = *(struct output **)list_get(bar->outputs, i); bar_output->registry = registry_poll(); @@ -152,7 +151,7 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { sway_abort("swaybar requires the compositor to support the desktop-shell extension."); } - struct output_state *output = bar_output->registry->outputs->items[bar_output->idx]; + struct output_state *output = *(struct output_state **)list_get(bar_output->registry->outputs, bar_output->idx); bar_output->window = window_setup(bar_output->registry, output->width / output->scale, 30, output->scale, false); @@ -190,18 +189,16 @@ void bar_run(struct bar *bar) { pfd[1].fd = bar->status_read_fd; pfd[1].events = POLLIN; - int i; - for (i = 0; i < bar->outputs->length; ++i) { - struct output *output = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + struct output *output = *(struct output **)list_get(bar->outputs, i); pfd[i+2].fd = wl_display_get_fd(output->registry->display); pfd[i+2].events = POLLIN; } while (1) { if (dirty) { - int i; - for (i = 0; i < bar->outputs->length; ++i) { - struct output *output = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + struct output *output = *(struct output **)list_get(bar->outputs, i); if (window_prerender(output->window) && output->window->cairo) { render(output, bar->config, bar->status); window_render(output->window); @@ -225,8 +222,8 @@ void bar_run(struct bar *bar) { } // dispatch wl_display events - for (i = 0; i < bar->outputs->length; ++i) { - struct output *output = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + struct output *output = *(struct output **)list_get(bar->outputs, i); if (pfd[i+2].revents & POLLIN) { if (wl_display_dispatch(output->registry->display) == -1) { sway_log(L_ERROR, "failed to dispatch wl: %d", errno); @@ -239,9 +236,8 @@ void bar_run(struct bar *bar) { } void free_workspaces(list_t *workspaces) { - int i; - for (i = 0; i < workspaces->length; ++i) { - struct workspace *ws = workspaces->items[i]; + for (size_t i = 0; i < workspaces->length; ++i) { + struct workspace *ws = *(struct workspace **)list_get(workspaces, i); free(ws->name); free(ws); } @@ -264,9 +260,9 @@ static void free_output(struct output *output) { } static void free_outputs(list_t *outputs) { - int i; - for (i = 0; i < outputs->length; ++i) { - free_output(outputs->items[i]); + for (size_t i = 0; i < outputs->length; ++i) { + struct output *item = *(struct output **)list_get(outputs, i); + free_output(item); } list_free(outputs); } diff --git a/swaybar/config.c b/swaybar/config.c index 1d8020223..5cebad5e5 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -43,7 +43,7 @@ struct config *init_config() { config->wrap_scroll = false; config->workspace_buttons = true; config->all_outputs = false; - config->outputs = create_list(); + config->outputs = list_new(sizeof(char *), 0); /* height */ config->height = 0; diff --git a/swaybar/ipc.c b/swaybar/ipc.c index b08eeea89..ebed88586 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -84,25 +84,25 @@ static void ipc_parse_config(struct config *config, const char *payload) { } // free previous outputs list - int i; - for (i = 0; i < config->outputs->length; ++i) { - free(config->outputs->items[i]); + for (size_t i = 0; i < config->outputs->length; ++i) { + free(*(char **)list_get(config->outputs, i)); } list_free(config->outputs); - config->outputs = create_list(); + config->outputs = list_new(sizeof(char *), 0); if (outputs) { int length = json_object_array_length(outputs); json_object *output; const char *output_str; - for (i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { output = json_object_array_get_idx(outputs, i); output_str = json_object_get_string(output); if (strcmp("*", output_str) == 0) { config->all_outputs = true; break; } - list_add(config->outputs, strdup(output_str)); + char *ptr = strdup(output_str); + list_add(config->outputs, &ptr); } } else { config->all_outputs = true; @@ -214,13 +214,12 @@ static void ipc_parse_config(struct config *config, const char *payload) { } static void ipc_update_workspaces(struct bar *bar) { - int i; - for (i = 0; i < bar->outputs->length; ++i) { - struct output *output = bar->outputs->items[i]; + for (size_t i = 0; i < bar->outputs->length; ++i) { + struct output *output = *(struct output **)list_get(bar->outputs, i); if (output->workspaces) { free_workspaces(output->workspaces); } - output->workspaces = create_list(); + output->workspaces = list_new(sizeof(struct workspace *), 0); } uint32_t len = 0; @@ -234,7 +233,7 @@ static void ipc_update_workspaces(struct bar *bar) { int length = json_object_array_length(results); json_object *ws_json; json_object *num, *name, *visible, *focused, *out, *urgent; - for (i = 0; i < length; ++i) { + for (int i = 0; i < length; ++i) { ws_json = json_object_array_get_idx(results, i); json_object_object_get_ex(ws_json, "num", &num); @@ -244,9 +243,8 @@ static void ipc_update_workspaces(struct bar *bar) { json_object_object_get_ex(ws_json, "output", &out); json_object_object_get_ex(ws_json, "urgent", &urgent); - int j; - for (j = 0; j < bar->outputs->length; ++j) { - struct output *output = bar->outputs->items[j]; + for (size_t j = 0; j < bar->outputs->length; ++j) { + struct output *output = *(struct output **)list_get(bar->outputs, j); if (strcmp(json_object_get_string(out), output->name) == 0) { struct workspace *ws = malloc(sizeof(struct workspace)); ws->num = json_object_get_int(num); @@ -261,7 +259,7 @@ static void ipc_update_workspaces(struct bar *bar) { output->focused = true; } ws->urgent = json_object_get_boolean(urgent); - list_add(output->workspaces, ws); + list_add(output->workspaces, &ws); } } } @@ -301,9 +299,8 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { if (bar->config->all_outputs) { use_output = true; } else { - int j = 0; - for (j = 0; j < bar->config->outputs->length; ++j) { - const char *conf_name = bar->config->outputs->items[j]; + for (size_t j = 0; j < bar->config->outputs->length; ++j) { + const char *conf_name = *(char **)list_get(bar->config->outputs, j); if (strcasecmp(name, conf_name) == 0) { use_output = true; break; diff --git a/swaybar/render.c b/swaybar/render.c index 2eae997fc..e77dac0c2 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -275,8 +275,6 @@ static void render_binding_mode_indicator(struct window *window, struct config * } void render(struct output *output, struct config *config, struct status_line *line) { - int i; - struct window *window = output->window; cairo_t *cairo = window->cairo; bool is_focused = output->focused; @@ -316,8 +314,8 @@ void render(struct output *output, struct config *config, struct status_line *li } else if (line->protocol == I3BAR && line->block_line) { double pos = (window->width * window->scale) - 0.5; bool edge = true; - for (i = line->block_line->length - 1; i >= 0; --i) { - struct status_block *block = line->block_line->items[i]; + for (ssize_t i = line->block_line->length - 1; i >= 0; --i) { + struct status_block *block = *(struct status_block **)list_get(line->block_line, i); if (block->full_text && block->full_text[0]) { render_block(window, config, block, &pos, edge, is_focused); edge = false; @@ -330,8 +328,8 @@ void render(struct output *output, struct config *config, struct status_line *li // Workspaces if (config->workspace_buttons) { - for (i = 0; i < output->workspaces->length; ++i) { - struct workspace *ws = output->workspaces->items[i]; + for (size_t i = 0; i < output->workspaces->length; ++i) { + struct workspace *ws = *(struct workspace **)list_get(output->workspaces, i); render_workspace_button(window, config, ws, &x); } } diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 83e8ce2c7..8cb8b12cb 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -62,11 +62,15 @@ static void parse_json(struct bar *bar, const char *text) { } if (bar->status->block_line) { - list_foreach(bar->status->block_line, free_status_block); + //list_foreach(bar->status->block_line, free_status_block); + for (size_t i = 0; i < bar->status->block_line->length; ++i) { + struct status_block *item = *(struct status_block **)list_get(bar->status->block_line, i); + free_status_block(item); + } list_free(bar->status->block_line); } - bar->status->block_line = create_list(); + bar->status->block_line = list_new(sizeof(struct status_block *), 0); int i; for (i = 0; i < json_object_array_length(results); ++i) { @@ -199,7 +203,7 @@ static void parse_json(struct bar *bar, const char *text) { new->border_right = 1; } - list_add(bar->status->block_line, new); + list_add(bar->status->block_line, &new); } json_object_put(results); @@ -438,7 +442,7 @@ bool handle_status_line(struct bar *bar) { struct status_line *init_status_line() { struct status_line *line = malloc(sizeof(struct status_line)); - line->block_line = create_list(); + line->block_line = list_new(sizeof(struct status_block *), 0); line->text_line = NULL; line->protocol = UNDEF; @@ -447,7 +451,11 @@ struct status_line *init_status_line() { void free_status_line(struct status_line *line) { if (line->block_line) { - list_foreach(line->block_line, free_status_block); + //list_foreach(line->block_line, free_status_block); + for (size_t i = 0; i < line->block_line->length; ++i) { + struct status_block *item = *(struct status_block **)list_get(line->block_line, i); + free_status_block(item); + } list_free(line->block_line); } } diff --git a/swaybg/main.c b/swaybg/main.c index 9dba0c8f8..b4ae0ffe4 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -25,9 +25,8 @@ enum scaling_mode { }; void sway_terminate(int exit_code) { - int i; - for (i = 0; i < surfaces->length; ++i) { - struct window *window = surfaces->items[i]; + for (size_t i = 0; i < surfaces->length; ++i) { + struct window *window = *(struct window **)list_get(surfaces, i); window_teardown(window); } list_free(surfaces); @@ -54,7 +53,7 @@ bool is_valid_color(const char *color) { int main(int argc, const char **argv) { init_log(L_INFO); - surfaces = create_list(); + surfaces = list_new(sizeof(struct window *), 0); registry = registry_poll(); if (argc != 4) { @@ -66,9 +65,8 @@ int main(int argc, const char **argv) { } int desired_output = atoi(argv[1]); - sway_log(L_INFO, "Using output %d of %d", desired_output, registry->outputs->length); - int i; - struct output_state *output = registry->outputs->items[desired_output]; + sway_log(L_INFO, "Using output %d of %zu", desired_output, registry->outputs->length); + struct output_state *output = *(struct output_state **)list_get(registry->outputs, desired_output); struct window *window = window_setup(registry, output->width, output->height, output->scale, false); if (!window) { @@ -76,7 +74,7 @@ int main(int argc, const char **argv) { } desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); window_make_shell(window); - list_add(surfaces, window); + list_add(surfaces, &window); if (strcmp(argv[3], "solid_color") == 0 && is_valid_color(argv[2])) { cairo_set_source_u32(window->cairo, parse_color(argv[2])); @@ -119,8 +117,8 @@ int main(int argc, const char **argv) { int wwidth = window->width * window->scale; int wheight = window->height * window->scale; - for (i = 0; i < surfaces->length; ++i) { - struct window *window = surfaces->items[i]; + for (size_t i = 0; i < surfaces->length; ++i) { + struct window *window = *(struct window **)list_get(surfaces, i); if (window_prerender(window) && window->cairo) { switch (scaling_mode) { case SCALING_MODE_STRETCH: @@ -196,8 +194,8 @@ int main(int argc, const char **argv) { while (wl_display_dispatch(registry->display) != -1); - for (i = 0; i < surfaces->length; ++i) { - struct window *window = surfaces->items[i]; + for (size_t i = 0; i < surfaces->length; ++i) { + struct window *window = *(struct window **)list_get(surfaces, i); window_teardown(window); } list_free(surfaces); diff --git a/swaylock/main.c b/swaylock/main.c index 475924091..86b422172 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -44,9 +44,8 @@ void sigalarm_handler(int sig) { } void sway_terminate(int exit_code) { - int i; - for (i = 0; i < render_data.surfaces->length; ++i) { - struct window *window = render_data.surfaces->items[i]; + for (size_t i = 0; i < render_data.surfaces->length; ++i) { + struct window *window = *(struct window **)list_get(render_data.surfaces, i); window_teardown(window); } list_free(render_data.surfaces); @@ -346,7 +345,6 @@ cairo_surface_t *load_image(char *image_path) { int main(int argc, char **argv) { const char *scaling_mode_str = "fit", *socket_path = NULL; - int i; void *images = NULL; config = init_config(); @@ -535,7 +533,7 @@ int main(int argc, char **argv) { password_size = 1024; password = malloc(password_size); password[0] = '\0'; - render_data.surfaces = create_list(); + render_data.surfaces = list_new(sizeof(struct window *), 0); if (!socket_path) { socket_path = get_socketpath(); if (!socket_path) { @@ -557,14 +555,14 @@ int main(int argc, char **argv) { registry->pointer = NULL; } - for (i = 0; i < registry->outputs->length; ++i) { - struct output_state *output = registry->outputs->items[i]; + for (size_t i = 0; i < registry->outputs->length; ++i) { + struct output_state *output = *(struct output_state **)list_get(registry->outputs, i); struct window *window = window_setup(registry, output->width, output->height, output->scale, true); if (!window) { sway_abort("Failed to create surfaces."); } - list_add(render_data.surfaces, window); + list_add(render_data.surfaces, &window); } registry->input->notify = notify_key; @@ -579,7 +577,7 @@ int main(int argc, char **argv) { char *outputs = ipc_single_command(socketfd, IPC_GET_OUTPUTS, "", &len); struct json_object *json_outputs = json_tokener_parse(outputs); - for (i = 0; i < registry->outputs->length; ++i) { + for (size_t i = 0; i < registry->outputs->length; ++i) { if (displays_paths[i * 2] != NULL) { for (int j = 0;; ++j) { if (j >= json_object_array_length(json_outputs)) { @@ -608,9 +606,9 @@ int main(int argc, char **argv) { bool locked = false; while (wl_display_dispatch(registry->display) != -1) { if (!locked) { - for (i = 0; i < registry->outputs->length; ++i) { - struct output_state *output = registry->outputs->items[i]; - struct window *window = render_data.surfaces->items[i]; + for (size_t i = 0; i < registry->outputs->length; ++i) { + struct output_state *output = *(struct output_state **)list_get(registry->outputs, i); + struct window *window = *(struct window **)list_get(render_data.surfaces, i); lock_set_lock_surface(registry->swaylock, output->output, window->surface); } locked = true; @@ -621,7 +619,7 @@ int main(int argc, char **argv) { if (render_data.num_images == -1) { cairo_surface_destroy(render_data.image); } else if (render_data.num_images >= 1) { - for (i = 0; i < registry->outputs->length; ++i) { + for (size_t i = 0; i < registry->outputs->length; ++i) { if (render_data.images[i] != NULL) { cairo_surface_destroy(render_data.images[i]); } @@ -629,8 +627,8 @@ int main(int argc, char **argv) { free(render_data.images); } - for (i = 0; i < render_data.surfaces->length; ++i) { - struct window *window = render_data.surfaces->items[i]; + for (size_t i = 0; i < render_data.surfaces->length; ++i) { + struct window *window = *(struct window **)list_get(render_data.surfaces, i); window_teardown(window); } list_free(render_data.surfaces); @@ -642,10 +640,9 @@ int main(int argc, char **argv) { } void render(struct render_data *render_data, struct lock_config *config) { - int i; - for (i = 0; i < render_data->surfaces->length; ++i) { - sway_log(L_DEBUG, "Render surface %d of %d", i, render_data->surfaces->length); - struct window *window = render_data->surfaces->items[i]; + for (size_t i = 0; i < render_data->surfaces->length; ++i) { + sway_log(L_DEBUG, "Render surface %zu of %zu", i, render_data->surfaces->length); + struct window *window = *(struct window **)list_get(render_data->surfaces, i); if (!window_prerender(window) || !window->cairo) { continue; } diff --git a/wayland/registry.c b/wayland/registry.c index 2df605a82..a29efd7c9 100644 --- a/wayland/registry.c +++ b/wayland/registry.c @@ -227,7 +227,7 @@ static void registry_global(void *data, struct wl_registry *registry, ostate->output = output; ostate->scale = 1; wl_output_add_listener(output, &output_listener, ostate); - list_add(reg->outputs, ostate); + list_add(reg->outputs, &ostate); } else if (strcmp(interface, desktop_shell_interface.name) == 0) { reg->desktop_shell = wl_registry_bind(registry, name, &desktop_shell_interface, version); } else if (strcmp(interface, lock_interface.name) == 0) { @@ -247,7 +247,7 @@ static const struct wl_registry_listener registry_listener = { struct registry *registry_poll(void) { struct registry *registry = malloc(sizeof(struct registry)); memset(registry, 0, sizeof(struct registry)); - registry->outputs = create_list(); + registry->outputs = list_new(sizeof(struct output_state *), 0); registry->input = calloc(sizeof(struct input), 1); registry->input->xkb.context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); From f4c90635ff0bba52a5fa948d5670ec894421dc27 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 12:56:36 +1200 Subject: [PATCH 10/17] fixes --- common/list.c | 2 +- sway/config.c | 5 +++-- sway/container.c | 2 +- sway/criteria.c | 4 ++-- sway/extensions.c | 6 +++--- sway/handlers.c | 2 +- sway/ipc-server.c | 2 +- sway/layout.c | 14 +++++++------- swaybar/ipc.c | 2 +- 9 files changed, 20 insertions(+), 19 deletions(-) diff --git a/common/list.c b/common/list.c index 16b5629a0..feaf8abf7 100644 --- a/common/list.c +++ b/common/list.c @@ -211,7 +211,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { - if (compare(key, &array[i]) == 0) { + if (compare(&key, &array[i]) == 0) { if (ret) { memcpy(ret, &array[i], size); } diff --git a/sway/config.c b/sway/config.c index d301deea7..12d05dae0 100644 --- a/sway/config.c +++ b/sway/config.c @@ -1230,7 +1230,8 @@ int workspace_output_cmp_workspace(const void *a, const void *b) { } int sway_binding_cmp_keys(const void *key, const void *item) { - const struct sway_binding *binda = item, *bindb = key; + const struct sway_binding *binda = *(struct sway_binding **)item; + const struct sway_binding *bindb = *(struct sway_binding **)key; // Count keys pressed for this binding. important so we check long before // short ones. for example mod+a+b before mod+a @@ -1277,7 +1278,7 @@ int sway_binding_cmp_keys(const void *key, const void *item) { int sway_binding_cmp(const void *a, const void *b) { int cmp = 0; - if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { + if ((cmp = sway_binding_cmp_keys(&a, &b)) != 0) { return cmp; } const struct sway_binding *binda = a, *bindb = b; diff --git a/sway/container.c b/sway/container.c index f25fccb41..6767b5034 100644 --- a/sway/container.c +++ b/sway/container.c @@ -373,7 +373,7 @@ swayc_t *new_floating_view(wlc_handle handle) { view->is_floating = true; // Case of focused workspace, just create as child of it - list_add(swayc_active_workspace()->floating, view); + list_add(swayc_active_workspace()->floating, &view); view->parent = swayc_active_workspace(); if (swayc_active_workspace()->focused == NULL) { set_focused_container_for(swayc_active_workspace(), view); diff --git a/sway/criteria.c b/sway/criteria.c index d5a580e2f..83a4993ec 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -376,7 +376,7 @@ list_t *criteria_for(swayc_t *cont) { for (size_t i = 0; i < config->criteria->length; i++) { struct criteria *bc = *(struct criteria **)list_get(config->criteria, i); if (criteria_test(cont, bc->tokens)) { - list_add(matches, bc); + list_add(matches, &bc); } } return matches; @@ -390,7 +390,7 @@ struct list_tokens { static void container_match_add(swayc_t *container, void *arg) { struct list_tokens *list_tokens = arg; if (criteria_test(container, list_tokens->tokens)) { - list_add(list_tokens->list, container); + list_add(list_tokens->list, &container); } } list_t *container_for(list_t *tokens) { diff --git a/sway/extensions.c b/sway/extensions.c index 38f8db717..f93d0d8b2 100644 --- a/sway/extensions.c +++ b/sway/extensions.c @@ -28,7 +28,7 @@ static struct panel_config *find_or_create_panel_config(struct wl_resource *reso sway_log(L_ERROR, "Unable to create panel config"); return NULL; } - list_add(desktop_shell.panels, config); + list_add(desktop_shell.panels, &config); config->wl_resource = resource; return config; } @@ -101,7 +101,7 @@ static void set_background(struct wl_client *client, struct wl_resource *resourc config->output = output; config->surface = wlc_resource_from_wl_surface_resource(surface); config->wl_surface_res = surface; - list_add(desktop_shell.backgrounds, config); + list_add(desktop_shell.backgrounds, &config); wl_resource_set_destructor(surface, background_surface_destructor); arrange_windows(swayc_by_handle(output), -1, -1); wlc_output_schedule_render(config->output); @@ -211,7 +211,7 @@ static void set_lock_surface(struct wl_client *client, struct wl_resource *resou desktop_shell.is_locked = true; input_init(); arrange_windows(workspace, -1, -1); - list_add(desktop_shell.lock_surfaces, surface); + list_add(desktop_shell.lock_surfaces, &surface); wl_resource_set_destructor(surface, lock_surface_destructor); } else { sway_log(L_ERROR, "Attempted to set lock surface to non-view"); diff --git a/sway/handlers.c b/sway/handlers.c index b7936942f..f0b714b92 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -1008,7 +1008,7 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w swayc_t *item = *(swayc_t **)list_get(pointer->parent->floating, i); if (item == pointer) { list_delete(pointer->parent->floating, i); - list_add(pointer->parent->floating, pointer); + list_add(pointer->parent->floating, &pointer); break; } } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 41951bbae..4464c5e34 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -286,7 +286,7 @@ void ipc_get_pixels(wlc_handle output) { for (size_t i = 0; i < ipc_get_pixel_requests->length; ++i) { req = *(struct get_pixels_request **)list_get(ipc_get_pixel_requests, i); if (req->output != output) { - list_add(unhandled, req); + list_add(unhandled, &req); continue; } diff --git a/sway/layout.c b/sway/layout.c index b2eab662b..b2d5cb301 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -63,7 +63,7 @@ int index_child(const swayc_t *child) { void add_child(swayc_t *parent, swayc_t *child) { sway_log(L_DEBUG, "Adding %p (%d, %fx%f) to %p (%d, %fx%f)", child, child->type, child->width, child->height, parent, parent->type, parent->width, parent->height); - list_add(parent->children, child); + list_add(parent->children, &child); child->parent = parent; // set focus for this container if (!parent->focused) { @@ -140,7 +140,7 @@ void add_floating(swayc_t *ws, swayc_t *child) { if (!sway_assert(ws->type == C_WORKSPACE, "Must be of workspace type")) { return; } - list_add(ws->floating, child); + list_add(ws->floating, &child); child->parent = ws; child->is_floating = true; if (!ws->focused) { @@ -154,19 +154,19 @@ swayc_t *add_sibling(swayc_t *fixed, swayc_t *active) { if (fixed->is_floating) { if (active->is_floating) { int i = index_child(fixed); - list_insert(parent->floating, i + 1, active); + list_insert(parent->floating, i + 1, &active); } else { - list_add(parent->children, active); + list_add(parent->children, &active); } } else { if (active->is_floating) { - list_add(parent->floating, active); + list_add(parent->floating, &active); } else { int i = index_child(fixed); if (is_auto_layout(parent->layout)) { - list_add(parent->children, active); + list_add(parent->children, &active); } else { - list_insert(parent->children, i + 1, active); + list_insert(parent->children, i + 1, &active); } } } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index ebed88586..ec3f81757 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -315,7 +315,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { // add bar to the output struct output *bar_output = new_output(name); bar_output->idx = i; - list_add(bar->outputs, bar_output); + list_add(bar->outputs, &bar_output); } free(res); json_object_put(outputs); From a4eeedfa342936203effa599dbbd54d0c9537dbf Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 13:37:17 +1200 Subject: [PATCH 11/17] Fixed lsearch related arguments. --- common/list.c | 7 ++++++- common/stringop.c | 4 ++++ include/list.h | 9 +++++++++ include/stringop.h | 4 ++++ sway/commands/assign.c | 2 +- sway/commands/bar/bindsym.c | 2 +- sway/commands/bind.c | 4 ++-- sway/commands/for_window.c | 2 +- sway/commands/mark.c | 6 +++--- sway/commands/no_focus.c | 2 +- sway/commands/output.c | 2 +- sway/commands/unmark.c | 2 +- sway/commands/workspace.c | 2 +- sway/config.c | 23 ++++++++++++----------- sway/criteria.c | 20 +++++++++++--------- 15 files changed, 58 insertions(+), 33 deletions(-) diff --git a/common/list.c b/common/list.c index feaf8abf7..16956632f 100644 --- a/common/list.c +++ b/common/list.c @@ -152,6 +152,11 @@ void *list_get(list_t *list, size_t index) { return &array[index]; } +void *list_getp(list_t *list, size_t index) { + void **elem = list_get(list, index); + return elem ? *elem : NULL; +} + void list_qsort(list_t *list, int compare(const void *, const void *)) { if (!sway_assert(list && compare, "Invalid argument")) { return; @@ -211,7 +216,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { - if (compare(&key, &array[i]) == 0) { + if (compare(key, &array[i]) == 0) { if (ret) { memcpy(ret, &array[i], size); } diff --git a/common/stringop.c b/common/stringop.c index 167562256..7a7696516 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -386,3 +386,7 @@ char *argsep(char **stringp, const char *delim) { found: return start; } + +int strcmp_ptr(const void *a, const void *b) { + return strcmp(*(char **)a, *(char **)b); +} diff --git a/include/list.h b/include/list.h index ee9143f71..5ab15c227 100644 --- a/include/list.h +++ b/include/list.h @@ -89,6 +89,15 @@ void list_swap(list_t *list, size_t i1, size_t i2); */ void *list_get(list_t *list, size_t index); +/* + * Gets an element of a list and dereferences it. + * For example, if you have a list of char *, this function + * will return a char * instead of a char **, unlike list_get. + * index must be less than the length of the list. + * list must be a list of pointers. + */ +void *list_getp(list_t *list, size_t index); + /* * Sorts the list using the stdlib qsort() function. */ diff --git a/include/stringop.h b/include/stringop.h index 7c29a745e..85659b0ad 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -14,6 +14,10 @@ char *strip_whitespace(char *str); char *strip_comments(char *str); void strip_quotes(char *str); +// strcmp that dereferences args first. +// Designed to be taken by list_lsearch and such +int strcmp_ptr(const void *a, const void *b); + // strcmp that also handles null pointers. int lenient_strcmp(char *a, char *b); diff --git a/sway/commands/assign.c b/sway/commands/assign.c index baeebc9c7..10b5e5d78 100644 --- a/sway/commands/assign.c +++ b/sway/commands/assign.c @@ -46,7 +46,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->criteria, criteria_cmp, crit, NULL) != -1) { + } else if (list_lsearch(config->criteria, criteria_cmp, &crit, NULL) != -1) { sway_log(L_DEBUG, "assign: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index 83c7ec38c..912286faa 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c @@ -34,7 +34,7 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { struct bar_config *bar = config->current_bar; struct sway_mouse_binding *dup; - ssize_t i = list_lsearch(bar->bindings, sway_mouse_binding_cmp_buttons, binding, &dup); + ssize_t i = list_lsearch(bar->bindings, sway_mouse_binding_cmp_buttons, &binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); free_sway_mouse_binding(dup); diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 2f504bc34..adfd05d75 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -81,7 +81,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { struct sway_mode *mode = config->current_mode; struct sway_binding *dup; - ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, binding, &dup); + ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, &binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]); free_sway_binding(dup); @@ -152,7 +152,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { struct sway_mode *mode = config->current_mode; struct sway_binding *dup; - ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, binding, &dup); + ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, &binding, &dup); if (i > -1) { if (dup->bindcode) { sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]); diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c index 49f2daa4a..d09490dca 100644 --- a/sway/commands/for_window.c +++ b/sway/commands/for_window.c @@ -30,7 +30,7 @@ struct cmd_results *cmd_for_window(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->criteria, criteria_cmp, crit, NULL) != -1) { + } else if (list_lsearch(config->criteria, criteria_cmp, &crit, NULL) != -1) { sway_log(L_DEBUG, "for_window: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/mark.c b/sway/commands/mark.c index de51604c9..66452a62c 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -12,7 +12,7 @@ static void find_marks_callback(swayc_t *container, void *_mark) { return; } - ssize_t index = list_lsearch(container->marks, (int (*)(const void *, const void *))strcmp, mark, NULL); + ssize_t index = list_lsearch(container->marks, strcmp_ptr, &mark, NULL); if (index != -1) { list_delete(container->marks, index); } @@ -51,7 +51,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { if (add) { ssize_t index; char *item; - if ((index = list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { + if ((index = list_lsearch(view->marks, strcmp_ptr, &mark, &item)) != -1) { if (toggle) { free(item); list_delete(view->marks, index); @@ -66,7 +66,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { list_add(view->marks, &mark); } } else { - if (toggle && list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, NULL) != -1) { + if (toggle && list_lsearch(view->marks, strcmp_ptr, &mark, NULL) != -1) { // Delete the list list_foreach(view->marks, list_elem_free); list_free(view->marks); diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c index dcecc3c4e..adcbbbf9d 100644 --- a/sway/commands/no_focus.c +++ b/sway/commands/no_focus.c @@ -30,7 +30,7 @@ struct cmd_results *cmd_no_focus(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->no_focus, criteria_cmp, crit, NULL) != -1) { + } else if (list_lsearch(config->no_focus, criteria_cmp, &crit, NULL) != -1) { sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/output.c b/sway/commands/output.c index d9ab5d0f2..1a9353974 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -157,7 +157,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { } struct output_config *oc; - ssize_t i = list_lsearch(config->output_configs, output_name_cmp, name, &oc); + ssize_t i = list_lsearch(config->output_configs, output_name_cmp, &name, &oc); if (i >= 0) { // merge existing config merge_output_config(oc, output); diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index 35d46a744..30c001173 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -12,7 +12,7 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { char *mark = join_args(argv, argc); char *item; ssize_t index; - if ((index = list_lsearch(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { + if ((index = list_lsearch(view->marks, strcmp_ptr, &mark, &item)) != -1) { free(item); list_delete(view->marks, index); diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index f79ec9443..34d14094d 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -36,7 +36,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { wso->output = strdup(argv[output_location + 1]); ssize_t i = -1; struct workspace_output *old; - if ((i = list_lsearch(config->workspace_outputs, workspace_output_cmp_workspace, wso, &old)) != -1) { + if ((i = list_lsearch(config->workspace_outputs, workspace_output_cmp_workspace, &wso, &old)) != -1) { free(old); // workspaces can only be assigned to a single output list_delete(config->workspace_outputs, i); } diff --git a/sway/config.c b/sway/config.c index 12d05dae0..d737ae65a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -395,9 +395,9 @@ cleanup: sway_abort("Unable to allocate config structures"); } -static int compare_modifiers(const void *left, const void *right) { - uint32_t a = *(uint32_t *)left; - uint32_t b = *(uint32_t *)right; +static int compare_modifiers(const void *key, const void *item) { + uint32_t a = **(uint32_t **)item; + uint32_t b = *(uint32_t *)key; return a - b; } @@ -831,11 +831,11 @@ int input_identifier_cmp(const void *key, const void *item) { return strcmp((*ic)->identifier, identifier); } -int output_name_cmp(const void *item, const void *data) { - const struct output_config *output = item; - const char *name = data; +int output_name_cmp(const void *key, const void *item) { + const struct output_config *const *output = item; + const char *const *name = key; - return strcmp(output->name, name); + return strcmp((*output)->name, *name); } void merge_input_config(struct input_config *dst, struct input_config *src) { @@ -1136,7 +1136,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { if (!oc || !oc->background) { // Look for a * config for background struct output_config *item; - if (list_lsearch(config->output_configs, output_name_cmp, "*", &item) != -1) { + const char *str = "*"; + if (list_lsearch(config->output_configs, output_name_cmp, &str, &item) != -1) { oc = item; } else { oc = NULL; @@ -1224,9 +1225,9 @@ char *do_var_replacement(char *str) { // the naming is intentional (albeit long): a workspace_output_cmp function // would compare two structs in full, while this method only compares the // workspace. -int workspace_output_cmp_workspace(const void *a, const void *b) { - const struct workspace_output *wsa = a, *wsb = b; - return lenient_strcmp(wsa->workspace, wsb->workspace); +int workspace_output_cmp_workspace(const void *key, const void *item) { + const struct workspace_output *const *wsa = item, *const *wsb = key; + return lenient_strcmp((*wsa)->workspace, (*wsb)->workspace); } int sway_binding_cmp_keys(const void *key, const void *item) { diff --git a/sway/criteria.c b/sway/criteria.c index 83a4993ec..f8a8d5097 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -240,8 +240,8 @@ ect_cleanup: static int regex_cmp(const void *key, const void *item) { const pcre *regex = key; - const char *str = item; - return pcre_exec(regex, NULL, str, strlen(str), 0, 0, NULL, 0); + const char *const *str = item; + return pcre_exec(regex, NULL, *str, strlen(*str), 0, 0, NULL, 0); } // test a single view if it matches list of criteria tokens (all of them). @@ -261,14 +261,15 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->class && strcmp(cont->class, focused->class) == 0) { matches++; } - } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) { + } else if (crit->regex && regex_cmp(crit->regex, &cont->class) == 0) { matches++; } break; case CRIT_CON_MARK: if (crit->regex && cont->marks && (list_lsearch(cont->marks, regex_cmp, crit->regex, NULL) != -1)) { // Make sure it isn't matching the NUL string - if ((strcmp(crit->raw, "") == 0) == (list_lsearch(cont->marks, (int (*)(const void *, const void *))strcmp, "", NULL) != -1)) { + const char *nul = ""; + if ((strcmp(crit->raw, "") == 0) == (list_lsearch(cont->marks, strcmp_ptr, &nul, NULL) != -1)) { ++matches; } } @@ -276,7 +277,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { case CRIT_ID: if (!cont->app_id) { // ignore - } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) { + } else if (crit->regex && regex_cmp(crit->regex, &cont->app_id) == 0) { matches++; } break; @@ -288,7 +289,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { matches++; } - } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) { + } else if (crit->regex && regex_cmp(crit->regex, &cont->instance) == 0) { matches++; } break; @@ -300,7 +301,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->name && strcmp(cont->name, focused->name) == 0) { matches++; } - } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { + } else if (crit->regex && regex_cmp(crit->regex, &cont->name) == 0) { matches++; } break; @@ -320,7 +321,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { matches++; } - } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) { + } else if (crit->regex && regex_cmp(crit->regex, &cont_ws->name) == 0) { matches++; } break; @@ -340,7 +341,8 @@ int criteria_cmp(const void *key, const void *item) { } else if (!key) { return 1; } - const struct criteria *crit_a = item, *crit_b = key; + const struct criteria *crit_a = *(struct criteria **)item; + const struct criteria *crit_b = *(struct criteria **)key; int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); if (cmp != 0) { return cmp; From 508200e83d082f99d352a8a4fb10676cbabdb04e Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 13:52:45 +1200 Subject: [PATCH 12/17] Changed to used list_getp --- sway/border.c | 10 ++-- sway/commands.c | 8 +-- sway/commands/bar.c | 2 +- sway/commands/bar/hidden_state.c | 2 +- sway/commands/bar/id.c | 2 +- sway/commands/bar/mode.c | 2 +- sway/commands/bar/modifier.c | 2 +- sway/commands/bar/output.c | 4 +- sway/commands/bind.c | 4 +- sway/commands/floating_mod.c | 2 +- sway/commands/focus.c | 12 ++-- sway/commands/gaps.c | 4 +- sway/commands/layout.c | 2 +- sway/commands/mode.c | 2 +- sway/commands/move.c | 2 +- sway/commands/output.c | 2 +- sway/commands/resize.c | 10 ++-- sway/commands/scratchpad.c | 2 +- sway/commands/set.c | 2 +- sway/config.c | 64 +++++++++++----------- sway/container.c | 54 +++++++++--------- sway/criteria.c | 8 +-- sway/debug_log.c | 4 +- sway/extensions.c | 8 +-- sway/focus.c | 2 +- sway/handlers.c | 54 +++++++++--------- sway/ipc-json.c | 10 ++-- sway/ipc-server.c | 18 +++--- sway/layout.c | 94 ++++++++++++++++---------------- sway/output.c | 10 ++-- sway/security.c | 12 ++-- sway/workspace.c | 26 ++++----- swaybar/bar.c | 22 ++++---- swaybar/ipc.c | 8 +-- swaybar/render.c | 4 +- swaybar/status_line.c | 4 +- swaybg/main.c | 8 +-- swaylock/main.c | 12 ++-- 38 files changed, 249 insertions(+), 249 deletions(-) diff --git a/sway/border.c b/sway/border.c index 73516381a..9f8afdb32 100644 --- a/sway/border.c +++ b/sway/border.c @@ -198,7 +198,7 @@ static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, int total_len = 0; for(int i = view->marks->length - 1; i >= 0; --i) { - char *mark = *(char **)list_get(view->marks, i); + char *mark = list_getp(view->marks, i); if (*mark != '_') { int width, height; get_text_size(cr, config->font, &width, &height, 1, false, "[%s]", mark); @@ -273,7 +273,7 @@ static char *generate_container_title(swayc_t *container) { for (size_t i = 0; i < container->children->length; ++i) { prev_name = name; - swayc_t* child = *(swayc_t **)list_get(container->children, i); + swayc_t* child = list_getp(container->children, i); const char *title = NULL; if (child->type == C_VIEW) { title = child->app_id ? child->app_id : @@ -330,7 +330,7 @@ void update_tabbed_stacked_titlebars(swayc_t *c, cairo_t *cr, struct wlc_geometr } for (size_t i = 0; i < c->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(c->children, i); + swayc_t *child = list_getp(c->children, i); update_tabbed_stacked_titlebars(child, cr, g, focused, focused_inactive); } } else { @@ -401,7 +401,7 @@ static void update_view_border(swayc_t *view) { // generate container titles for (size_t i = 0; i < p->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(p->children, i); + swayc_t *child = list_getp(p->children, i); if (child->type == C_CONTAINER) { generate_container_title(child); } @@ -469,7 +469,7 @@ void update_container_border(swayc_t *container) { return; } else { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); update_container_border(item); } } diff --git a/sway/commands.c b/sway/commands.c index ffade4cb5..e4ceafc81 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -121,7 +121,7 @@ void input_cmd_apply(struct input_config *input) { // will be applied during normal "new input" event from wlc. struct libinput_device *device = NULL; for (size_t i = 0; i < input_devices->length; ++i) { - device = *(struct libinput_device **)list_get(input_devices, i); + device = list_getp(input_devices, i); char* dev_identifier = libinput_dev_unique_id(device); if (!dev_identifier) { break; @@ -138,7 +138,7 @@ void input_cmd_apply(struct input_config *input) { void remove_view_from_scratchpad(swayc_t *view) { for (size_t i = 0; i < scratchpad->length; i++) { - swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + swayc_t *item = list_getp(scratchpad, i); if (item == view) { if (sp_index == 0) { sp_index = scratchpad->length - 1; @@ -454,7 +454,7 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { } else if (containers->length == 0) { break; } else { - current_container = *(swayc_t **)list_get(containers, i); + current_container = list_getp(containers, i); } sway_log(L_INFO, "Running on container '%s'", current_container->name); @@ -594,7 +594,7 @@ struct cmd_results *config_commands_command(char *exec) { struct command_policy *policy = NULL; for (size_t i = 0; i < config->command_policies->length; ++i) { - struct command_policy *p = *(struct command_policy **)list_get(config->command_policies, i); + struct command_policy *p = list_getp(config->command_policies, i); if (strcmp(p->command, cmd) == 0) { policy = p; break; diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 611ca9ac7..29ac878e2 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c @@ -39,7 +39,7 @@ struct cmd_results *cmd_bar(int argc, char **argv) { // set bar id for (size_t i = 0; i < config->bars->length; ++i) { - if (bar == *(struct bar_config **)list_get(config->bars, i)) { + if (bar == list_getp(config->bars, i)) { const int len = 5 + numlen(i); // "bar-" + i + \0 bar->id = malloc(len * sizeof(char)); if (bar->id) { diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c index dfc10ef3e..cdf0e1cf1 100644 --- a/sway/commands/bar/hidden_state.c +++ b/sway/commands/bar/hidden_state.c @@ -59,7 +59,7 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { } for (size_t i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); + struct bar_config *bar = list_getp(config->bars, i); if (id && strcmp(id, bar->id) == 0) { return bar_set_hidden_state(bar, state); } diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c index 0f686225b..7b17ca725 100644 --- a/sway/commands/bar/id.c +++ b/sway/commands/bar/id.c @@ -14,7 +14,7 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) { // check if id is used by a previously defined bar for (size_t i = 0; i < config->bars->length; ++i) { - struct bar_config *find = *(struct bar_config **)list_get(config->bars, i); + struct bar_config *find = list_getp(config->bars, i); if (strcmp(name, find->id) == 0 && config->current_bar != find) { return cmd_results_new(CMD_FAILURE, "id", "Id '%s' already defined for another bar. Id unchanged (%s).", diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c index 39fe61f7b..889e9bf98 100644 --- a/sway/commands/bar/mode.c +++ b/sway/commands/bar/mode.c @@ -64,7 +64,7 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) { } for (size_t i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); + struct bar_config *bar = list_getp(config->bars, i); if (id && strcmp(id, bar->id) == 0) { return bar_set_mode(bar, mode); } diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c index 7abec0f1e..89c89d90b 100644 --- a/sway/commands/bar/modifier.c +++ b/sway/commands/bar/modifier.c @@ -19,7 +19,7 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) { list_t *split = split_string(argv[0], "+"); for (size_t i = 0; i < split->length; ++i) { uint32_t tmp_mod; - char *item = *(char **)list_get(split, i); + char *item = list_getp(split, i); if ((tmp_mod = get_modifier_mask_by_name(item)) > 0) { mod |= tmp_mod; continue; diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c index 9aa938810..bb020eabc 100644 --- a/sway/commands/bar/output.c +++ b/sway/commands/bar/output.c @@ -25,7 +25,7 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { if (strcmp("*", output) == 0) { // remove all previous defined outputs and replace with '*' for (size_t i = 0; i < outputs->length; ++i) { - char *item = *(char **)list_get(outputs, i); + char *item = list_getp(outputs, i); free(item); list_delete(outputs, i); } @@ -33,7 +33,7 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) { // only add output if not already defined with either the same // name or as '*' for (size_t i = 0; i < outputs->length; ++i) { - const char *find = *(char **)list_get(outputs, i); + const char *find = list_getp(outputs, i); if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { add_output = 0; break; diff --git a/sway/commands/bind.c b/sway/commands/bind.c index adfd05d75..cecb72dbe 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -47,7 +47,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { for (size_t i = 0; i < split->length; ++i) { // Check for a modifier key uint32_t mod; - char *item = *(char **)list_get(split, i); + char *item = list_getp(split, i); if ((mod = get_modifier_mask_by_name(item)) > 0) { binding->modifiers |= mod; continue; @@ -131,7 +131,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { for (size_t i = 0; i < split->length; ++i) { // Check for a modifier key uint32_t mod; - char *item = *(char **)list_get(split, i); + char *item = list_getp(split, i); if ((mod = get_modifier_mask_by_name(item)) > 0) { binding->modifiers |= mod; continue; diff --git a/sway/commands/floating_mod.c b/sway/commands/floating_mod.c index c34af0ac9..aec85a1e4 100644 --- a/sway/commands/floating_mod.c +++ b/sway/commands/floating_mod.c @@ -17,7 +17,7 @@ struct cmd_results *cmd_floating_mod(int argc, char **argv) { // set modifier keys for (size_t i = 0; i < split->length; ++i) { - config->floating_mod |= get_modifier_mask_by_name(*(char **)list_get(split, i)); + config->floating_mod |= get_modifier_mask_by_name(list_getp(split, i)); } free_flat_list(split); if (!config->floating_mod) { diff --git a/sway/commands/focus.c b/sway/commands/focus.c index dfdb8df1f..fc6397216 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -60,17 +60,17 @@ struct cmd_results *cmd_focus(int argc, char **argv) { if (focused->is_floating) { if (workspace->children->length > 0) { for (size_t i = 0;i < workspace->floating->length; i++) { - swayc_t *item = *(swayc_t **)list_get(workspace->floating, i); + swayc_t *item = list_getp(workspace->floating, i); if (item == focused) { floating_toggled_index = i; break; } } if (workspace->children->length > tiled_toggled_index) { - swayc_t *item = *(swayc_t **)list_get(workspace->children, tiled_toggled_index); + swayc_t *item = list_getp(workspace->children, tiled_toggled_index); set_focused_container(get_focused_view(item)); } else { - swayc_t *item = *(swayc_t **)list_get(workspace->children, 0); + swayc_t *item = list_getp(workspace->children, 0); set_focused_container(get_focused_view(item)); tiled_toggled_index = 0; } @@ -78,17 +78,17 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } else { if (workspace->floating->length > 0) { for (size_t i = 0;i < workspace->children->length; i++) { - swayc_t *item = *(swayc_t **)list_get(workspace->children, i); + swayc_t *item = list_getp(workspace->children, i); if (item == focused) { tiled_toggled_index = i; break; } } if (workspace->floating->length > floating_toggled_index) { - swayc_t *floating = *(swayc_t **)list_get(workspace->floating, floating_toggled_index); + swayc_t *floating = list_getp(workspace->floating, floating_toggled_index); set_focused_container(get_focused_view(floating)); } else { - swayc_t *floating = *(swayc_t **)list_get(workspace->floating, workspace->floating->length - 1); + swayc_t *floating = list_getp(workspace->floating, workspace->floating->length - 1); set_focused_container(get_focused_view(floating)); tiled_toggled_index = workspace->floating->length - 1; } diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c index 5c554f7e8..1f5b4f40c 100644 --- a/sway/commands/gaps.c +++ b/sway/commands/gaps.c @@ -134,9 +134,9 @@ struct cmd_results *cmd_gaps(int argc, char **argv) { } else if (inout == OUTER) { //resize all workspace. for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *op = *(swayc_t **)list_get(root_container.children, i); + swayc_t *op = list_getp(root_container.children, i); for (size_t j = 0; j < op->children->length; ++j) { - swayc_t *ws = *(swayc_t **)list_get(op->children, j); + swayc_t *ws = list_getp(op->children, j); if (method == SET) { ws->gaps = amount; } else if ((ws->gaps += amount) < 0) { diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 83f87df5a..b00d175ad 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -156,7 +156,7 @@ static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char ** for (int i = container->nb_master; i >= 0 && (size_t)i < container->children->length && i != (int)container->nb_master + inc;) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); item->height = -1; item->width = -1; i += inc > 0 ? 1 : -1; diff --git a/sway/commands/mode.c b/sway/commands/mode.c index 2d9de6c56..b8a1ad0b1 100644 --- a/sway/commands/mode.c +++ b/sway/commands/mode.c @@ -23,7 +23,7 @@ struct cmd_results *cmd_mode(int argc, char **argv) { struct sway_mode *mode = NULL; // Find mode for (size_t i = 0; i < config->modes->length; ++i) { - struct sway_mode *find = *(struct sway_mode **)list_get(config->modes, i); + struct sway_mode *find = list_getp(config->modes, i); if (strcasecmp(find->name, mode_name) == 0) { mode = find; break; diff --git a/sway/commands/move.c b/sway/commands/move.c index f4bf38946..f3479d3c3 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -127,7 +127,7 @@ struct cmd_results *cmd_move(int argc, char **argv) { } swayc_t *view = current_container; for (size_t i = 0; i < scratchpad->length; i++) { - swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + swayc_t *item = list_getp(scratchpad, i); if (item == view) { hide_view_in_scratchpad(view); sp_view = NULL; diff --git a/sway/commands/output.c b/sway/commands/output.c index 1a9353974..b28233e07 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -178,7 +178,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { // will be applied during normal "new output" event from wlc. swayc_t *cont = NULL; for (size_t i = 0; i < root_container.children->length; ++i) { - cont = *(swayc_t **)list_get(root_container.children, i); + cont = list_getp(root_container.children, i); if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { apply_output_config(output, cont); diff --git a/sway/commands/resize.c b/sway/commands/resize.c index c86487dc9..12ec87162 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -118,12 +118,12 @@ static bool resize_tiled(int amount, bool use_width) { // 2. Ensure that the resize operation will not make one of the resized containers drop // below the "sane" size threshold. bool valid = true; - swayc_t *focused = *(swayc_t **)list_get(parent->children, idx_focused); + swayc_t *focused = list_getp(parent->children, idx_focused); size_t start = use_major ? 0 : (size_t)auto_group_start_index(parent, idx_focused); size_t end = use_major ? parent->children->length : (size_t)auto_group_end_index(parent, idx_focused); sway_log(L_DEBUG, "Check children of container %p [%zu,%zu[", container, start, end); for (size_t i = start; i < end; ) { - swayc_t *sibling = *(swayc_t **)list_get(parent->children, i); + swayc_t *sibling = list_getp(parent->children, i); double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; @@ -149,7 +149,7 @@ static bool resize_tiled(int amount, bool use_width) { if (valid) { for (size_t i = start; i < end; ) { int next_i = use_major ? (size_t)auto_group_end_index(parent, i) : (i + 1); - swayc_t *sibling = *(swayc_t **)list_get(parent->children, i); + swayc_t *sibling = list_getp(parent->children, i); double pixels = amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y; bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y; @@ -162,7 +162,7 @@ static bool resize_tiled(int amount, bool use_width) { sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after"); if (use_major) { for (int j = i; j < next_i; ++j) { - swayc_t *item = *(swayc_t **)list_get(parent->children, j); + swayc_t *item = list_getp(parent->children, j); recursive_resize(item, pixels, use_width ? (is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) : @@ -177,7 +177,7 @@ static bool resize_tiled(int amount, bool use_width) { } else { if (use_major) { for (int j = i; j < next_i; ++j) { - swayc_t *item = *(swayc_t **)list_get(parent->children, j); + swayc_t *item = list_getp(parent->children, j); recursive_resize(item, pixels / 2, use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP); recursive_resize(item, pixels / 2, diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c index ba04f5dc3..1b5729ef8 100644 --- a/sway/commands/scratchpad.c +++ b/sway/commands/scratchpad.c @@ -10,7 +10,7 @@ static swayc_t *fetch_view_from_scratchpad() { if ((size_t)sp_index >= scratchpad->length) { sp_index = 0; } - swayc_t *view = *(swayc_t **)list_get(scratchpad, sp_index++); + swayc_t *view = list_getp(scratchpad, sp_index++); if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { wlc_view_set_output(view->handle, swayc_active_output()->handle); diff --git a/sway/commands/set.c b/sway/commands/set.c index 670004e88..41f881ef2 100644 --- a/sway/commands/set.c +++ b/sway/commands/set.c @@ -38,7 +38,7 @@ struct cmd_results *cmd_set(int argc, char **argv) { struct sway_variable *var = NULL; // Find old variable if it exists for (size_t i = 0; i < config->symbols->length; ++i) { - var = *(struct sway_variable **)list_get(config->symbols, i); + var = list_getp(config->symbols, i); if (strcmp(var->name, argv[0]) == 0) { break; } diff --git a/sway/config.c b/sway/config.c index d737ae65a..89ab80738 100644 --- a/sway/config.c +++ b/sway/config.c @@ -56,7 +56,7 @@ static void free_mode(struct sway_mode *mode) { } free(mode->name); for (size_t i = 0; mode->bindings && i < mode->bindings->length; ++i) { - free_binding(*(struct sway_binding **)list_get(mode->bindings, i)); + free_binding(list_getp(mode->bindings, i)); } list_free(mode->bindings); free(mode); @@ -72,7 +72,7 @@ static void free_bar(struct bar_config *bar) { free(bar->font); free(bar->separator_symbol); for (size_t i = 0; bar->bindings && i < bar->bindings->length; ++i) { - free_sway_mouse_binding(*(struct sway_mouse_binding **)list_get(bar->bindings, i)); + free_sway_mouse_binding(list_getp(bar->bindings, i)); } list_free(bar->bindings); @@ -143,7 +143,7 @@ static void pid_workspace_cleanup() { // work backwards through list and remove any entries // older than PID_WORKSPACE_TIMEOUT for (ssize_t i = config->pid_workspaces->length - 1; i > -1; i--) { - pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); + pw = list_getp(config->pid_workspaces, i); if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { free_pid_workspace(pw); @@ -173,7 +173,7 @@ void pid_workspace_add(struct pid_workspace *pw) { // work backwards through list and delete any entries that // have the same pid as that in our new pid_workspace for (ssize_t i = config->pid_workspaces->length - 1; i > -1; i--) { - list_pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); + list_pw = list_getp(config->pid_workspaces, i); if (pw->pid == list_pw->pid) { free_pid_workspace(list_pw); @@ -216,59 +216,59 @@ void free_config(struct sway_config *config) { } size_t i; for (i = 0; config->symbols && i < config->symbols->length; ++i) { - free_variable(*(struct sway_variable **)list_get(config->symbols, i)); + free_variable(list_getp(config->symbols, i)); } list_free(config->symbols); for (i = 0; config->modes && i < config->modes->length; ++i) { - free_mode(*(struct sway_mode **)list_get(config->modes, i)); + free_mode(list_getp(config->modes, i)); } list_free(config->modes); for (i = 0; config->bars && i < config->bars->length; ++i) { - free_bar(*(struct bar_config **)list_get(config->bars, i)); + free_bar(list_getp(config->bars, i)); } list_free(config->bars); free_flat_list(config->cmd_queue); for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { - free_workspace_output(*(struct workspace_output **)list_get(config->workspace_outputs, i)); + free_workspace_output(list_getp(config->workspace_outputs, i)); } list_free(config->workspace_outputs); for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { - free_pid_workspace(*(struct pid_workspace **)list_get(config->pid_workspaces, i)); + free_pid_workspace(list_getp(config->pid_workspaces, i)); } list_free(config->pid_workspaces); for (i = 0; config->criteria && i < config->criteria->length; ++i) { - free_criteria(*(struct criteria **)list_get(config->criteria, i)); + free_criteria(list_getp(config->criteria, i)); } list_free(config->criteria); for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { - free_criteria(*(struct criteria **)list_get(config->no_focus, i)); + free_criteria(list_getp(config->no_focus, i)); } list_free(config->no_focus); for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { - free_input_config(*(struct input_config **)list_get(config->input_configs, i)); + free_input_config(list_getp(config->input_configs, i)); } list_free(config->input_configs); for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { - free_output_config(*(struct output_config **)list_get(config->output_configs, i)); + free_output_config(list_getp(config->output_configs, i)); } list_free(config->output_configs); for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { - free_command_policy(*(struct command_policy **)list_get(config->command_policies, i)); + free_command_policy(list_getp(config->command_policies, i)); } list_free(config->command_policies); for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { - free_feature_policy(*(struct feature_policy **)list_get(config->feature_policies, i)); + free_feature_policy(list_getp(config->feature_policies, i)); } list_free(config->feature_policies); @@ -409,7 +409,7 @@ void update_active_bar_modifiers() { } for (size_t i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); + struct bar_config *bar = list_getp(config->bars, i); if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { if (list_lsearch(config->active_bar_modifiers, compare_modifiers, &bar->modifier, NULL) < 0) { list_add(config->active_bar_modifiers, &bar->modifier); @@ -549,7 +549,7 @@ bool load_main_config(const char *file, bool is_active) { list_qsort(secconfigs, qstrcmp); for (size_t i = 0; i < secconfigs->length; ++i) { - char *_path = *(char **)list_get(secconfigs, i); + char *_path = list_getp(secconfigs, i); if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) { sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path); success = false; @@ -605,7 +605,7 @@ static bool load_include_config(const char *path, const char *parent_dir, struct // check if config has already been included for (size_t j = 0; j < config->config_chain->length; ++j) { - char *old_path = *(char **)list_get(config->config_chain, j); + char *old_path = list_getp(config->config_chain, j); if (strcmp(real_path, old_path) == 0) { sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path); free(real_path); @@ -774,7 +774,7 @@ bool read_config(FILE *file, struct sway_config *config) { switch(block) { case CMD_BLOCK_MODE: sway_log(L_DEBUG, "End of mode block"); - config->current_mode = *(struct sway_mode **)list_get(config->modes, 0); + config->current_mode = list_getp(config->modes, 0); block = CMD_BLOCK_END; break; @@ -1000,7 +1000,7 @@ void terminate_swaybg(pid_t pid) { static bool active_output(const char *name) { for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *cont = *(swayc_t **)list_get(root_container.children, i); + swayc_t *cont = list_getp(root_container.children, i); if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { return true; } @@ -1014,11 +1014,11 @@ void load_swaybars() { list_t *bars = list_new(sizeof(struct bar_config *), 0); struct bar_config *bar = NULL; for (size_t i = 0; i < config->bars->length; ++i) { - bar = *(struct bar_config **)list_get(config->bars, i); + bar = list_getp(config->bars, i); bool apply = false; if (bar->outputs) { for (size_t j = 0; j < bar->outputs->length; ++j) { - char *o = *(char **)list_get(bar->outputs, j); + char *o = list_getp(bar->outputs, j); if (!strcmp(o, "*") || active_output(o)) { apply = true; break; @@ -1033,7 +1033,7 @@ void load_swaybars() { } for (size_t i = 0; i < bars->length; ++i) { - bar = *(struct bar_config **)list_get(bars, i); + bar = list_getp(bars, i); if (bar->pid != 0) { terminate_swaybar(bar->pid); } @@ -1123,7 +1123,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } else { int x = 0; for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (c->type == C_OUTPUT) { if (c->width + c->x > x) { x = c->width + c->x; @@ -1146,7 +1146,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { size_t output_i; for (output_i = 0; output_i < root_container.children->length; ++output_i) { - swayc_t *item = *(swayc_t **)list_get(root_container.children, output_i); + swayc_t *item = list_getp(root_container.children, output_i); if (item == output) { break; } @@ -1192,7 +1192,7 @@ char *do_var_replacement(char *str) { size_t i; // Find matching variable for (i = 0; i < config->symbols->length; ++i) { - struct sway_variable *var = *(struct sway_variable **)list_get(config->symbols, i); + struct sway_variable *var = list_getp(config->symbols, i); int vnlen = strlen(var->name); if (strncmp(find, var->name, vnlen) == 0) { int vvlen = strlen(var->value); @@ -1255,15 +1255,15 @@ int sway_binding_cmp_keys(const void *key, const void *item) { } struct wlc_modifiers no_mods = { 0, 0 }; for (size_t i = 0; i < binda->keys->length; i++) { - xkb_keysym_t ka = **(xkb_keysym_t **)list_get(binda->keys, i), - kb = **(xkb_keysym_t **)list_get(bindb->keys, i); + xkb_keysym_t ka = *(xkb_keysym_t *)list_getp(binda->keys, i), + kb = *(xkb_keysym_t *)list_getp(bindb->keys, i); if (binda->bindcode) { - uint32_t *keycode = *(uint32_t **)list_get(binda->keys, i); + uint32_t *keycode = list_getp(binda->keys, i); ka = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); } if (bindb->bindcode) { - uint32_t *keycode = *(uint32_t **)list_get(bindb->keys, i); + uint32_t *keycode = list_getp(bindb->keys, i); kb = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); } @@ -1293,7 +1293,7 @@ int sway_binding_cmp_qsort(const void *a, const void *b) { void free_sway_binding(struct sway_binding *binding) { if (binding->keys) { for (size_t i = 0; i < binding->keys->length; i++) { - free(*(void **)list_get(binding->keys, i)); + free(list_getp(binding->keys, i)); } list_free(binding->keys); } @@ -1351,7 +1351,7 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) { free_sway_binding(new_sb); return NULL; } - *key = **(xkb_keysym_t **)list_get(sb->keys, i); + *key = *(xkb_keysym_t *)list_getp(sb->keys, i); list_add(new_sb->keys, &key); } diff --git a/sway/container.c b/sway/container.c index 6767b5034..a82c01764 100644 --- a/sway/container.c +++ b/sway/container.c @@ -48,7 +48,7 @@ static void free_swayc(swayc_t *cont) { // remove children until there are no more, free_swayc calls // remove_child, which removes child from this container while (cont->children->length) { - free_swayc(*(swayc_t **)list_get(cont->children, 0)); + free_swayc(list_getp(cont->children, 0)); } list_free(cont->children); } @@ -57,7 +57,7 @@ static void free_swayc(swayc_t *cont) { } if (cont->floating) { while (cont->floating->length) { - free_swayc(*(swayc_t **)list_get(cont->floating, 0)); + free_swayc(list_getp(cont->floating, 0)); } list_free(cont->floating); } @@ -100,7 +100,7 @@ static void update_root_geometry() { int child_height; for (size_t i = 0; i < root_container.children->length; ++i) { - child = *(swayc_t **)list_get(root_container.children, i); + child = list_getp(root_container.children, i); child_width = child->width + child->x; child_height = child->height + child->y; if (child_width > width) { @@ -124,7 +124,7 @@ swayc_t *new_output(wlc_handle handle) { const char *name = wlc_output_get_name(handle); // Find current outputs to see if this already exists for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *op = *(swayc_t **)list_get(root_container.children, i); + swayc_t *op = list_getp(root_container.children, i); const char *op_name = op->name; if (op_name && name && strcmp(op_name, name) == 0) { sway_log(L_DEBUG, "restoring output %" PRIuPTR ":%s", handle, op_name); @@ -136,7 +136,7 @@ swayc_t *new_output(wlc_handle handle) { struct output_config *oc = NULL, *all = NULL; for (size_t i = 0; i < config->output_configs->length; ++i) { - struct output_config *cur = *(struct output_config **)list_get(config->output_configs, i); + struct output_config *cur = list_getp(config->output_configs, i); if (strcasecmp(name, cur->name) == 0) { sway_log(L_DEBUG, "Matched output config for %s", name); oc = cur; @@ -177,7 +177,7 @@ swayc_t *new_output(wlc_handle handle) { if (name) { for (size_t i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); + struct workspace_output *wso = list_getp(config->workspace_outputs, i); if (strcasecmp(wso->output, name) == 0) { sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output); // Check if any other workspaces are using this name @@ -202,7 +202,7 @@ swayc_t *new_output(wlc_handle handle) { ws->is_focused = true; } else { sort_workspaces(output); - set_focused_container(*(swayc_t **)list_get(output->children, 0)); + set_focused_container(list_getp(output->children, 0)); } free(ws_name); @@ -262,7 +262,7 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) { workspace->focused = cont; // set all children focu to container for (size_t i = 0; i < workspace->children->length; ++i) { - (*(swayc_t **)list_get(workspace->children, i))->parent = cont; + ((swayc_t *)list_getp(workspace->children, i))->parent = cont; } // Swap children list_t *tmp_list = workspace->children; @@ -432,11 +432,11 @@ swayc_t *destroy_output(swayc_t *output) { // TODO also check if there will ever be no outputs except for exiting // program if (root_container.children->length > 1) { - int p = *(swayc_t **)list_get(root_container.children, 0) == output; + int p = list_getp(root_container.children, 0) == output; // Move workspace from this output to another output - swayc_t *item = *(swayc_t **)list_get(root_container.children, p); + swayc_t *item = list_getp(root_container.children, p); while (output->children->length) { - swayc_t *child = *(swayc_t **)list_get(output->children, 0); + swayc_t *child = list_getp(output->children, 0); remove_child(child); add_child(item, child); } @@ -471,7 +471,7 @@ swayc_t *destroy_workspace(swayc_t *workspace) { // Move children to a different workspace on this output swayc_t *new_workspace = NULL; for(size_t i = 0; i < output->children->length; i++) { - new_workspace = *(swayc_t **)list_get(output->children, i); + new_workspace = list_getp(output->children, i); if (new_workspace != workspace) { break; } @@ -481,12 +481,12 @@ swayc_t *destroy_workspace(swayc_t *workspace) { workspace->name, new_workspace->name); for (size_t i = 0; i < workspace->children->length; i++) { - swayc_t *item = *(swayc_t **)list_get(workspace->children, i); + swayc_t *item = list_getp(workspace->children, i); move_container_to(item, new_workspace); } for (size_t i = 0; i < workspace->floating->length; i++) { - swayc_t *item = *(swayc_t **)list_get(workspace->floating, i); + swayc_t *item = list_getp(workspace->floating, i); move_container_to(item, new_workspace); } } @@ -533,14 +533,14 @@ swayc_t *swayc_by_test(swayc_t *container, bool (*test)(swayc_t *view, void *dat // Special case for checking floating stuff if (container->type == C_WORKSPACE) { for (size_t i = 0; i < container->floating->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->floating, i); + swayc_t *child = list_getp(container->floating, i); if (test(child, data)) { return child; } } } for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); if (test(child, data)) { return child; } else { @@ -718,7 +718,7 @@ swayc_t *container_under_pointer(void) { i = len = lookup->floating->length; bool got_floating = false; while (--i > -1) { - swayc_t *item = *(swayc_t **)list_get(lookup->floating, i); + swayc_t *item = list_getp(lookup->floating, i); if (pointer_test(item, &origin)) { lookup = item; got_floating = true; @@ -732,7 +732,7 @@ swayc_t *container_under_pointer(void) { // search children size_t i, len = lookup->children->length; for (i = 0; i < len; ++i) { - swayc_t *item = *(swayc_t **)list_get(lookup->children, i); + swayc_t *item = list_getp(lookup->children, i); if (pointer_test(item, &origin)) { lookup = item; break; @@ -754,7 +754,7 @@ swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), swayc_t *con; if (container->type == C_WORKSPACE) { for (size_t i = 0; i < container->floating->length; ++i) { - con = *(swayc_t **)list_get(container->floating, i); + con = list_getp(container->floating, i); if (f(con, data)) { return con; } @@ -766,7 +766,7 @@ swayc_t *container_find(swayc_t *container, bool (*f)(swayc_t *, const void *), } for (size_t i = 0; i < container->children->length; ++i) { - con = *(swayc_t **)list_get(container->children, i); + con = list_getp(container->children, i); if (f(con, data)) { return con; } @@ -836,13 +836,13 @@ void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), voi f(container, data); if (container->children) { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); container_map(child, f, data); } } if (container->floating) { for (size_t i = 0; i < container->floating->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->floating, i); + swayc_t *child = list_getp(container->floating, i); container_map(child, f, data); } } @@ -867,13 +867,13 @@ void update_visibility_output(swayc_t *container, wlc_handle output) { else { if (container->children) { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); update_visibility_output(item, output); } } if (container->floating) { for (size_t i = 0; i < container->floating->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(container->floating, i); + swayc_t *item = list_getp(container->floating, i); update_visibility_output(item, output); } } @@ -887,7 +887,7 @@ void update_visibility(swayc_t *container) { container->visible = true; if (container->children) { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); update_visibility(item); } } @@ -897,7 +897,7 @@ void update_visibility(swayc_t *container) { container->visible = true; if (container->children) { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); update_visibility_output(item, container->handle); } } @@ -980,7 +980,7 @@ swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { ? L_HORIZ : L_VERT; if (new_major != prev_major) { for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); double h = child->height; child->height = child->width; child->width = h; diff --git a/sway/criteria.c b/sway/criteria.c index f8a8d5097..cd87ee4f2 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -53,7 +53,7 @@ static void free_crit_token(struct crit_token *crit) { static void free_crit_tokens(list_t *crit_tokens) { for (size_t i = 0; i < crit_tokens->length; i++) { - free_crit_token(*(struct crit_token **)list_get(crit_tokens, i)); + free_crit_token(list_getp(crit_tokens, i)); } list_free(crit_tokens); } @@ -251,7 +251,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { } size_t matches = 0; for (size_t i = 0; i < tokens->length; i++) { - struct crit_token *crit = *(struct crit_token **)list_get(tokens, i); + struct crit_token *crit = list_getp(tokens, i); switch (crit->type) { case CRIT_CLASS: if (!cont->class) { @@ -365,7 +365,7 @@ void free_criteria(struct criteria *crit) { bool criteria_any(swayc_t *cont, list_t *criteria) { for (size_t i = 0; i < criteria->length; i++) { - struct criteria *bc = *(struct criteria **)list_get(criteria, i); + struct criteria *bc = list_getp(criteria, i); if (criteria_test(cont, bc->tokens)) { return true; } @@ -376,7 +376,7 @@ bool criteria_any(swayc_t *cont, list_t *criteria) { list_t *criteria_for(swayc_t *cont) { list_t *matches = list_new(sizeof(struct criteria *), 0); for (size_t i = 0; i < config->criteria->length; i++) { - struct criteria *bc = *(struct criteria **)list_get(config->criteria, i); + struct criteria *bc = list_getp(config->criteria, i); if (criteria_test(cont, bc->tokens)) { list_add(matches, &bc); } diff --git a/sway/debug_log.c b/sway/debug_log.c index b886e34fe..66dbf6398 100644 --- a/sway/debug_log.c +++ b/sway/debug_log.c @@ -59,7 +59,7 @@ void layout_log(const swayc_t *c, int depth) { for (i = 0; i < e; ++i) { fputc('|',stderr); for (d = 0; d < depth; ++d) fputc('-', stderr); - layout_log(*(swayc_t **)list_get(c->children, i), depth + 1); + layout_log(list_getp(c->children, i), depth + 1); } } if (c->type == C_WORKSPACE) { @@ -68,7 +68,7 @@ void layout_log(const swayc_t *c, int depth) { for (i = 0; i < e; ++i) { fputc('|',stderr); for (d = 0; d < depth; ++d) fputc('=', stderr); - layout_log(*(swayc_t **)list_get(c->floating, i), depth + 1); + layout_log(list_getp(c->floating, i), depth + 1); } } } diff --git a/sway/extensions.c b/sway/extensions.c index f93d0d8b2..85a052284 100644 --- a/sway/extensions.c +++ b/sway/extensions.c @@ -16,7 +16,7 @@ struct desktop_shell_state desktop_shell; static struct panel_config *find_or_create_panel_config(struct wl_resource *resource) { for (size_t i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *conf = *(struct panel_config **)list_get(desktop_shell.panels, i); + struct panel_config *conf = list_getp(desktop_shell.panels, i); if (conf->wl_resource == resource) { sway_log(L_DEBUG, "Found existing panel config for resource %p", resource); return conf; @@ -36,7 +36,7 @@ static struct panel_config *find_or_create_panel_config(struct wl_resource *reso void background_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Background surface killed"); for (size_t i = 0; i < desktop_shell.backgrounds->length; ++i) { - struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); + struct background_config *config = list_getp(desktop_shell.backgrounds, i); if (config->wl_surface_res == resource) { list_delete(desktop_shell.backgrounds, i); break; @@ -47,7 +47,7 @@ void background_surface_destructor(struct wl_resource *resource) { void panel_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Panel surface killed"); for (size_t i = 0; i < desktop_shell.panels->length; ++i) { - struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); + struct panel_config *config = list_getp(desktop_shell.panels, i); if (config->wl_surface_res == resource) { list_delete(desktop_shell.panels, i); arrange_windows(&root_container, -1, -1); @@ -59,7 +59,7 @@ void panel_surface_destructor(struct wl_resource *resource) { void lock_surface_destructor(struct wl_resource *resource) { sway_log(L_DEBUG, "Lock surface killed"); for (size_t i = 0; i < desktop_shell.lock_surfaces->length; ++i) { - struct wl_resource *surface = *(struct wl_resource **)list_get(desktop_shell.lock_surfaces, i); + struct wl_resource *surface = list_getp(desktop_shell.lock_surfaces, i); if (surface == resource) { list_delete(desktop_shell.lock_surfaces, i); arrange_windows(&root_container, -1, -1); diff --git a/sway/focus.c b/sway/focus.c index 955bfe817..4312fd48e 100644 --- a/sway/focus.c +++ b/sway/focus.c @@ -252,7 +252,7 @@ swayc_t *get_focused_float(swayc_t *ws) { ws = swayc_active_workspace(); } if (ws->floating->length) { - return *(swayc_t **)list_get(ws->floating, ws->floating->length - 1); + return list_getp(ws->floating, ws->floating->length - 1); } return NULL; } diff --git a/sway/handlers.c b/sway/handlers.c index f0b714b92..3394b858e 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -36,7 +36,7 @@ static struct panel_config *if_panel_find_config(struct wl_client *client) { for (size_t i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); + struct panel_config *config = list_getp(desktop_shell.panels, i); if (config->client == client) { return config; } @@ -46,7 +46,7 @@ static struct panel_config *if_panel_find_config(struct wl_client *client) { static struct background_config *if_background_find_config(struct wl_client *client) { for (size_t i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); + struct background_config *config = list_getp(desktop_shell.backgrounds, i); if (config->client == client) { return config; } @@ -97,7 +97,7 @@ static void update_panel_geometry(struct panel_config *config) { static void update_panel_geometries(wlc_handle output) { for (size_t i = 0; i < desktop_shell.panels->length; i++) { - struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); + struct panel_config *config = list_getp(desktop_shell.panels, i); if (config->output == output) { update_panel_geometry(config); } @@ -112,7 +112,7 @@ static void update_background_geometry(struct background_config *config) { static void update_background_geometries(wlc_handle output) { for (size_t i = 0; i < desktop_shell.backgrounds->length; i++) { - struct background_config *config = *(struct background_config **)list_get(desktop_shell.backgrounds, i); + struct background_config *config = list_getp(desktop_shell.backgrounds, i); if (config->output == output) { update_background_geometry(config); } @@ -134,7 +134,7 @@ static bool handle_input_created(struct libinput_device *device) { struct input_config *ic = NULL; for (size_t i = 0; i < config->input_configs->length; ++i) { - struct input_config *cur = *(struct input_config **)list_get(config->input_configs, i); + struct input_config *cur = list_getp(config->input_configs, i); if (strcasecmp(identifier, cur->identifier) == 0) { sway_log(L_DEBUG, "Matched input config for %s", identifier); @@ -155,7 +155,7 @@ static bool handle_input_created(struct libinput_device *device) { static void handle_input_destroyed(struct libinput_device *device) { for (size_t i = 0; i < input_devices->length; ++i) { - struct libinput_device *item = *(struct libinput_device **)list_get(input_devices, i); + struct libinput_device *item = list_getp(input_devices, i); if (item == device) { list_delete(input_devices, i); break; @@ -175,7 +175,7 @@ static bool handle_output_created(wlc_handle output) { // Switch to workspace if we need to if (swayc_active_workspace() == NULL) { - swayc_t *ws = *(swayc_t **)list_get(op->children, 0); + swayc_t *ws = list_getp(op->children, 0); workspace_switch(ws); } @@ -190,18 +190,18 @@ static void handle_output_destroyed(wlc_handle output) { size_t i; list_t *list = root_container.children; for (i = 0; i < list->length; ++i) { - if ((*(swayc_t **)list_get(list, i))->handle == output) { + if (((swayc_t *)list_getp(list, i))->handle == output) { break; } } if (i < list->length) { - destroy_output(*(swayc_t **)list_get(list, i)); + destroy_output(list_getp(list, i)); } else { return; } if (list->length > 0) { // switch to other outputs active workspace - workspace_switch((*(swayc_t **)list_get(root_container.children, 0))->focused); + workspace_switch(((swayc_t *)list_getp(root_container.children, 0))->focused); } } @@ -246,12 +246,12 @@ static void ws_cleanup() { if (!root_container.children) return; while (i < root_container.children->length) { - op = *(swayc_t **)list_get(root_container.children, i++); + op = list_getp(root_container.children, i++); if (!op->children) continue; j = 0; while (j < op->children->length) { - ws = *(swayc_t **)list_get(op->children, j++); + ws = list_getp(op->children, j++); if (ws->children->length == 0 && ws->floating->length == 0 && ws != op->focused) { if (destroy_workspace(ws)) { j--; @@ -399,7 +399,7 @@ static bool handle_view_created(wlc_handle handle) { // TODO find a better way of doing this // Or to focused container else { - focused = get_focused_container(*(swayc_t **)list_get(focused->parent->children, 0)); + focused = get_focused_container(list_getp(focused->parent->children, 0)); } } } @@ -463,7 +463,7 @@ static bool handle_view_created(wlc_handle handle) { // check if it matches for_window in config and execute if so list_t *criteria = criteria_for(newview); for (size_t i = 0; i < criteria->length; i++) { - struct criteria *crit = *(struct criteria **)list_get(criteria, i); + struct criteria *crit = list_getp(criteria, i); sway_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'", crit->crit_raw, newview, crit->cmdlist); struct cmd_results *res = handle_command(crit->cmdlist, CONTEXT_CRITERIA); @@ -556,9 +556,9 @@ static void handle_view_destroyed(wlc_handle handle) { } else { // Is it unmanaged? for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *output = *(swayc_t **)list_get(root_container.children, i); + swayc_t *output = list_getp(root_container.children, i); for (size_t j = 0; j < output->unmanaged->length; ++j) { - wlc_handle *_handle = *(wlc_handle **)list_get(output->unmanaged, j); + wlc_handle *_handle = list_getp(output->unmanaged, j); if (*_handle == handle) { list_delete(output->unmanaged, j); free(_handle); @@ -689,13 +689,13 @@ static void handle_binding_command(struct sway_binding *binding) { static bool handle_bindsym(struct sway_binding *binding, uint32_t keysym, uint32_t keycode) { for (size_t i = 0; i < binding->keys->length; ++i) { if (binding->bindcode) { - xkb_keycode_t *key = *(xkb_keycode_t **)list_get(binding->keys, i); + xkb_keycode_t *key = list_getp(binding->keys, i); if (keycode == *key) { handle_binding_command(binding); return true; } } else { - xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, i); + xkb_keysym_t *key = list_getp(binding->keys, i); if (keysym == *key) { handle_binding_command(binding); return true; @@ -710,12 +710,12 @@ static bool valid_bindsym(struct sway_binding *binding) { bool match = false; for (size_t i = 0; i < binding->keys->length; ++i) { if (binding->bindcode) { - xkb_keycode_t *key = *(xkb_keycode_t **)list_get(binding->keys, i); + xkb_keycode_t *key = list_getp(binding->keys, i); if ((match = check_key(0, *key)) == false) { break; } } else { - xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, i); + xkb_keysym_t *key = list_getp(binding->keys, i); if ((match = check_key(*key, 0)) == false) { break; } @@ -727,7 +727,7 @@ static bool valid_bindsym(struct sway_binding *binding) { static bool handle_bindsym_release(struct sway_binding *binding) { if (binding->keys->length == 1) { - xkb_keysym_t *key = *(xkb_keysym_t **)list_get(binding->keys, 0); + xkb_keysym_t *key = list_getp(binding->keys, 0); if (check_released_key(*key)) { handle_binding_command(binding); return true; @@ -763,7 +763,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier // handle bar modifiers pressed/released uint32_t modifier; for (size_t i = 0; i < config->active_bar_modifiers->length; ++i) { - modifier = **(uint32_t **)list_get(config->active_bar_modifiers, i); + modifier = *(uint32_t *)list_getp(config->active_bar_modifiers, i); switch (modifier_state_changed(modifiers->mods, modifier)) { case MOD_STATE_PRESSED: @@ -780,7 +780,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier // handle bindings list_t *candidates = list_new(sizeof(struct sway_binding *), 0); for (size_t i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); + struct sway_binding *binding = list_getp(mode->bindings, i); if ((modifiers->mods ^ binding->modifiers) == 0) { switch (state) { case WLC_KEY_STATE_PRESSED: { @@ -799,7 +799,7 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier } for (size_t i = 0; i < candidates->length; ++i) { - struct sway_binding *binding = *(struct sway_binding **)list_get(candidates, i); + struct sway_binding *binding = list_getp(candidates, i); if (state == WLC_KEY_STATE_PRESSED) { if (!binding->release && handle_bindsym(binding, sym, key)) { list_free(candidates); @@ -910,7 +910,7 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w struct sway_mode *mode = config->current_mode; // handle bindings for (size_t i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); + struct sway_binding *binding = list_getp(mode->bindings, i); if ((modifiers->mods ^ binding->modifiers) == 0) { switch (state) { case WLC_BUTTON_STATE_PRESSED: { @@ -1005,7 +1005,7 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w // Send to front if floating if (pointer->is_floating) { for (size_t i = 0; i < pointer->parent->floating->length; i++) { - swayc_t *item = *(swayc_t **)list_get(pointer->parent->floating, i); + swayc_t *item = list_getp(pointer->parent->floating, i); if (item == pointer) { list_delete(pointer->parent->floating, i); list_add(pointer->parent->floating, &pointer); @@ -1067,7 +1067,7 @@ static void handle_wlc_ready(void) { // Execute commands until there are none left config->active = true; while (config->cmd_queue->length) { - char *line = *(char **)list_get(config->cmd_queue, 0); + char *line = list_getp(config->cmd_queue, 0); struct cmd_results *res = handle_command(line, CONTEXT_CONFIG); if (res->status != CMD_SUCCESS) { sway_log(L_ERROR, "Error on line '%s': %s", line, res->error); diff --git a/sway/ipc-json.c b/sway/ipc-json.c index e2f05a8de..0741db12e 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -146,7 +146,7 @@ static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) // window is in the scratchpad ? changed : none static const char *ipc_json_get_scratchpad_state(swayc_t *c) { for (size_t i = 0; i < scratchpad->length; i++) { - swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + swayc_t *item = list_getp(scratchpad, i); if (item == c) { return "changed"; } @@ -421,7 +421,7 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) { if (bar->outputs && bar->outputs->length > 0) { json_object *outputs = json_object_new_array(); for (size_t i = 0; i < bar->outputs->length; ++i) { - const char *name = *(char **)list_get(bar->outputs, i); + const char *name = list_getp(bar->outputs, i); json_object_array_add(outputs, json_object_new_string(name)); } json_object_object_add(json, "outputs", outputs); @@ -436,7 +436,7 @@ json_object *ipc_json_describe_container_recursive(swayc_t *c) { json_object *floating = json_object_new_array(); if (c->type != C_VIEW && c->floating && c->floating->length > 0) { for (size_t i = 0; i < c->floating->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(c->floating, i); + swayc_t *item = list_getp(c->floating, i); json_object_array_add(floating, ipc_json_describe_container_recursive(item)); } } @@ -445,7 +445,7 @@ json_object *ipc_json_describe_container_recursive(swayc_t *c) { json_object *children = json_object_new_array(); if (c->type != C_VIEW && c->children && c->children->length > 0) { for (size_t i = 0; i < c->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(c->children, i); + swayc_t *item = list_getp(c->children, i); json_object_array_add(children, ipc_json_describe_container_recursive(item)); } } @@ -454,7 +454,7 @@ json_object *ipc_json_describe_container_recursive(swayc_t *c) { if (c->type == C_ROOT) { json_object *scratchpad_json = json_object_new_array(); for (size_t i = 0; i < scratchpad->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(scratchpad, i); + swayc_t *item = list_getp(scratchpad, i); json_object_array_add(scratchpad_json, ipc_json_describe_container_recursive(item)); } json_object_object_add(object, "scratchpad", scratchpad_json); diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 4464c5e34..a2fc7c3a6 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -261,7 +261,7 @@ void ipc_client_disconnect(struct ipc_client *client) { sway_log(L_INFO, "IPC Client %d disconnected", client->fd); wlc_event_source_remove(client->event_source); size_t i = 0; - while (i < ipc_client_list->length && *(struct ipc_client **)list_get(ipc_client_list, i) != client) i++; + while (i < ipc_client_list->length && list_getp(ipc_client_list, i) != client) i++; list_delete(ipc_client_list, i); close(client->fd); free(client); @@ -284,7 +284,7 @@ void ipc_get_pixels(wlc_handle output) { struct get_pixels_request *req; for (size_t i = 0; i < ipc_get_pixel_requests->length; ++i) { - req = *(struct get_pixels_request **)list_get(ipc_get_pixel_requests, i); + req = list_getp(ipc_get_pixel_requests, i); if (req->output != output) { list_add(unhandled, &req); continue; @@ -421,7 +421,7 @@ void ipc_client_handle_command(struct ipc_client *client) { json_object *inputs = json_object_new_array(); if (input_devices) { for(size_t i = 0; ilength; i++) { - struct libinput_device *device = *(struct libinput_device **)list_get(input_devices, i); + struct libinput_device *device = list_getp(input_devices, i); json_object_array_add(inputs, ipc_json_describe_input(device)); } } @@ -533,7 +533,7 @@ void ipc_client_handle_command(struct ipc_client *client) { // Send list of configured bar IDs json_object *bars = json_object_new_array(); for (size_t i = 0; i < config->bars->length; ++i) { - struct bar_config *bar = *(struct bar_config **)list_get(config->bars, i); + struct bar_config *bar = list_getp(config->bars, i); json_object_array_add(bars, json_object_new_string(bar->id)); } const char *json_string = json_object_to_json_string(bars); @@ -543,7 +543,7 @@ void ipc_client_handle_command(struct ipc_client *client) { // Send particular bar's details struct bar_config *bar = NULL; for (size_t i = 0; i < config->bars->length; ++i) { - bar = *(struct bar_config **)list_get(config->bars, i); + bar = list_getp(config->bars, i); if (strcmp(buf, bar->id) == 0) { break; } @@ -624,7 +624,7 @@ static void ipc_get_marks_callback(swayc_t *container, void *data) { json_object *object = (json_object *)data; if (container->marks) { for (size_t i = 0; i < container->marks->length; ++i) { - char *mark = *(char **)list_get(container->marks, i); + char *mark = list_getp(container->marks, i); json_object_array_add(object, json_object_new_string(mark)); } } @@ -652,7 +652,7 @@ void ipc_send_event(const char *json_string, enum ipc_command_type event) { } for (size_t i = 0; i < ipc_client_list->length; i++) { - struct ipc_client *client = *(struct ipc_client **)list_get(ipc_client_list, i); + struct ipc_client *client = list_getp(ipc_client_list, i); if (!(client->security_policy & security_mask)) { continue; } @@ -775,7 +775,7 @@ void ipc_event_binding_keyboard(struct sway_binding *sb) { if (sb->bindcode) { // bindcode: populate input_codes uint32_t keycode; for (size_t i = 0; i < sb->keys->length; ++i) { - keycode = **(uint32_t **)list_get(sb->keys, i); + keycode = *(uint32_t *)list_getp(sb->keys, i); json_object_array_add(input_codes, json_object_new_int(keycode)); if (i == 0) { input_code = keycode; @@ -785,7 +785,7 @@ void ipc_event_binding_keyboard(struct sway_binding *sb) { uint32_t keysym; char buffer[64]; for (size_t i = 0; i < sb->keys->length; ++i) { - keysym = **(uint32_t **)list_get(sb->keys, i); + keysym = *(uint32_t *)list_getp(sb->keys, i); if (xkb_keysym_get_name(keysym, buffer, 64) > 0) { json_object *str = json_object_new_string(buffer); json_object_array_add(symbols, str); diff --git a/sway/layout.c b/sway/layout.c index b2d5cb301..ec92b8f0e 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -40,7 +40,7 @@ int index_child(const swayc_t *child) { if (!child->is_floating) { len = parent->children->length; for (i = 0; i < len; ++i) { - swayc_t *item = *(swayc_t **)list_get(parent->children, i); + swayc_t *item = list_getp(parent->children, i); if (item == child) { break; } @@ -48,7 +48,7 @@ int index_child(const swayc_t *child) { } else { len = parent->floating->length; for (i = 0; i < len; ++i) { - swayc_t *item = *(swayc_t **)list_get(parent->floating, i); + swayc_t *item = list_getp(parent->floating, i); if (item == child) { break; } @@ -108,10 +108,10 @@ void insert_child(swayc_t *parent, swayc_t *child, size_t index) { for (size_t i = index; i < parent->children->length;) { size_t start = auto_group_start_index(parent, i); size_t end = auto_group_end_index(parent, i); - swayc_t *first = *(swayc_t **)list_get(parent->children, start); + swayc_t *first = list_getp(parent->children, start); if (start + 1 < parent->children->length) { /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(*(swayc_t **)list_get(parent->children, start + 1)); + *get_maj_dim(first) = *get_maj_dim(list_getp(parent->children, start + 1)); } else { /* new group, let the apply_layout handle it */ first->height = first->width = 0; @@ -119,7 +119,7 @@ void insert_child(swayc_t *parent, swayc_t *child, size_t index) { } double remaining = *get_min_dim(parent); for (size_t j = end - 1; j > start; --j) { - swayc_t *sibling = *(swayc_t **)list_get(parent->children, j); + swayc_t *sibling = list_getp(parent->children, j); if (sibling == child) { /* the inserted child won't yet have its minor dimension set */ @@ -217,7 +217,7 @@ swayc_t *remove_child(swayc_t *child) { if (child->is_floating) { // Special case for floating views for (i = 0; i < parent->floating->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(parent->floating, i); + swayc_t *item = list_getp(parent->floating, i); if (item == child) { list_delete(parent->floating, i); break; @@ -226,7 +226,7 @@ swayc_t *remove_child(swayc_t *child) { i = 0; } else { for (i = 0; i < parent->children->length; ++i) { - swayc_t *item = *(swayc_t **)list_get(parent->children, i); + swayc_t *item = list_getp(parent->children, i); if (item == child) { list_delete(parent->children, i); break; @@ -246,24 +246,24 @@ swayc_t *remove_child(swayc_t *child) { for (size_t j = parent->children->length - 1; j >= i;) { size_t start = auto_group_start_index(parent, j); size_t end = auto_group_end_index(parent, j); - swayc_t *first = *(swayc_t **)list_get(parent->children, start); + swayc_t *first = list_getp(parent->children, start); if (i == start) { /* removed element was first child in the current group, use its size along the major axis */ *get_maj_dim(first) = *get_maj_dim(child); } else if (start > i) { /* preserve the group's dimension along major axis */ - *get_maj_dim(first) = *get_maj_dim(*(swayc_t **)list_get(parent->children, start - 1)); + *get_maj_dim(first) = *get_maj_dim(list_getp(parent->children, start - 1)); } if (end != parent->children->length) { double remaining = *get_min_dim(parent); for (size_t k = start; k < end - 1; ++k) { - swayc_t *sibling = *(swayc_t **)list_get(parent->children, k); + swayc_t *sibling = list_getp(parent->children, k); remaining -= *get_min_dim(sibling); } /* last element of the group gets remaining size, elements that don't change groups keep their ratio */ - *get_min_dim(*(swayc_t **)list_get(parent->children, end - 1)) = remaining; + *get_min_dim(list_getp(parent->children, end - 1)) = remaining; } /* else last group, let apply_layout handle it */ j = start - 1; } @@ -272,9 +272,9 @@ swayc_t *remove_child(swayc_t *child) { // Set focused to new container if (parent->focused == child) { if (parent->children->length > 0) { - parent->focused = *(swayc_t **)list_get(parent->children, i ? i-1:0); + parent->focused = list_getp(parent->children, i ? i-1:0); } else if (parent->floating && parent->floating->length) { - parent->focused = *(swayc_t **)list_get(parent->floating, parent->floating->length - 1); + parent->focused = list_getp(parent->floating, parent->floating->length - 1); } else { parent->focused = NULL; } @@ -335,9 +335,9 @@ void swap_geometry(swayc_t *a, swayc_t *b) { static void swap_children(swayc_t *container, size_t a, size_t b) { if (a < container->children->length && b < container->children->length && a != b) { - swayc_t *pa = *(swayc_t **)list_get(container->children, a); - swayc_t *pb = *(swayc_t **)list_get(container->children, b); - *(swayc_t **)list_get(container->children, a) = *(swayc_t **)list_get(container->children, b); + swayc_t *pa = list_getp(container->children, a); + swayc_t *pb = list_getp(container->children, b); + *(swayc_t **)list_get(container->children, a) = list_getp(container->children, b); *(swayc_t **)list_get(container->children, b) = pa; if (is_auto_layout(container->layout)) { size_t ga = auto_group_index(container, a); @@ -384,7 +384,7 @@ void move_container(swayc_t *container, enum movement_direction dir, int move_am // swap first child in auto layout with currently focused child if (is_auto_layout(parent->layout)) { int focused_idx = index_child(container); - swayc_t *first = *(swayc_t **)list_get(parent->children, 0); + swayc_t *first = list_getp(parent->children, 0); if (focused_idx > 0) { list_swap(parent->children, 0, focused_idx); swap_geometry(first, container); @@ -444,7 +444,7 @@ void move_container(swayc_t *container, enum movement_direction dir, int move_am // when it has not, legal insertion position is 0:len-1 if (desired >= 0 && desired - ascended < (ssize_t)parent->children->length) { if (!ascended) { - child = *(swayc_t **)list_get(parent->children, desired); + child = list_getp(parent->children, desired); // Move container into sibling container if (child->type == C_CONTAINER) { parent = child; @@ -676,7 +676,7 @@ void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { if (prev_layout != L_TABBED && prev_layout != L_STACKED) { // cache current geometry for all non-float children for (size_t i = 0; i < parent->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(parent->children, i); + swayc_t *child = list_getp(parent->children, i); child->cached_geometry.origin.x = child->x; child->cached_geometry.origin.y = child->y; child->cached_geometry.size.w = child->width; @@ -688,7 +688,7 @@ void update_layout_geometry(swayc_t *parent, enum swayc_layouts prev_layout) { if (prev_layout == L_TABBED || prev_layout == L_STACKED) { // recover cached geometry for all non-float children for (size_t i = 0; i < parent->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(parent->children, i); + swayc_t *child = list_getp(parent->children, i); // only recoverer cached geometry if non-zero if (!wlc_geometry_equals(&child->cached_geometry, &wlc_geometry_zero)) { child->x = child->cached_geometry.origin.x; @@ -833,7 +833,7 @@ void update_geometry(swayc_t *container) { w = geometry.size.w / l; r = geometry.size.w % l; for (size_t i = 0; i < parent->children->length; ++i) { - swayc_t *view = *(swayc_t **)list_get(parent->children, i); + swayc_t *view = list_getp(parent->children, i); if (view == container) { x = w * i; if ((ssize_t)i == l - 1) { @@ -861,7 +861,7 @@ void update_geometry(swayc_t *container) { } else if (parent->layout == L_STACKED && parent->children->length > 1) { int y = 0; for (size_t i = 0; i < parent->children->length; ++i) { - swayc_t *view = *(swayc_t **)list_get(parent->children, i); + swayc_t *view = list_getp(parent->children, i); if (view == container) { y = title_bar_height * i; } @@ -968,7 +968,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { switch (container->type) { case C_ROOT: for (i = 0; i < container->children->length; ++i) { - swayc_t *output = *(swayc_t **)list_get(container->children, i); + swayc_t *output = list_getp(container->children, i); sway_log(L_DEBUG, "Arranging output '%s' at %f,%f", output->name, output->x, output->y); arrange_windows_r(output, -1, -1); } @@ -985,12 +985,12 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { } // arrange all workspaces: for (i = 0; i < container->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); arrange_windows_r(child, -1, -1); } // Bring all unmanaged views to the front for (i = 0; i < container->unmanaged->length; ++i) { - wlc_handle *handle = *(wlc_handle **)list_get(container->unmanaged, i); + wlc_handle *handle = list_getp(container->unmanaged, i); wlc_view_bring_to_front(*handle); } return; @@ -999,7 +999,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { swayc_t *output = swayc_parent_by_type(container, C_OUTPUT); width = output->width, height = output->height; for (i = 0; i < desktop_shell.panels->length; ++i) { - struct panel_config *config = *(struct panel_config **)list_get(desktop_shell.panels, i); + struct panel_config *config = list_getp(desktop_shell.panels, i); if (config->output == output->handle) { struct wlc_size size = *wlc_surface_get_size(config->surface); sway_log(L_DEBUG, "-> Found panel for this workspace: %ux%u, position: %u", size.w, size.h, config->panel_position); @@ -1105,7 +1105,7 @@ static void arrange_windows_r(swayc_t *container, double width, double height) { // Arrage floating layouts for workspaces last if (container->type == C_WORKSPACE) { for (size_t i = 0; i < container->floating->length; ++i) { - swayc_t *view = *(swayc_t **)list_get(container->floating, i); + swayc_t *view = list_getp(container->floating, i); if (view->type == C_VIEW) { update_geometry(view); sway_log(L_DEBUG, "Set floating view to %.f x %.f @ %.f, %.f", @@ -1127,7 +1127,7 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, double scale = 0; // Calculate total width for (int i = start; i < end; ++i) { - double *old_width = &(*(swayc_t **)list_get(container->children, i))->width; + double *old_width = &((swayc_t *)list_getp(container->children, i))->width; if (*old_width <= 0) { if (end - start > 1) { *old_width = width / (end - start - 1); @@ -1145,7 +1145,7 @@ void apply_horiz_layout(swayc_t *container, const double x, const double y, sway_log(L_DEBUG, "Arranging %p horizontally", container); swayc_t *focused = NULL; for (int i = start; i < end; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, width, scale); @@ -1180,7 +1180,7 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, double scale = 0; // Calculate total height for (i = start; i < end; ++i) { - double *old_height = &(*(swayc_t **)list_get(container->children, i))->height; + double *old_height = &((swayc_t *)list_getp(container->children, i))->height; if (*old_height <= 0) { if (end - start > 1) { *old_height = height / (end - start - 1); @@ -1198,7 +1198,7 @@ void apply_vert_layout(swayc_t *container, const double x, const double y, sway_log(L_DEBUG, "Arranging %p vertically", container); swayc_t *focused = NULL; for (i = start; i < end; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will scale %f by %f)", child, child->type, height, scale); @@ -1230,7 +1230,7 @@ void apply_tabbed_or_stacked_layout(swayc_t *container, double x, double y, double width, double height) { swayc_t *focused = NULL; for (size_t i = 0; i < container->children->length; ++i) { - swayc_t *child = *(swayc_t **)list_get(container->children, i); + swayc_t *child = list_getp(container->children, i); child->x = x; child->y = y; if (child == container->focused) { @@ -1322,7 +1322,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, for (size_t group = 0; group < nb_groups; ++group) { int idx; if (auto_group_bounds(container, group, &idx, NULL)) { - swayc_t *child = *(swayc_t **)list_get(container->children, idx); + swayc_t *child = list_getp(container->children, idx); double *dim = group_layout == L_HORIZ ? &child->height : &child->width; if (*dim <= 0) { // New child with uninitialized dimension @@ -1377,7 +1377,7 @@ void arrange_windows(swayc_t *container, double width, double height) { void arrange_backgrounds(void) { for (size_t i = 0; i < desktop_shell.backgrounds->length; ++i) { - struct background_config *bg = *(struct background_config **)list_get(desktop_shell.backgrounds, i); + struct background_config *bg = list_getp(desktop_shell.backgrounds, i); wlc_view_send_to_back(bg->handle); } } @@ -1395,10 +1395,10 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_dir switch (dir) { case MOVE_LEFT: // get most right child of new output - return *(swayc_t **)list_get(ws->children, ws->children->length-1); + return list_getp(ws->children, ws->children->length-1); case MOVE_RIGHT: // get most left child of new output - return *(swayc_t **)list_get(ws->children, 0); + return list_getp(ws->children, 0); case MOVE_UP: case MOVE_DOWN: { @@ -1408,10 +1408,10 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output, enum movement_dir if (parent->layout == L_VERT) { if (dir == MOVE_UP) { // get child furthest down on new output - return *(swayc_t **)list_get(parent->children, parent->children->length-1); + return list_getp(parent->children, parent->children->length-1); } else if (dir == MOVE_DOWN) { // get child furthest up on new output - return *(swayc_t **)list_get(parent->children, 0); + return list_getp(parent->children, 0); } } return focused_view; @@ -1450,7 +1450,7 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio if (desired < 0) { desired += parent->children->length; } - return *(swayc_t **)list_get(parent->children, desired); + return list_getp(parent->children, desired); } } @@ -1518,11 +1518,11 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio if (can_move) { if (container->is_floating) { if (desired < 0) { - wrap_candidate = *(swayc_t **)list_get(parent->floating, parent->floating->length-1); + wrap_candidate = list_getp(parent->floating, parent->floating->length-1); } else if (desired >= (ssize_t)parent->floating->length){ - wrap_candidate = *(swayc_t **)list_get(parent->floating, 0); + wrap_candidate = list_getp(parent->floating, 0); } else { - wrap_candidate = *(swayc_t **)list_get(parent->floating, desired); + wrap_candidate = list_getp(parent->floating, desired); } if (wrap_candidate) { wlc_view_bring_to_front(wrap_candidate->handle); @@ -1533,16 +1533,16 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio int len = parent->children->length; if (!wrap_candidate && len > 1) { if (desired < 0) { - wrap_candidate = *(swayc_t **)list_get(parent->children, len-1); + wrap_candidate = list_getp(parent->children, len-1); } else { - wrap_candidate = *(swayc_t **)list_get(parent->children, 0); + wrap_candidate = list_getp(parent->children, 0); } if (config->force_focus_wrapping) { return wrap_candidate; } } } else { - swayc_t *item = *(swayc_t **)list_get(parent->children, desired); + swayc_t *item = list_getp(parent->children, desired); sway_log(L_DEBUG, "%s cont %d-%p dir %i sibling %d: %p", __func__, idx, container, dir, desired, item); return item; @@ -1579,12 +1579,12 @@ void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge ed } if (layout_match) { for (size_t i = 0; i < container->children->length; i++) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); recursive_resize(item, amount/container->children->length, edge); } } else { for (size_t i = 0; i < container->children->length; i++) { - swayc_t *item = *(swayc_t **)list_get(container->children, i); + swayc_t *item = list_getp(container->children, i); recursive_resize(item, amount, edge); } } diff --git a/sway/output.c b/sway/output.c index 0ae36ef1c..31ac8dd19 100644 --- a/sway/output.c +++ b/sway/output.c @@ -38,7 +38,7 @@ swayc_t *output_by_name(const char* name, const struct wlc_point *abs_pos) { } } else { for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) { return c; } @@ -59,7 +59,7 @@ swayc_t *swayc_opposite_output(enum movement_direction dir, case MOVE_LEFT: case MOVE_RIGHT: ; for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (abs_pos->y >= c->y && abs_pos->y <= c->y + c->height) { if (!opposite) { opposite = c; @@ -74,7 +74,7 @@ swayc_t *swayc_opposite_output(enum movement_direction dir, case MOVE_UP: case MOVE_DOWN: ; for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (abs_pos->x >= c->x && abs_pos->x <= c->x + c->width) { if (!opposite) { opposite = c; @@ -117,7 +117,7 @@ swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, case MOVE_RIGHT: ; double delta_y = 0; for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (c == output || c->type != C_OUTPUT) { continue; } @@ -170,7 +170,7 @@ swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir, case MOVE_DOWN: ; double delta_x = 0; for (size_t i = 0; i < root_container.children->length; ++i) { - swayc_t *c = *(swayc_t **)list_get(root_container.children, i); + swayc_t *c = list_getp(root_container.children, i); if (c == output || c->type != C_OUTPUT) { continue; } diff --git a/sway/security.c b/sway/security.c index e27a57b88..f7b9afcd4 100644 --- a/sway/security.c +++ b/sway/security.c @@ -49,7 +49,7 @@ struct feature_policy *alloc_feature_policy(const char *program) { return NULL; } for (size_t i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = *(struct feature_policy **)list_get(config->feature_policies, i); + struct feature_policy *policy = list_getp(config->feature_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; break; @@ -77,7 +77,7 @@ struct ipc_policy *alloc_ipc_policy(const char *program) { return NULL; } for (size_t i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = *(struct ipc_policy **)list_get(config->ipc_policies, i); + struct ipc_policy *policy = list_getp(config->ipc_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; break; @@ -144,7 +144,7 @@ struct feature_policy *get_feature_policy(const char *name) { struct feature_policy *policy = NULL; for (size_t i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *p = *(struct feature_policy **)list_get(config->feature_policies, i); + struct feature_policy *p = list_getp(config->feature_policies, i); if (strcmp(p->program, name) == 0) { policy = p; break; @@ -165,7 +165,7 @@ uint32_t get_feature_policy_mask(pid_t pid) { const char *link = get_pid_exe(pid); for (size_t i = 0; i < config->feature_policies->length; ++i) { - struct feature_policy *policy = *(struct feature_policy **)list_get(config->feature_policies, i); + struct feature_policy *policy = list_getp(config->feature_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; } @@ -182,7 +182,7 @@ uint32_t get_ipc_policy_mask(pid_t pid) { const char *link = get_pid_exe(pid); for (size_t i = 0; i < config->ipc_policies->length; ++i) { - struct ipc_policy *policy = *(struct ipc_policy **)list_get(config->ipc_policies, i); + struct ipc_policy *policy = list_getp(config->ipc_policies, i); if (strcmp(policy->program, "*") == 0) { default_policy = policy->features; } @@ -198,7 +198,7 @@ uint32_t get_command_policy_mask(const char *cmd) { uint32_t default_policy = 0; for (size_t i = 0; i < config->command_policies->length; ++i) { - struct command_policy *policy = *(struct command_policy **)list_get(config->command_policies, i); + struct command_policy *policy = list_getp(config->command_policies, i); if (strcmp(policy->command, "*") == 0) { default_policy = policy->context; } diff --git a/sway/workspace.c b/sway/workspace.c index 37b67c1b8..7667faf59 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -30,7 +30,7 @@ struct workspace_by_number_data { static bool workspace_valid_on_output(const char *output_name, const char *ws_name) { for (size_t i = 0; i < config->workspace_outputs->length; ++i) { - struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); + struct workspace_output *wso = list_getp(config->workspace_outputs, i); if (strcasecmp(wso->workspace, ws_name) == 0) { if (strcasecmp(wso->output, output_name) != 0) { return false; @@ -51,7 +51,7 @@ char *workspace_next_name(const char *output_name) { int order = INT_MAX; char *target = NULL; for (size_t i = 0; i < mode->bindings->length; ++i) { - struct sway_binding *binding = *(struct sway_binding **)list_get(mode->bindings, i); + struct sway_binding *binding = list_getp(mode->bindings, i); char *cmdlist = strdup(binding->command); char *dup = cmdlist; char *name = NULL; @@ -133,12 +133,12 @@ swayc_t *workspace_create(const char* name) { // Search for workspace<->output pair size_t i, e = config->workspace_outputs->length; for (i = 0; i < e; ++i) { - struct workspace_output *wso = *(struct workspace_output **)list_get(config->workspace_outputs, i); + struct workspace_output *wso = list_getp(config->workspace_outputs, i); if (strcasecmp(wso->workspace, name) == 0) { // Find output to use if it exists e = root_container.children->length; for (i = 0; i < e; ++i) { - parent = *(swayc_t **)list_get(root_container.children, i); + parent = list_getp(root_container.children, i); if (strcmp(parent->name, wso->output) == 0) { return new_workspace(parent, name); } @@ -206,9 +206,9 @@ swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) { } for (size_t i = 0; i < output->children->length; i++) { - swayc_t *item = *(swayc_t **)list_get(output->children, i); + swayc_t *item = list_getp(output->children, i); if (item == output->focused) { - return *(swayc_t **)list_get(output->children, (wrap(i + (next ? 1 : -1), output->children->length))); + return list_getp(output->children, (wrap(i + (next ? 1 : -1), output->children->length))); } } @@ -231,18 +231,18 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) { size_t start = next ? 0 : 1; size_t end = next ? (current_output->children->length) - 1 : current_output->children->length; for (size_t i = start; i < end; i++) { - swayc_t *item = *(swayc_t **)list_get(current_output->children, i); + swayc_t *item = list_getp(current_output->children, i); if (item == workspace) { - return *(swayc_t **)list_get(current_output->children, i + offset); + return list_getp(current_output->children, i + offset); } } // Given workspace is the first/last on the output, jump to the previous/next output size_t num_outputs = root_container.children->length; for (size_t i = 0; i < num_outputs; i++) { - swayc_t *item = *(swayc_t **)list_get(root_container.children, i); + swayc_t *item = list_getp(root_container.children, i); if (item == current_output) { - swayc_t *next_output = *(swayc_t **)list_get(root_container.children, wrap(i + offset, num_outputs)); + swayc_t *next_output = list_getp(root_container.children, wrap(i + offset, num_outputs)); return workspace_output_prev_next_impl(next_output, next); } } @@ -294,13 +294,13 @@ bool workspace_switch(swayc_t *workspace) { // don't change list while traversing it, use intermediate list instead list_t *stickies = list_new(sizeof(swayc_t *), 0); for (size_t i = 0; i < active_ws->floating->length; i++) { - swayc_t *cont = *(swayc_t **)list_get(active_ws->floating, i); + swayc_t *cont = list_getp(active_ws->floating, i); if (cont->sticky) { list_add(stickies, &cont); } } for (size_t i = 0; i < stickies->length; i++) { - swayc_t *cont = *(swayc_t **)list_get(stickies, i); + swayc_t *cont = list_getp(stickies, i); sway_log(L_DEBUG, "Moving sticky container %p to %p:%s", cont, workspace, workspace->name); swayc_t *parent = remove_child(cont); @@ -336,7 +336,7 @@ swayc_t *workspace_for_pid(pid_t pid) { do { for (i = 0; i < config->pid_workspaces->length; i++) { - pw = *(struct pid_workspace **)list_get(config->pid_workspaces, i); + pw = list_getp(config->pid_workspaces, i); pid_t *pw_pid = pw->pid; if (pid == *pw_pid) { diff --git a/swaybar/bar.c b/swaybar/bar.c index 90b4c615f..8992ece10 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -68,7 +68,7 @@ static void mouse_button_notify(struct window *window, int x, int y, struct output *clicked_output = NULL; for (size_t i = 0; i < swaybar.outputs->length; i++) { - struct output *output = *(struct output **)list_get(swaybar.outputs, i); + struct output *output = list_getp(swaybar.outputs, i); if (window == output->window) { clicked_output = output; break; @@ -81,7 +81,7 @@ static void mouse_button_notify(struct window *window, int x, int y, double button_x = 0.5; for (size_t i = 0; i < clicked_output->workspaces->length; i++) { - struct workspace *workspace = *(struct workspace **)list_get(clicked_output->workspaces, i); + struct workspace *workspace = list_getp(clicked_output->workspaces, i); int button_width, button_height; workspace_button_size(window, workspace->name, &button_width, &button_height); @@ -102,7 +102,7 @@ static void mouse_scroll_notify(struct window *window, enum scroll_direction dir // Find output this window lives on struct output *output = NULL; for (i = 0; i < swaybar.outputs->length; ++i) { - output = *(struct output **)list_get(swaybar.outputs, i); + output = list_getp(swaybar.outputs, i); if (output->window == window) { break; } @@ -112,7 +112,7 @@ static void mouse_scroll_notify(struct window *window, enum scroll_direction dir } int focused = -1; for (i = 0; i < output->workspaces->length; ++i) { - struct workspace *ws = *(struct workspace **)list_get(output->workspaces, i); + struct workspace *ws = list_getp(output->workspaces, i); if (ws->focused) { focused = i; break; @@ -143,7 +143,7 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { ipc_bar_init(bar, bar_id); for (size_t i = 0; i < bar->outputs->length; ++i) { - struct output *bar_output = *(struct output **)list_get(bar->outputs, i); + struct output *bar_output = list_getp(bar->outputs, i); bar_output->registry = registry_poll(); @@ -151,7 +151,7 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { sway_abort("swaybar requires the compositor to support the desktop-shell extension."); } - struct output_state *output = *(struct output_state **)list_get(bar_output->registry->outputs, bar_output->idx); + struct output_state *output = list_getp(bar_output->registry->outputs, bar_output->idx); bar_output->window = window_setup(bar_output->registry, output->width / output->scale, 30, output->scale, false); @@ -190,7 +190,7 @@ void bar_run(struct bar *bar) { pfd[1].events = POLLIN; for (size_t i = 0; i < bar->outputs->length; ++i) { - struct output *output = *(struct output **)list_get(bar->outputs, i); + struct output *output = list_getp(bar->outputs, i); pfd[i+2].fd = wl_display_get_fd(output->registry->display); pfd[i+2].events = POLLIN; } @@ -198,7 +198,7 @@ void bar_run(struct bar *bar) { while (1) { if (dirty) { for (size_t i = 0; i < bar->outputs->length; ++i) { - struct output *output = *(struct output **)list_get(bar->outputs, i); + struct output *output = list_getp(bar->outputs, i); if (window_prerender(output->window) && output->window->cairo) { render(output, bar->config, bar->status); window_render(output->window); @@ -223,7 +223,7 @@ void bar_run(struct bar *bar) { // dispatch wl_display events for (size_t i = 0; i < bar->outputs->length; ++i) { - struct output *output = *(struct output **)list_get(bar->outputs, i); + struct output *output = list_getp(bar->outputs, i); if (pfd[i+2].revents & POLLIN) { if (wl_display_dispatch(output->registry->display) == -1) { sway_log(L_ERROR, "failed to dispatch wl: %d", errno); @@ -237,7 +237,7 @@ void bar_run(struct bar *bar) { void free_workspaces(list_t *workspaces) { for (size_t i = 0; i < workspaces->length; ++i) { - struct workspace *ws = *(struct workspace **)list_get(workspaces, i); + struct workspace *ws = list_getp(workspaces, i); free(ws->name); free(ws); } @@ -261,7 +261,7 @@ static void free_output(struct output *output) { static void free_outputs(list_t *outputs) { for (size_t i = 0; i < outputs->length; ++i) { - struct output *item = *(struct output **)list_get(outputs, i); + struct output *item = list_getp(outputs, i); free_output(item); } list_free(outputs); diff --git a/swaybar/ipc.c b/swaybar/ipc.c index ec3f81757..be6a180de 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -85,7 +85,7 @@ static void ipc_parse_config(struct config *config, const char *payload) { // free previous outputs list for (size_t i = 0; i < config->outputs->length; ++i) { - free(*(char **)list_get(config->outputs, i)); + free(list_getp(config->outputs, i)); } list_free(config->outputs); config->outputs = list_new(sizeof(char *), 0); @@ -215,7 +215,7 @@ static void ipc_parse_config(struct config *config, const char *payload) { static void ipc_update_workspaces(struct bar *bar) { for (size_t i = 0; i < bar->outputs->length; ++i) { - struct output *output = *(struct output **)list_get(bar->outputs, i); + struct output *output = list_getp(bar->outputs, i); if (output->workspaces) { free_workspaces(output->workspaces); } @@ -244,7 +244,7 @@ static void ipc_update_workspaces(struct bar *bar) { json_object_object_get_ex(ws_json, "urgent", &urgent); for (size_t j = 0; j < bar->outputs->length; ++j) { - struct output *output = *(struct output **)list_get(bar->outputs, j); + struct output *output = list_getp(bar->outputs, j); if (strcmp(json_object_get_string(out), output->name) == 0) { struct workspace *ws = malloc(sizeof(struct workspace)); ws->num = json_object_get_int(num); @@ -300,7 +300,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { use_output = true; } else { for (size_t j = 0; j < bar->config->outputs->length; ++j) { - const char *conf_name = *(char **)list_get(bar->config->outputs, j); + const char *conf_name = list_getp(bar->config->outputs, j); if (strcasecmp(name, conf_name) == 0) { use_output = true; break; diff --git a/swaybar/render.c b/swaybar/render.c index e77dac0c2..3d2779bdd 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -315,7 +315,7 @@ void render(struct output *output, struct config *config, struct status_line *li double pos = (window->width * window->scale) - 0.5; bool edge = true; for (ssize_t i = line->block_line->length - 1; i >= 0; --i) { - struct status_block *block = *(struct status_block **)list_get(line->block_line, i); + struct status_block *block = list_getp(line->block_line, i); if (block->full_text && block->full_text[0]) { render_block(window, config, block, &pos, edge, is_focused); edge = false; @@ -329,7 +329,7 @@ void render(struct output *output, struct config *config, struct status_line *li // Workspaces if (config->workspace_buttons) { for (size_t i = 0; i < output->workspaces->length; ++i) { - struct workspace *ws = *(struct workspace **)list_get(output->workspaces, i); + struct workspace *ws = list_getp(output->workspaces, i); render_workspace_button(window, config, ws, &x); } } diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 8cb8b12cb..68a105772 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -64,7 +64,7 @@ static void parse_json(struct bar *bar, const char *text) { if (bar->status->block_line) { //list_foreach(bar->status->block_line, free_status_block); for (size_t i = 0; i < bar->status->block_line->length; ++i) { - struct status_block *item = *(struct status_block **)list_get(bar->status->block_line, i); + struct status_block *item = list_getp(bar->status->block_line, i); free_status_block(item); } list_free(bar->status->block_line); @@ -453,7 +453,7 @@ void free_status_line(struct status_line *line) { if (line->block_line) { //list_foreach(line->block_line, free_status_block); for (size_t i = 0; i < line->block_line->length; ++i) { - struct status_block *item = *(struct status_block **)list_get(line->block_line, i); + struct status_block *item = list_getp(line->block_line, i); free_status_block(item); } list_free(line->block_line); diff --git a/swaybg/main.c b/swaybg/main.c index b4ae0ffe4..fc1721e1b 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -26,7 +26,7 @@ enum scaling_mode { void sway_terminate(int exit_code) { for (size_t i = 0; i < surfaces->length; ++i) { - struct window *window = *(struct window **)list_get(surfaces, i); + struct window *window = list_getp(surfaces, i); window_teardown(window); } list_free(surfaces); @@ -66,7 +66,7 @@ int main(int argc, const char **argv) { int desired_output = atoi(argv[1]); sway_log(L_INFO, "Using output %d of %zu", desired_output, registry->outputs->length); - struct output_state *output = *(struct output_state **)list_get(registry->outputs, desired_output); + struct output_state *output = list_getp(registry->outputs, desired_output); struct window *window = window_setup(registry, output->width, output->height, output->scale, false); if (!window) { @@ -118,7 +118,7 @@ int main(int argc, const char **argv) { int wheight = window->height * window->scale; for (size_t i = 0; i < surfaces->length; ++i) { - struct window *window = *(struct window **)list_get(surfaces, i); + struct window *window = list_getp(surfaces, i); if (window_prerender(window) && window->cairo) { switch (scaling_mode) { case SCALING_MODE_STRETCH: @@ -195,7 +195,7 @@ int main(int argc, const char **argv) { while (wl_display_dispatch(registry->display) != -1); for (size_t i = 0; i < surfaces->length; ++i) { - struct window *window = *(struct window **)list_get(surfaces, i); + struct window *window = list_getp(surfaces, i); window_teardown(window); } list_free(surfaces); diff --git a/swaylock/main.c b/swaylock/main.c index 86b422172..63d3c9ba9 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -45,7 +45,7 @@ void sigalarm_handler(int sig) { void sway_terminate(int exit_code) { for (size_t i = 0; i < render_data.surfaces->length; ++i) { - struct window *window = *(struct window **)list_get(render_data.surfaces, i); + struct window *window = list_getp(render_data.surfaces, i); window_teardown(window); } list_free(render_data.surfaces); @@ -556,7 +556,7 @@ int main(int argc, char **argv) { } for (size_t i = 0; i < registry->outputs->length; ++i) { - struct output_state *output = *(struct output_state **)list_get(registry->outputs, i); + struct output_state *output = list_getp(registry->outputs, i); struct window *window = window_setup(registry, output->width, output->height, output->scale, true); if (!window) { @@ -607,8 +607,8 @@ int main(int argc, char **argv) { while (wl_display_dispatch(registry->display) != -1) { if (!locked) { for (size_t i = 0; i < registry->outputs->length; ++i) { - struct output_state *output = *(struct output_state **)list_get(registry->outputs, i); - struct window *window = *(struct window **)list_get(render_data.surfaces, i); + struct output_state *output = list_getp(registry->outputs, i); + struct window *window = list_getp(render_data.surfaces, i); lock_set_lock_surface(registry->swaylock, output->output, window->surface); } locked = true; @@ -628,7 +628,7 @@ int main(int argc, char **argv) { } for (size_t i = 0; i < render_data.surfaces->length; ++i) { - struct window *window = *(struct window **)list_get(render_data.surfaces, i); + struct window *window = list_getp(render_data.surfaces, i); window_teardown(window); } list_free(render_data.surfaces); @@ -642,7 +642,7 @@ int main(int argc, char **argv) { void render(struct render_data *render_data, struct lock_config *config) { for (size_t i = 0; i < render_data->surfaces->length; ++i) { sway_log(L_DEBUG, "Render surface %zu of %zu", i, render_data->surfaces->length); - struct window *window = *(struct window **)list_get(render_data->surfaces, i); + struct window *window = list_getp(render_data->surfaces, i); if (!window_prerender(window) || !window->cairo) { continue; } From 6b05f44339fe4c0e240ec5251185a0f595d5702e Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 14:10:11 +1200 Subject: [PATCH 13/17] Updated list operations --- common/list.c | 73 ++++++++++++++++++++++-------------------- common/stringop.c | 2 +- include/list.h | 37 +++++++++++---------- sway/commands/mark.c | 6 ++-- sway/commands/unmark.c | 3 +- 5 files changed, 63 insertions(+), 58 deletions(-) diff --git a/common/list.c b/common/list.c index 16956632f..98005d9e0 100644 --- a/common/list.c +++ b/common/list.c @@ -54,26 +54,6 @@ static bool resize(list_t *list) { return true; } -void list_add(list_t *list, const void *data) { - if (!sway_assert(list && data, "Invalid argument") || !resize(list)) { - return; - } - - size_t size = list->memb_size; - uint8_t (*array)[size] = list->items; - - memcpy(&array[list->length], data, size); - ++list->length; -} - -void *list_alloc(list_t *list) { - if (!sway_assert(list, "Invalid argument") || !resize(list)) { - return NULL; - } - - return (uint8_t *)list->items + list->memb_size * list->length++; -} - void list_insert(list_t *list, size_t index, const void *data) { if (!sway_assert(list && data && index <= list->length, "Invalid argument") || !resize(list)) { @@ -88,6 +68,14 @@ void list_insert(list_t *list, size_t index, const void *data) { ++list->length; } +void list_add(list_t *list, const void *data) { + if (!sway_assert(list && data, "Invalid argument")) { + return; + } + + list_insert(list, list->length, data); +} + static void shrink(list_t *list) { /* We shrink very sparse lists, but only down to a certain size. * The choice of >= 8 is somewhat arbitrary, but leaves a minimum @@ -105,15 +93,6 @@ static void shrink(list_t *list) { } } -void list_remove(list_t *list) { - if (!sway_assert(list, "Invalid argument") || list->length == 0) { - return; - } - - --list->length; - shrink(list); -} - void list_delete(list_t *list, size_t index) { if (!sway_assert(list && index < list->length, "Invalid argument")) { return; @@ -127,6 +106,14 @@ void list_delete(list_t *list, size_t index) { shrink(list); } +void list_remove(list_t *list) { + if (!sway_assert(list, "Invalid argument") || list->length == 0) { + return; + } + + list_delete(list, list->length - 1); +} + void list_swap(list_t *list, size_t i1, size_t i2) { if (!sway_assert(list && i1 < list->length && i2 < list->length, "Invalid argument")) { return; @@ -240,7 +227,20 @@ void list_foreach(list_t *list, void callback(void *item)) { } } -void list_free_with(list_t *list, void callback(void *item)) { +void list_foreachp(list_t *list, void callback(void *item)) { + if (!sway_assert(list && callback, "Invalid argument")) { + return; + } + + size_t size = list->memb_size; + uint8_t (*array)[size] = list->items; + + for (size_t i = 0; i < list->length; ++i) { + callback(*(void **)&array[i]); + } +} + +void list_free_with(list_t *list, freefn_t callback) { if (!sway_assert(callback, "Invalid argument") || !list) { return; } @@ -249,6 +249,15 @@ void list_free_with(list_t *list, void callback(void *item)) { list_free(list); } +void list_free_withp(list_t *list, freefn_t callback) { + if (!sway_assert(callback, "Invalid argument") || !list) { + return; + } + + list_foreachp(list, callback); + list_free(list); +} + void *list_end(list_t *list) { if (!sway_assert(list, "Invalid argument")) { return NULL; @@ -256,7 +265,3 @@ void *list_end(list_t *list) { return (uint8_t *)list->items + list->memb_size * list->length; } - -void list_elem_free(void *item) { - free(*(void **)item); -} diff --git a/common/stringop.c b/common/stringop.c index 7a7696516..14dd4c3fc 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -84,7 +84,7 @@ list_t *split_string(const char *str, const char *delims) { } void free_flat_list(list_t *list) { - list_free_with(list, list_elem_free); + list_free_withp(list, free); } char **split_args(const char *start, int *argc) { diff --git a/include/list.h b/include/list.h index 5ab15c227..757a71f6c 100644 --- a/include/list.h +++ b/include/list.h @@ -27,36 +27,30 @@ list_t *list_new(size_t memb_size, size_t capacity); */ void list_free(list_t *list); +typedef void (*freefn_t)(void *arg); + /* * Frees a list, calling a function on each element before doing so. * If list is null, no action is taken. - * DO NOT pass free as the callback. Use list_elem_free if - * you want to do that. + * DO NOT pass free as the callback. Use list_free_withp for that. */ -void list_free_with(list_t *list, void callback(void *item)); +void list_free_with(list_t *list, freefn_t callback); /* - * This is a convinience function designed to be used with - * list_free_with or list_foreach. - * It calls the stdlib free function on each element. - * We can't pass in free directly, as each pointer needs to be - * dereferenced first. - * This should only be used on lists of pointers that were - * allocated using malloc (or similar). + * Frees a list, calling a function on each element after it's + * dereferenced before doing do. + * For example, if you have a list of char * allocated with malloc, + * it would be safe to use free with this function. + * If list is null, no action is taken. + * list must be a list of pointers. */ -void list_elem_free(void *item); +void list_free_withp(list_t *list, freefn_t callback); /* * Adds an element at the end of the list. */ void list_add(list_t *list, const void *data); -/* - * Adds an uninitialized element at the end of the list, - * and returns a pointer to it. - */ -void *list_alloc(list_t *list); - /* * Deletes the last element of the list. * If the list is empty, no action is taken. @@ -132,6 +126,15 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void */ void list_foreach(list_t *list, void callback(void *item)); +/* + * Calls a function on every item in the list, after dereferencing + * the element. + * For example, if you have a list of char *, you will receive a char * + * in callback, instead of a char **, unlike list_foreach. + * list must be a list of pointers. + */ +void list_foreachp(list_t *list, void callback(void *item)); + /* * Returns a pointer to just past the end of the list. * This can be used to write for loops more easily. diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 66452a62c..a93239a20 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -68,13 +68,11 @@ struct cmd_results *cmd_mark(int argc, char **argv) { } else { if (toggle && list_lsearch(view->marks, strcmp_ptr, &mark, NULL) != -1) { // Delete the list - list_foreach(view->marks, list_elem_free); - list_free(view->marks); + list_free_withp(view->marks, free); view->marks = NULL; } else { // Delete and replace with a new list - list_foreach(view->marks, list_elem_free); - list_free(view->marks); + list_free_withp(view->marks, free); view->marks = list_new(sizeof(char *), 0); list_add(view->marks, &mark); diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index 30c001173..938c0a84d 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -23,8 +23,7 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { } free(mark); } else { - list_foreach(view->marks, list_elem_free); - list_free(view->marks); + list_free_with(view->marks, free); view->marks = NULL; } } From 861e32a8ab0df73cbb7a7d28391d4bdc4861ddb6 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 14:26:13 +1200 Subject: [PATCH 14/17] Changed free_config --- include/list.h | 15 ++++++----- sway/config.c | 68 +++++++++----------------------------------------- 2 files changed, 19 insertions(+), 64 deletions(-) diff --git a/include/list.h b/include/list.h index 757a71f6c..b66ff72fc 100644 --- a/include/list.h +++ b/include/list.h @@ -37,10 +37,10 @@ typedef void (*freefn_t)(void *arg); void list_free_with(list_t *list, freefn_t callback); /* - * Frees a list, calling a function on each element after it's - * dereferenced before doing do. + * Frees a list, calling a function on each dereferenced element. * For example, if you have a list of char * allocated with malloc, - * it would be safe to use free with this function. + * callback would receive a char * instead of a char **, meaning it's + * safe to use free as the callback. * If list is null, no action is taken. * list must be a list of pointers. */ @@ -84,7 +84,7 @@ void list_swap(list_t *list, size_t i1, size_t i2); void *list_get(list_t *list, size_t index); /* - * Gets an element of a list and dereferences it. + * Gets an dereferenced element of a list. * For example, if you have a list of char *, this function * will return a char * instead of a char **, unlike list_get. * index must be less than the length of the list. @@ -127,10 +127,9 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void void list_foreach(list_t *list, void callback(void *item)); /* - * Calls a function on every item in the list, after dereferencing - * the element. - * For example, if you have a list of char *, you will receive a char * - * in callback, instead of a char **, unlike list_foreach. + * Calls a function on every dereferenced item in the list. + * For example, if you have a list of char *, callback will + * receive a char * instead of a char **, unlike list_foreach. * list must be a list of pointers. */ void list_foreachp(list_t *list, void callback(void *item)); diff --git a/sway/config.c b/sway/config.c index 89ab80738..a579dddb4 100644 --- a/sway/config.c +++ b/sway/config.c @@ -214,63 +214,19 @@ void free_config(struct sway_config *config) { if (!config) { return; } - size_t i; - for (i = 0; config->symbols && i < config->symbols->length; ++i) { - free_variable(list_getp(config->symbols, i)); - } - list_free(config->symbols); - for (i = 0; config->modes && i < config->modes->length; ++i) { - free_mode(list_getp(config->modes, i)); - } - list_free(config->modes); - - for (i = 0; config->bars && i < config->bars->length; ++i) { - free_bar(list_getp(config->bars, i)); - } - list_free(config->bars); - - free_flat_list(config->cmd_queue); - - for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { - free_workspace_output(list_getp(config->workspace_outputs, i)); - } - list_free(config->workspace_outputs); - - for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { - free_pid_workspace(list_getp(config->pid_workspaces, i)); - } - list_free(config->pid_workspaces); - - for (i = 0; config->criteria && i < config->criteria->length; ++i) { - free_criteria(list_getp(config->criteria, i)); - } - list_free(config->criteria); - - for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { - free_criteria(list_getp(config->no_focus, i)); - } - list_free(config->no_focus); - - for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { - free_input_config(list_getp(config->input_configs, i)); - } - list_free(config->input_configs); - - for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { - free_output_config(list_getp(config->output_configs, i)); - } - list_free(config->output_configs); - - for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { - free_command_policy(list_getp(config->command_policies, i)); - } - list_free(config->command_policies); - - for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { - free_feature_policy(list_getp(config->feature_policies, i)); - } - list_free(config->feature_policies); + list_free_withp(config->symbols, (freefn_t)free_variable); + list_free_withp(config->modes, (freefn_t)free_mode); + list_free_withp(config->bars, (freefn_t)free_bar); + list_free_withp(config->cmd_queue, free); + list_free_withp(config->workspace_outputs, (freefn_t)free_workspace_output); + list_free_withp(config->pid_workspaces, (freefn_t)free_pid_workspace); + list_free_withp(config->criteria, (freefn_t)free_criteria); + list_free_withp(config->no_focus, (freefn_t)free_criteria); + list_free_withp(config->input_configs, (freefn_t)free_input_config); + list_free_withp(config->output_configs, (freefn_t)free_output_config); + list_free_withp(config->command_policies, (freefn_t)free_command_policy); + list_free_withp(config->feature_policies, (freefn_t)free_feature_policy); list_free(config->active_bar_modifiers); free_flat_list(config->config_chain); From 6460a15d949eba3f79428a08df88854b9ede611d Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 14:34:36 +1200 Subject: [PATCH 15/17] Fixed warning with clang --- sway/layout.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sway/layout.c b/sway/layout.c index ec92b8f0e..7489a24af 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1656,7 +1656,7 @@ int auto_group_start_index(const swayc_t *container, int index) { } else { start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); } - return MIN(start_idx, container->children->length); + return MIN(start_idx, (ssize_t)container->children->length); } } @@ -1684,7 +1684,7 @@ int auto_group_end_index(const swayc_t *container, int index) { nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); } } - return MIN(nxt_idx, container->children->length); + return MIN(nxt_idx, (ssize_t)container->children->length); } } From f5d346d304632b85d8c3690129d51f91be338b49 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 14 May 2017 13:51:24 +1200 Subject: [PATCH 16/17] fixes --- common/stringop.c | 10 +++------- include/stringop.h | 3 +-- sway/commands/bar/modifier.c | 4 ++-- sway/commands/bind.c | 8 ++++---- sway/commands/floating_mod.c | 2 +- sway/config.c | 8 ++++---- wayland/registry.c | 2 +- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/common/stringop.c b/common/stringop.c index 14dd4c3fc..b2a64aec1 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -83,10 +83,6 @@ list_t *split_string(const char *str, const char *delims) { return res; } -void free_flat_list(list_t *list) { - list_free_withp(list, free); -} - char **split_args(const char *start, int *argc) { *argc = 0; int alloc = 2; @@ -312,18 +308,18 @@ char *join_list(list_t *list, char *separator) { } for (size_t i = 0; i < list->length; i++) { - char *item = *(char **)list_get(list, i); + char *item = list_getp(list, i); len += strlen(item); } char *res = malloc(len); - char *item = *(char **)list_get(list, 0); + char *item = list_getp(list, 0); char *p = res + strlen(item); strcpy(res, item); for (size_t i = 1; i < list->length; i++) { - item = *(char **)list_get(list, i); + item = list_getp(list, i); if (sep_len) { memcpy(p, separator, sep_len); p += sep_len; diff --git a/include/stringop.h b/include/stringop.h index 85659b0ad..96d56a020 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -21,9 +21,8 @@ int strcmp_ptr(const void *a, const void *b); // strcmp that also handles null pointers. int lenient_strcmp(char *a, char *b); -// Simply split a string with delims, free with `free_flat_list` +// Simply split a string with delims, free with `list_free_withp(..., free)` list_t *split_string(const char *str, const char *delims); -void free_flat_list(list_t *list); // Splits an argument string, keeping quotes intact char **split_args(const char *str, int *argc); diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c index 89c89d90b..4eb577026 100644 --- a/sway/commands/bar/modifier.c +++ b/sway/commands/bar/modifier.c @@ -24,11 +24,11 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) { mod |= tmp_mod; continue; } else { - free_flat_list(split); + list_free_withp(split, free); return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", item); } } - free_flat_list(split); + list_free_withp(split, free); config->current_bar->modifier = mod; sway_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); diff --git a/sway/commands/bind.c b/sway/commands/bind.c index cecb72dbe..528e47557 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -63,21 +63,21 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { } if (!sym) { free_sway_binding(binding); - free_flat_list(split); + list_free_withp(split, free); return cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", item); } xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); if (!key) { free_sway_binding(binding); - free_flat_list(split); + list_free_withp(split, free); return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding"); } *key = sym; list_add(binding->keys, &key); } - free_flat_list(split); + list_free_withp(split, free); struct sway_mode *mode = config->current_mode; struct sway_binding *dup; @@ -148,7 +148,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { *key = keycode - 8; list_add(binding->keys, &key); } - free_flat_list(split); + list_free_withp(split, free); struct sway_mode *mode = config->current_mode; struct sway_binding *dup; diff --git a/sway/commands/floating_mod.c b/sway/commands/floating_mod.c index aec85a1e4..b8f853214 100644 --- a/sway/commands/floating_mod.c +++ b/sway/commands/floating_mod.c @@ -19,7 +19,7 @@ struct cmd_results *cmd_floating_mod(int argc, char **argv) { for (size_t i = 0; i < split->length; ++i) { config->floating_mod |= get_modifier_mask_by_name(list_getp(split, i)); } - free_flat_list(split); + list_free_withp(split, free); if (!config->floating_mod) { error = cmd_results_new(CMD_INVALID, "floating_modifier", "Unknown keys %s", argv[0]); return error; diff --git a/sway/config.c b/sway/config.c index a579dddb4..3b3fbf9ed 100644 --- a/sway/config.c +++ b/sway/config.c @@ -45,7 +45,7 @@ static void free_binding(struct sway_binding *bind) { if (!bind) { return; } - free_flat_list(bind->keys); + list_free_withp(bind->keys, free); free(bind->command); free(bind); } @@ -77,7 +77,7 @@ static void free_bar(struct bar_config *bar) { list_free(bar->bindings); if (bar->outputs) { - free_flat_list(bar->outputs); + list_free_withp(bar->outputs, free); } if (bar->pid != 0) { @@ -229,7 +229,7 @@ void free_config(struct sway_config *config) { list_free_withp(config->feature_policies, (freefn_t)free_feature_policy); list_free(config->active_bar_modifiers); - free_flat_list(config->config_chain); + list_free_withp(config->config_chain, free); free(config->font); free(config->floating_scroll_up_cmd); free(config->floating_scroll_down_cmd); @@ -514,7 +514,7 @@ bool load_main_config(const char *file, bool is_active) { } } - free_flat_list(secconfigs); + list_free_withp(secconfigs, free); } success = success && load_config(path, config); diff --git a/wayland/registry.c b/wayland/registry.c index a29efd7c9..d599c9786 100644 --- a/wayland/registry.c +++ b/wayland/registry.c @@ -287,7 +287,7 @@ void registry_teardown(struct registry *registry) { wl_display_disconnect(registry->display); } if (registry->outputs) { - free_flat_list(registry->outputs); + list_free_withp(registry->outputs, free); } free(registry); } From 69f03bbde2adc4f30cb844db341882288c5fccc2 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 14 May 2017 14:32:04 +1200 Subject: [PATCH 17/17] Undo seaching changes. --- common/list.c | 42 ++++++++++++++++------------- include/list.h | 12 +++++---- sway/commands.c | 2 +- sway/commands/assign.c | 2 +- sway/commands/bar/bindsym.c | 2 +- sway/commands/bind.c | 4 +-- sway/commands/for_window.c | 2 +- sway/commands/mark.c | 6 ++--- sway/commands/no_focus.c | 2 +- sway/commands/output.c | 2 +- sway/commands/unmark.c | 2 +- sway/commands/workspace.c | 2 +- sway/config.c | 54 ++++++++++++++++--------------------- sway/criteria.c | 32 ++++++++++------------ 14 files changed, 80 insertions(+), 86 deletions(-) diff --git a/common/list.c b/common/list.c index 98005d9e0..4b0c3a304 100644 --- a/common/list.c +++ b/common/list.c @@ -174,24 +174,6 @@ void list_isort(list_t *list, int compare(const void *, const void *)) { } } -ssize_t list_bsearch(const list_t *list, int compare(const void *key, const void *item), - const void *key, void *ret) { - - if (!sway_assert(list && compare && key, "Invalid argument")) { - return -1; - } - - const uint8_t *ptr = bsearch(key, list->items, list->length, list->memb_size, compare); - if (!ptr) { - return -1; - } else { - if (ret) { - memcpy(ret, ptr, list->memb_size); - } - return (ptr - (uint8_t *)list->items) / list->memb_size; - } -} - ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void *item), const void *key, void *ret) { @@ -203,7 +185,29 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void uint8_t (*array)[size] = list->items; for (size_t i = 0; i < list->length; ++i) { - if (compare(key, &array[i]) == 0) { + if (compare(&array[i], key) == 0) { + if (ret) { + memcpy(ret, &array[i], size); + } + return i; + } + } + + return -1; +} + +ssize_t list_lsearchp(const list_t *list, int compare(const void *key, const void *item), + const void *key, void *ret) { + + if (!sway_assert(list && compare && key, "Invalid argument")) { + return -1; + } + + size_t size = list->memb_size; + uint8_t (*array)[size] = list->items; + + for (size_t i = 0; i < list->length; ++i) { + if (compare(*(void **)&array[i], key) == 0) { if (ret) { memcpy(ret, &array[i], size); } diff --git a/include/list.h b/include/list.h index b66ff72fc..09b8840ae 100644 --- a/include/list.h +++ b/include/list.h @@ -105,20 +105,22 @@ void list_qsort(list_t *list, int compare(const void *left, const void *right)); void list_isort(list_t *list, int compare(const void *left, const void *right)); /* - * Returns the index of key in the list, using the stdlib bsearch() function, + * Returns the index of the key in the list, using a linear search, * or -1 if it was not found. If ret is not null, the found element will be * copied into it. - * The list must be sorted. */ -ssize_t list_bsearch(const list_t *list, int compare(const void *key, const void *item), +ssize_t list_lsearch(const list_t *list, int compare(const void *item, const void *key), const void *key, void *ret); /* * Returns the index of the key in the list, using a linear search, * or -1 if it was not found. If ret is not null, the found element will be - * copied into it. + * copied into it. The item passed into compare will be dereferenced first. + * For example, if you have a list of char *, it will get a char * instead + * of a char **, unlike list_lsearch. + * list must be a list of pointers. */ -ssize_t list_lsearch(const list_t *list, int compare(const void *key, const void *item), +ssize_t list_lsearchp(const list_t *list, int compare(const void *item, const void *key), const void *key, void *ret); /* diff --git a/sway/commands.c b/sway/commands.c index e4ceafc81..e6c2e6242 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -103,7 +103,7 @@ void hide_view_in_scratchpad(swayc_t *sp_view) { void input_cmd_apply(struct input_config *input) { struct input_config *ic; - ssize_t i = list_lsearch(config->input_configs, input_identifier_cmp, input->identifier, &ic); + ssize_t i = list_lsearchp(config->input_configs, input_identifier_cmp, input->identifier, &ic); if (i >= 0) { // merge existing config merge_input_config(ic, input); diff --git a/sway/commands/assign.c b/sway/commands/assign.c index 10b5e5d78..d5a90296c 100644 --- a/sway/commands/assign.c +++ b/sway/commands/assign.c @@ -46,7 +46,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->criteria, criteria_cmp, &crit, NULL) != -1) { + } else if (list_lsearchp(config->criteria, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "assign: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c index 912286faa..212e77a50 100644 --- a/sway/commands/bar/bindsym.c +++ b/sway/commands/bar/bindsym.c @@ -34,7 +34,7 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { struct bar_config *bar = config->current_bar; struct sway_mouse_binding *dup; - ssize_t i = list_lsearch(bar->bindings, sway_mouse_binding_cmp_buttons, &binding, &dup); + ssize_t i = list_lsearchp(bar->bindings, sway_mouse_binding_cmp_buttons, binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]); free_sway_mouse_binding(dup); diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 528e47557..fdf9c1eae 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -81,7 +81,7 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) { struct sway_mode *mode = config->current_mode; struct sway_binding *dup; - ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, &binding, &dup); + ssize_t i = list_lsearchp(mode->bindings, sway_binding_cmp_keys, binding, &dup); if (i > -1) { sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]); free_sway_binding(dup); @@ -152,7 +152,7 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) { struct sway_mode *mode = config->current_mode; struct sway_binding *dup; - ssize_t i = list_lsearch(mode->bindings, sway_binding_cmp_keys, &binding, &dup); + ssize_t i = list_lsearchp(mode->bindings, sway_binding_cmp_keys, binding, &dup); if (i > -1) { if (dup->bindcode) { sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]); diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c index d09490dca..884421580 100644 --- a/sway/commands/for_window.c +++ b/sway/commands/for_window.c @@ -30,7 +30,7 @@ struct cmd_results *cmd_for_window(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->criteria, criteria_cmp, &crit, NULL) != -1) { + } else if (list_lsearchp(config->criteria, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "for_window: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/mark.c b/sway/commands/mark.c index a93239a20..0bf823b7a 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -12,7 +12,7 @@ static void find_marks_callback(swayc_t *container, void *_mark) { return; } - ssize_t index = list_lsearch(container->marks, strcmp_ptr, &mark, NULL); + ssize_t index = list_lsearchp(container->marks, (int (*)(const void *, const void *))strcmp, mark, NULL); if (index != -1) { list_delete(container->marks, index); } @@ -51,7 +51,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { if (add) { ssize_t index; char *item; - if ((index = list_lsearch(view->marks, strcmp_ptr, &mark, &item)) != -1) { + if ((index = list_lsearchp(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { if (toggle) { free(item); list_delete(view->marks, index); @@ -66,7 +66,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { list_add(view->marks, &mark); } } else { - if (toggle && list_lsearch(view->marks, strcmp_ptr, &mark, NULL) != -1) { + if (toggle && list_lsearchp(view->marks, (int (*)(const void *, const void *))strcmp, mark, NULL) != -1) { // Delete the list list_free_withp(view->marks, free); view->marks = NULL; diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c index adcbbbf9d..e38febb0b 100644 --- a/sway/commands/no_focus.c +++ b/sway/commands/no_focus.c @@ -30,7 +30,7 @@ struct cmd_results *cmd_no_focus(int argc, char **argv) { } else if (crit->tokens->length == 0) { error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); free_criteria(crit); - } else if (list_lsearch(config->no_focus, criteria_cmp, &crit, NULL) != -1) { + } else if (list_lsearchp(config->no_focus, criteria_cmp, crit, NULL) != -1) { sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); free_criteria(crit); } else { diff --git a/sway/commands/output.c b/sway/commands/output.c index b28233e07..3b7062cfe 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -157,7 +157,7 @@ struct cmd_results *cmd_output(int argc, char **argv) { } struct output_config *oc; - ssize_t i = list_lsearch(config->output_configs, output_name_cmp, &name, &oc); + ssize_t i = list_lsearchp(config->output_configs, output_name_cmp, name, &oc); if (i >= 0) { // merge existing config merge_output_config(oc, output); diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index 938c0a84d..0ad22bbbd 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -12,7 +12,7 @@ struct cmd_results *cmd_unmark(int argc, char **argv) { char *mark = join_args(argv, argc); char *item; ssize_t index; - if ((index = list_lsearch(view->marks, strcmp_ptr, &mark, &item)) != -1) { + if ((index = list_lsearchp(view->marks, (int (*)(const void *, const void *))strcmp, mark, &item)) != -1) { free(item); list_delete(view->marks, index); diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 34d14094d..d8817259b 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -36,7 +36,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { wso->output = strdup(argv[output_location + 1]); ssize_t i = -1; struct workspace_output *old; - if ((i = list_lsearch(config->workspace_outputs, workspace_output_cmp_workspace, &wso, &old)) != -1) { + if ((i = list_lsearchp(config->workspace_outputs, workspace_output_cmp_workspace, wso, &old)) != -1) { free(old); // workspaces can only be assigned to a single output list_delete(config->workspace_outputs, i); } diff --git a/sway/config.c b/sway/config.c index 3b3fbf9ed..5f2115b04 100644 --- a/sway/config.c +++ b/sway/config.c @@ -351,9 +351,9 @@ cleanup: sway_abort("Unable to allocate config structures"); } -static int compare_modifiers(const void *key, const void *item) { - uint32_t a = **(uint32_t **)item; - uint32_t b = *(uint32_t *)key; +static int compare_modifiers(const void *left, const void *right) { + uint32_t a = *(uint32_t *)left; + uint32_t b = *(uint32_t *)right; return a - b; } @@ -367,7 +367,7 @@ void update_active_bar_modifiers() { for (size_t i = 0; i < config->bars->length; ++i) { struct bar_config *bar = list_getp(config->bars, i); if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { - if (list_lsearch(config->active_bar_modifiers, compare_modifiers, &bar->modifier, NULL) < 0) { + if (list_lsearchp(config->active_bar_modifiers, compare_modifiers, &bar->modifier, NULL) < 0) { list_add(config->active_bar_modifiers, &bar->modifier); } } @@ -781,17 +781,17 @@ bool read_config(FILE *file, struct sway_config *config) { return success; } -int input_identifier_cmp(const void *key, const void *item) { - const struct input_config *const *ic = item; - const char *identifier = key; - return strcmp((*ic)->identifier, identifier); +int input_identifier_cmp(const void *item, const void *data) { + const struct input_config *ic = item; + const char *identifier = data; + return strcmp(ic->identifier, identifier); } -int output_name_cmp(const void *key, const void *item) { - const struct output_config *const *output = item; - const char *const *name = key; +int output_name_cmp(const void *item, const void *data) { + const struct output_config *output = item; + const char *name = data; - return strcmp((*output)->name, *name); + return strcmp(output->name, name); } void merge_input_config(struct input_config *dst, struct input_config *src) { @@ -1093,7 +1093,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { // Look for a * config for background struct output_config *item; const char *str = "*"; - if (list_lsearch(config->output_configs, output_name_cmp, &str, &item) != -1) { + if (list_lsearchp(config->output_configs, output_name_cmp, str, &item) != -1) { oc = item; } else { oc = NULL; @@ -1181,14 +1181,13 @@ char *do_var_replacement(char *str) { // the naming is intentional (albeit long): a workspace_output_cmp function // would compare two structs in full, while this method only compares the // workspace. -int workspace_output_cmp_workspace(const void *key, const void *item) { - const struct workspace_output *const *wsa = item, *const *wsb = key; - return lenient_strcmp((*wsa)->workspace, (*wsb)->workspace); +int workspace_output_cmp_workspace(const void *a, const void *b) { + const struct workspace_output *wsa = a, *wsb = b; + return lenient_strcmp(wsa->workspace, wsb->workspace); } -int sway_binding_cmp_keys(const void *key, const void *item) { - const struct sway_binding *binda = *(struct sway_binding **)item; - const struct sway_binding *bindb = *(struct sway_binding **)key; +int sway_binding_cmp_keys(const void *a, const void *b) { + const struct sway_binding *binda = a, *bindb = b; // Count keys pressed for this binding. important so we check long before // short ones. for example mod+a+b before mod+a @@ -1235,7 +1234,7 @@ int sway_binding_cmp_keys(const void *key, const void *item) { int sway_binding_cmp(const void *a, const void *b) { int cmp = 0; - if ((cmp = sway_binding_cmp_keys(&a, &b)) != 0) { + if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { return cmp; } const struct sway_binding *binda = a, *bindb = b; @@ -1247,20 +1246,13 @@ int sway_binding_cmp_qsort(const void *a, const void *b) { } void free_sway_binding(struct sway_binding *binding) { - if (binding->keys) { - for (size_t i = 0; i < binding->keys->length; i++) { - free(list_getp(binding->keys, i)); - } - list_free(binding->keys); - } - if (binding->command) { - free(binding->command); - } + list_free_withp(binding->keys, free); + free(binding->command); free(binding); } -int sway_mouse_binding_cmp_buttons(const void *key, const void *item) { - const struct sway_mouse_binding *binda = item, *bindb = key; +int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { + const struct sway_mouse_binding *binda = a, *bindb = b; if (binda->button > bindb->button) { return 1; } diff --git a/sway/criteria.c b/sway/criteria.c index cd87ee4f2..4aec3fd7e 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -238,10 +238,8 @@ ect_cleanup: return error; } -static int regex_cmp(const void *key, const void *item) { - const pcre *regex = key; - const char *const *str = item; - return pcre_exec(regex, NULL, *str, strlen(*str), 0, 0, NULL, 0); +static int regex_cmp(const char *item, const pcre *regex) { + return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); } // test a single view if it matches list of criteria tokens (all of them). @@ -261,15 +259,14 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->class && strcmp(cont->class, focused->class) == 0) { matches++; } - } else if (crit->regex && regex_cmp(crit->regex, &cont->class) == 0) { + } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) { matches++; } break; case CRIT_CON_MARK: - if (crit->regex && cont->marks && (list_lsearch(cont->marks, regex_cmp, crit->regex, NULL) != -1)) { + if (crit->regex && cont->marks && (list_lsearchp(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex, NULL) != -1)) { // Make sure it isn't matching the NUL string - const char *nul = ""; - if ((strcmp(crit->raw, "") == 0) == (list_lsearch(cont->marks, strcmp_ptr, &nul, NULL) != -1)) { + if ((strcmp(crit->raw, "") == 0) == (list_lsearchp(cont->marks, (int (*)(const void *, const void *))strcmp, "", NULL) != -1)) { ++matches; } } @@ -277,7 +274,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { case CRIT_ID: if (!cont->app_id) { // ignore - } else if (crit->regex && regex_cmp(crit->regex, &cont->app_id) == 0) { + } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) { matches++; } break; @@ -289,7 +286,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { matches++; } - } else if (crit->regex && regex_cmp(crit->regex, &cont->instance) == 0) { + } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) { matches++; } break; @@ -301,7 +298,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->name && strcmp(cont->name, focused->name) == 0) { matches++; } - } else if (crit->regex && regex_cmp(crit->regex, &cont->name) == 0) { + } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { matches++; } break; @@ -321,7 +318,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { matches++; } - } else if (crit->regex && regex_cmp(crit->regex, &cont_ws->name) == 0) { + } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) { matches++; } break; @@ -333,16 +330,15 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { return matches == tokens->length; } -int criteria_cmp(const void *key, const void *item) { - if (key == item) { +int criteria_cmp(const void *a, const void *b) { + if (a == b) { return 0; - } else if (!item) { + } else if (!a) { return -1; - } else if (!key) { + } else if (!b) { return 1; } - const struct criteria *crit_a = *(struct criteria **)item; - const struct criteria *crit_b = *(struct criteria **)key; + const struct criteria *crit_a = a, *crit_b = b; int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); if (cmp != 0) { return cmp;