Bug 102039 - LID_SWITCH_RELIABILITY=write_open doesn't work
Summary: LID_SWITCH_RELIABILITY=write_open doesn't work
Status: RESOLVED FIXED
Alias: None
Product: Wayland
Classification: Unclassified
Component: libinput (show other bugs)
Version: unspecified
Hardware: x86-64 (AMD64) Linux (All)
: medium major
Assignee: Wayland bug list
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-04 17:11 UTC by Jason D. Clinton
Modified: 2017-09-25 04:29 UTC (History)
3 users (show)

See Also:
i915 platform:
i915 features:


Attachments
evemu-describe of laptop keyboard (14.70 KB, text/plain)
2017-08-17 13:55 UTC, Jason D. Clinton
Details
udev: mark Razer built-in keyboard as internal (1.03 KB, patch)
2017-08-17 19:38 UTC, Peter Hutterer
Details | Splinter Review
0001-evdev-only-pair-a-keyboard-with-a-lid-switch-when-it.patch (1.63 KB, patch)
2017-09-12 00:15 UTC, Peter Hutterer
Details | Splinter Review
evemu-describe of video switch keyboard (6.92 KB, text/plain)
2017-09-13 01:16 UTC, Jason D. Clinton
Details
evemu-describe of regular internal keyboard (14.69 KB, text/plain)
2017-09-13 01:17 UTC, Jason D. Clinton
Details

Description Jason D. Clinton 2017-08-04 17:11:31 UTC
Background: Benjamin Tissoires reverted an upstream kernel change which was intended to make Linux more compatible with default Windows behavior:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/acpi/button.c?id=878d8db039daac0938238e9a40a5bd6e50ee3c9b

The original commit was to work around bugs like this one where the laptop lid switch works as expected in Windows without installing any vendor drivers:
https://bugzilla.kernel.org/show_bug.cgi?id=191211

With the revert, the bad behavior has returned (with kernel option as only recourse for users).

In the revert message, he suggests working around the problem in user space with a HWDB rule. I have created one:

$ cat /etc/udev/hwdb.d/91-libinput-model-quirks.hwdb
##########################################
# Razer
##########################################
# Razer Blade laptops need us to write the open lid switch event
libinput:name:*Lid Switch*:dmi:*svnRazer:pnBlade*
 LIBINPUT_ATTR_LID_SWITCH_RELIABILITY=write_open

And indeed, it sets the property:

$ udevadm info --path=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
N: input/event0
E: DEVNAME=/dev/input/event0
E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
E: ID_INPUT=1
E: ID_INPUT_SWITCH=1
E: ID_PATH=acpi-PNP0C0D:00
E: ID_PATH_TAG=acpi-PNP0C0D_00
E: LIBINPUT_ATTR_LID_SWITCH_RELIABILITY=write_open
E: LIBINPUT_DEVICE_GROUP=19/0/5/0:PNP0C0D/button
E: MAJOR=13
E: MINOR=64
E: SUBSYSTEM=input
E: TAGS=:power-switch:
E: USEC_INITIALIZED=64391649

However, libinput doesn't actually write open to the lid switch and prevent a system from suspending after lid open. Systemd eventually times out and sees that the lid is closed:

$ sudo cat /proc/acpi/button/lid/LID0/state 
state:      closed

$ libinput --version
1.8.0

Running Wayland on F26 with:
Linux version 4.13.0-0.rc3.git0.1.fc27.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 7.1.1 20170718 (Red Hat 7.1.1-6) (GCC)) #1 SMP Mon Jul 31 16:41:10 UTC 2017

Please fix libinput or revert the upstream kernel revert.
Comment 1 Jason D. Clinton 2017-08-04 17:13:11 UTC
CC'd Lv Zheng from Intel whose commit was reverted.
Comment 2 Peter Hutterer 2017-08-16 02:03:39 UTC
> $ sudo cat /proc/acpi/button/lid/LID0/state 
> state:      closed

libinput cannot write to /proc, it only writes to the lid switch device's event node. What's the state of the SW_LID there? Is that one correct?
Comment 3 Jason D. Clinton 2017-08-16 02:26:43 UTC
When closing the lid, suspending, opening the lid and resuming (all with the HWDB change above enabled), only the lid close event shows up:

$ sudo libinput-debug-events --device /dev/input/event0 
WARNING: This compatibility wrapper will be removed in the future. Please run 'libinput debug-events' instead
-event0   DEVICE_ADDED     Lid Switch                        seat0 default group1  cap:S
 event0   SWITCH_TOGGLE     +7.30s	switch lid state 1
^C

$ udevadm info --path=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
N: input/event0
E: DEVNAME=/dev/input/event0
E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:18/PNP0C09:01/PNP0C0D:00/input/input0/event0
E: ID_INPUT=1
E: ID_INPUT_SWITCH=1
E: ID_PATH=acpi-PNP0C0D:00
E: ID_PATH_TAG=acpi-PNP0C0D_00
E: LIBINPUT_ATTR_LID_SWITCH_RELIABILITY=write_open
E: LIBINPUT_DEVICE_GROUP=19/0/5/0:PNP0C0D/button
E: MAJOR=13
E: MINOR=64
E: SUBSYSTEM=input
E: TAGS=:power-switch:
E: USEC_INITIALIZED=12966795
Comment 4 Peter Hutterer 2017-08-16 04:38:23 UTC
Post the top of libinput-debug-events --verbose please, that should show if libinput pairs the switch correctly. The obvious question too: are you typing or using the touchpad?

