swc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/swc
Log | Files | Refs | README | LICENSE

commit c58bea181dc49705c9055b8b260e8fa32c2c8a1f
parent f80b1e4a83aaa30612f8e883197d58c53b4e7c10
Author: Michael Forney <mforney@mforney.org>
Date:   Thu, 24 Oct 2013 17:38:11 -0700

Move binding handling to binding.c and add to public API

Diffstat:
Mlibswc/Makefile.am | 3++-
Mlibswc/binding.c | 104++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mlibswc/binding.h | 46+++++++++++++++++++++++++++++-----------------
Mlibswc/compositor.c | 67+++++++------------------------------------------------------------
Mlibswc/compositor.h | 5-----
Mlibswc/keyboard.h | 2+-
Mlibswc/seat.c | 2--
Mlibswc/swc.c | 16+++++++++++++---
Mlibswc/swc.h | 22++++++++++++++++++++++
9 files changed, 175 insertions(+), 92 deletions(-)

diff --git a/libswc/Makefile.am b/libswc/Makefile.am @@ -35,7 +35,8 @@ libswc_la_SOURCES += \ swc.c swc.h \ window.c window.h \ shell.c shell.h \ - shell_surface.c shell_surface.h + shell_surface.c shell_surface.h \ + binding.c binding.h libswc_la_LIBADD = $(wayland_server_LIBS) $(udev_LIBS) $(libevdev_LIBS) \ $(xkbcommon_LIBS) $(drm_LIBS) $(pixman_LIBS) $(wld_LIBS) \ diff --git a/libswc/binding.c b/libswc/binding.c @@ -1,8 +1,106 @@ +/* swc: swc/binding.c + * + * Copyright (c) 2013 Michael Forney + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "swc.h" #include "binding.h" +#include "keyboard.h" + +#include <wayland-util.h> + +static struct wl_array key_bindings; -bool swc_binding_init(struct wcf_binding * binding, uint32_t value, - uint32_t modifiers, wcf_binding_handler_t handler, - void * data) +static bool handle_key(struct swc_keyboard * keyboard, uint32_t time, + uint32_t key, uint32_t state) { + struct swc_binding * binding; + char keysym_name[64]; + + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) + { + xkb_keysym_t keysym; + + keysym = xkb_state_key_get_one_sym(keyboard->xkb.state, key + 8); + + wl_array_for_each(binding, &key_bindings) + { + if (binding->value == keysym) + { + xkb_mod_mask_t mod_mask; + uint32_t modifiers = 0; + mod_mask = xkb_state_serialize_mods(keyboard->xkb.state, + XKB_STATE_MODS_EFFECTIVE); + mod_mask = xkb_state_mod_mask_remove_consumed(keyboard->xkb.state, key + 8, + mod_mask); + + if (mod_mask & (1 << keyboard->xkb.indices.ctrl)) + modifiers |= SWC_MOD_CTRL; + if (mod_mask & (1 << keyboard->xkb.indices.alt)) + modifiers |= SWC_MOD_ALT; + if (mod_mask & (1 << keyboard->xkb.indices.super)) + modifiers |= SWC_MOD_LOGO; + if (mod_mask & (1 << keyboard->xkb.indices.shift)) + modifiers |= SWC_MOD_SHIFT; + + if (binding->modifiers == SWC_MOD_ANY + || binding->modifiers == modifiers) + { + binding->handler(time, keysym, binding->data); + printf("\t-> handled\n"); + return true; + } + } + } + } + + return false; +} + +static const struct swc_keyboard_handler binding_handler = { + .key = &handle_key, +}; +const struct swc_keyboard_handler * swc_binding_handler = &binding_handler; + +bool swc_bindings_initialize() +{ + wl_array_init(&key_bindings); + + return true; +} + +void swc_bindings_finalize() +{ + wl_array_release(&key_bindings); +} + +void swc_add_key_binding(uint32_t modifiers, uint32_t value, + swc_binding_handler_t handler, void * data) +{ + struct swc_binding * binding; + + binding = wl_array_add(&key_bindings, sizeof *binding); + binding->value = value; + binding->modifiers = modifiers; + binding->handler = handler; + binding->data = data; } diff --git a/libswc/binding.h b/libswc/binding.h @@ -1,25 +1,37 @@ +/* swc: libswc/binding.h + * + * Copyright (c) 2013 Michael Forney + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #ifndef SWC_BINDING_H -#define SWC_BINDING_H 1 +#define SWC_BINDING_H -#include <stdint.h> -#include <linux/input.h> +#include <stdbool.h> -#define SWC_MOD_CTRL (1 << 0) -#define SWC_MOD_ALT (1 << 1) -#define SWC_MOD_LOGO (1 << 2) -#define SWC_MOD_SHIFT (1 << 3) -#define SWC_MOD_ANY (-1) +struct swc_keyboard_handler; -typedef void (* swc_binding_handler_t)(uint32_t time, uint32_t value, - void * data); +extern const struct swc_keyboard_handler * swc_binding_handler; -struct swc_binding -{ - uint32_t value; - uint32_t modifiers; - swc_binding_handler_t handler; - void * data; -}; +bool swc_bindings_initialize(); +void swc_bindings_finalize(); #endif diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <libudev.h> +#include "swc.h" #include "compositor.h" #include "compositor_surface.h" #include "cursor_surface.h" @@ -10,6 +11,7 @@ #include "event.h" #include "region.h" #include "data_device_manager.h" +#include "binding.h" #include "util.h" static const char default_seat[] = "seat0"; @@ -143,61 +145,6 @@ static void perform_update(void * data) } -static bool handle_key(struct swc_keyboard * keyboard, uint32_t time, - uint32_t key, uint32_t state) -{ - struct swc_seat * seat; - struct swc_binding * binding; - struct swc_compositor * compositor; - char keysym_name[64]; - - seat = CONTAINER_OF(keyboard, typeof(*seat), keyboard); - compositor = CONTAINER_OF(seat, typeof(*compositor), seat); - - if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - { - xkb_keysym_t keysym; - - keysym = xkb_state_key_get_one_sym(keyboard->xkb.state, key + 8); - - wl_array_for_each(binding, &compositor->key_bindings) - { - if (binding->value == keysym) - { - xkb_mod_mask_t mod_mask; - uint32_t modifiers = 0; - mod_mask = xkb_state_serialize_mods(keyboard->xkb.state, - XKB_STATE_MODS_EFFECTIVE); - mod_mask = xkb_state_mod_mask_remove_consumed(keyboard->xkb.state, key + 8, - mod_mask); - - if (mod_mask & (1 << keyboard->xkb.indices.ctrl)) - modifiers |= SWC_MOD_CTRL; - if (mod_mask & (1 << keyboard->xkb.indices.alt)) - modifiers |= SWC_MOD_ALT; - if (mod_mask & (1 << keyboard->xkb.indices.super)) - modifiers |= SWC_MOD_LOGO; - if (mod_mask & (1 << keyboard->xkb.indices.shift)) - modifiers |= SWC_MOD_SHIFT; - - if (binding->modifiers == SWC_MOD_ANY - || binding->modifiers == modifiers) - { - binding->handler(time, keysym, binding->data); - printf("\t-> handled\n"); - return true; - } - } - } - } - - return false; -} - -struct swc_keyboard_handler keyboard_handler = { - .key = &handle_key, -}; - static void handle_focus(struct swc_pointer * pointer) { struct swc_seat * seat; @@ -402,7 +349,7 @@ bool swc_compositor_initialize(struct swc_compositor * compositor, } swc_seat_add_event_sources(&compositor->seat, event_loop); - compositor->seat.keyboard.handler = &keyboard_handler; + compositor->seat.keyboard.handler = swc_binding_handler; compositor->seat.pointer.handler = &pointer_handler; wl_signal_add(&compositor->seat.pointer.event_signal, &compositor->pointer_listener); @@ -457,15 +404,15 @@ bool swc_compositor_initialize(struct swc_compositor * compositor, wl_array_init(&compositor->key_bindings); wl_signal_init(&compositor->destroy_signal); - swc_compositor_add_key_binding(compositor, - SWC_MOD_CTRL | SWC_MOD_ALT, XKB_KEY_BackSpace, &handle_terminate, display); + swc_add_key_binding(SWC_MOD_CTRL | SWC_MOD_ALT, XKB_KEY_BackSpace, + &handle_terminate, display); for (keysym = XKB_KEY_XF86Switch_VT_1; keysym <= XKB_KEY_XF86Switch_VT_12; ++keysym) { - swc_compositor_add_key_binding(compositor, SWC_MOD_ANY, keysym, - &handle_switch_vt, NULL); + swc_add_key_binding(SWC_MOD_ANY, keysym, + &handle_switch_vt, NULL); } diff --git a/libswc/compositor.h b/libswc/compositor.h @@ -3,7 +3,6 @@ #include "drm.h" #include "seat.h" -#include "binding.h" #include "renderer.h" #include <wayland-server.h> @@ -54,10 +53,6 @@ void swc_compositor_finish(struct swc_compositor * compositor); void swc_compositor_add_globals(struct swc_compositor * compositor, struct wl_display * display); -void swc_compositor_add_key_binding(struct swc_compositor * compositor, - uint32_t modifiers, xkb_keysym_t key, - swc_binding_handler_t handler, void * data); - void swc_compositor_schedule_update(struct swc_compositor * compositor, struct swc_output * output); diff --git a/libswc/keyboard.h b/libswc/keyboard.h @@ -25,7 +25,7 @@ struct swc_keyboard struct swc_input_focus focus; struct swc_input_focus_handler focus_handler; - struct swc_keyboard_handler * handler; + const struct swc_keyboard_handler * handler; struct swc_xkb xkb; struct wl_array keys; diff --git a/libswc/seat.c b/libswc/seat.c @@ -1,8 +1,6 @@ #include "seat.h" - #include "evdev_device.h" #include "util.h" -#include "binding.h" #include "event.h" #include <stdlib.h> diff --git a/libswc/swc.c b/libswc/swc.c @@ -22,6 +22,7 @@ */ #include "swc.h" +#include "binding.h" #include "compositor.h" #include "shell.h" @@ -38,10 +39,16 @@ bool swc_initialize(struct wl_display * display, { window_manager = wm; + if (!swc_bindings_initialize()) + { + fprintf(stderr, "Could not initialize bindings\n"); + goto error0; + } + if (!swc_compositor_initialize(&swc.compositor, display)) { fprintf(stderr, "Could not initialize compositor\n"); - goto error0; + goto error1; } swc_compositor_add_globals(&swc.compositor, display); @@ -49,13 +56,15 @@ bool swc_initialize(struct wl_display * display, if (!swc_shell_initialize(display)) { fprintf(stderr, "Could not initialize shell\n"); - goto error1; + goto error2; } return true; - error1: + error2: swc_compositor_finish(&swc.compositor); + error1: + swc_bindings_finalize(); error0: return false; } @@ -64,5 +73,6 @@ void swc_finalize() { swc_shell_finalize(); swc_compositor_finish(&swc.compositor); + swc_bindings_finalize(); } diff --git a/libswc/swc.h b/libswc/swc.h @@ -67,6 +67,28 @@ void swc_window_set_border(struct swc_window * window, uint32_t color, uint32_t width); /* }}} */ +/* Bindings {{{ */ +#define SWC_MOD_CTRL (1 << 0) +#define SWC_MOD_ALT (1 << 1) +#define SWC_MOD_LOGO (1 << 2) +#define SWC_MOD_SHIFT (1 << 3) +#define SWC_MOD_ANY (-1) + +typedef void (* swc_binding_handler_t)(uint32_t time, uint32_t value, + void * data); + +struct swc_binding +{ + uint32_t value; + uint32_t modifiers; + swc_binding_handler_t handler; + void * data; +}; + +void swc_add_key_binding(uint32_t modifiers, uint32_t value, + swc_binding_handler_t handler, void * data); +/* }}} */ + /* Events {{{ */ struct swc_event {