lid: track the notifier initial state internally, but not externally
authorBenjamin Tissoires <benjamin.tissoires@gmail.com>
Fri, 19 May 2017 10:55:54 +0000 (12:55 +0200)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 23 May 2017 05:10:10 +0000 (15:10 +1000)
What we do not want is libinput to believe the LID is closed while
it's not. But the internal notifier state need to be in sync with the evdev
node, or it's going to be a pain setting the keyboard listener.

But since we don't know if the state is reliable, we track the internal state
separately from the external state so that we can set up the keyboard listener
when the lid is closed, without having libinput actually send lid closed
events for unreliable devices.

https://bugs.freedesktop.org/show_bug.cgi?id=101099

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
src/evdev-lid.c

index 8090780..010cb42 100644 (file)
@@ -34,6 +34,7 @@ struct lid_switch_dispatch {
        enum switch_reliability reliability;
 
        bool lid_is_closed;
+       bool lid_is_closed_client_state;
 
        struct {
                struct evdev_device *keyboard;
@@ -50,6 +51,20 @@ lid_dispatch(struct evdev_dispatch *dispatch)
 }
 
 static void
+lid_switch_notify_toggle(struct lid_switch_dispatch *dispatch,
+                        struct evdev_device *device,
+                        uint64_t time)
+{
+       if (dispatch->lid_is_closed ^ dispatch->lid_is_closed_client_state) {
+               switch_notify_toggle(&device->base,
+                                    time,
+                                    LIBINPUT_SWITCH_LID,
+                                    dispatch->lid_is_closed);
+               dispatch->lid_is_closed_client_state = dispatch->lid_is_closed;
+       }
+}
+
+static void
 lid_switch_keyboard_event(uint64_t time,
                          struct libinput_event *event,
                          void *data)
@@ -79,10 +94,7 @@ lid_switch_keyboard_event(uint64_t time,
         * the key event.
         */
        dispatch->lid_is_closed = false;
-       switch_notify_toggle(&dispatch->device->base,
-                            time,
-                            LIBINPUT_SWITCH_LID,
-                            dispatch->lid_is_closed);
+       lid_switch_notify_toggle(dispatch, dispatch->device, time);
 }
 
 static void
@@ -120,16 +132,12 @@ lid_switch_process_switch(struct lid_switch_dispatch *dispatch,
 
                if (dispatch->lid_is_closed == is_closed)
                        return;
-
                lid_switch_toggle_keyboard_listener(dispatch,
                                                    is_closed);
 
                dispatch->lid_is_closed = is_closed;
 
-               switch_notify_toggle(&device->base,
-                                    time,
-                                    LIBINPUT_SWITCH_LID,
-                                    dispatch->lid_is_closed);
+               lid_switch_notify_toggle(dispatch, device, time);
                break;
        }
 }
@@ -249,10 +257,12 @@ lid_switch_sync_initial_state(struct evdev_device *device,
 {
        struct lid_switch_dispatch *dispatch = lid_dispatch(device->dispatch);
        struct libevdev *evdev = device->evdev;
-       bool is_closed = false;
 
        dispatch->reliability = evdev_read_switch_reliability_prop(device);
 
+       dispatch->lid_is_closed = libevdev_get_event_value(evdev, EV_SW, SW_LID);
+       dispatch->lid_is_closed_client_state = false;
+
        /* For the initial state sync, we depend on whether the lid switch
         * is reliable. If we know it's reliable, we sync as expected.
         * If we're not sure, we ignore the initial state and only sync on
@@ -260,24 +270,11 @@ lid_switch_sync_initial_state(struct evdev_device *device,
         * that always have the switch in 'on' state thus don't mess up our
         * touchpad.
         */
-       switch(dispatch->reliability) {
-       case RELIABILITY_UNKNOWN:
-       case RELIABILITY_WRITE_OPEN:
-               is_closed = false;
-               break;
-       case RELIABILITY_RELIABLE:
-               is_closed = libevdev_get_event_value(evdev, EV_SW, SW_LID);
-               break;
-       }
-
-       dispatch->lid_is_closed = is_closed;
-       if (dispatch->lid_is_closed) {
+       if (dispatch->lid_is_closed &&
+           dispatch->reliability == RELIABILITY_RELIABLE) {
                uint64_t time;
                time = libinput_now(evdev_libinput_context(device));
-               switch_notify_toggle(&device->base,
-                                    time,
-                                    LIBINPUT_SWITCH_LID,
-                                    LIBINPUT_SWITCH_STATE_ON);
+               lid_switch_notify_toggle(dispatch, device, time);
        }
 }