Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!
Did you like my answer?
To mimic a keypress to Weston from within a container, you are correct that /dev/uinput
is typically used for simulating input events. However, since /dev/uinput
is not writable from inside the container, you have a couple of options to work around this limitation:
Option 1: Modify Container Permissions
You can modify the container’s permissions to allow access to /dev/uinput
. This involves running the container with additional privileges and explicitly mapping the device.
-
Run the container with
--privileged
flag:
This flag gives the container extended privileges, which includes access to/dev/uinput
.docker run --privileged -v /dev/uinput:/dev/uinput <your-container-image>
-
Ensure
/dev/uinput
is available inside the container:
You can verify this by checking the device inside the container.docker exec -it <container-id> ls -l /dev/uinput
Option 2: Use Host System to Inject Events
If modifying the container permissions is not feasible, you can run a program on the host system that injects the keypress events into the container.
-
Write a program on the host system to simulate keypress events:
You can use a C/C++ program that writes to/dev/uinput
on the host system. -
Communicate with the container:
Use a mechanism like a socket or a shared file to trigger the keypress event from the container. The container can send a signal to the host program to simulate the keypress.
Example C Program to Simulate Keypress
Here is a simple example of a C program that simulates a keypress using /dev/uinput
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/uinput.h>
int main() {
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd < 0) {
perror("open");
return 1;
}
struct uinput_setup usetup;
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, KEY_B);
memset(&usetup, 0, sizeof(usetup));
snprintf(usetup.name, UINPUT_MAX_NAME_SIZE, "uinput-keyboard");
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234;
usetup.id.product = 0x5678;
usetup.id.version = 1;
ioctl(fd, UI_DEV_SETUP, &usetup);
ioctl(fd, UI_DEV_CREATE);
sleep(1);
struct input_event ev;
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_B;
ev.value = 1;
write(fd, &ev, sizeof(ev));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
write(fd, &ev, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_B;
ev.value = 0;
write(fd, &ev, sizeof(ev));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
write(fd, &ev, sizeof(ev));
sleep(1);
ioctl(fd, UI_DEV_DESTROY);
close(fd);
return 0;
}
Running the Program
-
Compile the program:
gcc -o keypress keypress.c
-
Run the program on the host system:
sudo ./keypress
This program will simulate pressing and releasing the ‘b’ key. You can adapt this approach to communicate with your container and trigger the keypress event as needed.
If you need further assistance or specific details about your setup, please provide more context or details about your environment.
Please, also check if these links can help you solve your problem: