我有一个运行Linux/X11的嵌入式设备,它连接到一个通过USB连接提供触摸事件的设备。此设备未被识别为任何形式的标准指针/鼠标输入。我想要做的是找到一种方法,当外部设备报告事件时,将鼠标事件“注入”到X11中。
这样做将消除我的应用程序(用C编写,使用Gtk+ )通过Gtk+调用来伪造鼠标按压的需要。
如果可以做到这一点,我的Gtk+应用程序就不需要知道或关心产生触摸事件的设备。它将作为标准鼠标事件出现在应用程序中。
有人知道如何在X11中插入合成鼠标事件吗?
现在,我正在做以下工作,这是可行的,但不是最优的。
GtkWidget *btnSpin; /* sample button */
gboolean buttonPress_cb( void *btn );
gboolean buttonDePress_cb( void *btn );
/* make this call after the device library calls the TouchEvent_cb() callback
and the application has determined which, if any, button was touched
In this example we are assuming btnSpin was touched.
This function will, in 5ms, begin the process of causing the button to do it's
normal animation ( button in, button out effects ) and then send the actual
button_clicked event to the button.
*/
g_timeout_add(5, (GSourceFunc) buttonPress_cb, (void *)btnSpin);
/* this callback is fired 5ms after the g_timeout_add() function above.
It first sets the button state to ACTIVE to begin the animation cycle (pressed look)
And then 250ms later calls buttonDePress_cb which will make the button look un-pressed
and then send the button_clicked event.
*/
gboolean buttonPress_cb( void *btn )
{
gtk_widget_set_state((GtkWidget *)btn, GTK_STATE_ACTIVE);
g_timeout_add(250, (GSourceFunc) buttonDePress_cb, btn);
return( FALSE );
}
/* Sets button state back to NORMAL ( not pressed look )
and sends the button_clicked event so that the registered signal handler for the
button can be activated
*/
gboolean buttonDePress_cb( void *btn )
{
gtk_widget_set_state( btn, GTK_STATE_NORMAL);
gtk_button_clicked( GTK_BUTTON( btn ));
return( FALSE );
}
发布于 2012-04-26 03:01:53
Linux输入系统有一个在用户空间实现输入设备的工具,称为uinput。您可以编写一个后台程序,该程序使用设备的回调库将输入事件发送到内核。然后,X服务器(假设它使用的是evdev输入模块)会像处理任何其他鼠标事件一样处理这些事件。
有一个名为libsuinput的库可以很容易地做到这一点。它甚至包含一个example mouse input program,您可以将其用作模型。但是,由于您的设备是基于触摸的设备,因此它可能会使用绝对轴(ABS_X,ABS_Y)而不是相对轴(REL_X,REL_Y)。
发布于 2012-04-26 00:25:00
有几种方法。
XSendEvent
。注意:一些应用程序框架会忽略与XSendEvent
一起发送的事件。我想Gtk+没有,但我没有检查过。XTestFakeMotionEvent
和XTestFakeButtonEvent
。您需要在X服务器上使用XTest扩展。发布于 2012-04-26 00:27:26
最酷的事情是在内核中实现一个设备驱动程序,它创建一个使用evdev协议的/dev/input/eventX
文件。如果你想这样做,我建议你读一本叫Linux Device Drivers
的书。这本书可以在网上免费获得。
如果您想在用户空间中做到这一点,我建议您使用Xlib (或XCB)。在普通的Xlib (C语言)上,可以使用X Test Extension
或XSendEvent()
。
xautomation包中还有一个名为xte的二进制文件(在Debian、sudo apt-get install xautomation
和man xte
上)。xte
非常容易使用,您还可以查看其源代码来了解如何使用X测试扩展。
指针:
https://stackoverflow.com/questions/10319519
复制相似问题