aboutsummaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--CMakeLists.txt4
-rw-r--r--common/list.c (renamed from sway/list.c)0
-rw-r--r--common/log.c (renamed from sway/log.c)88
-rw-r--r--include/client.h43
-rw-r--r--sway/debug_log.c102
-rw-r--r--swaybg/CMakeLists.txt10
-rw-r--r--swaybg/main.c23
-rw-r--r--wayland/client.c192
8 files changed, 370 insertions, 92 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d446c2b3..b75e8737 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@ set(CMAKE_C_FLAGS "-g")
4set(CMAKE_C_STANDARD 99) 4set(CMAKE_C_STANDARD 99)
5SET(CMAKE_C_EXTENSIONS OFF) 5SET(CMAKE_C_EXTENSIONS OFF)
6set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin/") 6set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin/")
7add_definitions("-Wall -Wextra -Wno-unused-parameter") 7add_definitions("-Wall -Wextra -Wno-unused-parameter -D_GNU_SOURCE")
8set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/CMake) 8set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/CMake)
9 9
10add_subdirectory(swaybg swaybg) 10add_subdirectory(swaybg swaybg)
@@ -44,6 +44,7 @@ find_package(PCRE REQUIRED)
44find_package(JsonC REQUIRED) 44find_package(JsonC REQUIRED)
45 45
46FILE(GLOB sources ${PROJECT_SOURCE_DIR}/sway/*.c) 46FILE(GLOB sources ${PROJECT_SOURCE_DIR}/sway/*.c)
47FILE(GLOB common ${PROJECT_SOURCE_DIR}/common/*.c)
47 48
48include_directories( 49include_directories(
49 ${WLC_INCLUDE_DIRS} 50 ${WLC_INCLUDE_DIRS}
@@ -55,6 +56,7 @@ include_directories(
55 56
56add_executable(sway 57add_executable(sway
57 ${sources} 58 ${sources}
59 ${common}
58) 60)
59 61
60target_link_libraries(sway 62target_link_libraries(sway
diff --git a/sway/list.c b/common/list.c
index 45efc16f..45efc16f 100644
--- a/sway/list.c
+++ b/common/list.c
diff --git a/sway/log.c b/common/log.c
index f2b828f9..02aac4c1 100644
--- a/sway/log.c
+++ b/common/log.c
@@ -157,91 +157,3 @@ void error_handler(int sig) {
157 } 157 }
158 exit(1); 158 exit(1);
159} 159}
160
161#include "workspace.h"
162
163/* XXX:DEBUG:XXX */
164static 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}
197void 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
221const 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.
231void 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 */
diff --git a/include/client.h b/include/client.h
new file mode 100644
index 00000000..ec3537ca
--- /dev/null
+++ b/include/client.h
@@ -0,0 +1,43 @@
1#ifndef _CLIENT_H
2#define _CLIENT_H
3
4#include <wayland-client.h>
5#include <cairo/cairo.h>
6#include <pango/pangocairo.h>
7#include "list.h"
8
9struct output_state {
10 struct wl_output *output;
11 uint32_t flags;
12 int width, height;
13};
14
15struct buffer {
16 struct wl_buffer *buffer;
17 struct wl_shm_pool *pool;
18 uint32_t width, height;
19};
20
21struct client_state {
22 struct wl_compositor *compositor;
23 struct wl_display *display;
24 struct wl_pointer *pointer;
25 struct wl_seat *seat;
26 struct wl_shell *shell;
27 struct wl_shm *shm;
28 struct buffer *buffer;
29 struct wl_surface *surface;
30 struct wl_shell_surface *shell_surface;
31 cairo_t *cairo;
32 cairo_surface_t *cairo_surface;
33 PangoContext *pango;
34 list_t *outputs;
35};
36
37struct client_state *client_setup(void);
38void client_teardown(struct client_state *state);
39struct buffer *create_memory_pool(struct client_state *state, int32_t width, int32_t height, uint32_t format);
40int client_prerender(struct client_state *state);
41int client_render(struct client_state *state);
42
43#endif
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
16extern log_importance_t v;
17
18/* XXX:DEBUG:XXX */
19static 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}
52void 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
76const 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.
86void 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/swaybg/CMakeLists.txt b/swaybg/CMakeLists.txt
index 89d8afde..9351441a 100644
--- a/swaybg/CMakeLists.txt
+++ b/swaybg/CMakeLists.txt
@@ -9,14 +9,20 @@ WAYLAND_ADD_PROTOCOL_CLIENT(proto-xdg-shell "xdg-shell.xml" xdg-shell)
9 9
10set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin/") 10set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin/")
11include_directories( 11include_directories(
12 ${CMAKE_CURRENT_SOURCE_DIR}/include 12 ${CMAKE_CURRENT_SOURCE_DIR}/../include
13 ${WAYLAND_CLIENT_INCLUDE_DIR} 13 ${WAYLAND_CLIENT_INCLUDE_DIR}
14 ${CAIRO_INCLUDE_DIRS} 14 ${CAIRO_INCLUDE_DIRS}
15 ${PANGO_INCLUDE_DIRS} 15 ${PANGO_INCLUDE_DIRS}
16) 16)
17 17
18FILE(GLOB sources ${PROJECT_SOURCE_DIR}/*.c)
19FILE(GLOB wl_sources ${PROJECT_SOURCE_DIR}/../wayland/*.c)
20FILE(GLOB common ${PROJECT_SOURCE_DIR}/../common/*.c)
21
18add_executable(swaybg 22add_executable(swaybg
19 main.c 23 ${sources}
24 ${wl_sources}
25 ${common}
20) 26)
21 27
22TARGET_LINK_LIBRARIES(swaybg ${WAYLAND_CLIENT_LIBRARIES} ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES}) 28TARGET_LINK_LIBRARIES(swaybg ${WAYLAND_CLIENT_LIBRARIES} ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES})
diff --git a/swaybg/main.c b/swaybg/main.c
index 4a8ef522..1b4af550 100644
--- a/swaybg/main.c
+++ b/swaybg/main.c
@@ -1,6 +1,27 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h>
3#include <wayland-client.h>
4#include "client.h"
5#include "log.h"
6
7struct client_state *state;
8
9void sway_terminate(void) {
10 client_teardown(state);
11 exit(1);
12}
2 13
3int main(int argc, char **argv) { 14int main(int argc, char **argv) {
4 printf("Hello world"); 15 init_log(L_INFO);
16 state = client_setup();
17
18 do {
19 if (!client_prerender(state)) continue;
20 cairo_set_source_rgb(state->cairo, 255, 0, 0);
21 cairo_rectangle(state->cairo, 0, 0, 100, 100);
22 cairo_fill(state->cairo);
23 } while (client_render(state));
24
25 client_teardown(state);
5 return 0; 26 return 0;
6} 27}
diff --git a/wayland/client.c b/wayland/client.c
new file mode 100644
index 00000000..c62f92bd
--- /dev/null
+++ b/wayland/client.c
@@ -0,0 +1,192 @@
1#include <wayland-client.h>
2#include <cairo/cairo.h>
3#include <pango/pangocairo.h>
4#include <stdlib.h>
5#include <string.h>
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>
9#include <sys/mman.h>
10#include "client.h"
11#include "list.h"
12#include "log.h"
13
14static void display_handle_mode(void *data, struct wl_output *wl_output,
15 uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
16 struct output_state *state = data;
17 if (flags & WL_OUTPUT_MODE_CURRENT) {
18 state->flags = flags;
19 state->width = width;
20 state->height = height;
21 }
22}
23
24static void display_handle_geometry(void *data, struct wl_output *wl_output,
25 int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
26 int32_t subpixel, const char *make, const char *model, int32_t transform) {
27 // this space intentionally left blank
28}
29
30static void display_handle_done(void *data, struct wl_output *wl_output) {
31 // this space intentionally left blank
32}
33
34static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) {
35 // this space intentionally left blank
36}
37
38static const struct wl_output_listener output_listener = {
39 .mode = display_handle_mode,
40 .geometry = display_handle_geometry,
41 .done = display_handle_done,
42 .scale = display_handle_scale
43};
44
45static void registry_global(void *data, struct wl_registry *registry,
46 uint32_t name, const char *interface, uint32_t version) {
47 struct client_state *state = data;
48
49 if (strcmp(interface, wl_compositor_interface.name) == 0) {
50 state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version);
51 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
52 state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version);
53 } else if (strcmp(interface, wl_shell_interface.name) == 0) {
54 state->shell = wl_registry_bind(registry, name, &wl_shell_interface, version);
55 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
56 state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
57 state->pointer = wl_seat_get_pointer(state->seat);
58 } else if (strcmp(interface, wl_output_interface.name) == 0) {
59 struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version);
60 struct output_state *ostate = malloc(sizeof(struct output_state));
61 ostate->output = output;
62 wl_output_add_listener(output, &output_listener, ostate);
63 list_add(state->outputs, ostate);
64 }
65}
66
67static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
68 // this space intentionally left blank
69}
70
71static const struct wl_registry_listener registry_listener = {
72 .global = registry_global,
73 .global_remove = registry_global_remove
74};
75
76static int create_pool_file(size_t size) {
77 static const char template[] = "/swaybg-XXXXXX";
78 const char *path = getenv("XDG_RUNTIME_DIR");
79 if (!path) {
80 return -1;
81 }
82
83 int ts = (path[strlen(path) - 1] == '/');
84
85 char *name = malloc(
86 strlen(template) +
87 strlen(path) +
88 (ts ? 1 : 0) + 1);
89 sprintf(name, "%s%s%s", path, ts ? "" : "/", template);
90
91 int fd = mkstemp(name);
92 free(name);
93
94 if (fd < 0) {
95 return -1;
96 }
97
98 if (ftruncate(fd, size) < 0) {
99 close(fd);
100 return -1;
101 }
102
103 return fd;
104}
105
106struct buffer *create_buffer(struct client_state *state,
107 int32_t width, int32_t height, uint32_t format) {
108
109 struct buffer *buf = malloc(sizeof(struct buffer));
110 memset(buf, 0, sizeof(struct buffer));
111 uint32_t stride = width * 4;
112 uint32_t size = stride * height;
113
114 int fd = create_pool_file(size);
115 void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
116 buf->pool = wl_shm_create_pool(state->shm, fd, size);
117 buf->buffer = wl_shm_pool_create_buffer(buf->pool, 0, width, height, stride, format);
118
119 state->cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride);
120 state->cairo = cairo_create(state->cairo_surface);
121 state->pango = pango_cairo_create_context(state->cairo);
122
123 sway_log(L_INFO, "%p %p", buf->pool, buf->buffer);
124 return buf;
125}
126
127struct client_state *client_setup(void) {
128 struct client_state *state = malloc(sizeof(struct client_state));
129 memset(state, 0, sizeof(struct client_state));
130 state->outputs = create_list();
131
132 state->display = wl_display_connect(NULL);
133 if (!state->display) {
134 sway_log(L_ERROR, "Error opening display");
135 client_teardown(state);
136 return NULL;
137 }
138
139 struct wl_registry *registry = wl_display_get_registry(state->display);
140 wl_registry_add_listener(registry, &registry_listener, state);
141 wl_display_roundtrip(state->display); // globals
142 wl_display_roundtrip(state->display); // listeners
143 wl_registry_destroy(registry);
144
145 state->buffer = create_buffer(state, 100, 100, WL_SHM_FORMAT_ARGB8888);
146 state->surface = wl_compositor_create_surface(state->compositor);
147 state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface);
148 wl_shell_surface_set_toplevel(state->shell_surface);
149
150 wl_surface_damage(state->surface, 0, 0, 100, 100);
151 wl_surface_attach(state->surface, state->buffer->buffer, 0, 0);
152 wl_surface_commit(state->surface);
153
154 return state;
155}
156
157int client_prerender(struct client_state *state) {
158 wl_display_dispatch_pending(state->display);
159 if (wl_display_flush(state->display) < 0 && errno != EAGAIN) {
160 return 0;
161 }
162 return 1;
163}
164
165int client_render(struct client_state *state) {
166 return wl_display_dispatch(state->display) != -1;
167}
168
169void client_teardown(struct client_state *state) {
170 if (state->pointer) {
171 wl_pointer_destroy(state->pointer);
172 }
173 if (state->seat) {
174 wl_seat_destroy(state->seat);
175 }
176 if (state->shell) {
177 wl_shell_destroy(state->shell);
178 }
179 if (state->shm) {
180 wl_shm_destroy(state->shm);
181 }
182 if (state->compositor) {
183 wl_compositor_destroy(state->compositor);
184 }
185 if (state->display) {
186 wl_display_disconnect(state->display);
187 }
188 if (state->outputs) {
189 // TODO: Free the outputs themselves
190 list_free(state->outputs);
191 }
192}