From: Johan Malm Date: Tue, 16 Jun 2020 06:21:53 +0000 (+0100) Subject: Add keybind.c X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=40f01ed3c927735670bc3235d88c26501493cf23;p=proto%2Flabwc.git Add keybind.c --- diff --git a/README.md b/README.md index 23daeacf..07f1e68f 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,11 @@ It is in early development and has the following aims: - wayland-protocols - xwayland - libxml2 +- glib-2.0 Will soon depend on -- cairo, pango, glib +- cairo, pango ## Roadmap diff --git a/include/rcxml.h b/include/rcxml.h index 4e1d4b0c..4bb7ab79 100644 --- a/include/rcxml.h +++ b/include/rcxml.h @@ -3,16 +3,32 @@ #include #include +#include +#include +#include #include "buf.h" +struct keybind { + uint32_t modifiers; + xkb_keysym_t *keysyms; + size_t keysyms_len; + char *action; + struct wl_list link; +}; + +void keybind_add(struct wl_list *keybinds, const char *keybind, const char *action); +void keybind_init(); +void keybind_print(); + struct rcxml { bool client_side_decorations; + struct wl_list keybinds; }; extern struct rcxml rc; -void rcxml_init(struct rcxml *rc); +void rcxml_init(); void rcxml_parse_xml(struct buf *b); void rcxml_read(const char *filename); void rcxml_get_nodenames(struct buf *b); diff --git a/meson.build b/meson.build index fe4feeb0..62a89284 100644 --- a/meson.build +++ b/meson.build @@ -41,6 +41,7 @@ wayland_server = dependency('wayland-server') wayland_protos = dependency('wayland-protocols') xkbcommon = dependency('xkbcommon') xml2 = dependency('libxml-2.0') +glib = dependency('glib-2.0') labwc_inc = include_directories('include') @@ -49,7 +50,7 @@ subdir('src') subdir('tests') labwc_deps = [ - server_protos, wayland_server, wlroots, xkbcommon, xml2 + server_protos, wayland_server, wlroots, xkbcommon, xml2, glib ] executable( diff --git a/src/config/keybind.c b/src/config/keybind.c new file mode 100644 index 00000000..942a3fa7 --- /dev/null +++ b/src/config/keybind.c @@ -0,0 +1,71 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include + +#include "rcxml.h" + +static uint32_t parse_modifier(const char *symname) +{ + if (!strcmp(symname, "S")) + return WLR_MODIFIER_SHIFT; + else if (!strcmp(symname, "C")) + return WLR_MODIFIER_CTRL; + else if (!strcmp(symname, "A")) + return WLR_MODIFIER_ALT; + else if (!strcmp(symname, "W")) + return WLR_MODIFIER_LOGO; + else + return 0; +} + +void keybind_add(struct wl_list *keybinds, const char *keybind, + const char *action) +{ + struct keybind *k = calloc(1, sizeof(struct keybind)); + xkb_keysym_t keysyms[32]; + gchar **symnames = g_strsplit(keybind, "-", -1); + for (int i = 0; symnames[i]; i++) { + char *symname = symnames[i]; + uint32_t modifier = parse_modifier(symname); + if (modifier != 0) { + k->modifiers |= modifier; + } else { + xkb_keysym_t sym = xkb_keysym_from_name( + symname, XKB_KEYSYM_NO_FLAGS); + if (sym == XKB_KEY_NoSymbol) { + fprintf(stderr, "unknown keybind (%s)\n", + symname); + free(k); + k = NULL; + break; + } + keysyms[k->keysyms_len] = sym; + k->keysyms_len++; + } + } + g_strfreev(symnames); + if (!k) + return; + wl_list_insert(keybinds, &k->link); + k->action = strdup(action); + k->keysyms = malloc(k->keysyms_len * sizeof(xkb_keysym_t)); + memcpy(k->keysyms, keysyms, k->keysyms_len * sizeof(xkb_keysym_t)); +} + +void keybind_init() +{ + keybind_add(&rc.keybinds, "A-Escape", "exit"); + keybind_add(&rc.keybinds, "A-F2", "cycle"); +} + +void keybind_print() +{ + struct keybind *keybind; + wl_list_for_each_reverse (keybind, &rc.keybinds, link) { + printf("KEY=%s\n", keybind->action); + for (size_t i = 0; i < keybind->keysyms_len; i++) + printf(" %d\n", keybind->keysyms[i]); + } +} diff --git a/src/config/meson.build b/src/config/meson.build index 19872e98..ac8e6012 100644 --- a/src/config/meson.build +++ b/src/config/meson.build @@ -1,3 +1,4 @@ labwc_sources += files( 'rcxml.c', + 'keybind.c', ) diff --git a/src/config/rcxml.c b/src/config/rcxml.c index c917e6ab..27417869 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "rcxml.h" @@ -162,9 +163,12 @@ void rcxml_parse_xml(struct buf *b) xmlCleanupParser(); } -void rcxml_init(struct rcxml *rc) +void rcxml_init() { LIBXML_TEST_VERSION + wl_list_init(&rc.keybinds); + keybind_init(); + keybind_print(); } void rcxml_read(const char *filename) diff --git a/src/main.c b/src/main.c index 23547c09..de84e6b5 100644 --- a/src/main.c +++ b/src/main.c @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) return 0; } - rcxml_init(&rc); + rcxml_init(); rcxml_read("data/rc.xml"); theme_read("data/themerc"); diff --git a/tests/meson.build b/tests/meson.build index e128600b..685361f9 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,9 +1,18 @@ +lib_xml2 = library('libxml-2.0') +lib_wayland_server = library('wayland-server') +lib_xkbcommon = library('xkbcommon') +lib_glib = library('glib-2.0') + rcxml_lib = static_library( 'rcxml', - sources: files('../src/config/rcxml.c', '../src/common/buf.c'), - dependencies: xml2, + sources: files( + '../src/config/rcxml.c', + '../src/config/keybind.c', + '../src/common/buf.c' + ), + dependencies: [xml2, wayland_server, xkbcommon, glib], include_directories: [labwc_inc], - link_with: library('libxml-2.0'), + link_with: [lib_xml2, lib_wayland_server, lib_xkbcommon, lib_glib], ) rcxml_tests = [ @@ -17,7 +26,7 @@ foreach t : rcxml_tests testname, sources: [t, 'tap.c'], include_directories: [labwc_inc], - link_with: rcxml_lib, + link_with: [rcxml_lib], ) test(testname, exe) endforeach diff --git a/tests/t1000-rcxml-simple-parse.c b/tests/t1000-rcxml-simple-parse.c index c57cb74c..cb41d0c1 100644 --- a/tests/t1000-rcxml-simple-parse.c +++ b/tests/t1000-rcxml-simple-parse.c @@ -27,7 +27,7 @@ int main(int argc, char **argv) exit(1); write(fd, src, sizeof(src) - 1); - rcxml_init(&rc); + rcxml_init(); rcxml_read(template); unlink(template); diff --git a/tests/t1001-rcxml-nodenames-simple.c b/tests/t1001-rcxml-nodenames-simple.c index fa2823be..88c5a6f7 100644 --- a/tests/t1001-rcxml-nodenames-simple.c +++ b/tests/t1001-rcxml-nodenames-simple.c @@ -35,7 +35,7 @@ int main(int argc, char **argv) plan(1); diag("Parse simple rc.xml and read nodenames"); - rcxml_init(&rc); + rcxml_init(); rcxml_get_nodenames(&actual); rcxml_parse_xml(&source); printf("%s\n", actual.buf);