diff options
author | 2015-11-12 19:04:01 -0500 | |
---|---|---|
committer | 2015-11-12 19:04:01 -0500 | |
commit | bfcabe48ef3fc7a0388de007504fc232f826fb84 (patch) | |
tree | 8bef61a10259765dbafed49c9a2a76b4bf9ced2d /sway | |
parent | Merge branch 'master' of github.com:SirCmpwn/sway (diff) | |
download | sway-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 'sway')
-rw-r--r-- | sway/debug_log.c | 102 | ||||
-rw-r--r-- | sway/list.c | 55 | ||||
-rw-r--r-- | sway/log.c | 247 |
3 files changed, 102 insertions, 302 deletions
diff --git a/sway/debug_log.c b/sway/debug_log.c new file mode 100644 index 00000000..4cb97561 --- /dev/null +++ b/sway/debug_log.c | |||
@@ -0,0 +1,102 @@ | |||
1 | #include "log.h" | ||
2 | #include "sway.h" | ||
3 | #include <stdarg.h> | ||
4 | #include <stdio.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <libgen.h> | ||
7 | #include <fcntl.h> | ||
8 | #include <unistd.h> | ||
9 | #include <signal.h> | ||
10 | #include <errno.h> | ||
11 | #include <string.h> | ||
12 | #include <stringop.h> | ||
13 | #include <execinfo.h> | ||
14 | #include "workspace.h" | ||
15 | |||
16 | extern log_importance_t v; | ||
17 | |||
18 | /* XXX:DEBUG:XXX */ | ||
19 | static void container_log(const swayc_t *c) { | ||
20 | fprintf(stderr, "focus:%c|", | ||
21 | c == get_focused_view(&root_container) ? 'K': | ||
22 | c == get_focused_container(&root_container) ? 'F' : // Focused | ||
23 | c == swayc_active_workspace() ? 'W' : // active workspace | ||
24 | c == &root_container ? 'R' : // root | ||
25 | 'X');// not any others | ||
26 | fprintf(stderr,"(%p)",c); | ||
27 | fprintf(stderr,"(p:%p)",c->parent); | ||
28 | fprintf(stderr,"(f:%p)",c->focused); | ||
29 | fprintf(stderr,"(h:%ld)",c->handle); | ||
30 | fprintf(stderr,"Type:"); | ||
31 | fprintf(stderr, | ||
32 | c->type == C_ROOT ? "Root|" : | ||
33 | c->type == C_OUTPUT ? "Output|" : | ||
34 | c->type == C_WORKSPACE ? "Workspace|" : | ||
35 | c->type == C_CONTAINER ? "Container|" : | ||
36 | c->type == C_VIEW ? "View|" : "Unknown|"); | ||
37 | fprintf(stderr,"layout:"); | ||
38 | fprintf(stderr, | ||
39 | c->layout == L_NONE ? "NONE|" : | ||
40 | c->layout == L_HORIZ ? "Horiz|": | ||
41 | c->layout == L_VERT ? "Vert|": | ||
42 | c->layout == L_STACKED ? "Stacked|": | ||
43 | c->layout == L_FLOATING ? "Floating|": | ||
44 | "Unknown|"); | ||
45 | fprintf(stderr, "w:%.f|h:%.f|", c->width, c->height); | ||
46 | fprintf(stderr, "x:%.f|y:%.f|", c->x, c->y); | ||
47 | fprintf(stderr, "g:%d|",c->gaps); | ||
48 | fprintf(stderr, "vis:%c|", c->visible?'t':'f'); | ||
49 | fprintf(stderr, "name:%.16s|", c->name); | ||
50 | fprintf(stderr, "children:%d\n",c->children?c->children->length:0); | ||
51 | } | ||
52 | void layout_log(const swayc_t *c, int depth) { | ||
53 | if (L_DEBUG > v) return; | ||
54 | int i, d; | ||
55 | int e = c->children ? c->children->length : 0; | ||
56 | container_log(c); | ||
57 | if (e) { | ||
58 | for (i = 0; i < e; ++i) { | ||
59 | fputc('|',stderr); | ||
60 | for (d = 0; d < depth; ++d) fputc('-', stderr); | ||
61 | layout_log(c->children->items[i], depth + 1); | ||
62 | } | ||
63 | } | ||
64 | if (c->type == C_WORKSPACE) { | ||
65 | e = c->floating?c->floating->length:0; | ||
66 | if (e) { | ||
67 | for (i = 0; i < e; ++i) { | ||
68 | fputc('|',stderr); | ||
69 | for (d = 0; d < depth; ++d) fputc('=', stderr); | ||
70 | layout_log(c->floating->items[i], depth + 1); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
76 | const char *swayc_type_string(enum swayc_types type) { | ||
77 | return type == C_ROOT ? "ROOT" : | ||
78 | type == C_OUTPUT ? "OUTPUT" : | ||
79 | type == C_WORKSPACE ? "WORKSPACE" : | ||
80 | type == C_CONTAINER ? "CONTAINER" : | ||
81 | type == C_VIEW ? "VIEW" : | ||
82 | "UNKNOWN"; | ||
83 | } | ||
84 | |||
85 | // Like sway_log, but also appends some info about given container to log output. | ||
86 | void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) { | ||
87 | sway_assert(cont, "swayc_log: no container ..."); | ||
88 | va_list args; | ||
89 | va_start(args, format); | ||
90 | char *txt = malloc(128); | ||
91 | vsprintf(txt, format, args); | ||
92 | va_end(args); | ||
93 | |||
94 | char *debug_txt = malloc(32); | ||
95 | snprintf(debug_txt, 32, "%s '%s'", swayc_type_string(cont->type), cont->name); | ||
96 | |||
97 | sway_log(verbosity, "%s (%s)", txt, debug_txt); | ||
98 | free(txt); | ||
99 | free(debug_txt); | ||
100 | } | ||
101 | |||
102 | /* XXX:DEBUG:XXX */ | ||
diff --git a/sway/list.c b/sway/list.c deleted file mode 100644 index 45efc16f..00000000 --- a/sway/list.c +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | #include "list.h" | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | |||
6 | list_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 | |||
14 | static 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 | |||
21 | void list_free(list_t *list) { | ||
22 | if (list == NULL) { | ||
23 | return; | ||
24 | } | ||
25 | free(list->items); | ||
26 | free(list); | ||
27 | } | ||
28 | |||
29 | void list_add(list_t *list, void *item) { | ||
30 | list_resize(list); | ||
31 | list->items[list->length++] = item; | ||
32 | } | ||
33 | |||
34 | void 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 | |||
41 | void 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 | |||
46 | void 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 | |||
53 | void list_sort(list_t *list, int compare(const void *left, const void *right)) { | ||
54 | qsort(list->items, list->length, sizeof(void *), compare); | ||
55 | } | ||
diff --git a/sway/log.c b/sway/log.c deleted file mode 100644 index f2b828f9..00000000 --- a/sway/log.c +++ /dev/null | |||
@@ -1,247 +0,0 @@ | |||
1 | #include "log.h" | ||
2 | #include "sway.h" | ||
3 | #include <stdarg.h> | ||
4 | #include <stdio.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <libgen.h> | ||
7 | #include <fcntl.h> | ||
8 | #include <unistd.h> | ||
9 | #include <signal.h> | ||
10 | #include <errno.h> | ||
11 | #include <string.h> | ||
12 | #include <stringop.h> | ||
13 | #include <execinfo.h> | ||
14 | |||
15 | int colored = 1; | ||
16 | log_importance_t loglevel_default = L_ERROR; | ||
17 | log_importance_t v = L_SILENT; | ||
18 | |||
19 | static const char *verbosity_colors[] = { | ||
20 | [L_SILENT] = "", | ||
21 | [L_ERROR ] = "\x1B[1;31m", | ||
22 | [L_INFO ] = "\x1B[1;34m", | ||
23 | [L_DEBUG ] = "\x1B[1;30m", | ||
24 | }; | ||
25 | |||
26 | void init_log(log_importance_t verbosity) { | ||
27 | if (verbosity != L_DEBUG) { | ||
28 | // command "debuglog" needs to know the user specified log level when | ||
29 | // turning off debug logging. | ||
30 | loglevel_default = verbosity; | ||
31 | } | ||
32 | v = verbosity; | ||
33 | signal(SIGSEGV, error_handler); | ||
34 | signal(SIGABRT, error_handler); | ||
35 | } | ||
36 | |||
37 | void set_log_level(log_importance_t verbosity) { | ||
38 | v = verbosity; | ||
39 | } | ||
40 | |||
41 | void reset_log_level(void) { | ||
42 | v = loglevel_default; | ||
43 | } | ||
44 | |||
45 | bool toggle_debug_logging(void) { | ||
46 | v = (v == L_DEBUG) ? loglevel_default : L_DEBUG; | ||
47 | return (v == L_DEBUG); | ||
48 | } | ||
49 | |||
50 | void sway_log_colors(int mode) { | ||
51 | colored = (mode == 1) ? 1 : 0; | ||
52 | } | ||
53 | |||
54 | void sway_abort(const char *format, ...) { | ||
55 | fprintf(stderr, "ERROR: "); | ||
56 | va_list args; | ||
57 | va_start(args, format); | ||
58 | vfprintf(stderr, format, args); | ||
59 | va_end(args); | ||
60 | fprintf(stderr, "\n"); | ||
61 | sway_terminate(); | ||
62 | } | ||
63 | |||
64 | #ifndef NDEBUG | ||
65 | void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) { | ||
66 | #else | ||
67 | void _sway_log(log_importance_t verbosity, const char* format, ...) { | ||
68 | #endif | ||
69 | if (verbosity <= v) { | ||
70 | unsigned int c = verbosity; | ||
71 | if (c > sizeof(verbosity_colors) / sizeof(char *)) { | ||
72 | c = sizeof(verbosity_colors) / sizeof(char *) - 1; | ||
73 | } | ||
74 | |||
75 | if (colored && isatty(STDERR_FILENO)) { | ||
76 | fprintf(stderr, "%s", verbosity_colors[c]); | ||
77 | } | ||
78 | |||
79 | va_list args; | ||
80 | va_start(args, format); | ||
81 | #ifndef NDEBUG | ||
82 | char *file = strdup(filename); | ||
83 | fprintf(stderr, "[%s:%d] ", basename(file), line); | ||
84 | free(file); | ||
85 | #endif | ||
86 | vfprintf(stderr, format, args); | ||
87 | va_end(args); | ||
88 | |||
89 | if (colored && isatty(STDERR_FILENO)) { | ||
90 | fprintf(stderr, "\x1B[0m"); | ||
91 | } | ||
92 | fprintf(stderr, "\n"); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | void sway_log_errno(log_importance_t verbosity, char* format, ...) { | ||
97 | if (verbosity <= v) { | ||
98 | unsigned int c = verbosity; | ||
99 | if (c > sizeof(verbosity_colors) / sizeof(char *)) { | ||
100 | c = sizeof(verbosity_colors) / sizeof(char *) - 1; | ||
101 | } | ||
102 | |||
103 | if (colored && isatty(STDERR_FILENO)) { | ||
104 | fprintf(stderr, "%s", verbosity_colors[c]); | ||
105 | } | ||
106 | |||
107 | va_list args; | ||
108 | va_start(args, format); | ||
109 | vfprintf(stderr, format, args); | ||
110 | va_end(args); | ||
111 | |||
112 | fprintf(stderr, ": "); | ||
113 | fprintf(stderr, "%s", strerror(errno)); | ||
114 | |||
115 | if (colored && isatty(STDERR_FILENO)) { | ||
116 | fprintf(stderr, "\x1B[0m"); | ||
117 | } | ||
118 | fprintf(stderr, "\n"); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | bool _sway_assert(bool condition, const char* format, ...) { | ||
123 | if (condition) { | ||
124 | return true; | ||
125 | } | ||
126 | |||
127 | va_list args; | ||
128 | va_start(args, format); | ||
129 | sway_log(L_ERROR, format, args); | ||
130 | va_end(args); | ||
131 | |||
132 | #ifndef NDEBUG | ||
133 | raise(SIGABRT); | ||
134 | #endif | ||
135 | |||
136 | return false; | ||
137 | } | ||
138 | |||
139 | void error_handler(int sig) { | ||
140 | int i; | ||
141 | int max_lines = 20; | ||
142 | void *array[max_lines]; | ||
143 | char **bt; | ||
144 | size_t bt_len; | ||
145 | |||
146 | sway_log(L_ERROR, "Error: Signal %d. Printing backtrace", sig); | ||
147 | bt_len = backtrace(array, max_lines); | ||
148 | bt = backtrace_symbols(array, bt_len); | ||
149 | if (!bt) { | ||
150 | sway_log(L_ERROR, "Could not allocate sufficient memory for backtrace_symbols(), falling back to stderr"); | ||
151 | backtrace_symbols_fd(array, bt_len, STDERR_FILENO); | ||
152 | exit(1); | ||
153 | } | ||
154 | |||
155 | for (i = 0; (size_t)i < bt_len; i++) { | ||
156 | sway_log(L_ERROR, "Backtrace: %s", bt[i]); | ||
157 | } | ||
158 | exit(1); | ||
159 | } | ||
160 | |||
161 | #include "workspace.h" | ||
162 | |||
163 | /* XXX:DEBUG:XXX */ | ||
164 | static void container_log(const swayc_t *c) { | ||
165 | fprintf(stderr, "focus:%c|", | ||
166 | c == get_focused_view(&root_container) ? 'K': | ||
167 | c == get_focused_container(&root_container) ? 'F' : // Focused | ||
168 | c == swayc_active_workspace() ? 'W' : // active workspace | ||
169 | c == &root_container ? 'R' : // root | ||
170 | 'X');// not any others | ||
171 | fprintf(stderr,"(%p)",c); | ||
172 | fprintf(stderr,"(p:%p)",c->parent); | ||
173 | fprintf(stderr,"(f:%p)",c->focused); | ||
174 | fprintf(stderr,"(h:%ld)",c->handle); | ||
175 | fprintf(stderr,"Type:"); | ||
176 | fprintf(stderr, | ||
177 | c->type == C_ROOT ? "Root|" : | ||
178 | c->type == C_OUTPUT ? "Output|" : | ||
179 | c->type == C_WORKSPACE ? "Workspace|" : | ||
180 | c->type == C_CONTAINER ? "Container|" : | ||
181 | c->type == C_VIEW ? "View|" : "Unknown|"); | ||
182 | fprintf(stderr,"layout:"); | ||
183 | fprintf(stderr, | ||
184 | c->layout == L_NONE ? "NONE|" : | ||
185 | c->layout == L_HORIZ ? "Horiz|": | ||
186 | c->layout == L_VERT ? "Vert|": | ||
187 | c->layout == L_STACKED ? "Stacked|": | ||
188 | c->layout == L_FLOATING ? "Floating|": | ||
189 | "Unknown|"); | ||
190 | fprintf(stderr, "w:%.f|h:%.f|", c->width, c->height); | ||
191 | fprintf(stderr, "x:%.f|y:%.f|", c->x, c->y); | ||
192 | fprintf(stderr, "g:%d|",c->gaps); | ||
193 | fprintf(stderr, "vis:%c|", c->visible?'t':'f'); | ||
194 | fprintf(stderr, "name:%.16s|", c->name); | ||
195 | fprintf(stderr, "children:%d\n",c->children?c->children->length:0); | ||
196 | } | ||
197 | void layout_log(const swayc_t *c, int depth) { | ||
198 | if (L_DEBUG > v) return; | ||
199 | int i, d; | ||
200 | int e = c->children ? c->children->length : 0; | ||
201 | container_log(c); | ||
202 | if (e) { | ||
203 | for (i = 0; i < e; ++i) { | ||
204 | fputc('|',stderr); | ||
205 | for (d = 0; d < depth; ++d) fputc('-', stderr); | ||
206 | layout_log(c->children->items[i], depth + 1); | ||
207 | } | ||
208 | } | ||
209 | if (c->type == C_WORKSPACE) { | ||
210 | e = c->floating?c->floating->length:0; | ||
211 | if (e) { | ||
212 | for (i = 0; i < e; ++i) { | ||
213 | fputc('|',stderr); | ||
214 | for (d = 0; d < depth; ++d) fputc('=', stderr); | ||
215 | layout_log(c->floating->items[i], depth + 1); | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | |||
221 | const char *swayc_type_string(enum swayc_types type) { | ||
222 | return type == C_ROOT ? "ROOT" : | ||
223 | type == C_OUTPUT ? "OUTPUT" : | ||
224 | type == C_WORKSPACE ? "WORKSPACE" : | ||
225 | type == C_CONTAINER ? "CONTAINER" : | ||
226 | type == C_VIEW ? "VIEW" : | ||
227 | "UNKNOWN"; | ||
228 | } | ||
229 | |||
230 | // Like sway_log, but also appends some info about given container to log output. | ||
231 | void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) { | ||
232 | sway_assert(cont, "swayc_log: no container ..."); | ||
233 | va_list args; | ||
234 | va_start(args, format); | ||
235 | char *txt = malloc(128); | ||
236 | vsprintf(txt, format, args); | ||
237 | va_end(args); | ||
238 | |||
239 | char *debug_txt = malloc(32); | ||
240 | snprintf(debug_txt, 32, "%s '%s'", swayc_type_string(cont->type), cont->name); | ||
241 | |||
242 | sway_log(verbosity, "%s (%s)", txt, debug_txt); | ||
243 | free(txt); | ||
244 | free(debug_txt); | ||
245 | } | ||
246 | |||
247 | /* XXX:DEBUG:XXX */ | ||