linux輸入子系統介紹

來源:互聯網
上載者:User

linux系統提供了input子系統,按鍵、觸控螢幕、鍵盤、滑鼠等輸入裝置都可以利用input介面函數來實現裝置驅動。

在linux核心中,input裝置用input_dev結構體描述,試用input子系統實現輸入裝置驅動的時候,驅動的核心工作是向系統報告按鍵、觸控螢幕、鍵盤、滑鼠等輸入事件(event,通過input_event結構體描述),不再需要關心檔案操作介面,因為input子系統已經完成了檔案操作介面。驅動報告的事件經過InputCore和Eventhandler最終到達使用者空間。

通過input子系統,具體的輸入裝置驅動只需要完成如下工作。

(1)在模組載入函數中告知input子系統它可以報告的事件。

裝置驅動通過set_bit()告訴input子系統它支援哪些事件,如下所示:

set_bit(EV_KEY, button_dev.evbit);

(2)在模組載入函數中註冊輸入裝置,註冊函數為:

int input_register_device(struct input_dev *dev);

(3)在按鍵按下/抬起、觸控螢幕被觸摸/抬起/移動、滑鼠被移動/單擊/抬起時通過input_report_xxx()報告發生的事件及對應的索引值/座標等狀態。

主要的事件類型包括EV_KEY(按鍵事件)、EV_REL(相對值,如游標移動,報告的是相對最後一次位置的位移)和EV_ABS(絕對值,如觸控螢幕和操縱杆,它們工作在絕對座標系統)。

用於報告EV_KEY、EV_REL和EV_ABS事件的函數分別為:

void input_report_key(struct input_dev *dev, unsigned int code, int value);void input_report_rel(struct input_dev *dev, unsigned int code, int value);void input_report_abs(struct input_dev *dev, unsigned int code, int value);

input_sync()用於事件同步,它告知事件的接收者驅動已經發出了一個完整的報告。

例如,在觸控螢幕裝置驅動中,一次座標及按下狀態的整個報告過程如下:

input_report_abs(input_dev, ABS_X, x);   //X座標input_report_abs(input_dev, ABS_Y, y);   //Y座標input_report_abs(input_dev, ABS_PRESSURE, pres);   //壓力input_sync(input_dev);  //同步

(4)在模組卸載函數中登出輸入裝置,函數為:

void input_unregister_device(struct input_dev *dev);

如下是一個最簡單的使用input介面實現按鍵裝置驅動的例子,它在中斷服務程式中向系統報告按鍵及同步事件。

/*在按鍵中斷中報告事件*/static void button_interrupt(int irq, void *dummy, struct pt_regs *fp){    input_report_key(&button_dev, BTN_!, inb(BUTTON_PORT), 1);    input_sync(&button_dev);}static int __init button_init(void){    /*申請中斷*/    if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)){    printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);    return -EBUSY;}    button_dev.evbit[0] = BIT(EV_KEY);    //支援EV_KEY事件    button_dev.keybit[LONG(BIT_0)] = BIT(BTN_0);    input_register_device(&button_dev);  //註冊input裝置}static void __exit button_exit(void){    input_unregister_device(&button_dev);    free_irq(BUTTON_IRQ, button_interrupt);}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.