Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include "compositor.h" |
| 28 | |
| 29 | struct weston_binding { |
| 30 | uint32_t key; |
| 31 | uint32_t button; |
| 32 | uint32_t axis; |
| 33 | uint32_t modifier; |
| 34 | void *handler; |
| 35 | void *data; |
| 36 | struct wl_list link; |
| 37 | }; |
| 38 | |
| 39 | static struct weston_binding * |
| 40 | weston_compositor_add_binding(struct weston_compositor *compositor, |
| 41 | uint32_t key, uint32_t button, uint32_t axis, |
| 42 | uint32_t modifier, void *handler, void *data) |
| 43 | { |
| 44 | struct weston_binding *binding; |
| 45 | |
| 46 | binding = malloc(sizeof *binding); |
| 47 | if (binding == NULL) |
| 48 | return NULL; |
| 49 | |
| 50 | binding->key = key; |
| 51 | binding->button = button; |
| 52 | binding->axis = axis; |
| 53 | binding->modifier = modifier; |
| 54 | binding->handler = handler; |
| 55 | binding->data = data; |
| 56 | |
| 57 | return binding; |
| 58 | } |
| 59 | |
| 60 | WL_EXPORT struct weston_binding * |
| 61 | weston_compositor_add_key_binding(struct weston_compositor *compositor, |
| 62 | uint32_t key, uint32_t modifier, |
| 63 | weston_key_binding_handler_t handler, |
| 64 | void *data) |
| 65 | { |
| 66 | struct weston_binding *binding; |
| 67 | |
| 68 | binding = weston_compositor_add_binding(compositor, key, 0, 0, |
| 69 | modifier, handler, data); |
| 70 | if (binding == NULL) |
| 71 | return NULL; |
| 72 | |
| 73 | wl_list_insert(compositor->key_binding_list.prev, &binding->link); |
| 74 | |
| 75 | return binding; |
| 76 | } |
| 77 | |
| 78 | WL_EXPORT struct weston_binding * |
| 79 | weston_compositor_add_button_binding(struct weston_compositor *compositor, |
| 80 | uint32_t button, uint32_t modifier, |
| 81 | weston_button_binding_handler_t handler, |
| 82 | void *data) |
| 83 | { |
| 84 | struct weston_binding *binding; |
| 85 | |
| 86 | binding = weston_compositor_add_binding(compositor, 0, button, 0, |
| 87 | modifier, handler, data); |
| 88 | if (binding == NULL) |
| 89 | return NULL; |
| 90 | |
| 91 | wl_list_insert(compositor->button_binding_list.prev, &binding->link); |
| 92 | |
| 93 | return binding; |
| 94 | } |
| 95 | |
| 96 | WL_EXPORT struct weston_binding * |
| 97 | weston_compositor_add_axis_binding(struct weston_compositor *compositor, |
| 98 | uint32_t axis, uint32_t modifier, |
| 99 | weston_axis_binding_handler_t handler, |
| 100 | void *data) |
| 101 | { |
| 102 | struct weston_binding *binding; |
| 103 | |
| 104 | binding = weston_compositor_add_binding(compositor, 0, 0, axis, |
| 105 | modifier, handler, data); |
| 106 | if (binding == NULL) |
| 107 | return NULL; |
| 108 | |
| 109 | wl_list_insert(compositor->axis_binding_list.prev, &binding->link); |
| 110 | |
| 111 | return binding; |
| 112 | } |
| 113 | |
| 114 | WL_EXPORT struct weston_binding * |
| 115 | weston_compositor_add_debug_binding(struct weston_compositor *compositor, |
| 116 | uint32_t key, |
| 117 | weston_key_binding_handler_t handler, |
| 118 | void *data) |
| 119 | { |
| 120 | struct weston_binding *binding; |
| 121 | |
| 122 | binding = weston_compositor_add_binding(compositor, key, 0, 0, 0, |
| 123 | handler, data); |
| 124 | |
| 125 | wl_list_insert(compositor->debug_binding_list.prev, &binding->link); |
| 126 | |
| 127 | return binding; |
| 128 | } |
| 129 | |
| 130 | WL_EXPORT void |
| 131 | weston_binding_destroy(struct weston_binding *binding) |
| 132 | { |
| 133 | wl_list_remove(&binding->link); |
| 134 | free(binding); |
| 135 | } |
| 136 | |
| 137 | WL_EXPORT void |
| 138 | weston_binding_list_destroy_all(struct wl_list *list) |
| 139 | { |
| 140 | struct weston_binding *binding, *tmp; |
| 141 | |
| 142 | wl_list_for_each_safe(binding, tmp, list, link) |
| 143 | weston_binding_destroy(binding); |
| 144 | } |
| 145 | |
| 146 | struct binding_keyboard_grab { |
| 147 | uint32_t key; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 148 | struct weston_keyboard_grab grab; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 152 | binding_key(struct weston_keyboard_grab *grab, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 153 | uint32_t time, uint32_t key, uint32_t state_w) |
| 154 | { |
| 155 | struct binding_keyboard_grab *b = |
| 156 | container_of(grab, struct binding_keyboard_grab, grab); |
| 157 | struct wl_resource *resource; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 158 | enum wl_keyboard_key_state state = state_w; |
| 159 | uint32_t serial; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 160 | struct weston_keyboard *keyboard = grab->keyboard; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 161 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 162 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 163 | if (key == b->key) { |
| 164 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 165 | weston_keyboard_end_grab(grab->keyboard); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 166 | if (keyboard->input_method_resource) |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 167 | keyboard->grab = &keyboard->input_method_grab; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 168 | free(b); |
| 169 | } |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame^] | 170 | } else if (!wl_list_empty(&keyboard->focus_resource_list)) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 171 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame^] | 172 | wl_resource_for_each(resource, &keyboard->focus_resource_list) { |
| 173 | wl_keyboard_send_key(resource, |
| 174 | serial, |
| 175 | time, |
| 176 | key, |
| 177 | state); |
| 178 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 183 | binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 184 | uint32_t mods_depressed, uint32_t mods_latched, |
| 185 | uint32_t mods_locked, uint32_t group) |
| 186 | { |
| 187 | struct wl_resource *resource; |
| 188 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame^] | 189 | wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) { |
| 190 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 191 | mods_latched, mods_locked, group); |
| 192 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 195 | static const struct weston_keyboard_grab_interface binding_grab = { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 196 | binding_key, |
| 197 | binding_modifiers, |
| 198 | }; |
| 199 | |
| 200 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 201 | install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 202 | { |
| 203 | struct binding_keyboard_grab *grab; |
| 204 | |
| 205 | grab = malloc(sizeof *grab); |
| 206 | grab->key = key; |
| 207 | grab->grab.interface = &binding_grab; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 208 | weston_keyboard_start_grab(seat->keyboard, &grab->grab); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | WL_EXPORT void |
| 212 | weston_compositor_run_key_binding(struct weston_compositor *compositor, |
| 213 | struct weston_seat *seat, |
| 214 | uint32_t time, uint32_t key, |
| 215 | enum wl_keyboard_key_state state) |
| 216 | { |
| 217 | struct weston_binding *b; |
| 218 | |
| 219 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 220 | return; |
| 221 | |
| 222 | wl_list_for_each(b, &compositor->key_binding_list, link) { |
| 223 | if (b->key == key && b->modifier == seat->modifier_state) { |
| 224 | weston_key_binding_handler_t handler = b->handler; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 225 | handler(seat, time, key, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 226 | |
| 227 | /* If this was a key binding and it didn't |
| 228 | * install a keyboard grab, install one now to |
| 229 | * swallow the key release. */ |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 230 | if (seat->keyboard->grab == |
| 231 | &seat->keyboard->default_grab) |
| 232 | install_binding_grab(seat, time, key); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | WL_EXPORT void |
| 238 | weston_compositor_run_button_binding(struct weston_compositor *compositor, |
| 239 | struct weston_seat *seat, |
| 240 | uint32_t time, uint32_t button, |
| 241 | enum wl_pointer_button_state state) |
| 242 | { |
| 243 | struct weston_binding *b; |
| 244 | |
| 245 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 246 | return; |
| 247 | |
| 248 | wl_list_for_each(b, &compositor->button_binding_list, link) { |
| 249 | if (b->button == button && b->modifier == seat->modifier_state) { |
| 250 | weston_button_binding_handler_t handler = b->handler; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 251 | handler(seat, time, button, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 256 | WL_EXPORT int |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 257 | weston_compositor_run_axis_binding(struct weston_compositor *compositor, |
| 258 | struct weston_seat *seat, |
| 259 | uint32_t time, uint32_t axis, |
| 260 | wl_fixed_t value) |
| 261 | { |
| 262 | struct weston_binding *b; |
| 263 | |
| 264 | wl_list_for_each(b, &compositor->axis_binding_list, link) { |
| 265 | if (b->axis == axis && b->modifier == seat->modifier_state) { |
| 266 | weston_axis_binding_handler_t handler = b->handler; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 267 | handler(seat, time, axis, value, b->data); |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 268 | return 1; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 269 | } |
| 270 | } |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 271 | |
| 272 | return 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | WL_EXPORT int |
| 276 | weston_compositor_run_debug_binding(struct weston_compositor *compositor, |
| 277 | struct weston_seat *seat, |
| 278 | uint32_t time, uint32_t key, |
| 279 | enum wl_keyboard_key_state state) |
| 280 | { |
| 281 | weston_key_binding_handler_t handler; |
| 282 | struct weston_binding *binding; |
| 283 | int count = 0; |
| 284 | |
| 285 | wl_list_for_each(binding, &compositor->debug_binding_list, link) { |
| 286 | if (key != binding->key) |
| 287 | continue; |
| 288 | |
| 289 | count++; |
| 290 | handler = binding->handler; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 291 | handler(seat, time, key, binding->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return count; |
| 295 | } |