From a2896f33a45d488f4717ef5d5129f5fade80a713 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 13 May 2017 00:22:49 +1200 Subject: [PATCH] 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);