Add keybind.c

This commit is contained in:
Johan Malm 2020-06-16 07:21:53 +01:00
parent 5bd55570ba
commit 40f01ed3c9
10 changed files with 114 additions and 11 deletions

View file

@ -16,10 +16,11 @@ It is in early development and has the following aims:
- wayland-protocols - wayland-protocols
- xwayland - xwayland
- libxml2 - libxml2
- glib-2.0
Will soon depend on Will soon depend on
- cairo, pango, glib - cairo, pango
## Roadmap ## Roadmap

View file

@ -3,16 +3,32 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <wlr/types/wlr_keyboard.h>
#include <wayland-server-core.h>
#include <xkbcommon/xkbcommon.h>
#include "buf.h" #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 { struct rcxml {
bool client_side_decorations; bool client_side_decorations;
struct wl_list keybinds;
}; };
extern struct rcxml rc; extern struct rcxml rc;
void rcxml_init(struct rcxml *rc); void rcxml_init();
void rcxml_parse_xml(struct buf *b); void rcxml_parse_xml(struct buf *b);
void rcxml_read(const char *filename); void rcxml_read(const char *filename);
void rcxml_get_nodenames(struct buf *b); void rcxml_get_nodenames(struct buf *b);

View file

@ -41,6 +41,7 @@ wayland_server = dependency('wayland-server')
wayland_protos = dependency('wayland-protocols') wayland_protos = dependency('wayland-protocols')
xkbcommon = dependency('xkbcommon') xkbcommon = dependency('xkbcommon')
xml2 = dependency('libxml-2.0') xml2 = dependency('libxml-2.0')
glib = dependency('glib-2.0')
labwc_inc = include_directories('include') labwc_inc = include_directories('include')
@ -49,7 +50,7 @@ subdir('src')
subdir('tests') subdir('tests')
labwc_deps = [ labwc_deps = [
server_protos, wayland_server, wlroots, xkbcommon, xml2 server_protos, wayland_server, wlroots, xkbcommon, xml2, glib
] ]
executable( executable(

71
src/config/keybind.c Normal file
View file

@ -0,0 +1,71 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#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]);
}
}

View file

@ -1,3 +1,4 @@
labwc_sources += files( labwc_sources += files(
'rcxml.c', 'rcxml.c',
'keybind.c',
) )

View file

@ -8,6 +8,7 @@
#include <libxml/tree.h> #include <libxml/tree.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <wayland-server-core.h>
#include "rcxml.h" #include "rcxml.h"
@ -162,9 +163,12 @@ void rcxml_parse_xml(struct buf *b)
xmlCleanupParser(); xmlCleanupParser();
} }
void rcxml_init(struct rcxml *rc) void rcxml_init()
{ {
LIBXML_TEST_VERSION LIBXML_TEST_VERSION
wl_list_init(&rc.keybinds);
keybind_init();
keybind_print();
} }
void rcxml_read(const char *filename) void rcxml_read(const char *filename)

View file

@ -27,7 +27,7 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
rcxml_init(&rc); rcxml_init();
rcxml_read("data/rc.xml"); rcxml_read("data/rc.xml");
theme_read("data/themerc"); theme_read("data/themerc");

View file

@ -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_lib = static_library(
'rcxml', 'rcxml',
sources: files('../src/config/rcxml.c', '../src/common/buf.c'), sources: files(
dependencies: xml2, '../src/config/rcxml.c',
'../src/config/keybind.c',
'../src/common/buf.c'
),
dependencies: [xml2, wayland_server, xkbcommon, glib],
include_directories: [labwc_inc], include_directories: [labwc_inc],
link_with: library('libxml-2.0'), link_with: [lib_xml2, lib_wayland_server, lib_xkbcommon, lib_glib],
) )
rcxml_tests = [ rcxml_tests = [
@ -17,7 +26,7 @@ foreach t : rcxml_tests
testname, testname,
sources: [t, 'tap.c'], sources: [t, 'tap.c'],
include_directories: [labwc_inc], include_directories: [labwc_inc],
link_with: rcxml_lib, link_with: [rcxml_lib],
) )
test(testname, exe) test(testname, exe)
endforeach endforeach

View file

@ -27,7 +27,7 @@ int main(int argc, char **argv)
exit(1); exit(1);
write(fd, src, sizeof(src) - 1); write(fd, src, sizeof(src) - 1);
rcxml_init(&rc); rcxml_init();
rcxml_read(template); rcxml_read(template);
unlink(template); unlink(template);

View file

@ -35,7 +35,7 @@ int main(int argc, char **argv)
plan(1); plan(1);
diag("Parse simple rc.xml and read nodenames"); diag("Parse simple rc.xml and read nodenames");
rcxml_init(&rc); rcxml_init();
rcxml_get_nodenames(&actual); rcxml_get_nodenames(&actual);
rcxml_parse_xml(&source); rcxml_parse_xml(&source);
printf("%s\n", actual.buf); printf("%s\n", actual.buf);