commit b8396e4c9f02150caa04d3762ac58bed0416b737
parent c464cf5b33932465447b047ab2bba046af0131b5
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Sun, 27 Jun 2021 14:53:00 -0500
mowc: volume button shows and hides overlay
Diffstat:
M | mowc.c | | | 60 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git a/mowc.c b/mowc.c
@@ -2,12 +2,16 @@
#include <stdlib.h>
#include <string.h>
#include <swc.h>
+#include <unistd.h>
#include <wayland-server.h>
#include <xkbcommon/xkbcommon.h>
+#define OVERLAY_PROGRAM MOWC_LIBEXEC "/overlay"
+
static struct wl_display *display;
static struct swc_screen *curscreen;
static struct swc_window *curwindow;
+static struct swc_window *overlay;
void
destroy(void *data)
@@ -15,8 +19,24 @@ destroy(void *data)
curwindow = NULL;
}
+void
+app_id_changed(void *data)
+{
+ struct swc_window *window = data;
+ if (!overlay && window->app_id && strcmp(window->app_id, "overlay") == 0) {
+ overlay = window;
+ swc_window_set_geometry(overlay, &curscreen->usable_geometry);
+ if (curwindow == window)
+ curwindow = NULL;
+
+ swc_window_hide(window);
+ return;
+ }
+}
+
struct swc_window_handler window_handler = {
.destroy = destroy,
+ .app_id_changed = app_id_changed,
};
void
@@ -29,13 +49,15 @@ new_screen(struct swc_screen *screen)
void
new_window(struct swc_window *window)
{
+ fprintf(stderr, "new window: %s, %s!\n", window->app_id, window->title);
+
if (curwindow)
swc_window_close(curwindow);
curwindow = window;
swc_window_set_geometry(window, &curscreen->usable_geometry);
swc_window_focus(window);
- swc_window_set_handler(window, &window_handler, NULL);
+ swc_window_set_handler(window, &window_handler, window);
swc_window_show(window);
}
@@ -77,6 +99,36 @@ power_handler(void *data, uint32_t time, uint32_t value, uint32_t state)
justawoke = true;
}
+bool overlay_active = false;
+
+static void
+overlay_handler(void *data, uint32_t time, uint32_t value, uint32_t state)
+{
+ if (!state) // ignore releases
+ return;
+
+ if (overlay_active) {
+ overlay_active = false;
+ swc_window_hide(overlay);
+ } else {
+ overlay_active = true;
+ swc_window_show(overlay);
+ }
+}
+
+static int
+launchchild(char *prog)
+{
+ pid_t pid = fork();
+ if (pid < 0) {
+ return -1;
+ } else if (pid == 0) {
+ execl(prog, prog, NULL);
+ }
+ return 0;
+}
+
+
int
main(int argc, char *argv[])
{
@@ -96,6 +148,12 @@ main(int argc, char *argv[])
return 1;
swc_add_binding(SWC_BINDING_KEY, 0, XKB_KEY_XF86PowerOff, power_handler, NULL);
+ swc_add_binding(SWC_BINDING_KEY, 0, XKB_KEY_XF86AudioLowerVolume, overlay_handler, NULL);
+
+ if (launchchild(OVERLAY_PROGRAM) < 0) {
+ fprintf(stderr, "failed to launch overlay\n");
+ return 1;
+ }
event_loop = wl_display_get_event_loop(display);
wl_display_run(display);