Skip to contentSkip to conversation

Input: Add a Kconfig to emulate KEY_SCREENLOCK with META + L

PATCH Superseded Mario Limonciello 2 participants 4 messages Apr 17, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Input: Add a Kconfig to emulate KEY_SCREENLOCK with META + L

From: Mario Limonciello <[email protected]>

In the PC industry KEY_SCREENLOCK isn't used as frequently as it used to be. Modern versions of Windows [1], GNOME and KDE support "META" + "L" to lock the screen. Modern hardware [2] also sends this sequence of events for keys with a silkscreen for screen lock.

Introduced a new Kconfig option that will change KEY_SCREENLOCK when emitted by driver to META + L.

---
v3:
* Emulation in the input core instead

 drivers/input/Kconfig |  8 ++++++++
 drivers/input/input.c | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)
drivers/input/Kconfig +8 -0
index 88ecdf5218ee9..ffb4163fe315f 100644
@@ -174,6 +174,14 @@ config INPUT_APMPOWER
 	  To compile this driver as a module, choose M here: the
 	  module will be called apm-power.
 
+config INPUT_SCREENLOCK_EMULATION
+	bool "Pass KEY_SCREENLOCK as META + L"
+	help
+	  Say Y here if you want KEY_SCREENLOCK to be passed to userspace as
+	  META + L.
+
+	  If unsure, say Y.
+
 comment "Input Device Drivers"
 
 source "drivers/input/keyboard/Kconfig"
drivers/input/input.c +19 -0
index dfeace85c4710..08a857cea0c5d 100644
@@ -370,6 +370,13 @@ void input_handle_event(struct input_dev *dev,
 	}
 }
 
