Start a graphical application from kernel module?
My current problem is that: on my Acer Nitro laptop keyboard, there is a Nitro key – which is a special key that no other laptop brand has. You see in the image below that there is a weird N key next to the backspace key – that’s the Nitro key.
I want to develop an GUI application such that whenever the Nitro button is pressed, my application shows up. I am using Ubuntu 22.04.
From what I have found, I think I would have to write a LKM to capture the Nitro key press event and start the application from there. I am following the answer here (https://stackoverflow.com/questions/33836541/linux-kernel-how-to-capture-a-key-press-and-replace-it-with-another-key) to capture the key press event.
I wonder if it is okay to start a GTK application within the LKM – in other words, putting the sample code here (https://www.gtk.org/docs/getting-started/hello-world) in my kernel module code. Also, I wonder if I am on the right track to achieve my goal.
Thanks in advance!
[UPD] Clarification on what I want: Acer Nitro laptops usually have an app called Nitrosense that provides a GUI to change the keyboard color and fan speed. The Nitro key when pressed shows users the GUI. However, Nitrosense app is only for Windows – I want to write the same thing for Ubuntu. So far I found various repos on changing the keyboard color and fan speed, but there is no repo for an app that opens when I press the Nitro key, so I want to build such thing. I’m using GNOME 42 on Ubuntu 22.04. What I want is whenever the user (already logged in) presses Nitro key, a GUI automatically shows for me to do stuffs.
[UPD2] I also found this repo https://github.com/kphanipavan/PredatorNonSense which seems trying to do the same thing as my concern on another Acer brand. I think it might be helpful, will see.
However, when I tried
xev, there was nothing printed, which makes me think that the X Server doesn’t recognize the Nitro key press event. I guess I should look for documentation to write a module to send the Nitro key event to X Server?
I tried to go to Keyboard settings in GNOME Control Center to try to add a keyboard shortcut, but it also didn’t recognize the Nitro key – maybe because GNOME is running on X Server and X Server doesn’t recognize the Nitro key event?
I also looked at the repo I mentioned in UPD2, so what the author did is also polling via continuously
grep the log from
evtest to see if the key is pressed – check out this shell command https://github.com/kphanipavan/PredatorNonSense/blob/master/PNS.sh.
What I think I can also do is to write a simple systemd service to poll the keyboard device file and check if the Nitro is pressed. However, one mentions that this might waste quite a lot of resources. Any suggestion about this?
I want to develop an GUI application such that whenever the Nitro button is pressed, my application shows up.
So you need to expose the Nitro button for example as a kernel input layer event. Then some userspace application can act on it and start your UI application.
Remember that Linux is multiuser, the system is designed for a situation where you have a single machine and dozens of people using a GUI with different accounts. So you can’t just "start an application", you need to find the right user. But the kernel is user agnostic.
That means something for this particular user has to act on it. The simplest place to put this is the Window Manager configuration for that particular user (which would be you, I assume).
I wonder if it is okay to start a GTK application within the LKM
It’s not ok, and it’s not going to work.
evtest to see if the Nitro already produces a keypress event. If it does, you don’t have to write a kernel module. Then use
xev to see if it already gets translated into an X event.
If it doesn’t produce a keypress event, the search for the actual event will get interesting…
Finally, find out what window manager your desktop uses, then google documentation for your window manager, and find out how to launch programs for a certain keypress.
(BTW, it’s usually better to just paste text indented by four blanks instead of an image)
So that’s an ordinary key press and release event for code 425
If you want to make your own keyboard layout that maps this code to an X keysym, I recommend reading An Unreliable Guide to XKB Configuration (2004). Copy an existing layout with a new name and then modify it, that way you won’t lose it when the system packages get updated.
If you want a small user-space program to listen for that key, pick a language with a library for the kernel input layer, e.g. Python. Make sure to use the symlink in
/dev/input/by-path/... that corresponds to your keyboard, the
/dev/input/eventX numbers are not stable across boots.
Considering that your system is likely to be already configured for thermal management (via the ACPI thermald daemon or else) I would definitely not try playing with fan speeds in their back.
Regarding the capability to change the color of the keyboard, you are correct writing that this can only be done through some appropriate device driver.
Your keyboard (some HID compatible device almost certainly) should necessarily expose to the kernel some output device (different from the input device associated with the transmit of keyboard keys) that needs to be handled properly if wanting to send whatever order to your keyboard)
You can take inspiration from the Acer Gaming RGB keyboard backlight and Turbo mode Linux kernel module
This should basically aim at exposing controls in the /sys/devices tree.
The way you could act on these controls would be up to you in userland, from boldly echoing to the /sys/devices controls to the conception of whatever gtk widget.
Having this activated using whatever keyboard key (including your special Nitro key) being a simple keyboard mapping problem your DE might be able to handle through basic system settings.