|
| 1 | +/* |
| 2 | + * Minimal xx-hotkey demo client. |
| 3 | + * |
| 4 | + * Requests a single global hotkey (Super+Space) and prints the lifecycle and |
| 5 | + * trigger/release events. It requires a compositor that implements |
| 6 | + * xx_hotkey_manager_v1; without one it just reports that the global is |
| 7 | + * missing. |
| 8 | + * |
| 9 | + * Build with `make xx-hotkey-demo` |
| 10 | + */ |
| 11 | +#include "xx-hotkey-v1-client-protocol.h" |
| 12 | +#include <stdbool.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <string.h> |
| 16 | +#include <wayland-client.h> |
| 17 | +#include <xkbcommon/xkbcommon-keysyms.h> |
| 18 | +#include <xkbcommon/xkbcommon.h> |
| 19 | + |
| 20 | +static struct xx_hotkey_manager_v1 *manager; |
| 21 | + |
| 22 | +static const char *deny_reason_str(uint32_t reason) { |
| 23 | + switch (reason) { |
| 24 | + case XX_HOTKEY_V1_DENY_REASON_ALREADY_BOUND: |
| 25 | + return "already_bound"; |
| 26 | + case XX_HOTKEY_V1_DENY_REASON_NOT_PERMITTED: |
| 27 | + return "not_permitted"; |
| 28 | + case XX_HOTKEY_V1_DENY_REASON_INVALID: |
| 29 | + return "invalid"; |
| 30 | + default: |
| 31 | + return "unknown"; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +static const char *revoke_reason_str(uint32_t reason) { |
| 36 | + switch (reason) { |
| 37 | + case XX_HOTKEY_V1_REVOKE_REASON_REMOVED: |
| 38 | + return "removed"; |
| 39 | + case XX_HOTKEY_V1_REVOKE_REASON_SUPERSEDED: |
| 40 | + return "superseded"; |
| 41 | + case XX_HOTKEY_V1_REVOKE_REASON_NOT_PERMITTED: |
| 42 | + return "not_permitted"; |
| 43 | + default: |
| 44 | + return "unknown"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static void hk_bound(void *data, struct xx_hotkey_v1 *hk) { |
| 49 | + (void)data; |
| 50 | + (void)hk; |
| 51 | + printf("bound: hotkey is active\n"); |
| 52 | +} |
| 53 | + |
| 54 | +static void hk_denied(void *data, struct xx_hotkey_v1 *hk, uint32_t reason, |
| 55 | + const char *message) { |
| 56 | + (void)data; |
| 57 | + (void)hk; |
| 58 | + if (message && message[0]) |
| 59 | + printf("denied: %s (%s)\n", deny_reason_str(reason), message); |
| 60 | + else |
| 61 | + printf("denied: %s\n", deny_reason_str(reason)); |
| 62 | +} |
| 63 | + |
| 64 | +static void hk_revoked(void *data, struct xx_hotkey_v1 *hk, uint32_t reason, |
| 65 | + const char *message) { |
| 66 | + (void)data; |
| 67 | + (void)hk; |
| 68 | + if (message && message[0]) |
| 69 | + printf("revoked: %s (%s)\n", revoke_reason_str(reason), message); |
| 70 | + else |
| 71 | + printf("revoked: %s\n", revoke_reason_str(reason)); |
| 72 | +} |
| 73 | + |
| 74 | +static void hk_triggered(void *data, struct xx_hotkey_v1 *hk, uint32_t serial, |
| 75 | + uint32_t time) { |
| 76 | + (void)data; |
| 77 | + (void)hk; |
| 78 | + printf("triggered (serial=%u, time=%u)\n", serial, time); |
| 79 | +} |
| 80 | + |
| 81 | +static void hk_released(void *data, struct xx_hotkey_v1 *hk, uint32_t serial, |
| 82 | + uint32_t time) { |
| 83 | + (void)data; |
| 84 | + (void)hk; |
| 85 | + printf("released (serial=%u, time=%u)\n", serial, time); |
| 86 | +} |
| 87 | + |
| 88 | +static const struct xx_hotkey_v1_listener hk_listener = { |
| 89 | + .bound = hk_bound, |
| 90 | + .denied = hk_denied, |
| 91 | + .revoked = hk_revoked, |
| 92 | + .triggered = hk_triggered, |
| 93 | + .released = hk_released, |
| 94 | +}; |
| 95 | + |
| 96 | +static void reg_global(void *data, struct wl_registry *reg, uint32_t name, |
| 97 | + const char *interface, uint32_t version) { |
| 98 | + (void)data; |
| 99 | + (void)version; |
| 100 | + if (strcmp(interface, xx_hotkey_manager_v1_interface.name) == 0) |
| 101 | + manager = wl_registry_bind(reg, name, &xx_hotkey_manager_v1_interface, 1); |
| 102 | +} |
| 103 | + |
| 104 | +static void reg_global_remove(void *data, struct wl_registry *reg, uint32_t name) { |
| 105 | + (void)data; |
| 106 | + (void)reg; |
| 107 | + (void)name; |
| 108 | +} |
| 109 | + |
| 110 | +static const struct wl_registry_listener reg_listener = { |
| 111 | + .global = reg_global, |
| 112 | + .global_remove = reg_global_remove, |
| 113 | +}; |
| 114 | + |
| 115 | +int main(void) { |
| 116 | + struct wl_display *display = wl_display_connect(NULL); |
| 117 | + if (!display) { |
| 118 | + fprintf(stderr, "failed to connect to a wayland display\n"); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + |
| 122 | + struct wl_registry *registry = wl_display_get_registry(display); |
| 123 | + wl_registry_add_listener(registry, ®_listener, NULL); |
| 124 | + wl_display_roundtrip(display); |
| 125 | + |
| 126 | + if (!manager) { |
| 127 | + fprintf(stderr, "compositor does not implement xx_hotkey_manager_v1\n"); |
| 128 | + wl_display_disconnect(display); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + |
| 132 | + xx_hotkey_manager_v1_set_app_id(manager, "com.example.xx-hotkey-demo"); |
| 133 | + |
| 134 | + struct xx_hotkey_v1 *hotkey = |
| 135 | + xx_hotkey_manager_v1_create_hotkey(manager, "toggle-launcher"); |
| 136 | + xx_hotkey_v1_add_listener(hotkey, &hk_listener, NULL); |
| 137 | + |
| 138 | + xx_hotkey_v1_set_description(hotkey, "Toggle the launcher"); |
| 139 | + xx_hotkey_v1_set_key_trigger(hotkey, XKB_KEY_space, |
| 140 | + XX_HOTKEY_MANAGER_V1_MODIFIERS_SUPER); |
| 141 | + xx_hotkey_v1_commit(hotkey); |
| 142 | + |
| 143 | + printf("requested Super+Space; waiting for events (Ctrl-C to quit)...\n"); |
| 144 | + |
| 145 | + while (wl_display_dispatch(display) != -1) |
| 146 | + ; |
| 147 | + |
| 148 | + xx_hotkey_v1_destroy(hotkey); |
| 149 | + xx_hotkey_manager_v1_destroy(manager); |
| 150 | + wl_registry_destroy(registry); |
| 151 | + wl_display_disconnect(display); |
| 152 | + return 0; |
| 153 | +} |
0 commit comments