新聞中心
隨著科技的發(fā)展,觸屏技術(shù)已成為當(dāng)今時代的基礎(chǔ)部分。 linux系統(tǒng)內(nèi)置了觸摸屏支持,支持多種使用場景。作為Linux系統(tǒng)開發(fā)者或者用戶,要了解觸摸屏代碼是非常有必要的,下面就來介紹一些linux觸摸屏代碼的基礎(chǔ)知識,供大家參考。

首先,Linux系統(tǒng)中觸摸屏的原理是通過將電容接收的觸摸,然后在系統(tǒng)內(nèi)部進行脈沖處理,最后將觸摸數(shù)據(jù)反饋到操作系統(tǒng)當(dāng)中。 開發(fā)者在編寫觸摸屏相關(guān)的代碼時,常用的開源驅(qū)動有X Window System的xdtk庫和Input subsystem的Android系統(tǒng)的libinput庫。
Xdtk庫是X Window system的觸摸屏標(biāo)準(zhǔn)庫,操作系統(tǒng)中常見的幾種觸摸屏基本都支持,不同屏幕觸摸器可以使用X窗口系統(tǒng)內(nèi)部函數(shù)來識別和調(diào)節(jié)。下面是使用Xdtk庫實現(xiàn)觸摸屏的示例代碼:
#include
#include
#include
XdeviceInfo* xdeviceinfo = XIQueryDevice(disp, deviceid, &ndevices);
XDevice* device = XIQuerytouchBegin(disp, deviceid, ndevices, xdeviceinfo);
XEVENTClass classes[3];
int nclasses = 0;
XIQueryTouch(device, &classes[nclasses], 3);
XSelectInput(disp, rootWindow, RootTouchActivateMask | classes[nclasses]);
long event_mask = FakeInputEventsMask;
XISetMask(device, XI_TouchBegin, &event_mask, 1);
while (1) {
XEvent ev;
XNextEvent(disp, &ev);
if(ev.type == XI_TOUCH_BEGIN) {
/* Handle Touch begin events */
}
}
XIQueryTouchEnd(disp, device);
此外,Android系統(tǒng)也使用libinput庫去識別觸摸屏,這些庫大量使用于Android觸摸屏應(yīng)用。使用libinput庫,可以對不同類型的觸摸器實施更靈活的控制,如多點觸摸,手勢識別等等功能。大多數(shù)的Android設(shè)備都會安裝有l(wèi)ibinput庫,下面是使用libinput庫實現(xiàn)觸摸屏的示例代碼:
#include
int main(int argc, char **argv)
{
struct libinput *li = libinput_create();
struct libinput_device *dev = libinput_find_touch_device(li);
int x, y;
// Setup touch events
libinput_device_enable_events(dev, LIBINPUT_EVENT_TOUCH_MOTION);
libinput_device_enable_events(dev, LIBINPUT_EVENT_TOUCH_DOWN);
libinput_device_enable_events(dev, LIBINPUT_EVENT_TOUCH_UP);
while (1) {
struct libinput_event *event;
struct libinput_event_touch *touch;
event = libinput_get_event(li);
switch (libinput_event_get_type(event)) {
case LIBINPUT_EVENT_TOUCH_MOTION:
touch = libinput_event_get_touch_event(event);
x = libinput_event_touch_get_x(touch);
y = libinput_event_touch_get_y(touch);
break;
case LIBINPUT_EVENT_TOUCH_DOWN:
// Your code for handling touches down
break;
case LIBINPUT_EVENT_TOUCH_UP:
// Your code for handling touches up
break;
default:
break;
}
libinput_event_destroy(event);
}
libinput_destroy(li);
return 0;
}
因此,Linux系統(tǒng)下支持觸摸屏的代碼有X Window system的xdtk庫和Input subsystem的Android系統(tǒng)的libinput庫。了解并熟練掌握這些庫,可以幫助我們快速實現(xiàn)Linux觸摸屏應(yīng)用。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機、虛擬主機、域名注冊、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
本文標(biāo)題:「深入了解Linux觸摸屏代碼」(linux觸摸屏代碼)
鏈接URL:http://www.dlmjj.cn/article/djdcdij.html


咨詢
建站咨詢
