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