1.在dts中(kernel/arch/arm/boot/dts/rk3288-androID.dtsi)添加键值属性,可以仿照power按键配置
@H_419_18@wake-key { gpios = <&gpio0 14 GPIO_ACTIVE_LOW>; linux,code = <117>; pinctrl-names = "default"; pinctrl-0 = <&pmic_int>; label = "wake"; gpio-key,wakeup;};
因我们使用的键值 *** 作是通过gpio触发的,则配置成gpio-key。
linux,code 键码可以查看内核自定义键码来选择。定义在kernel/include/uAPI/linux/input-event-codes.h下。
/* * Event types */ #define EV_SYN 0x00#define EV_KEY 0x01#define EV_REL 0x02#define EV_ABS 0x03#define EV_MSC 0x04#define EV_SW 0x05#define EV_LED 0x11#define EV_SND 0x12#define EV_REP 0x14#define EV_FF 0x15#define EV_PWR 0x16#define EV_FF_STATUS 0x17#define EV_MAX 0x1f#define EV_CNT (EV_MAX+1)/* * Keys and buttons * * Most of the keys/buttons are modeled after USB HUT 1.12 * (see http://www.usb.org/developers/hIDpage). * Abbreviations in the comments: * AC - Application Control * AL - Application Launch button * SC - System Control */#define KEY_RIGHTALT 100#define KEY_liNEFeed 101#define KEY_HOME 102#define KEY_UP 103#define KEY_PAGEUP 104#define KEY_left 105#define KEY_RIGHT 106#define KEY_END 107#define KEY_DOWN 108#define KEY_PAGEDOWN 109#define KEY_INSERT 110#define KEY_DELETE 111#define KEY_MACRO 112#define KEY_MUTE 113#define KEY_VolUMEDOWN 114#define KEY_VolUMEUP 115#define KEY_POWER 116 /* SC System Power Down */#define KEY_KPEQUAL 117#define KEY_KPPLUSMINUS 118#define KEY_PAUSE 119#define KEY_SCALE 120 /* AL Compiz Scale (Expose) */#define KEY_KPCOMMA 121#define KEY_HANGEul 122#define KEY_HANGUEL KEY_HANGEul#define KEY_HANJA 123#define KEY_YEN 124#define KEY_leftMeta 125#define KEY_RIGHTMeta 126#define KEY_COMPOSE 127#define KEY_Stop 128 /* AC Stop */#define KEY_AGAIN 129#define KEY_PROPS 130 /* AC PropertIEs */#define KEY_UNDO 131 /* AC Undo */#define KEY_FRONT 132#define KEY_copY 133 /* AC copy */#define KEY_OPEN 134 /* AC Open */#define KEY_PASTE 135 /* AC Paste */#define KEY_FIND 136 /* AC Search */#define KEY_CUT 137 /* AC Cut */#define KEY_HELP 138 /* AL Integrated Help Center */#define KEY_MENU 139 /* Menu (show menu) */#define KEY_CALC 140 /* AL Calculator */#define KEY_SETUP 141#define KEY_SLEEP 142 /* SC System Sleep */#define KEY_WAKEUP 143 /* System Wake Up */#define KEY_file 144 /* AL Local Machine browser */#define KEY_SENDfile 145#define KEY_DELETEfile 146#define KEY_XFER 147#define KEY_PROG1 148#define KEY_PROG2 149#define KEY_WWW 150 /* AL Internet browser */#define KEY_MSDOS 151#define KEY_COFFEE 152 /* AL Terminal Lock/Screensaver */#define KEY_SCREENLOCK KEY_COFFEE#define KEY_ROTATE_disPLAY 153 /* display orIEntation for e.g. tablets */#define KEY_DIRECTION KEY_ROTATE_disPLAY#define KEY_CYCLEwindows 154#define KEY_MAIL 155#define KEY_BOOKMARKS 156 /* AC Bookmarks */#define KEY_COmpuTER 157#define KEY_BACK 158 /* AC Back */#define KEY_FORWARD 159 /* AC Forward */#define KEY_CLOSECD 160#define KEY_EJECTCD 161#define KEY_EJECTCLOSECD 162#define KEY_NEXTSONG 163#define KEY_PLAYPAUSE 164#define KEY_PREVIoUSSONG 165#define KEY_StopCD 166#define KEY_RECORD 167#define KEY_REWIND 168#define KEY_PHONE 169 /* Media Select Telephone */#define KEY_ISO 170#define KEY_CONfig 171 /* AL Consumer Control Configuration */#define KEY_HOMEPAGE 172 /* AC Home */#define KEY_REFRESH 173 /* AC Refresh */#define KEY_EXIT 174 /* AC Exit */
因键值较多,就只列一部分出来,在定义键码的时候,注意不要与现正在使用的键码重复。如不确认哪些键码是已经使用的,可以在该文件中添加自定义键码。
在dts中添加键值属性后,需要在驱动(我的是在/kernel/drivers/input/keyboard/rk_reys.c)里,做dts解析:
/* parse info from dt */ ddata->nbuttons = key_num; error = rk_keys_parse_dt(ddata, pdev); if (error) goto fail0;
如该按键需要以中断的方式触发,则可以为这个按键申请中断:
error =devm_request_irq(dev, irq, keys_isr1, IRQF_TRIGGER_FALliNG|IRQF_TRIGGER_RISING, button->desc ? button->desc:"wake",button);
键值上报:1.注册input事件:
通过input_register_device()
函数来向内核注册input事件。
2.input事件上报:
input事件上报是通过input_event()
或input_report_key()
函数上报。
input_event()函数简介:
函数原型:
voID input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value){ unsigned long flags; if (is_event_supported(type, dev->evbit, EV_MAX)) { spin_lock_irqsave(&dev->event_lock, flags); add_input_randomness(type, code, value); input_handle_event(dev, type, code, value); spin_unlock_irqrestore(&dev->event_lock, flags); } } EXPORT_SYMBol(input_event);
参数介绍:
struct input_dev *dev:需要上报的事件
unsigned int type:上报的事件类型(在前面的input-event-codes.h文件中有定义)
unsigned int code:上报的键码
int value :上报的键值
使用实例:
static irqreturn_t keys_isr1(int irq, voID *dev_ID){ struct rk_keys_button *button = (struct rk_keys_button *)dev_ID; struct rk_keys_drvdata *pdata = dev_get_drvdata(button->dev); struct input_dev *input = pdata->input; int irq_status = gpio_get_value(14); if (0==irq_status){ //插入就触发中断,,拔出来也触发中断。 input_event(input, EV_KEY, 118, 1); input_sync(input); udelay(50); input_event(input, EV_KEY, 118, 0); input_sync(input); } else if (1==irq_status){ input_event(input, EV_KEY, 117, 1); input_sync(input); udelay(50); input_event(input, EV_KEY, 117, 0); input_sync(input); } return IRQ_HANDLED;}
注意:input_event()上报按键事件必须先上报1(意为按下),再上报0(意为d起),不能单独上报1,或者上报0。
kernel部分添加完成后,可通过getevent命令来确认事件有无上报成功。
内核部分确认调试成功后,需要将键值上报至上层:
1.修改按键布局映射文件,都是以.kl结尾的文件,可通过cat bus/input/devices
命令来查看我们系统目前正在使用的布局文件是哪个。
我使用的是/device/rockchip/common/rk29-keypad.kl布局文件
key 59 MENUkey 102 HOMEkey 114 VolUME_DOWNkey 115 VolUME_UPkey 116 POWERkey 143 NOTIFICATIONkey 158 BACKkey 212 CAMERAkey 217 SEARCH
在这后面添加key 117 F1
则表示内核上报的117键值映射到系统的键值为F1
系统的键值定义在frameworks/base/core/java/androID/vIEw/KeyEvent.java下。
添加完成后可以在frameworks/base/services/core/java/com/androID/server/policy/PhoneWindowManager.java里添加打印信息,查看键值是否上报成功。
@H_419_18@public long interceptKeyBeforedispatching(windowstate win, KeyEvent event, int policyFlags) { final boolean keyguardOn = keyguardOn(); final int keyCode = event.getKeyCode(); final int repeatCount = event.getRepeatCount(); final int MetaState = event.getMetaState(); final int flags = event.getFlags(); final boolean down = event.getAction() == KeyEvent.ACTION_DOWN; final boolean canceled = event.isCanceled(); if (DEBUG_input) { Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount=" + repeatCount + " keyguardOn=" + keyguardOn + " mHomepressed=" + mHomepressed + " canceled=" + canceled); }
上层APK可通过调用frameworks/base/core/java/androID/vIEw/KeyEvent.java中的接口去拦截键值
@H_419_18@public interface Callback { boolean onKeyDown(int keyCode, KeyEvent event); boolean onKeyLongPress(int keyCode, KeyEvent event); boolean onKeyUp(int keyCode, KeyEvent event); boolean onKeyMultiple(int keyCode, int count, KeyEvent event);}总结
以上是内存溢出为你收集整理的Android 添加键值并上报从驱动到上层全部内容,希望文章能够帮你解决Android 添加键值并上报从驱动到上层所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)