shl: move log.[ch] to shl_log.[ch]
[platform/upstream/kmscon.git] / src / uterm_input_uxkb.c
index 969072d..3053dfc 100644 (file)
 #include <string.h>
 #include <unistd.h>
 #include <xkbcommon/xkbcommon.h>
-#include "log.h"
 #include "shl_hook.h"
+#include "shl_log.h"
 #include "shl_misc.h"
-#include "uterm.h"
 #include "uterm_input.h"
+#include "uterm_input_internal.h"
 
 #define LOG_SUBSYSTEM "input_uxkb"
 
 int uxkb_desc_init(struct uterm_input *input,
+                  const char *model,
                   const char *layout,
                   const char *variant,
                   const char *options)
@@ -48,7 +49,7 @@ int uxkb_desc_init(struct uterm_input *input,
        int ret;
        struct xkb_rule_names rmlvo = {
                .rules = "evdev",
-               .model = "",
+               .model = model,
                .layout = layout,
                .variant = variant,
                .options = options,
@@ -62,10 +63,11 @@ int uxkb_desc_init(struct uterm_input *input,
 
        input->keymap = xkb_keymap_new_from_names(input->ctx, &rmlvo, 0);
        if (!input->keymap) {
-               log_warn("failed to create keymap (%s, %s, %s), "
+               log_warn("failed to create keymap (%s, %s, %s, %s), "
                         "reverting to default system keymap",
-                        layout, variant, options);
+                        model, layout, variant, options);
 
+               rmlvo.model = "";
                rmlvo.layout = "";
                rmlvo.variant = "";
                rmlvo.options = "";
@@ -79,8 +81,8 @@ int uxkb_desc_init(struct uterm_input *input,
                }
        }
 
-       log_debug("new keyboard description (%s, %s, %s)",
-                 layout, variant, options);
+       log_debug("new keyboard description (%s, %s, %s, %s)",
+                 model, layout, variant, options);
        return 0;
 
 err_ctx:
@@ -149,7 +151,7 @@ static void uxkb_dev_update_keyboard_leds(struct uterm_input_dev *dev)
                { LED_SCROLLL, XKB_LED_NAME_SCROLL },
        };
        struct input_event events[sizeof(leds) / sizeof(*leds)];
-       int i;
+       int i, ret;
 
        if (!(dev->capabilities & UTERM_DEVICE_HAS_LEDS))
                return;
@@ -164,7 +166,9 @@ static void uxkb_dev_update_keyboard_leds(struct uterm_input_dev *dev)
                        events[i].value = 1;
        }
 
-       write(dev->rfd, events, sizeof(events));
+       ret = write(dev->rfd, events, sizeof(events));
+       if (ret != sizeof(events))
+               log_warning("cannot update LED state (%d): %m", errno);
 }
 
 static inline int uxkb_dev_resize_event(struct uterm_input_dev *dev, size_t s)
@@ -337,28 +341,58 @@ int uxkb_dev_process(struct uterm_input_dev *dev,
        return 0;
 }
 
-/*
- * Call this when we regain control of the keyboard after losing it.
- * We don't reset the locked group, this should survive a VT switch, etc.
- */
-void uxkb_dev_reset(struct uterm_input_dev *dev)
+void uxkb_dev_sleep(struct uterm_input_dev *dev)
 {
-       struct xkb_state *state;
+       /*
+        * While the device is asleep, we don't receive key events. This
+        * means that when we wake up, the keyboard state may be different
+        * (e.g. some key is pressed but we don't know about it). This can
+        * cause various problems, like stuck modifiers: consider if we
+        * miss a release of the left Shift key. When the user presses it
+        * again, xkb_state_update_key() will think there is *another* left
+        * Shift key that was pressed. When the key is released, it's as if
+        * this "second" key was released, but the "first" is still left
+        * pressed.
+        * To handle this, when the device goes to sleep, we save our
+        * current knowledge of the keyboard's press/release state. On wake
+        * up, we compare the states before and after, and just feed
+        * xkb_state_update_key() the deltas.
+        */
+       memset(dev->key_state_bits, 0, sizeof(dev->key_state_bits));
+       errno = 0;
+       ioctl(dev->rfd, EVIOCGKEY(sizeof(dev->key_state_bits)),
+             dev->key_state_bits);
+       if (errno)
+               log_warn("failed to save keyboard state (%d): %m", errno);
+}
 
-       /* TODO: Urghs, while the input device was closed we might have missed
-        * some events that affect internal state. As xkbcommon does not provide
-        * a way to reset the internal state, we simply recreate the state. This
-        * should have the same effect.
-        * It also has a bug that if the CTRL-Release event is skipped, then
-        * every further release will never perform a _real_ release. Kind of
-        * buggy so we should fix it upstream. */
-       state = xkb_state_new(dev->input->keymap);
-       if (!state) {
-               log_warning("cannot recreate xkb-state");
+void uxkb_dev_wake_up(struct uterm_input_dev *dev)
+{
+       uint32_t code;
+       char *old_bits, cur_bits[sizeof(dev->key_state_bits)];
+       char old_bit, cur_bit;
+
+       old_bits = dev->key_state_bits;
+
+       memset(cur_bits, 0, sizeof(cur_bits));
+       errno = 0;
+       ioctl(dev->rfd, EVIOCGKEY(sizeof(cur_bits)), cur_bits);
+       if (errno) {
+               log_warn("failed to get current keyboard state (%d): %m",
+                        errno);
                return;
        }
-       xkb_state_unref(dev->state);
-       dev->state = state;
+
+       for (code = 0; code < KEY_CNT; code++) {
+               old_bit = (old_bits[code / 8] & (1 << (code % 8)));
+               cur_bit = (cur_bits[code / 8] & (1 << (code % 8)));
+
+               if (old_bit == cur_bit)
+                       continue;
+
+               xkb_state_update_key(dev->state, code + EVDEV_KEYCODE_OFFSET,
+                                    cur_bit ? XKB_KEY_DOWN : XKB_KEY_UP);
+       }
 
        uxkb_dev_update_keyboard_leds(dev);
 }