+static void handle_screenlock_as_meta_l(struct input_dev *dev, unsigned int type,
+					int value)
+{
+	input_handle_event(dev, type, KEY_LEFTMETA, value);
+	input_handle_event(dev, type, KEY_L, value);
+}
+
 /**
  * input_event() - report new input event
  * @dev: device that generated the event
@@ -392,6 +399,12 @@ void input_event(struct input_dev *dev,
 {
 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
 		guard(spinlock_irqsave)(&dev->event_lock);
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			handle_screenlock_as_meta_l(dev, type, value);
+			return;
+		}
+#endif
 		input_handle_event(dev, type, code, value);
 	}
 }
@@ -2134,6 +2147,12 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
 
 	switch (type) {
 	case EV_KEY:
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			__set_bit(KEY_L, dev->keybit);
+			__set_bit(KEY_LEFTMETA, dev->keybit);
+		}
+#endif
 		__set_bit(code, dev->keybit);
 		break;
 
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 2/2 platform/x86/amd: pmf: Use META + L for screen lock command

From: Mario Limonciello <[email protected]>

In practice userspace software doesn't react to KEY_SCREENLOCK by default. So any time that the PMF policies would suggest to lock the screen (for example from an HPD sensor event) userspace isn't configured to do it.

However userspace is configured for META + L as this is the default in the PC ecosystem. Adjust the PMF driver to select the Kconfig option that makes the input core send META + L.

---
v3:
* Use Kconfig from linux-input

---
 drivers/platform/x86/amd/pmf/Kconfig | 1 +
 1 file changed, 1 insertion(+)
drivers/platform/x86/amd/pmf/Kconfig +1 -0
index 25b8f7ae3abde..10089c69ce582 100644
@@ -12,6 +12,7 @@ config AMD_PMF
 	depends on TEE && AMDTEE
 	depends on AMD_SFH_HID
 	depends on HAS_IOMEM
+	select INPUT_SCREENLOCK_EMULATION
 	help
 	  This driver provides support for the AMD Platform Management Framework.
 	  The goal is to enhance end user experience by making AMD PCs smarter,
Signature · 1 line
2.43.0
#2 lore raw
In reply to Mario Limonciello

Hi Mario,

Quoted 73 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

In the PC industry KEY_SCREENLOCK isn't used as frequently as it used to be. Modern versions of Windows [1], GNOME and KDE support "META" + "L" to lock the screen. Modern hardware [2] also sends this sequence of events for keys with a silkscreen for screen lock.

Introduced a new Kconfig option that will change KEY_SCREENLOCK when emitted by driver to META + L.

---
v3:
* Emulation in the input core instead

 drivers/input/Kconfig |  8 ++++++++
 drivers/input/input.c | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)
drivers/input/Kconfig +8 -0
index 88ecdf5218ee9..ffb4163fe315f 100644
@@ -174,6 +174,14 @@ config INPUT_APMPOWER
 	  To compile this driver as a module, choose M here: the
 	  module will be called apm-power.
 
+config INPUT_SCREENLOCK_EMULATION
+	bool "Pass KEY_SCREENLOCK as META + L"
+	help
+	  Say Y here if you want KEY_SCREENLOCK to be passed to userspace as
+	  META + L.
+
+	  If unsure, say Y.
+
 comment "Input Device Drivers"
 
 source "drivers/input/keyboard/Kconfig"
drivers/input/input.c +13 -0
index dfeace85c4710..08a857cea0c5d 100644
@@ -370,6 +370,13 @@ void input_handle_event(struct input_dev *dev,
 	}
 }
 
+static void handle_screenlock_as_meta_l(struct input_dev *dev, unsigned int type,
+					int value)
+{
+	input_handle_event(dev, type, KEY_LEFTMETA, value);
+	input_handle_event(dev, type, KEY_L, value);
+}
+
 /**
  * input_event() - report new input event
  * @dev: device that generated the event
@@ -392,6 +399,12 @@ void input_event(struct input_dev *dev,
 {
 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
 		guard(spinlock_irqsave)(&dev->event_lock);
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			handle_screenlock_as_meta_l(dev, type, value);
+			return;
+		}
+#endif
 		input_handle_event(dev, type, code, value);
 	}
 }

drivers/input/input.c void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int · line 2154

Quoted 8 lines
drivers/input/input.c +5 -0
@@ -2134,6 +2147,12 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
 
 	switch (type) {
 	case EV_KEY:
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			__set_bit(KEY_L, dev->keybit);
+			__set_bit(KEY_LEFTMETA, dev->keybit);
+		}

Shouldn't there be a "break;" inside the if?

Since KEY_SCREENLOCK events will never get reported when CONFIG_INPUT_SCREENLOCK_EMULATION is set, I think it would be netter to not set it in dev->keybit ?

Regards,

Hans

Quoted 4 lines
drivers/input/input.c +1 -0
+#endif
 		__set_bit(code, dev->keybit);
 		break;
 
#3 lore raw
Quoted 87 lines · Hans de Goede

Hi Mario,

Quoted 73 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

In the PC industry KEY_SCREENLOCK isn't used as frequently as it used to be. Modern versions of Windows [1], GNOME and KDE support "META" + "L" to lock the screen. Modern hardware [2] also sends this sequence of events for keys with a silkscreen for screen lock.

Introduced a new Kconfig option that will change KEY_SCREENLOCK when emitted by driver to META + L.

---
v3:
* Emulation in the input core instead

  drivers/input/Kconfig |  8 ++++++++
  drivers/input/input.c | 19 +++++++++++++++++++
  2 files changed, 27 insertions(+)
drivers/input/Kconfig +8 -0
index 88ecdf5218ee9..ffb4163fe315f 100644
@@ -174,6 +174,14 @@ config INPUT_APMPOWER
  	  To compile this driver as a module, choose M here: the
  	  module will be called apm-power.
 
+config INPUT_SCREENLOCK_EMULATION
+	bool "Pass KEY_SCREENLOCK as META + L"
+	help
+	  Say Y here if you want KEY_SCREENLOCK to be passed to userspace as
+	  META + L.
+
+	  If unsure, say Y.
+
  comment "Input Device Drivers"
 
  source "drivers/input/keyboard/Kconfig"
drivers/input/input.c +13 -0
index dfeace85c4710..08a857cea0c5d 100644
@@ -370,6 +370,13 @@ void input_handle_event(struct input_dev *dev,
  	}
  }
 
+static void handle_screenlock_as_meta_l(struct input_dev *dev, unsigned int type,
+					int value)
+{
+	input_handle_event(dev, type, KEY_LEFTMETA, value);
+	input_handle_event(dev, type, KEY_L, value);
+}
+
  /**
   * input_event() - report new input event
   * @dev: device that generated the event
@@ -392,6 +399,12 @@ void input_event(struct input_dev *dev,
  {
  	if (is_event_supported(type, dev->evbit, EV_MAX)) {
  		guard(spinlock_irqsave)(&dev->event_lock);
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			handle_screenlock_as_meta_l(dev, type, value);
+			return;
+		}
+#endif
  		input_handle_event(dev, type, code, value);
  	}
  }

drivers/input/input.c void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int · line 2154

Quoted 8 lines
drivers/input/input.c +5 -0
@@ -2134,6 +2147,12 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
 
  	switch (type) {
  	case EV_KEY:
+#ifdef CONFIG_INPUT_SCREENLOCK_EMULATION
+		if (code == KEY_SCREENLOCK) {
+			__set_bit(KEY_L, dev->keybit);
+			__set_bit(KEY_LEFTMETA, dev->keybit);
+		}

Shouldn't there be a "break;" inside the if?

Quoted 5 lines

Since KEY_SCREENLOCK events will never get reported when CONFIG_INPUT_SCREENLOCK_EMULATION is set, I think it would be netter to not set it in dev->keybit ?

Regards,

This is something I wasn't sure about.

I was worried there might be other parts of the kernel that check for a capability of a specific keycode and conditionalize behavior.

If that isn't a concern yes I could add a break into this block in the next version.

Quoted 11 lines

Hans

Quoted 4 lines
drivers/input/input.c +1 -0
+#endif
  		__set_bit(code, dev->keybit);
  		break;