(In reply to Jason D. Clinton from comment #3)
> When closing the lid, suspending, opening the lid and resuming (all with the
> HWDB change above enabled), only the lid close event shows up:
> 
> $ sudo libinput-debug-events --device /dev/input/event0 
> WARNING: This compatibility wrapper will be removed in the future. Please
> run 'libinput debug-events' instead
> -event0   DEVICE_ADDED     Lid Switch                        seat0 default
> group1  cap:S
>  event0   SWITCH_TOGGLE     +7.30s	switch lid state 1
> ^C

this cannot work. libinput relies on other devices (keyboard, touchpad) to send events and thus signal that the lid is open. If you init libinput this way, it only listens to the lid device, so it can never detect any input that would trigger the write open. See http://who-t.blogspot.com.au/2017/02/libinput-and-lid-switch-events.html for an explanation
Comment 5 Jason D. Clinton 2017-08-16 14:02:22 UTC
$ sudo libinput-debug-events --verbose 
WARNING: This compatibility wrapper will be removed in the future. Please run 'libinput debug-events' instead
event3  - Power Button: is tagged by udev as: Keyboard
event3  - Power Button: device is a keyboard
event5  - Video Bus: is tagged by udev as: Keyboard
event5  - Video Bus: device is a keyboard
event0  - Lid Switch: is tagged by udev as: Switch
event0  - Lid Switch: device is a switch device
event0  - Lid Switch: Lid Switch: will write switch open events
event2  - Power Button: is tagged by udev as: Keyboard
event2  - Power Button: device is a keyboard
event1  - Sleep Button: is tagged by udev as: Keyboard
event1  - Sleep Button: device is a keyboard
event21 - Logitech Webcam C930e: is tagged by udev as: Keyboard
event21 - Logitech Webcam C930e: device is a keyboard
event6  - Razer Razer DeathAdder Elite: is tagged by udev as: Mouse
event6  - Razer Razer DeathAdder Elite: device is a pointer
event7  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event7  - Razer Razer DeathAdder Elite: device is a keyboard
event8  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event8  - Razer Razer DeathAdder Elite: device is a keyboard
event20 - USB Camera: USB Camera: is tagged by udev as: Keyboard
event20 - USB Camera: USB Camera: device is a keyboard
event9  - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event9  - Razer Razer Blade Stealth: device is a keyboard
event10 - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event10 - Razer Razer Blade Stealth: device is a keyboard
event11 - Razer Razer Blade Stealth: is tagged by udev as: Mouse
event11 - Razer Razer Blade Stealth: device is a pointer
event12 - Synaptics TM2438-005: is tagged by udev as: Touchpad
event12 - using pressure-based touch detection
event12 - thumb: enabled thumb detection (+pressure)
event12 - Synaptics TM2438-005: device is a touchpad
event12 - lid_switch: activated for Synaptics TM2438-005<->Lid Switch
event13 - HDA Intel PCH Mic: is tagged by udev as: Switch
event14 - HDA Intel PCH Headphone: is tagged by udev as: Switch
event15 - HDA Intel PCH HDMI/DP,pcm=3: is tagged by udev as: Switch
event16 - HDA Intel PCH HDMI/DP,pcm=7: is tagged by udev as: Switch
event17 - HDA Intel PCH HDMI/DP,pcm=8: is tagged by udev as: Switch
event18 - HDA Intel PCH HDMI/DP,pcm=9: is tagged by udev as: Switch
event19 - HDA Intel PCH HDMI/DP,pcm=10: is tagged by udev as: Switch
event4  - AT Raw Set 2 keyboard: is tagged by udev as: Keyboard
event4  - AT Raw Set 2 keyboard: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->AT Raw Set 2 keyboard
event12 - palm: dwt activated with Synaptics TM2438-005<->AT Raw Set 2 keyboard
-event3   DEVICE_ADDED     Power Button                      seat0 default group1  cap:k
-event5   DEVICE_ADDED     Video Bus                         seat0 default group2  cap:k
-event0   DEVICE_ADDED     Lid Switch                        seat0 default group3  cap:S
-event2   DEVICE_ADDED     Power Button                      seat0 default group4  cap:k
-event1   DEVICE_ADDED     Sleep Button                      seat0 default group5  cap:k
-event21  DEVICE_ADDED     Logitech Webcam C930e             seat0 default group6  cap:k
-event6   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:p left scroll-nat scroll-button
-event7   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:kp scroll-nat
-event8   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:k
-event20  DEVICE_ADDED     USB Camera: USB Camera            seat0 default group8  cap:k
-event9   DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:k
-event10  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:kp scroll-nat
-event11  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:p left scroll-nat scroll-button
-event12  DEVICE_ADDED     Synaptics TM2438-005              seat0 default group10 cap:pg  size 102x60mm tap(dl off) left scroll-nat scroll-2fg-edge click-buttonareas-clickfinger dwt-on
-event13  DEVICE_ADDED     HDA Intel PCH Mic                 seat0 default group11 cap:
-event14  DEVICE_ADDED     HDA Intel PCH Headphone           seat0 default group11 cap:
-event15  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=3       seat0 default group11 cap:
-event16  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=7       seat0 default group11 cap:
-event17  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=8       seat0 default group11 cap:
-event18  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=9       seat0 default group11 cap:
-event19  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=10      seat0 default group11 cap:
-event4   DEVICE_ADDED     AT Raw Set 2 keyboard             seat0 default group12 cap:k

AT Raw Set 2 is some kind of bizarre Razer keyboard device related to the caps lock light so that wouldn't work.

> this cannot work. libinput relies on other devices (keyboard, touchpad) to send events and thus signal that the lid is open. If you init libinput this way, it only listens to the lid device, so it can never detect any input that would trigger the write open. See http://who-t.blogspot.com.au/2017/02/libinput-and-lid-switch-events.html for an explanation

Yes. I had to unlock my screensaver on resume after lid open so I did interact with the keyboard and touchpad.
Comment 6 Peter Hutterer 2017-08-16 22:14:36 UTC
Which device is the internal keyboard? Looks like what we have to do is merely make sure that the pairing is correct. *attach* the evemu-describe of that keyboard please.
Comment 7 Jason D. Clinton 2017-08-17 13:55:40 UTC
Created attachment 133574 [details]
evemu-describe of laptop keyboard

This is the right one:

Device:           Razer Razer Blade Stealth
Kernel:           /dev/input/event10
Group:            9
Seat:             seat0, default
Capabilities:     keyboard pointer 

evemu-describe of this keyboard attached.
Comment 8 Peter Hutterer 2017-08-17 19:38:46 UTC
Created attachment 133580 [details] [review]
udev: mark Razer built-in keyboard as internal
Comment 9 Peter Hutterer 2017-08-17 19:41:59 UTC
Give that patch a try please, you'll need to update the hwdb after installing, see https://wayland.freedesktop.org/libinput/doc/latest/faq.html#faq_hwdb_changes

you don't actually need to build libinput, dropping the hwdb file in is sufficient. Make sure the new udev property is applied to the device before restarting.

There are two outstanding potential issues: one is that we may have to unset something on the "AT Raw Set 2 keyboard" (I'll need an evemu-describe for that too please) and the other is the question of whether that VID/PID is the same as on any external keyboards. I hope not.
Comment 10 Jason D. Clinton 2017-08-17 19:49:55 UTC
Great, I'll try that at home tonight.

As it happens, I have a Razer external keyboard attached to my work desktop:
Device:           Razer Razer BlackWidow Chroma V2
Kernel:           /dev/input/event5
Group:            4
Seat:             seat0, default
Capabilities:     keyboard

$ sudo evemu-describe /dev/input/event5 | head
# EVEMU 1.3
# Kernel: 4.9.0-3-amd64
# DMI: dmi:bvnHewlett-Packard:bvrM60v02.29:bd11/08/2016:svnHewlett-Packard:pnHPZ840Workstation:pvr:rvnHewlett-Packard:rn2129:rvr1.00:cvnHewlett-Packard:ct6:cvr:
# Input device name: "Razer Razer BlackWidow Chroma V2"
# Input device ID: bus 0x03 vendor 0x1532 product 0x221 version 0x111
Comment 11 Jason D. Clinton 2017-08-18 01:12:17 UTC
I did all of that and restarted. libinput shows that the right keyboard is listed as associated with the lid_switch now (and not the AT Raw):

$ sudo libinput-debug-events --verbose
WARNING: This compatibility wrapper will be removed in the future. Please run 'libinput debug-events' instead
event3  - Power Button: is tagged by udev as: Keyboard
event3  - Power Button: device is a keyboard
event4  - Video Bus: is tagged by udev as: Keyboard
event4  - Video Bus: device is a keyboard
event0  - Lid Switch: is tagged by udev as: Switch
event0  - Lid Switch: device is a switch device
event0  - Lid Switch: Lid Switch: will write switch open events
event2  - Power Button: is tagged by udev as: Keyboard
event2  - Power Button: device is a keyboard
event1  - Sleep Button: is tagged by udev as: Keyboard
event1  - Sleep Button: device is a keyboard
event20 - Logitech Webcam C930e: is tagged by udev as: Keyboard
event20 - Logitech Webcam C930e: device is a keyboard
event5  - Razer Razer DeathAdder Elite: is tagged by udev as: Mouse
event5  - Razer Razer DeathAdder Elite: device is a pointer
event6  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event6  - Razer Razer DeathAdder Elite: device is a keyboard
event7  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event7  - Razer Razer DeathAdder Elite: device is a keyboard
event19 - USB Camera: USB Camera: is tagged by udev as: Keyboard
event19 - USB Camera: USB Camera: device is a keyboard
event8  - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event8  - Razer Razer Blade Stealth: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->Razer Razer Blade Stealth
event9  - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event9  - Razer Razer Blade Stealth: device is a keyboard
event10 - Razer Razer Blade Stealth: is tagged by udev as: Mouse
event10 - Razer Razer Blade Stealth: device is a pointer
event11 - Synaptics TM2438-005: is tagged by udev as: Touchpad
event11 - using pressure-based touch detection
event11 - thumb: enabled thumb detection (+pressure)
event11 - Synaptics TM2438-005: device is a touchpad
event11 - lid_switch: activated for Synaptics TM2438-005<->Lid Switch
event11 - palm: dwt activated with Synaptics TM2438-005<->Razer Razer Blade Stealth
event12 - HDA Intel PCH Mic: is tagged by udev as: Switch
event13 - HDA Intel PCH Headphone: is tagged by udev as: Switch
event14 - HDA Intel PCH HDMI/DP,pcm=3: is tagged by udev as: Switch
event15 - HDA Intel PCH HDMI/DP,pcm=7: is tagged by udev as: Switch
event16 - HDA Intel PCH HDMI/DP,pcm=8: is tagged by udev as: Switch
event17 - HDA Intel PCH HDMI/DP,pcm=9: is tagged by udev as: Switch
event18 - HDA Intel PCH HDMI/DP,pcm=10: is tagged by udev as: Switch
event21 - AT Raw Set 2 keyboard: is tagged by udev as: Keyboard
event21 - AT Raw Set 2 keyboard: device is a keyboard
-event3   DEVICE_ADDED     Power Button                      seat0 default group1  cap:k
-event4   DEVICE_ADDED     Video Bus                         seat0 default group2  cap:k
-event0   DEVICE_ADDED     Lid Switch                        seat0 default group3  cap:S
-event2   DEVICE_ADDED     Power Button                      seat0 default group4  cap:k
-event1   DEVICE_ADDED     Sleep Button                      seat0 default group5  cap:k
-event20  DEVICE_ADDED     Logitech Webcam C930e             seat0 default group6  cap:k
-event5   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:p left scroll-nat scroll-button
-event6   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:kp scroll-nat
-event7   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:k
-event19  DEVICE_ADDED     USB Camera: USB Camera            seat0 default group8  cap:k
-event8   DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:k
-event9   DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:kp scroll-nat
-event10  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:p left scroll-nat scroll-button
-event11  DEVICE_ADDED     Synaptics TM2438-005              seat0 default group10 cap:pg  size 102x60mm tap(dl off) left scroll-nat scroll-2fg-edge click-buttonareas-clickfinger dwt-on
-event12  DEVICE_ADDED     HDA Intel PCH Mic                 seat0 default group11 cap:
-event13  DEVICE_ADDED     HDA Intel PCH Headphone           seat0 default group11 cap:
-event14  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=3       seat0 default group11 cap:
-event15  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=7       seat0 default group11 cap:
-event16  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=8       seat0 default group11 cap:
-event17  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=9       seat0 default group11 cap:
-event18  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=10      seat0 default group11 cap:
-event21  DEVICE_ADDED     AT Raw Set 2 keyboard             seat0 default group12 cap:k
...

However, it still doesn't write the lib switch open event back to event0:
...
[close lid]
-event0   SWITCH_TOGGLE     +2.21s	switch lid state 1
-event10  POINTER_MOTION    +8.28s	  0.30/  0.00
[open lid and start typing password]
-event9   KEYBOARD_KEY     +10.15s	*** (-1) pressed
...

No open event on event0 ever appears.
Comment 12 Peter Hutterer 2017-08-18 02:55:50 UTC
strange. Unfortunately, I don't know how to debug this remotely, it seems everything is set up correctly. I recommend disabling anything that handles lid switches and using evemu-event to fake those events (easier for debugging).
evemu-event --sync --type EV_SW --code SW_LID --value 1 /dev/input/eventX
to set the switch, value 0 to unset.

Run libinput-debug-events in gdb and see if lid_switch_keyboard_event() is ever called and also if it's set up correctly in lid_switch_toggle_keyboard_listener.
Comment 13 Peter Hutterer 2017-09-06 23:41:01 UTC
ping?
Comment 14 Jason D. Clinton 2017-09-07 18:02:39 UTC
Will poke around more within a week. One hypothesis is that there are multiple devices with the name "Razer Razer Blade Stealth" in the debug output and the udev rule is catching one of the other (built-in) keyboard devices with the same name.
Comment 15 Peter Hutterer 2017-09-07 23:00:53 UTC
oh, right. that could be the case. Should be easy to test for, add the sysname to the debug message and run libinput-debug-events --verbose again. If that's the case, I should be able to fix this quite easily.
Comment 16 Jason D. Clinton 2017-09-11 14:53:03 UTC
Hrm, yep, there are two internal keyboards with exactly the same identifiers:

$ sudo evemu-describe /dev/input/event9 | head
# EVEMU 1.3
# Kernel: 4.12.8-300.fc26.x86_64
# DMI: dmi:bvnRazer:bvr6.05:bd01/26/2017:svnRazer:pnBladeStealth:pvr2.04:rvnRazer:rnRazer:rvr:cvnRazer:ct9:cvr:
# Input device name: "Razer Razer Blade Stealth"
# Input device ID: bus 0x03 vendor 0x1532 product 0x220 version 0x111
# Supported events:
#   Event type 0 (EV_SYN)
#     Event code 0 (SYN_REPORT)
#     Event code 1 (SYN_CONFIG)
#     Event code 2 (SYN_MT_REPORT)

$ sudo evemu-describe /dev/input/event10 | head
# EVEMU 1.3
# Kernel: 4.12.8-300.fc26.x86_64
# DMI: dmi:bvnRazer:bvr6.05:bd01/26/2017:svnRazer:pnBladeStealth:pvr2.04:rvnRazer:rnRazer:rvr:cvnRazer:ct9:cvr:
# Input device name: "Razer Razer Blade Stealth"
# Input device ID: bus 0x03 vendor 0x1532 product 0x220 version 0x111
# Supported events:
#   Event type 0 (EV_SYN)
#     Event code 0 (SYN_REPORT)
#     Event code 1 (SYN_CONFIG)
#     Event code 2 (SYN_MT_REPORT)


I'm going to poke around and see if I can generate events on the first one. If so, I can try to generate those events after a lib open and confirm that the write open is sent.
Comment 17 Jason D. Clinton 2017-09-11 15:16:52 UTC
Yep, that's it.

I tried hitting all the keys and then again while holding "Fn" key. Fn is a Razer hardware modifier that is not visible to the kernel. When holding it, it turns off all the keyboard LED's except for the function key row and the arrow keys. For the vast majority of these keys, Fn+key causes the secondary label key press to be sent. For example, there's Fn+F6 that sends Media Play/Pause that's sent on event10 (the main keyboard device like any other key press).

There's exactly one key on the internal keyboard that maps to the first internal keyboard device (event9 below): Fn+F4 (switch video output). By the udev rule (because there's no way to disambiguate them) this is the one that is associated with the lid switch in the debug output. If I press Fn+F4 after opening the lid, the lid switch open event is written:

$ sudo libinput-debug-events --verbose
event3  - Power Button: is tagged by udev as: Keyboard
event3  - Power Button: device is a keyboard
event5  - Video Bus: is tagged by udev as: Keyboard
event5  - Video Bus: device is a keyboard
event0  - Lid Switch: is tagged by udev as: Switch
event0  - Lid Switch: device is a switch device
event0  - Lid Switch: Lid Switch: will write switch open events
event2  - Power Button: is tagged by udev as: Keyboard
event2  - Power Button: device is a keyboard
event1  - Sleep Button: is tagged by udev as: Keyboard
event1  - Sleep Button: device is a keyboard
event21 - Logitech Webcam C930e: is tagged by udev as: Keyboard
event21 - Logitech Webcam C930e: device is a keyboard
event6  - Razer Razer DeathAdder Elite: is tagged by udev as: Mouse
event6  - Razer Razer DeathAdder Elite: device is a pointer
event7  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event7  - Razer Razer DeathAdder Elite: device is a keyboard
event8  - Razer Razer DeathAdder Elite: is tagged by udev as: Keyboard
event8  - Razer Razer DeathAdder Elite: device is a keyboard
event20 - USB Camera: is tagged by udev as: Keyboard
event20 - USB Camera: device is a keyboard
event9  - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event9  - Razer Razer Blade Stealth: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->Razer Razer Blade Stealth
event10 - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event10 - Razer Razer Blade Stealth: device is a keyboard
event11 - Razer Razer Blade Stealth: is tagged by udev as: Mouse
event11 - Razer Razer Blade Stealth: device is a pointer
event12 - Synaptics TM2438-005: is tagged by udev as: Touchpad
event12 - using pressure-based touch detection
event12 - thumb: enabled thumb detection (+pressure)
event12 - Synaptics TM2438-005: device is a touchpad
event12 - lid_switch: activated for Synaptics TM2438-005<->Lid Switch
event12 - palm: dwt activated with Synaptics TM2438-005<->Razer Razer Blade Stealth
event13 - HDA Intel PCH Mic: is tagged by udev as: Switch
event14 - HDA Intel PCH Headphone: is tagged by udev as: Switch
event15 - HDA Intel PCH HDMI/DP,pcm=3: is tagged by udev as: Switch
event16 - HDA Intel PCH HDMI/DP,pcm=7: is tagged by udev as: Switch
event17 - HDA Intel PCH HDMI/DP,pcm=8: is tagged by udev as: Switch
event18 - HDA Intel PCH HDMI/DP,pcm=9: is tagged by udev as: Switch
event19 - HDA Intel PCH HDMI/DP,pcm=10: is tagged by udev as: Switch
event4  - AT Raw Set 2 keyboard: is tagged by udev as: Keyboard
event4  - AT Raw Set 2 keyboard: device is a keyboard
-event3   DEVICE_ADDED     Power Button                      seat0 default group1  cap:k
-event5   DEVICE_ADDED     Video Bus                         seat0 default group2  cap:k
-event0   DEVICE_ADDED     Lid Switch                        seat0 default group3  cap:S
-event2   DEVICE_ADDED     Power Button                      seat0 default group4  cap:k
-event1   DEVICE_ADDED     Sleep Button                      seat0 default group5  cap:k
-event21  DEVICE_ADDED     Logitech Webcam C930e             seat0 default group6  cap:k
-event6   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:p left scroll-nat scroll-button
-event7   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:kp scroll-nat
-event8   DEVICE_ADDED     Razer Razer DeathAdder Elite      seat0 default group7  cap:k
-event20  DEVICE_ADDED     USB Camera                        seat0 default group8  cap:k
-event9   DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:k
-event10  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:kp scroll-nat
-event11  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:p left scroll-nat scroll-button
-event12  DEVICE_ADDED     Synaptics TM2438-005              seat0 default group10 cap:pg  size 102x60mm tap(dl off) left scroll-nat scroll-2fg-edge click-buttonareas-clickfinger dwt-on
-event13  DEVICE_ADDED     HDA Intel PCH Mic                 seat0 default group11 cap:
-event14  DEVICE_ADDED     HDA Intel PCH Headphone           seat0 default group11 cap:
-event15  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=3       seat0 default group11 cap:
-event16  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=7       seat0 default group11 cap:
-event17  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=8       seat0 default group11 cap:
-event18  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=9       seat0 default group11 cap:
-event19  DEVICE_ADDED     HDA Intel PCH HDMI/DP,pcm=10      seat0 default group11 cap:
-event4   DEVICE_ADDED     AT Raw Set 2 keyboard             seat0 default group12 cap:k
[...]
[close lid, suspend, resume, open lid, hit Fn+F4]
-event0   SWITCH_TOGGLE    +151.91s	switch lid state 0
-event9   KEYBOARD_KEY     +151.91s	KEY_RIGHTMETA (126) pressed
 event9   KEYBOARD_KEY     +151.91s	*** (-1) pressed
 event9   KEYBOARD_KEY     +152.01s	*** (-1) released
 event9   KEYBOARD_KEY     +152.12s	KEY_RIGHTMETA (126) released

Living with hitting Fn+F4 after opening the lid as a workaround goes is fairly undesirable because hitting that key causes the internal Intel GPU to go through the act of switching through all external video output ports to find an external monitor. This causes the internal display to be blank for ~3 seconds.

So, how do we have the second keyboard that has this label be associated with the lid switch, also? It seems that the video switch keyboard is always getting enumerated first.
Comment 18 Peter Hutterer 2017-09-12 00:15:28 UTC
Created attachment 134172 [details] [review]
0001-evdev-only-pair-a-keyboard-with-a-lid-switch-when-it.patch

Simple patch to check for a keyboard to be a real keyboard before pairing to avoid pairing with keyboards that only have special keys. Give this one a try please.

Note: I'm flying a bit blind here because the evemu-describe output was truncated so I don't know how the other keyboard devices look. I'll need the full output for both (as attachment please).
Comment 19 Jason D. Clinton 2017-09-13 01:11:23 UTC
Patch is against master. I took a pass at backporting to F26 1.8.2 but there's been so much churn in evdev.c that it doesn't seem viable. I'll have to upgrade to a master snapshot before I can make any progress on testing. In the meantime, I'm attaching the evemu-describe output. It doesn't look like using the letter event codes will work because they show up on both devices.
Comment 20 Jason D. Clinton 2017-09-13 01:16:26 UTC
Created attachment 134183 [details]
evemu-describe of video switch keyboard
Comment 21 Jason D. Clinton 2017-09-13 01:17:01 UTC
Created attachment 134184 [details]
evemu-describe of regular internal keyboard
Comment 22 Jason D. Clinton 2017-09-13 01:19:03 UTC
Here's an abbreviated diff -u of evemu-describe output comparing the video switch keyboard and the regular internal keyboard. There's a few good candidates here:

$ diff -u evemu-describe_video-switch-keyboard.txt evemu-describe_regular-internal-keyboard.txt 
--- evemu-describe_video-switch-keyboard.txt	2017-09-12 19:14:32.676017849 -0600
+++ evemu-describe_regular-internal-keyboard.txt	2017-09-12 19:15:02.265330448 -0600
@@ -155,10 +155,15 @@
 #     Event code 136 (KEY_FIND)
 #     Event code 137 (KEY_CUT)
 #     Event code 138 (KEY_HELP)
+#     Event code 139 (KEY_MENU)
 #     Event code 140 (KEY_CALC)
 #     Event code 142 (KEY_SLEEP)
+#     Event code 143 (KEY_WAKEUP)
+#     Event code 144 (KEY_FILE)
 #     Event code 150 (KEY_WWW)
 #     Event code 152 (KEY_COFFEE)
+#     Event code 155 (KEY_MAIL)
+#     Event code 156 (KEY_BOOKMARKS)
 #     Event code 158 (KEY_BACK)
 #     Event code 159 (KEY_FORWARD)
 #     Event code 161 (KEY_EJECTCD)
@@ -166,12 +171,19 @@
 #     Event code 164 (KEY_PLAYPAUSE)
 #     Event code 165 (KEY_PREVIOUSSONG)
 #     Event code 166 (KEY_STOPCD)
+#     Event code 167 (KEY_RECORD)
+#     Event code 168 (KEY_REWIND)
+#     Event code 169 (KEY_PHONE)
+#     Event code 171 (KEY_CONFIG)
+#     Event code 172 (KEY_HOMEPAGE)
 #     Event code 173 (KEY_REFRESH)
+#     Event code 174 (KEY_EXIT)
 #     Event code 176 (KEY_EDIT)
 #     Event code 177 (KEY_SCROLLUP)
 #     Event code 178 (KEY_SCROLLDOWN)
 #     Event code 179 (KEY_KPLEFTPAREN)
 #     Event code 180 (KEY_KPRIGHTPAREN)
+#     Event code 181 (KEY_NEW)
 #     Event code 183 (KEY_F13)
 #     Event code 184 (KEY_F14)
 #     Event code 185 (KEY_F15)
@@ -184,16 +196,268 @@
 #     Event code 192 (KEY_F22)
 #     Event code 193 (KEY_F23)
 #     Event code 194 (KEY_F24)
+#     Event code 206 (KEY_CLOSE)
+#     Event code 207 (KEY_PLAY)
+#     Event code 208 (KEY_FASTFORWARD)
+#     Event code 209 (KEY_BASSBOOST)
+#     Event code 210 (KEY_PRINT)
+#     Event code 212 (KEY_CAMERA)
+#     Event code 216 (KEY_CHAT)
+#     Event code 217 (KEY_SEARCH)
+#     Event code 219 (KEY_FINANCE)
+#     Event code 224 (KEY_BRIGHTNESSDOWN)
+#     Event code 225 (KEY_BRIGHTNESSUP)
+#     Event code 228 (KEY_KBDILLUMTOGGLE)
+#     Event code 234 (KEY_SAVE)
+#     Event code 235 (KEY_DOCUMENTS)
 #     Event code 240 (KEY_UNKNOWN)
+#     Event code 241 (KEY_VIDEO_NEXT)
+#     Event code 244 (KEY_BRIGHTNESS_AUTO)
+#     Event code 256 (BTN_0)
+#     Event code 353 (KEY_SELECT)
+#     Event code 354 (KEY_GOTO)
+#     Event code 358 (KEY_INFO)
+#     Event code 362 (KEY_PROGRAM)
+#     Event code 366 (KEY_PVR)
+#     Event code 370 (KEY_SUBTITLE)
+#     Event code 372 (KEY_ZOOM)
+#     Event code 374 (KEY_KEYBOARD)
+#     Event code 376 (KEY_PC)
+#     Event code 377 (KEY_TV)
+#     Event code 378 (KEY_TV2)
+#     Event code 379 (KEY_VCR)
+#     Event code 380 (KEY_VCR2)
+#     Event code 381 (KEY_SAT)
+#     Event code 383 (KEY_CD)
+#     Event code 384 (KEY_TAPE)
+#     Event code 386 (KEY_TUNER)
+#     Event code 387 (KEY_PLAYER)
+#     Event code 389 (KEY_DVD)
+#     Event code 392 (KEY_AUDIO)
+#     Event code 393 (KEY_VIDEO)
+#     Event code 396 (KEY_MEMO)
+#     Event code 397 (KEY_CALENDAR)
+#     Event code 398 (KEY_RED)
+#     Event code 399 (KEY_GREEN)
+#     Event code 400 (KEY_YELLOW)
+#     Event code 401 (KEY_BLUE)
+#     Event code 402 (KEY_CHANNELUP)
+#     Event code 403 (KEY_CHANNELDOWN)
+#     Event code 405 (KEY_LAST)
+#     Event code 407 (KEY_NEXT)
+#     Event code 408 (KEY_RESTART)
+#     Event code 409 (KEY_SLOW)
+#     Event code 410 (KEY_SHUFFLE)
+#     Event code 412 (KEY_PREVIOUS)
+#     Event code 416 (KEY_VIDEOPHONE)
+#     Event code 417 (KEY_GAMES)
+#     Event code 418 (KEY_ZOOMIN)
+#     Event code 419 (KEY_ZOOMOUT)
+#     Event code 420 (KEY_ZOOMRESET)
+#     Event code 421 (KEY_WORDPROCESSOR)
+#     Event code 422 (KEY_EDITOR)
+#     Event code 423 (KEY_SPREADSHEET)
+#     Event code 424 (KEY_GRAPHICSEDITOR)
+#     Event code 425 (KEY_PRESENTATION)
+#     Event code 426 (KEY_DATABASE)
+#     Event code 427 (KEY_NEWS)
+#     Event code 428 (KEY_VOICEMAIL)
+#     Event code 429 (KEY_ADDRESSBOOK)
+#     Event code 430 (KEY_MESSENGER)
+#     Event code 431 (KEY_DISPLAYTOGGLE)
+#     Event code 432 (KEY_SPELLCHECK)
+#     Event code 433 (KEY_LOGOFF)
+#     Event code 439 (KEY_MEDIA_REPEAT)
+#     Event code 442 (KEY_IMAGES)
+#     Event code 576 (KEY_BUTTONCONFIG)
+#     Event code 577 (KEY_TASKMANAGER)
+#     Event code 578 (KEY_JOURNAL)
+#     Event code 579 (KEY_CONTROLPANEL)
+#     Event code 580 (KEY_APPSELECT)
+#     Event code 581 (KEY_SCREENSAVER)
+#     Event code 582 (KEY_VOICECOMMAND)
+#     Event code 592 (KEY_BRIGHTNESS_MIN)
+#     Event code 593 (KEY_BRIGHTNESS_MAX)
+#   Event type 2 (EV_REL)
+#     Event code 6 (REL_HWHEEL)
+#   Event type 3 (EV_ABS)
+#     Event code 32 (ABS_VOLUME)
+#       Value        0
+#       Min          0
+#       Max        572
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 40 (ABS_MISC)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 41 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 42 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 43 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 44 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 45 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 46 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 47 (ABS_MT_SLOT)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 48 (ABS_MT_TOUCH_MAJOR)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 49 (ABS_MT_TOUCH_MINOR)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 50 (ABS_MT_WIDTH_MAJOR)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 51 (ABS_MT_WIDTH_MINOR)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 52 (ABS_MT_ORIENTATION)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 53 (ABS_MT_POSITION_X)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 54 (ABS_MT_POSITION_Y)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 55 (ABS_MT_TOOL_TYPE)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 56 (ABS_MT_BLOB_ID)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 57 (ABS_MT_TRACKING_ID)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 58 (ABS_MT_PRESSURE)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 59 (ABS_MT_DISTANCE)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 60 (ABS_MT_TOOL_X)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 61 (ABS_MT_TOOL_Y)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 62 ((null))
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
+#     Event code 63 (ABS_MAX)
+#       Value        0
+#       Min          0
+#       Max        255
+#       Fuzz         0
+#       Flat         0
+#       Resolution   0
 #   Event type 4 (EV_MSC)
 #     Event code 4 (MSC_SCAN)
-#   Event type 17 (EV_LED)
-#     Event code 0 (LED_NUML)
-#        State 0
-#     Event code 1 (LED_CAPSL)
-#        State 0
-#     Event code 2 (LED_SCROLLL)
-#        State 0
 #   Event type 20 (EV_REP)
 #     Event code 0 (REP_DELAY)
 #     Event code 1 (REP_MAX)
Comment 23 Peter Hutterer 2017-09-13 05:02:58 UTC
(In reply to Jason D. Clinton from comment #22)
> Here's an abbreviated diff -u of evemu-describe output comparing the video
> switch keyboard and the regular internal keyboard. There's a few good
> candidates here:

thanks, unfortunately none of them are reliable across devices. I think I might need some other solution here. Don't bother with the patch above, it won't work.
Comment 24 Peter Hutterer 2017-09-19 01:20:47 UTC
Please give https://github.com/whot/libinput/tree/wip/lid-switch-issue-v2 a try, thanks
Comment 25 Jason D. Clinton 2017-09-20 02:44:30 UTC
Yep, that fixes it. I followed https://wayland.freedesktop.org/libinput/doc/latest/building_libinput.html to overwrite system version in F26.

Here's an abbreviated log showing that it works:

$ sudo libinput debug-events --verbose
[...]
event0  - Lid Switch: is tagged by udev as: Switch
event0  - Lid Switch: device is a switch device
event0  - Lid Switch: will write switch open events
[...]
event9  - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event9  - Razer Razer Blade Stealth: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->Razer Razer Blade Stealth
event10 - Razer Razer Blade Stealth: is tagged by udev as: Keyboard
event10 - Razer Razer Blade Stealth: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->Razer Razer Blade Stealth
[...]
event4  - AT Raw Set 2 keyboard: is tagged by udev as: Keyboard
event4  - AT Raw Set 2 keyboard: device is a keyboard
event0  - lid: keyboard paired with Lid Switch<->AT Raw Set 2 keyboard
[...]
-event0   DEVICE_ADDED     Lid Switch                        seat0 default group3  cap:S
[...]
-event9   DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:k
-event10  DEVICE_ADDED     Razer Razer Blade Stealth         seat0 default group9  cap:kp scroll-nat
[...]
-event4   DEVICE_ADDED     AT Raw Set 2 keyboard             seat0 default group12 cap:k
[close lid]
event12 - lid: suspend touchpad
-event0   SWITCH_TOGGLE    +13.22s	switch lid state 1
-event11  POINTER_MOTION   +18.57s	  0.30/  0.00
event4  - AT Raw Set 2 keyboard: device removed
-event4   DEVICE_REMOVED   AT Raw Set 2 keyboard             seat0 default group12 cap:k
[open lid]
event12 - lid: resume touchpad
-event0   SWITCH_TOGGLE    +21.13s	switch lid state 0
-event10  KEYBOARD_KEY     +21.13s	*** (-1) pressed

Nice work!
Comment 26 Jason D. Clinton 2017-09-20 02:47:39 UTC
By the way, I noticed that the patch at tip-of-branch only has:

  ##########################################
 +# Razer
 +##########################################
 +libinput:keyboard:input:b0003v1532p0220*
 + LIBINPUT_ATTR_KEYBOARD_INTEGRATION=internal

and doesn't set 

 libinput:name:*Lid Switch*:dmi:*svnRazer:pnBlade*
  LIBINPUT_ATTR_LID_SWITCH_RELIABILITY=write_open

Is that intentional? It wasn't clear to me from the patch diff that this wasn't needed anymore. (I tested with this in my /etc/udev/hwdb.d since I'd already installed it previously.)
Comment 27 Peter Hutterer 2017-09-20 03:06:03 UTC
oh, whoops. I forgot about that. squashed in and force-pushed to the same branch, please test again to make sure I didn't add any typos. Before testing, please move the /etc/udev/hwdb.d/91-libinput-model-quirks.hwdb file out of the way, thanks.
Comment 28 Jason D. Clinton 2017-09-20 03:22:07 UTC
Retested w/o my own hwdb file: works!

Thanks so much for tracking this down.
Comment 29 Peter Hutterer 2017-09-20 04:56:46 UTC
thanks for testing, patch is on the list now

https://lists.freedesktop.org/archives/wayland-devel/2017-September/035084.html
Comment 30 Peter Hutterer 2017-09-25 04:29:56 UTC
commit 23a611ea1e5b8f0cf43130a45bbd304235a763dd
Author: Peter Hutterer <peter.hutterer@who-t.net>
Date:   Tue Sep 19 10:10:22 2017 +1000

    fallback: allow for multiple keyboards to toggle the lid switch


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.