aboutsummaryrefslogtreecommitdiffstats
path: root/common/list.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-11-12 19:04:01 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-11-12 19:04:01 -0500
commitbfcabe48ef3fc7a0388de007504fc232f826fb84 (patch)
tree8bef61a10259765dbafed49c9a2a76b4bf9ced2d /common/list.c
parentMerge branch 'master' of github.com:SirCmpwn/sway (diff)
downloadsway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.gz
sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.zst
sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.zip
Start fleshing out wayland client implementation
This introduces a basic shared framework for making wayland clients within sway itself.
Diffstat (limited to 'common/list.c')
-rw-r--r--common/list.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/common/list.c b/common/list.c
new file mode 100644
index 00000000..45efc16f
--- /dev/null
+++ b/common/list.c
@@ -0,0 +1,55 @@
1#include "list.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6list_t *create_list(void) {
7 list_t *list = malloc(sizeof(list_t));
8 list->capacity = 10;
9 list->length = 0;
10 list->items = malloc(sizeof(void*) * list->capacity);
11 return list;
12}
13
14static void list_resize(list_t *list) {
15 if (list->length == list->capacity) {
16 list->capacity += 10;
17 list->items = realloc(list->items, sizeof(void*) * list->capacity);
18 }
19}
20
21void list_free(list_t *list) {
22 if (list == NULL) {
23 return;
24 }
25 free(list->items);
26 free(list);
27}
28
29void list_add(list_t *list, void *item) {
30 list_resize(list);
31 list->items[list->length++] = item;
32}
33
34void list_insert(list_t *list, int index, void *item) {
35 list_resize(list);
36 memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index));
37 list->length++;
38 list->items[index] = item;
39}
40
41void list_del(list_t *list, int index) {
42 list->length--;
43 memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index));
44}
45
46void list_cat(list_t *list, list_t *source) {
47 int i;
48 for (i = 0; i < source->length; ++i) {
49 list_add(list, source->items[i]);
50 }
51}
52
53void list_sort(list_t *list, int compare(const void *left, const void *right)) {
54 qsort(list->items, list->length, sizeof(void *), compare);
55}