commit c58bea1

Michael Forney  ·  2013-10-25 00:38:11 +0000 UTC
parent f80b1e4
Move binding handling to binding.c and add to public API
9 files changed,  +175, -92
+2, -1
 1@@ -35,7 +35,8 @@ libswc_la_SOURCES += \
 2     swc.c swc.h \
 3     window.c window.h \
 4     shell.c shell.h \
 5-    shell_surface.c shell_surface.h
 6+    shell_surface.c shell_surface.h \
 7+    binding.c binding.h
 8 
 9 libswc_la_LIBADD = $(wayland_server_LIBS) $(udev_LIBS) $(libevdev_LIBS) \
10                    $(xkbcommon_LIBS) $(drm_LIBS) $(pixman_LIBS) $(wld_LIBS) \
+101, -3
  1@@ -1,8 +1,106 @@
  2+/* swc: swc/binding.c
  3+ *
  4+ * Copyright (c) 2013 Michael Forney
  5+ *
  6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
  7+ * of this software and associated documentation files (the "Software"), to deal
  8+ * in the Software without restriction, including without limitation the rights
  9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10+ * copies of the Software, and to permit persons to whom the Software is
 11+ * furnished to do so, subject to the following conditions:
 12+ *
 13+ * The above copyright notice and this permission notice shall be included in
 14+ * all copies or substantial portions of the Software.
 15+ *
 16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22+ * SOFTWARE.
 23+ */
 24+
 25+#include "swc.h"
 26 #include "binding.h"
 27+#include "keyboard.h"
 28+
 29+#include <wayland-util.h>
 30+
 31+static struct wl_array key_bindings;
 32 
 33-bool swc_binding_init(struct wcf_binding * binding, uint32_t value,
 34-                      uint32_t modifiers, wcf_binding_handler_t handler,
 35-                      void * data)
 36+static bool handle_key(struct swc_keyboard * keyboard, uint32_t time,
 37+                       uint32_t key, uint32_t state)
 38 {
 39+    struct swc_binding * binding;
 40+    char keysym_name[64];
 41+
 42+    if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
 43+    {
 44+        xkb_keysym_t keysym;
 45+
 46+        keysym = xkb_state_key_get_one_sym(keyboard->xkb.state, key + 8);
 47+
 48+        wl_array_for_each(binding, &key_bindings)
 49+        {
 50+            if (binding->value == keysym)
 51+            {
 52+                xkb_mod_mask_t mod_mask;
 53+                uint32_t modifiers = 0;
 54+                mod_mask = xkb_state_serialize_mods(keyboard->xkb.state,
 55+                                                    XKB_STATE_MODS_EFFECTIVE);
 56+                mod_mask = xkb_state_mod_mask_remove_consumed(keyboard->xkb.state, key + 8,
 57+                                                              mod_mask);
 58+
 59+                if (mod_mask & (1 << keyboard->xkb.indices.ctrl))
 60+                    modifiers |= SWC_MOD_CTRL;
 61+                if (mod_mask & (1 << keyboard->xkb.indices.alt))
 62+                    modifiers |= SWC_MOD_ALT;
 63+                if (mod_mask & (1 << keyboard->xkb.indices.super))
 64+                    modifiers |= SWC_MOD_LOGO;
 65+                if (mod_mask & (1 << keyboard->xkb.indices.shift))
 66+                    modifiers |= SWC_MOD_SHIFT;
 67+
 68+                if (binding->modifiers == SWC_MOD_ANY
 69+                    || binding->modifiers == modifiers)
 70+                {
 71+                    binding->handler(time, keysym, binding->data);
 72+                    printf("\t-> handled\n");
 73+                    return true;
 74+                }
 75+            }
 76+        }
 77+    }
 78+
 79+    return false;
 80+}
 81+
 82+static const struct swc_keyboard_handler binding_handler = {
 83+    .key = &handle_key,
 84+};
 85+const struct swc_keyboard_handler * swc_binding_handler = &binding_handler;
 86+
 87+bool swc_bindings_initialize()
 88+{
 89+    wl_array_init(&key_bindings);
 90+
 91+    return true;
 92+}
 93+
 94+void swc_bindings_finalize()
 95+{
 96+    wl_array_release(&key_bindings);
 97+}
 98+
 99+void swc_add_key_binding(uint32_t modifiers, uint32_t value,
100+                         swc_binding_handler_t handler, void * data)
101+{
102+    struct swc_binding * binding;
103+
104+    binding = wl_array_add(&key_bindings, sizeof *binding);
105+    binding->value = value;
106+    binding->modifiers = modifiers;
107+    binding->handler = handler;
108+    binding->data = data;
109 }
110 
+29, -17
 1@@ -1,25 +1,37 @@
 2+/* swc: libswc/binding.h
 3+ *
 4+ * Copyright (c) 2013 Michael Forney
 5+ *
 6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
 7+ * of this software and associated documentation files (the "Software"), to deal
 8+ * in the Software without restriction, including without limitation the rights
 9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25 #ifndef SWC_BINDING_H
26-#define SWC_BINDING_H 1
27+#define SWC_BINDING_H
28 
29-#include <stdint.h>
30-#include <linux/input.h>
31+#include <stdbool.h>
32 
33-#define SWC_MOD_CTRL    (1 << 0)
34-#define SWC_MOD_ALT     (1 << 1)
35-#define SWC_MOD_LOGO    (1 << 2)
36-#define SWC_MOD_SHIFT   (1 << 3)
37-#define SWC_MOD_ANY     (-1)
38+struct swc_keyboard_handler;
39 
40-typedef void (* swc_binding_handler_t)(uint32_t time, uint32_t value,
41-                                       void * data);
42+extern const struct swc_keyboard_handler * swc_binding_handler;
43 
44-struct swc_binding
45-{
46-    uint32_t value;
47-    uint32_t modifiers;
48-    swc_binding_handler_t handler;
49-    void * data;
50-};
51+bool swc_bindings_initialize();
52+void swc_bindings_finalize();
53 
54 #endif
55 
+7, -60
  1@@ -2,6 +2,7 @@
  2 #include <stdio.h>
  3 #include <libudev.h>
  4 
  5+#include "swc.h"
  6 #include "compositor.h"
  7 #include "compositor_surface.h"
  8 #include "cursor_surface.h"
  9@@ -10,6 +11,7 @@
 10 #include "event.h"
 11 #include "region.h"
 12 #include "data_device_manager.h"
 13+#include "binding.h"
 14 #include "util.h"
 15 
 16 static const char default_seat[] = "seat0";
 17@@ -143,61 +145,6 @@ static void perform_update(void * data)
 18 
 19 }
 20 
 21-static bool handle_key(struct swc_keyboard * keyboard, uint32_t time,
 22-                       uint32_t key, uint32_t state)
 23-{
 24-    struct swc_seat * seat;
 25-    struct swc_binding * binding;
 26-    struct swc_compositor * compositor;
 27-    char keysym_name[64];
 28-
 29-    seat = CONTAINER_OF(keyboard, typeof(*seat), keyboard);
 30-    compositor = CONTAINER_OF(seat, typeof(*compositor), seat);
 31-
 32-    if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
 33-    {
 34-        xkb_keysym_t keysym;
 35-
 36-        keysym = xkb_state_key_get_one_sym(keyboard->xkb.state, key + 8);
 37-
 38-        wl_array_for_each(binding, &compositor->key_bindings)
 39-        {
 40-            if (binding->value == keysym)
 41-            {
 42-                xkb_mod_mask_t mod_mask;
 43-                uint32_t modifiers = 0;
 44-                mod_mask = xkb_state_serialize_mods(keyboard->xkb.state,
 45-                                                    XKB_STATE_MODS_EFFECTIVE);
 46-                mod_mask = xkb_state_mod_mask_remove_consumed(keyboard->xkb.state, key + 8,
 47-                                                              mod_mask);
 48-
 49-                if (mod_mask & (1 << keyboard->xkb.indices.ctrl))
 50-                    modifiers |= SWC_MOD_CTRL;
 51-                if (mod_mask & (1 << keyboard->xkb.indices.alt))
 52-                    modifiers |= SWC_MOD_ALT;
 53-                if (mod_mask & (1 << keyboard->xkb.indices.super))
 54-                    modifiers |= SWC_MOD_LOGO;
 55-                if (mod_mask & (1 << keyboard->xkb.indices.shift))
 56-                    modifiers |= SWC_MOD_SHIFT;
 57-
 58-                if (binding->modifiers == SWC_MOD_ANY
 59-                    || binding->modifiers == modifiers)
 60-                {
 61-                    binding->handler(time, keysym, binding->data);
 62-                    printf("\t-> handled\n");
 63-                    return true;
 64-                }
 65-            }
 66-        }
 67-    }
 68-
 69-    return false;
 70-}
 71-
 72-struct swc_keyboard_handler keyboard_handler = {
 73-    .key = &handle_key,
 74-};
 75-
 76 static void handle_focus(struct swc_pointer * pointer)
 77 {
 78     struct swc_seat * seat;
 79@@ -402,7 +349,7 @@ bool swc_compositor_initialize(struct swc_compositor * compositor,
 80     }
 81 
 82     swc_seat_add_event_sources(&compositor->seat, event_loop);
 83-    compositor->seat.keyboard.handler = &keyboard_handler;
 84+    compositor->seat.keyboard.handler = swc_binding_handler;
 85     compositor->seat.pointer.handler = &pointer_handler;
 86     wl_signal_add(&compositor->seat.pointer.event_signal,
 87                   &compositor->pointer_listener);
 88@@ -457,15 +404,15 @@ bool swc_compositor_initialize(struct swc_compositor * compositor,
 89     wl_array_init(&compositor->key_bindings);
 90     wl_signal_init(&compositor->destroy_signal);
 91 
 92-    swc_compositor_add_key_binding(compositor,
 93-        SWC_MOD_CTRL | SWC_MOD_ALT, XKB_KEY_BackSpace, &handle_terminate, display);
 94+    swc_add_key_binding(SWC_MOD_CTRL | SWC_MOD_ALT, XKB_KEY_BackSpace,
 95+                        &handle_terminate, display);
 96 
 97     for (keysym = XKB_KEY_XF86Switch_VT_1;
 98          keysym <= XKB_KEY_XF86Switch_VT_12;
 99          ++keysym)
100     {
101-        swc_compositor_add_key_binding(compositor, SWC_MOD_ANY, keysym,
102-                                       &handle_switch_vt, NULL);
103+        swc_add_key_binding(SWC_MOD_ANY, keysym,
104+                            &handle_switch_vt, NULL);
105     }
106 
107 
+0, -5
 1@@ -3,7 +3,6 @@
 2 
 3 #include "drm.h"
 4 #include "seat.h"
 5-#include "binding.h"
 6 #include "renderer.h"
 7 
 8 #include <wayland-server.h>
 9@@ -54,10 +53,6 @@ void swc_compositor_finish(struct swc_compositor * compositor);
10 void swc_compositor_add_globals(struct swc_compositor * compositor,
11                                 struct wl_display * display);
12 
13-void swc_compositor_add_key_binding(struct swc_compositor * compositor,
14-                                    uint32_t modifiers, xkb_keysym_t key,
15-                                    swc_binding_handler_t handler, void * data);
16-
17 void swc_compositor_schedule_update(struct swc_compositor * compositor,
18                                     struct swc_output * output);
19 
+1, -1
1@@ -25,7 +25,7 @@ struct swc_keyboard
2     struct swc_input_focus focus;
3     struct swc_input_focus_handler focus_handler;
4 
5-    struct swc_keyboard_handler * handler;
6+    const struct swc_keyboard_handler * handler;
7 
8     struct swc_xkb xkb;
9     struct wl_array keys;
+0, -2
1@@ -1,8 +1,6 @@
2 #include "seat.h"
3-
4 #include "evdev_device.h"
5 #include "util.h"
6-#include "binding.h"
7 #include "event.h"
8 
9 #include <stdlib.h>
+13, -3
 1@@ -22,6 +22,7 @@
 2  */
 3 
 4 #include "swc.h"
 5+#include "binding.h"
 6 #include "compositor.h"
 7 #include "shell.h"
 8 
 9@@ -38,10 +39,16 @@ bool swc_initialize(struct wl_display * display,
10 {
11     window_manager = wm;
12 
13+    if (!swc_bindings_initialize())
14+    {
15+        fprintf(stderr, "Could not initialize bindings\n");
16+        goto error0;
17+    }
18+
19     if (!swc_compositor_initialize(&swc.compositor, display))
20     {
21         fprintf(stderr, "Could not initialize compositor\n");
22-        goto error0;
23+        goto error1;
24     }
25 
26     swc_compositor_add_globals(&swc.compositor, display);
27@@ -49,13 +56,15 @@ bool swc_initialize(struct wl_display * display,
28     if (!swc_shell_initialize(display))
29     {
30         fprintf(stderr, "Could not initialize shell\n");
31-        goto error1;
32+        goto error2;
33     }
34 
35     return true;
36 
37-  error1:
38+  error2:
39     swc_compositor_finish(&swc.compositor);
40+  error1:
41+    swc_bindings_finalize();
42   error0:
43     return false;
44 }
45@@ -64,5 +73,6 @@ void swc_finalize()
46 {
47     swc_shell_finalize();
48     swc_compositor_finish(&swc.compositor);
49+    swc_bindings_finalize();
50 }
51 
+22, -0
 1@@ -67,6 +67,28 @@ void swc_window_set_border(struct swc_window * window,
 2                            uint32_t color, uint32_t width);
 3 /* }}} */
 4 
 5+/* Bindings {{{ */
 6+#define SWC_MOD_CTRL    (1 << 0)
 7+#define SWC_MOD_ALT     (1 << 1)
 8+#define SWC_MOD_LOGO    (1 << 2)
 9+#define SWC_MOD_SHIFT   (1 << 3)
10+#define SWC_MOD_ANY     (-1)
11+
12+typedef void (* swc_binding_handler_t)(uint32_t time, uint32_t value,
13+                                       void * data);
14+
15+struct swc_binding
16+{
17+    uint32_t value;
18+    uint32_t modifiers;
19+    swc_binding_handler_t handler;
20+    void * data;
21+};
22+
23+void swc_add_key_binding(uint32_t modifiers, uint32_t value,
24+                         swc_binding_handler_t handler, void * data);
25+/* }}} */
26+
27 /* Events {{{ */
28 struct swc_event
29 {