diff options
Diffstat (limited to 'wayland/window.c')
-rw-r--r-- | wayland/window.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/wayland/window.c b/wayland/window.c new file mode 100644 index 00000000..af50c04c --- /dev/null +++ b/wayland/window.c | |||
@@ -0,0 +1,123 @@ | |||
1 | #include <wayland-client.h> | ||
2 | #include <wayland-cursor.h> | ||
3 | #include "wayland-xdg-shell-client-protocol.h" | ||
4 | #include "wayland-desktop-shell-client-protocol.h" | ||
5 | #include <cairo/cairo.h> | ||
6 | #include <pango/pangocairo.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | #include <stdio.h> | ||
10 | #include <unistd.h> | ||
11 | #include <errno.h> | ||
12 | #include <sys/mman.h> | ||
13 | #include "client/window.h" | ||
14 | #include "client/buffer.h" | ||
15 | #include "list.h" | ||
16 | #include "log.h" | ||
17 | |||
18 | static void pointer_handle_enter(void *data, struct wl_pointer *pointer, | ||
19 | uint32_t serial, struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w) { | ||
20 | sway_log(L_INFO, "Set cursor"); | ||
21 | struct window *window = data; | ||
22 | struct wl_cursor_image *image = window->cursor.cursor->images[0]; | ||
23 | wl_pointer_set_cursor(pointer, serial, window->cursor.surface, image->hotspot_x, image->hotspot_y); | ||
24 | } | ||
25 | |||
26 | static void pointer_handle_leave(void *data, struct wl_pointer *pointer, | ||
27 | uint32_t serial, struct wl_surface *surface) { | ||
28 | } | ||
29 | |||
30 | static void pointer_handle_motion(void *data, struct wl_pointer *pointer, | ||
31 | uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) { | ||
32 | } | ||
33 | |||
34 | static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial, | ||
35 | uint32_t time, uint32_t button, uint32_t state_w) { | ||
36 | } | ||
37 | |||
38 | static void pointer_handle_axis(void *data, struct wl_pointer *pointer, | ||
39 | uint32_t time, uint32_t axis, wl_fixed_t value) { | ||
40 | } | ||
41 | |||
42 | static const struct wl_pointer_listener pointer_listener = { | ||
43 | .enter = pointer_handle_enter, | ||
44 | .leave = pointer_handle_leave, | ||
45 | .motion = pointer_handle_motion, | ||
46 | .button = pointer_handle_button, | ||
47 | .axis = pointer_handle_axis | ||
48 | }; | ||
49 | |||
50 | void shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface, | ||
51 | uint32_t edges, int32_t width, int32_t height) { | ||
52 | struct window *window = data; | ||
53 | window->width = width; | ||
54 | window->height = height; | ||
55 | } | ||
56 | |||
57 | static const struct wl_shell_surface_listener surface_listener = { | ||
58 | .configure = shell_surface_configure | ||
59 | }; | ||
60 | |||
61 | struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height, bool shell_surface) { | ||
62 | struct window *window = malloc(sizeof(struct window)); | ||
63 | memset(window, 0, sizeof(struct window)); | ||
64 | window->width = width; | ||
65 | window->height = height; | ||
66 | window->registry = registry; | ||
67 | |||
68 | window->surface = wl_compositor_create_surface(registry->compositor); | ||
69 | if (shell_surface) { | ||
70 | window->shell_surface = wl_shell_get_shell_surface(registry->shell, window->surface); | ||
71 | wl_shell_surface_add_listener(window->shell_surface, &surface_listener, window); | ||
72 | wl_shell_surface_set_toplevel(window->shell_surface); | ||
73 | } | ||
74 | if (registry->pointer) { | ||
75 | sway_log(L_INFO, "Register pointer"); | ||
76 | wl_pointer_add_listener(registry->pointer, &pointer_listener, window); | ||
77 | } | ||
78 | |||
79 | window->cursor.cursor_theme = wl_cursor_theme_load("default", 32, registry->shm); // TODO: let you customize this | ||
80 | window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr"); | ||
81 | window->cursor.surface = wl_compositor_create_surface(registry->compositor); | ||
82 | |||
83 | struct wl_cursor_image *image = window->cursor.cursor->images[0]; | ||
84 | struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image); | ||
85 | wl_surface_attach(window->cursor.surface, cursor_buf, 0, 0); | ||
86 | wl_surface_damage(window->cursor.surface, 0, 0, image->width, image->height); | ||
87 | wl_surface_commit(window->cursor.surface); | ||
88 | |||
89 | return window; | ||
90 | } | ||
91 | |||
92 | static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { | ||
93 | struct window *window = data; | ||
94 | wl_callback_destroy(callback); | ||
95 | window->frame_cb = NULL; | ||
96 | } | ||
97 | |||
98 | static const struct wl_callback_listener listener = { | ||
99 | frame_callback | ||
100 | }; | ||
101 | |||
102 | int window_prerender(struct window *window) { | ||
103 | if (window->frame_cb) { | ||
104 | return 0; | ||
105 | } | ||
106 | |||
107 | get_next_buffer(window); | ||
108 | return 1; | ||
109 | } | ||
110 | |||
111 | int window_render(struct window *window) { | ||
112 | window->frame_cb = wl_surface_frame(window->surface); | ||
113 | wl_callback_add_listener(window->frame_cb, &listener, window); | ||
114 | |||
115 | wl_surface_damage(window->surface, 0, 0, window->buffer->width, window->buffer->height); | ||
116 | wl_surface_attach(window->surface, window->buffer->buffer, 0, 0); | ||
117 | wl_surface_commit(window->surface); | ||
118 | |||
119 | return 1; | ||
120 | } | ||
121 | |||
122 | void window_teardown(struct window *window) { | ||
123 | } | ||