diff options
Diffstat (limited to 'sway/config/seat.c')
-rw-r--r-- | sway/config/seat.c | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/sway/config/seat.c b/sway/config/seat.c new file mode 100644 index 00000000..3a2fdaa6 --- /dev/null +++ b/sway/config/seat.c | |||
@@ -0,0 +1,127 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include "sway/config.h" | ||
5 | #include "log.h" | ||
6 | |||
7 | struct seat_config *new_seat_config(const char* name) { | ||
8 | struct seat_config *seat = calloc(1, sizeof(struct seat_config)); | ||
9 | if (!seat) { | ||
10 | sway_log(L_DEBUG, "Unable to allocate seat config"); | ||
11 | return NULL; | ||
12 | } | ||
13 | |||
14 | sway_log(L_DEBUG, "new_seat_config(%s)", name); | ||
15 | seat->name = strdup(name); | ||
16 | if (!sway_assert(seat->name, "could not allocate name for seat")) { | ||
17 | return NULL; | ||
18 | } | ||
19 | |||
20 | seat->attachments = create_list(); | ||
21 | if (!sway_assert(seat->attachments, | ||
22 | "could not allocate seat attachments list")) { | ||
23 | return NULL; | ||
24 | } | ||
25 | |||
26 | return seat; | ||
27 | } | ||
28 | |||
29 | struct seat_attachment_config *seat_attachment_config_new() { | ||
30 | struct seat_attachment_config *attachment = | ||
31 | calloc(1, sizeof(struct seat_attachment_config)); | ||
32 | if (!attachment) { | ||
33 | sway_log(L_DEBUG, "cannot allocate attachment config"); | ||
34 | return NULL; | ||
35 | } | ||
36 | return attachment; | ||
37 | } | ||
38 | |||
39 | static void seat_attachment_config_free( | ||
40 | struct seat_attachment_config *attachment) { | ||
41 | free(attachment->identifier); | ||
42 | free(attachment); | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | static struct seat_attachment_config *seat_attachment_config_copy( | ||
47 | struct seat_attachment_config *attachment) { | ||
48 | struct seat_attachment_config *copy = seat_attachment_config_new(); | ||
49 | if (!copy) { | ||
50 | return NULL; | ||
51 | } | ||
52 | |||
53 | copy->identifier = strdup(attachment->identifier); | ||
54 | |||
55 | return copy; | ||
56 | } | ||
57 | |||
58 | static void merge_seat_attachment_config(struct seat_attachment_config *dest, | ||
59 | struct seat_attachment_config *source) { | ||
60 | // nothing to merge yet, but there will be some day | ||
61 | } | ||
62 | |||
63 | void merge_seat_config(struct seat_config *dest, struct seat_config *source) { | ||
64 | if (source->name) { | ||
65 | free(dest->name); | ||
66 | dest->name = strdup(source->name); | ||
67 | } | ||
68 | |||
69 | for (int i = 0; i < source->attachments->length; ++i) { | ||
70 | struct seat_attachment_config *source_attachment = | ||
71 | source->attachments->items[i]; | ||
72 | bool found = false; | ||
73 | for (int j = 0; j < dest->attachments->length; ++j) { | ||
74 | struct seat_attachment_config *dest_attachment = | ||
75 | dest->attachments->items[j]; | ||
76 | if (strcmp(source_attachment->identifier, | ||
77 | dest_attachment->identifier) == 0) { | ||
78 | merge_seat_attachment_config(dest_attachment, | ||
79 | source_attachment); | ||
80 | found = true; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | if (!found) { | ||
85 | struct seat_attachment_config *copy = | ||
86 | seat_attachment_config_copy(source_attachment); | ||
87 | if (copy) { | ||
88 | list_add(dest->attachments, copy); | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | |||
94 | void free_seat_config(struct seat_config *seat) { | ||
95 | if (!seat) { | ||
96 | return; | ||
97 | } | ||
98 | |||
99 | free(seat->name); | ||
100 | for (int i = 0; i < seat->attachments->length; ++i) { | ||
101 | struct seat_attachment_config *attachment = | ||
102 | seat->attachments->items[i]; | ||
103 | seat_attachment_config_free(attachment); | ||
104 | } | ||
105 | |||
106 | list_free(seat->attachments); | ||
107 | free(seat); | ||
108 | } | ||
109 | |||
110 | int seat_name_cmp(const void *item, const void *data) { | ||
111 | const struct seat_config *sc = item; | ||
112 | const char *name = data; | ||
113 | return strcmp(sc->name, name); | ||
114 | } | ||
115 | |||
116 | struct seat_attachment_config *seat_config_get_attachment( | ||
117 | struct seat_config *seat_config, char *identifier) { | ||
118 | for (int i = 0; i < seat_config->attachments->length; ++i) { | ||
119 | struct seat_attachment_config *attachment = | ||
120 | seat_config->attachments->items[i]; | ||
121 | if (strcmp(attachment->identifier, identifier) == 0) { | ||
122 | return attachment; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | return NULL; | ||
127 | } | ||