cursor_shape.c (5361B)
1 /* cursor-shape-v1 */ 2 3 #include "cursor_shape.h" 4 #include "internal.h" 5 #include "pointer.h" 6 #include "seat.h" 7 #include "util.h" 8 9 #include <wayland-server.h> 10 #include "cursor-shape-v1-server-protocol.h" 11 12 /* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */ 13 static const char *const names[] = { 14 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default", 15 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu", 16 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help", 17 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer", 18 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress", 19 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait", 20 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell", 21 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair", 22 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text", 23 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text", 24 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias", 25 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy", 26 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move", 27 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop", 28 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed", 29 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab", 30 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing", 31 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize", 32 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize", 33 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize", 34 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize", 35 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize", 36 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize", 37 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize", 38 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize", 39 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize", 40 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize", 41 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize", 42 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize", 43 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize", 44 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize", 45 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll", 46 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in", 47 [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out", 48 }; 49 50 #define NAMES (sizeof names / sizeof names[0]) 51 52 static void 53 pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource, 54 uint32_t serial, uint32_t shape) 55 { 56 struct pointer *pointer = wl_resource_get_user_data(resource); 57 58 (void)serial; 59 if (shape < 1 || shape >= NAMES || !names[shape]) { 60 wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, 61 "no such shape: %u", shape); 62 return; 63 } 64 /* only what has the pointer says what it looks like */ 65 if (!pointer || client != pointer->focus.client) { 66 return; 67 } 68 pointer_set_shape(pointer, names[shape]); 69 } 70 71 static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = { 72 .destroy = destroy_resource, 73 .set_shape = pointer_set_shape_request, 74 }; 75 76 /* a tablet tool has no pointer here: it is answered and does nothing */ 77 static void 78 tool_set_shape_request(struct wl_client *client, struct wl_resource *resource, 79 uint32_t serial, uint32_t shape) 80 { 81 (void)client; 82 (void)serial; 83 (void)shape; 84 (void)resource; 85 } 86 87 static const struct wp_cursor_shape_device_v1_interface tool_device_impl = { 88 .destroy = destroy_resource, 89 .set_shape = tool_set_shape_request, 90 }; 91 92 static void 93 get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id, 94 struct wl_resource *pointer_resource) 95 { 96 struct wl_resource *device = wl_resource_create( 97 client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); 98 99 if (!device) { 100 wl_resource_post_no_memory(resource); 101 return; 102 } 103 wl_resource_set_implementation(device, &pointer_device_impl, 104 wl_resource_get_user_data(pointer_resource), NULL); 105 } 106 107 static void 108 get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id, 109 struct wl_resource *tool) 110 { 111 struct wl_resource *device = wl_resource_create( 112 client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); 113 114 (void)tool; 115 if (!device) { 116 wl_resource_post_no_memory(resource); 117 return; 118 } 119 wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL); 120 } 121 122 static const struct wp_cursor_shape_manager_v1_interface manager_impl = { 123 .destroy = destroy_resource, 124 .get_pointer = get_pointer, 125 .get_tablet_tool_v2 = get_tablet_tool_v2, 126 }; 127 128 static void 129 bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) 130 { 131 struct wl_resource *resource; 132 133 (void)data; 134 resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id); 135 if (!resource) { 136 wl_client_post_no_memory(client); 137 return; 138 } 139 wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); 140 } 141 142 struct wl_global * 143 cursor_shape_manager_create(struct wl_display *display) 144 { 145 return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager); 146 }