/*
 * vim:ts=4:sw=4:expandtab
 *
 * i3 - an improved dynamic tiling window manager
 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
 *
 * key_press.c: key press handler
 *
 */
#include "all.h"
#include "guile.h"
#include <xkbcommon/xkbcommon.h>

/*
 * There was a KeyPress or KeyRelease (both events have the same fields). We
 * compare this key code with our bindings table and pass the bound action to
 * parse_command().
 *
 */
void handle_key_press(xcb_key_press_event_t *event) {
    const bool key_release = (event->response_type == XCB_KEY_RELEASE);

    last_timestamp = event->time;

    /* printf("%s %d, state raw = 0x%x\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state); */

    /* int col = (event->state & XCB_MOD_MASK_SHIFT); */
    int col = (event->state & XCB_MOD_MASK_ANY);
    xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, col);
    if (sym == XKB_KEY_NoSymbol) {
      ELOG("Could not translate string to key symbol: \"%s\"\n", sym);
      return;
    }
    
    char buf[7];
    xkb_keysym_to_utf8(sym, buf, 7);
      
    // Let guile resolve the binding for us
    guile_hook("key-press-hook",
	       scm_list_3(scm_from_int(event->response_type),
			  scm_from_int(event->state),
			  scm_from_utf8_string(buf)));
      
    /* Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event); */
    
    /* /\* if we couldn't find a binding, we are done *\/ */
    /* if (bind == NULL) */
    /*     return; */

    /* CommandResult *result = run_binding(bind, NULL); */
    /* command_result_free(result); */
}
