wlr_virtual_pointer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // One virtual pointer for wlroots-style compositors (sway, labwc, Hyprland, niri), kept for as long as stdin is open:
  2. // wlrctl makes a new device per call and the seat's pointer capability comes and goes with it.
  3. // stdin lines: "abs <x> <y> <width> <height>" (position within a layout extent), "quit"; each answered with "ok"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <wayland-client.h>
  7. #include "wlr-virtual-pointer-unstable-v1-client-protocol.h"
  8. static struct zwlr_virtual_pointer_manager_v1* manager;
  9. static struct wl_seat* seat;
  10. static void global (void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
  11. if (strcmp (interface, zwlr_virtual_pointer_manager_v1_interface.name) == 0) {
  12. manager = wl_registry_bind (registry, name, &zwlr_virtual_pointer_manager_v1_interface, 1);
  13. } else if (strcmp (interface, wl_seat_interface.name) == 0) {
  14. seat = wl_registry_bind (registry, name, &wl_seat_interface, 1);
  15. }
  16. }
  17. static void global_remove (void* data, struct wl_registry* registry, uint32_t name) { }
  18. static const struct wl_registry_listener registry_listener = { global, global_remove };
  19. int main (void) {
  20. struct wl_display* display = wl_display_connect (NULL);
  21. if (!display) {
  22. fprintf (stderr, "no wayland display\n");
  23. return 1;
  24. }
  25. wl_registry_add_listener (wl_display_get_registry (display), &registry_listener, NULL);
  26. wl_display_roundtrip (display);
  27. if (!manager) {
  28. fprintf (stderr, "the compositor has no zwlr_virtual_pointer_manager_v1\n");
  29. return 1;
  30. }
  31. struct zwlr_virtual_pointer_v1* pointer = zwlr_virtual_pointer_manager_v1_create_virtual_pointer (manager, seat);
  32. char line[256];
  33. uint32_t time = 1;
  34. wl_display_roundtrip (display);
  35. while (fgets (line, sizeof line, stdin)) {
  36. unsigned x, y, width, height;
  37. if (sscanf (line, "abs %u %u %u %u", &x, &y, &width, &height) == 4) {
  38. zwlr_virtual_pointer_v1_motion_absolute (pointer, time++, x, y, width, height);
  39. zwlr_virtual_pointer_v1_frame (pointer);
  40. } else if (strncmp (line, "quit", 4) == 0) {
  41. break;
  42. }
  43. wl_display_roundtrip (display);
  44. printf ("ok\n");
  45. fflush (stdout);
  46. }
  47. zwlr_virtual_pointer_v1_destroy (pointer);
  48. wl_display_roundtrip (display);
  49. wl_display_disconnect (display);
  50. return 0;
  51. }