aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-03-30 22:42:59 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-03-30 22:44:08 -0400
commit2a5108a2786383cf5c3bcefd653605c916193837 (patch)
tree10b69216bee8adb69fd81ebbeb05c8a7c36c51a3 /swaybar
parentImplement scroll wheel workspace switching (diff)
downloadsway-2a5108a2786383cf5c3bcefd653605c916193837.tar.gz
sway-2a5108a2786383cf5c3bcefd653605c916193837.tar.zst
sway-2a5108a2786383cf5c3bcefd653605c916193837.zip
Implement workspace switch on click
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/bar.c34
-rw-r--r--swaybar/render.c39
2 files changed, 56 insertions, 17 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c
index 5679a892..f743236c 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -34,12 +34,6 @@ static void bar_init(struct swaybar *bar) {
34 wl_list_init(&bar->outputs); 34 wl_list_init(&bar->outputs);
35} 35}
36 36
37struct swaybar_output *new_output(const char *name) {
38 struct swaybar_output *output = malloc(sizeof(struct swaybar_output));
39 output->name = strdup(name);
40 return output;
41}
42
43static void layer_surface_configure(void *data, 37static void layer_surface_configure(void *data,
44 struct zwlr_layer_surface_v1 *surface, 38 struct zwlr_layer_surface_v1 *surface,
45 uint32_t serial, uint32_t width, uint32_t height) { 39 uint32_t serial, uint32_t width, uint32_t height) {
@@ -91,20 +85,39 @@ static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
91 85
92static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, 86static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
93 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { 87 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
94 // TODO 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);
95} 91}
96 92
97static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, 93static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
98 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { 94 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
99 wlr_log(L_DEBUG, "button"); 95 struct swaybar *bar = data;
100 // TODO 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 }
101} 114}
102 115
103static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, 116static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
104 uint32_t time, uint32_t axis, wl_fixed_t value) { 117 uint32_t time, uint32_t axis, wl_fixed_t value) {
105 struct swaybar *bar = data; 118 struct swaybar *bar = data;
106 struct swaybar_output *output = bar->pointer.current; 119 struct swaybar_output *output = bar->pointer.current;
107 if (!output) { 120 if (!sway_assert(output, "axis with no active output")) {
108 return; 121 return;
109 } 122 }
110 double amt = wl_fixed_to_double(value); 123 double amt = wl_fixed_to_double(value);
@@ -206,6 +219,7 @@ static void handle_global(void *data, struct wl_registry *registry,
206 &wl_output_interface, 1); 219 &wl_output_interface, 1);
207 output->index = index++; 220 output->index = index++;
208 wl_list_init(&output->workspaces); 221 wl_list_init(&output->workspaces);
222 wl_list_init(&output->hotspots);
209 wl_list_insert(&bar->outputs, &output->link); 223 wl_list_insert(&bar->outputs, &output->link);
210 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { 224 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
211 bar->layer_shell = wl_registry_bind( 225 bar->layer_shell = wl_registry_bind(
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);