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