summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-03-31 00:10:20 -0400
committerLibravatar GitHub <noreply@github.com>2018-03-31 00:10:20 -0400
commit0441d124799dca510b3bf6621267b7838730af1a (patch)
treed5301c12dec48b570cee5d0a6daa3333ff4f575d
parentUse wlr_surface_point_accepts_input (diff)
parentImplement workspace switch on click (diff)
downloadsway-0441d124799dca510b3bf6621267b7838730af1a.tar.gz
sway-0441d124799dca510b3bf6621267b7838730af1a.tar.zst
sway-0441d124799dca510b3bf6621267b7838730af1a.zip
Merge pull request #1676 from swaywm/swaybar-input
Swaybar input
-rw-r--r--include/swaybar/bar.h23
-rw-r--r--include/swaybar/ipc.h1
-rw-r--r--meson.build1
-rw-r--r--swaybar/bar.c167
-rw-r--r--swaybar/ipc.c8
-rw-r--r--swaybar/meson.build4
-rw-r--r--swaybar/render.c39
7 files changed, 226 insertions, 17 deletions
diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h
index 1bf2ea2d..74292519 100644
--- a/include/swaybar/bar.h
+++ b/include/swaybar/bar.h
@@ -2,20 +2,39 @@
2#define _SWAYBAR_BAR_H 2#define _SWAYBAR_BAR_H
3#include <wayland-client.h> 3#include <wayland-client.h>
4#include "pool-buffer.h" 4#include "pool-buffer.h"
5#include "list.h"
6 5
7struct swaybar_config; 6struct swaybar_config;
8struct swaybar_output; 7struct swaybar_output;
9struct swaybar_workspace; 8struct swaybar_workspace;
10 9
10struct swaybar_pointer {
11 struct wl_pointer *pointer;
12 struct wl_cursor_theme *cursor_theme;
13 struct wl_cursor_image *cursor_image;
14 struct wl_surface *cursor_surface;
15 struct swaybar_output *current;
16 int x, y;
17};
18
19struct swaybar_hotspot {
20 struct wl_list link;
21 int x, y, width, height;
22 void (*callback)(struct swaybar_output *output,
23 int x, int y, uint32_t button, void *data);
24 void (*destroy)(void *data);
25 void *data;
26};
27
11struct swaybar { 28struct swaybar {
12 struct wl_display *display; 29 struct wl_display *display;
13 struct wl_compositor *compositor; 30 struct wl_compositor *compositor;
14 struct zwlr_layer_shell_v1 *layer_shell; 31 struct zwlr_layer_shell_v1 *layer_shell;
15 struct wl_shm *shm; 32 struct wl_shm *shm;
33 struct wl_seat *seat;
16 34
17 struct swaybar_config *config; 35 struct swaybar_config *config;
18 struct swaybar_output *focused_output; 36 struct swaybar_output *focused_output;
37 struct swaybar_pointer pointer;
19 struct status_line *status; 38 struct status_line *status;
20 39
21 int ipc_event_socketfd; 40 int ipc_event_socketfd;
@@ -32,6 +51,7 @@ struct swaybar_output {
32 struct zwlr_layer_surface_v1 *layer_surface; 51 struct zwlr_layer_surface_v1 *layer_surface;
33 52
34 struct wl_list workspaces; 53 struct wl_list workspaces;
54 struct wl_list hotspots;
35 55
36 char *name; 56 char *name;
37 size_t index; 57 size_t index;
@@ -51,7 +71,6 @@ struct swaybar_workspace {
51 bool urgent; 71 bool urgent;
52}; 72};
53 73
54// TODO: Rename stuff to match wlroots conventions (init/create/etc)
55void bar_setup(struct swaybar *bar, 74void bar_setup(struct swaybar *bar,
56 const char *socket_path, 75 const char *socket_path,
57 const char *bar_id); 76 const char *bar_id);
diff --git a/include/swaybar/ipc.h b/include/swaybar/ipc.h
index 278baef0..6ea7c4d6 100644
--- a/include/swaybar/ipc.h
+++ b/include/swaybar/ipc.h
@@ -6,5 +6,6 @@
6void ipc_initialize(struct swaybar *bar, const char *bar_id); 6void ipc_initialize(struct swaybar *bar, const char *bar_id);
7bool handle_ipc_event(struct swaybar *bar); 7bool handle_ipc_event(struct swaybar *bar);
8void ipc_get_workspaces(struct swaybar *bar); 8void ipc_get_workspaces(struct swaybar *bar);
9void ipc_send_workspace_command(struct swaybar *bar, const char *ws);
9 10
10#endif 11#endif
diff --git a/meson.build b/meson.build
index 49824b30..01788fd9 100644
--- a/meson.build
+++ b/meson.build
@@ -24,6 +24,7 @@ pcre = dependency('libpcre')
24wlroots = dependency('wlroots', fallback: ['wlroots', 'wlroots']) 24wlroots = dependency('wlroots', fallback: ['wlroots', 'wlroots'])
25wayland_server = dependency('wayland-server') 25wayland_server = dependency('wayland-server')
26wayland_client = dependency('wayland-client') 26wayland_client = dependency('wayland-client')
27wayland_cursor = dependency('wayland-cursor')
27wayland_egl = dependency('wayland-egl') 28wayland_egl = dependency('wayland-egl')
28wayland_protos = dependency('wayland-protocols') 29wayland_protos = dependency('wayland-protocols')
29xkbcommon = dependency('xkbcommon') 30xkbcommon = dependency('xkbcommon')
diff --git a/swaybar/bar.c b/swaybar/bar.c
index 0fc41517..f743236c 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -9,7 +9,13 @@
9#include <sys/wait.h> 9#include <sys/wait.h>
10#include <unistd.h> 10#include <unistd.h>
11#include <wayland-client.h> 11#include <wayland-client.h>
12#include <wayland-cursor.h>
12#include <wlr/util/log.h> 13#include <wlr/util/log.h>
14#ifdef __FreeBSD__
15#include <dev/evdev/input-event-codes.h>
16#else
17#include <linux/input-event-codes.h>
18#endif
13#include "swaybar/render.h" 19#include "swaybar/render.h"
14#include "swaybar/config.h" 20#include "swaybar/config.h"
15#include "swaybar/event_loop.h" 21#include "swaybar/event_loop.h"
@@ -18,6 +24,7 @@
18#include "swaybar/ipc.h" 24#include "swaybar/ipc.h"
19#include "ipc-client.h" 25#include "ipc-client.h"
20#include "list.h" 26#include "list.h"
27#include "log.h"
21#include "pango.h" 28#include "pango.h"
22#include "pool-buffer.h" 29#include "pool-buffer.h"
23#include "wlr-layer-shell-unstable-v1-client-protocol.h" 30#include "wlr-layer-shell-unstable-v1-client-protocol.h"
@@ -27,12 +34,6 @@ static void bar_init(struct swaybar *bar) {
27 wl_list_init(&bar->outputs); 34 wl_list_init(&bar->outputs);
28} 35}
29 36
30struct swaybar_output *new_output(const char *name) {
31 struct swaybar_output *output = malloc(sizeof(struct swaybar_output));
32 output->name = strdup(name);
33 return output;
34}
35
36static void layer_surface_configure(void *data, 37static void layer_surface_configure(void *data,
37 struct zwlr_layer_surface_v1 *surface, 38 struct zwlr_layer_surface_v1 *surface,
38 uint32_t serial, uint32_t width, uint32_t height) { 39 uint32_t serial, uint32_t width, uint32_t height) {
@@ -56,12 +57,156 @@ struct zwlr_layer_surface_v1_listener layer_surface_listener = {
56 .closed = layer_surface_closed, 57 .closed = layer_surface_closed,
57}; 58};
58 59
60static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
61 uint32_t serial, struct wl_surface *surface,
62 wl_fixed_t surface_x, wl_fixed_t surface_y) {
63 struct swaybar *bar = data;
64 struct swaybar_pointer *pointer = &bar->pointer;
65 struct swaybar_output *output;
66 wl_list_for_each(output, &bar->outputs, link) {
67 if (output->surface == surface) {
68 pointer->current = output;
69 break;
70 }
71 }
72 wl_surface_attach(pointer->cursor_surface,
73 wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
74 wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
75 pointer->cursor_image->hotspot_x,
76 pointer->cursor_image->hotspot_y);
77 wl_surface_commit(pointer->cursor_surface);
78}
79
80static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
81 uint32_t serial, struct wl_surface *surface) {
82 struct swaybar *bar = data;
83 bar->pointer.current = NULL;
84}
85
86static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
87 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
88 struct swaybar *bar = data;
89 bar->pointer.x = wl_fixed_to_int(surface_x);
90 bar->pointer.y = wl_fixed_to_int(surface_y);
91}
92
93static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
94 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
95 struct swaybar *bar = data;
96 struct swaybar_pointer *pointer = &bar->pointer;
97 struct swaybar_output *output = pointer->current;
98 if (!sway_assert(output, "button with no active output")) {
99 return;
100 }
101 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
102 return;
103 }
104 struct swaybar_hotspot *hotspot;
105 wl_list_for_each(hotspot, &output->hotspots, link) {
106 if (pointer->x >= hotspot->x
107 && pointer->y >= hotspot->y
108 && pointer->x < hotspot->x + hotspot->width
109 && pointer->y < hotspot->y + hotspot->height) {
110 hotspot->callback(output, pointer->x, pointer->y,
111 button, hotspot->data);
112 }
113 }
114}
115
116static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
117 uint32_t time, uint32_t axis, wl_fixed_t value) {
118 struct swaybar *bar = data;
119 struct swaybar_output *output = bar->pointer.current;
120 if (!sway_assert(output, "axis with no active output")) {
121 return;
122 }
123 double amt = wl_fixed_to_double(value);
124 if (!bar->config->wrap_scroll) {
125 int i = 0;
126 struct swaybar_workspace *ws = NULL;
127 wl_list_for_each(ws, &output->workspaces, link) {
128 if (ws->focused) {
129 break;
130 }
131 ++i;
132 }
133 int len = wl_list_length(&output->workspaces);
134 if (!sway_assert(i != len, "axis with null workspace")) {
135 return;
136 }
137 if (i == 0 && amt > 0) {
138 return; // Do not wrap
139 }
140 if (i == len - 1 && amt < 0) {
141 return; // Do not wrap
142 }
143 }
144
145 const char *workspace_name =
146 amt < 0 ? "prev_on_output" : "next_on_output";
147 ipc_send_workspace_command(bar, workspace_name);
148}
149
150static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
151 // Who cares
152}
153
154static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
155 uint32_t axis_source) {
156 // Who cares
157}
158
159static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
160 uint32_t time, uint32_t axis) {
161 // Who cares
162}
163
164static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
165 uint32_t axis, int32_t discrete) {
166 // Who cares
167}
168
169struct wl_pointer_listener pointer_listener = {
170 .enter = wl_pointer_enter,
171 .leave = wl_pointer_leave,
172 .motion = wl_pointer_motion,
173 .button = wl_pointer_button,
174 .axis = wl_pointer_axis,
175 .frame = wl_pointer_frame,
176 .axis_source = wl_pointer_axis_source,
177 .axis_stop = wl_pointer_axis_stop,
178 .axis_discrete = wl_pointer_axis_discrete,
179};
180
181static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
182 enum wl_seat_capability caps) {
183 struct swaybar *bar = data;
184 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
185 bar->pointer.pointer = wl_seat_get_pointer(wl_seat);
186 wl_pointer_add_listener(bar->pointer.pointer, &pointer_listener, bar);
187 }
188}
189
190static void seat_handle_name(void *data, struct wl_seat *wl_seat,
191 const char *name) {
192 // Who cares
193}
194
195const struct wl_seat_listener seat_listener = {
196 .capabilities = seat_handle_capabilities,
197 .name = seat_handle_name,
198};
199
59static void handle_global(void *data, struct wl_registry *registry, 200static void handle_global(void *data, struct wl_registry *registry,
60 uint32_t name, const char *interface, uint32_t version) { 201 uint32_t name, const char *interface, uint32_t version) {
61 struct swaybar *bar = data; 202 struct swaybar *bar = data;
62 if (strcmp(interface, wl_compositor_interface.name) == 0) { 203 if (strcmp(interface, wl_compositor_interface.name) == 0) {
63 bar->compositor = wl_registry_bind(registry, name, 204 bar->compositor = wl_registry_bind(registry, name,
64 &wl_compositor_interface, 1); 205 &wl_compositor_interface, 1);
206 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
207 bar->seat = wl_registry_bind(registry, name,
208 &wl_seat_interface, 1);
209 wl_seat_add_listener(bar->seat, &seat_listener, bar);
65 } else if (strcmp(interface, wl_shm_interface.name) == 0) { 210 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
66 bar->shm = wl_registry_bind(registry, name, 211 bar->shm = wl_registry_bind(registry, name,
67 &wl_shm_interface, 1); 212 &wl_shm_interface, 1);
@@ -74,6 +219,7 @@ static void handle_global(void *data, struct wl_registry *registry,
74 &wl_output_interface, 1); 219 &wl_output_interface, 1);
75 output->index = index++; 220 output->index = index++;
76 wl_list_init(&output->workspaces); 221 wl_list_init(&output->workspaces);
222 wl_list_init(&output->hotspots);
77 wl_list_insert(&bar->outputs, &output->link); 223 wl_list_insert(&bar->outputs, &output->link);
78 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { 224 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
79 bar->layer_shell = wl_registry_bind( 225 bar->layer_shell = wl_registry_bind(
@@ -116,6 +262,15 @@ void bar_setup(struct swaybar *bar,
116 wl_registry_add_listener(registry, &registry_listener, bar); 262 wl_registry_add_listener(registry, &registry_listener, bar);
117 wl_display_roundtrip(bar->display); 263 wl_display_roundtrip(bar->display);
118 assert(bar->compositor && bar->layer_shell && bar->shm); 264 assert(bar->compositor && bar->layer_shell && bar->shm);
265 struct swaybar_pointer *pointer = &bar->pointer;
266
267 assert(pointer->cursor_theme = wl_cursor_theme_load(NULL, 16, bar->shm));
268 struct wl_cursor *cursor;
269 assert(cursor = wl_cursor_theme_get_cursor(
270 pointer->cursor_theme, "left_ptr"));
271 pointer->cursor_image = cursor->images[0];
272 assert(pointer->cursor_surface =
273 wl_compositor_create_surface(bar->compositor));
119 274
120 // TODO: we might not necessarily be meant to do all of the outputs 275 // TODO: we might not necessarily be meant to do all of the outputs
121 struct swaybar_output *output; 276 struct swaybar_output *output;
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 326f25cc..64583df0 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -8,6 +8,14 @@
8#include "swaybar/ipc.h" 8#include "swaybar/ipc.h"
9#include "ipc-client.h" 9#include "ipc-client.h"
10 10
11void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
12 const char *fmt = "workspace \"%s\"";
13 uint32_t size = snprintf(NULL, 0, fmt, ws);
14 char command[size];
15 snprintf(command, size, fmt, ws);
16 ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size);
17}
18
11char *parse_font(const char *font) { 19char *parse_font(const char *font) {
12 char *new_font = NULL; 20 char *new_font = NULL;
13 if (strncmp("pango:", font, 6) == 0) { 21 if (strncmp("pango:", font, 6) == 0) {
diff --git a/swaybar/meson.build b/swaybar/meson.build
index d15e8b5c..bf6f6d7a 100644
--- a/swaybar/meson.build
+++ b/swaybar/meson.build
@@ -1,6 +1,5 @@
1executable( 1executable(
2 'swaybar', 2 'swaybar', [
3 [
4 'bar.c', 3 'bar.c',
5 'config.c', 4 'config.c',
6 'event_loop.c', 5 'event_loop.c',
@@ -20,6 +19,7 @@ executable(
20 pangocairo, 19 pangocairo,
21 rt, 20 rt,
22 wayland_client, 21 wayland_client,
22 wayland_cursor,
23 wlroots, 23 wlroots,
24 ], 24 ],
25 link_with: [lib_sway_common, lib_sway_client], 25 link_with: [lib_sway_common, lib_sway_client],
diff --git a/swaybar/render.c b/swaybar/render.c
index 3d9ef66b..c2358724 100644
--- a/swaybar/render.c
+++ b/swaybar/render.c
@@ -1,3 +1,4 @@
1#define _POSIX_C_SOURCE 200809L
1#include <limits.h> 2#include <limits.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdint.h> 4#include <stdint.h>
@@ -8,6 +9,7 @@
8#include "pool-buffer.h" 9#include "pool-buffer.h"
9#include "swaybar/bar.h" 10#include "swaybar/bar.h"
10#include "swaybar/config.h" 11#include "swaybar/config.h"
12#include "swaybar/ipc.h"
11#include "swaybar/render.h" 13#include "swaybar/render.h"
12#include "swaybar/status_line.h" 14#include "swaybar/status_line.h"
13#include "wlr-layer-shell-unstable-v1-client-protocol.h" 15#include "wlr-layer-shell-unstable-v1-client-protocol.h"
@@ -108,9 +110,14 @@ static const char *strip_workspace_number(const char *ws_name) {
108 return ws_name; 110 return ws_name;
109} 111}
110 112
113static void workspace_hotspot_callback(struct swaybar_output *output,
114 int x, int y, uint32_t button, void *data) {
115 ipc_send_workspace_command(output->bar, (const char *)data);
116}
117
111static uint32_t render_workspace_button(cairo_t *cairo, 118static uint32_t render_workspace_button(cairo_t *cairo,
112 struct swaybar_config *config, struct swaybar_workspace *ws, 119 struct swaybar_output *output, struct swaybar_config *config,
113 double *x, uint32_t height) { 120 struct swaybar_workspace *ws, double *x, uint32_t height) {
114 const char *name = ws->name; 121 const char *name = ws->name;
115 if (config->strip_workspace_numbers) { 122 if (config->strip_workspace_numbers) {
116 name = strip_workspace_number(ws->name); 123 name = strip_workspace_number(ws->name);
@@ -156,8 +163,18 @@ static uint32_t render_workspace_button(cairo_t *cairo,
156 cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y)); 163 cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
157 pango_printf(cairo, config->font, 1, true, "%s", name); 164 pango_printf(cairo, config->font, 1, true, "%s", name);
158 165
166 struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
167 hotspot->x = *x;
168 hotspot->y = 0;
169 hotspot->height = height;
170 hotspot->width = width;
171 hotspot->callback = workspace_hotspot_callback;
172 hotspot->destroy = free;
173 hotspot->data = strdup(name);
174 wl_list_insert(&output->hotspots, &hotspot->link);
175
159 *x += width; 176 *x += width;
160 return ideal_height; 177 return height;
161} 178}
162 179
163static uint32_t render_to_cairo(cairo_t *cairo, 180static uint32_t render_to_cairo(cairo_t *cairo,
@@ -184,8 +201,8 @@ static uint32_t render_to_cairo(cairo_t *cairo,
184 if (config->workspace_buttons) { 201 if (config->workspace_buttons) {
185 struct swaybar_workspace *ws; 202 struct swaybar_workspace *ws;
186 wl_list_for_each_reverse(ws, &output->workspaces, link) { 203 wl_list_for_each_reverse(ws, &output->workspaces, link) {
187 uint32_t h = render_workspace_button( 204 uint32_t h = render_workspace_button(cairo,
188 cairo, config, ws, &x, output->height); 205 output, config, ws, &x, output->height);
189 max_height = h > max_height ? h : max_height; 206 max_height = h > max_height ? h : max_height;
190 } 207 }
191 } 208 }
@@ -203,8 +220,16 @@ static uint32_t render_to_cairo(cairo_t *cairo,
203 return max_height > output->height ? max_height : output->height; 220 return max_height > output->height ? max_height : output->height;
204} 221}
205 222
206void render_frame(struct swaybar *bar, 223void render_frame(struct swaybar *bar, struct swaybar_output *output) {
207 struct swaybar_output *output) { 224 struct swaybar_hotspot *hotspot, *tmp;
225 wl_list_for_each_safe(hotspot, tmp, &output->hotspots, link) {
226 if (hotspot->destroy) {
227 hotspot->destroy(hotspot->data);
228 }
229 wl_list_remove(&hotspot->link);
230 free(hotspot);
231 }
232
208 cairo_surface_t *recorder = cairo_recording_surface_create( 233 cairo_surface_t *recorder = cairo_recording_surface_create(
209 CAIRO_CONTENT_COLOR_ALPHA, NULL); 234 CAIRO_CONTENT_COLOR_ALPHA, NULL);
210 cairo_t *cairo = cairo_create(recorder); 235 cairo_t *cairo = cairo_create(recorder);