实现嵌入式QT按键

要想让qt支持咱自己的按键驱动实现 如 Qt::Key_PageDown 等则需要修改qt键盘事件,修改其驱动支持。
1. 驱动
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>

#define DEVICE_NAME "buttons"
	struct button_irq_desc {
	int irq;
	int pin;
	int pin_setting;
	int number;
	char *name;
};


//按键所对应的中断和GPG口
static struct button_irq_desc button_irqs [] = {

{IRQ_EINT8 , S3C2410_GPG0 , S3C2410_GPG0_EINT8 , 0, "KEY0"},
{IRQ_EINT11, S3C2410_GPG3 , S3C2410_GPG3_EINT11 , 1, "KEY1"},
{IRQ_EINT13, S3C2410_GPG5 , S3C2410_GPG5_EINT13 , 2, "KEY2"},
{IRQ_EINT15, S3C2410_GPG7 , S3C2410_GPG7_EINT15 , 3, "KEY3"},
{IRQ_EINT14, S3C2410_GPG6 , S3C2410_GPG6_EINT14 , 4, "KEY4"},
{IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, 5, "KEY5"},

};
//按键开始值
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
	struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
	int down;

	down = !s3c2410_gpio_getpin(button_irqs->pin);
	if (down != (key_values[button_irqs->number] & 1)) { // Changed
	key_values[button_irqs->number] = '0' + down;
	ev_press = 1;
	wake_up_interruptible(&button_waitq);
	}
	return IRQ_RETVAL(IRQ_HANDLED);
}

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
	int i;
	int err;
	for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) 
	{
		err = request_irq(button_irqs[i].irq, buttons_interrupt,
		IRQ_TYPE_EDGE_BOTH,
		button_irqs[i].name, (void *)&button_irqs[i]);
		if (err)
			break;
	}
	if (err) 
	{
		i--;
		for (; i >= 0; i--) 
		{
			disable_irq(button_irqs[i].irq);
			free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
		}
		return -EBUSY;
	}
	ev_press = 1;
	return 0;
}

static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
	int i;
	for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) 
	{
		free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
	}
	return 0;
}

static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t*offp)
{
	unsigned long err;
	if (!ev_press) 
	{
		if (filp->f_flags & O_NONBLOCK)
		return -EAGAIN;
		else
			wait_event_interruptible(button_waitq, ev_press);
	}

	ev_press = 0;
	err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));
	return err ? -EFAULT : min(sizeof(key_values), count);
}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{
	unsigned int mask = 0;
	poll_wait(file, &button_waitq, wait);
	if (ev_press)
	mask |= POLLIN | POLLRDNORM;
	return mask;
}

static struct file_operations dev_fops = {
	.owner = THIS_MODULE,
	.open = s3c24xx_buttons_open,
	.release = s3c24xx_buttons_close,
	.read = s3c24xx_buttons_read,
	.poll = s3c24xx_buttons_poll,
};
	
static struct miscdevice misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &dev_fops,
};

static int __init dev_init(void)
{
	int ret;
	ret = misc_register(&misc);
	printk (DEVICE_NAME"\tinitialized\n");
	return ret;
}

static void __exit dev_exit(void)
{
	misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

2. 配置键盘驱动的qt
(echo yes ; echo yes ) | ./configure \
-prefix /usr/local/Trolltech/QtEmbedded-4.5.0-fr-arm \
-no-armfpa  \
-release -shared \
-embedded arm \
-xplatform qws/linux-arm-g++ \
-depths 16,18,24 \
-fast \
-optimized-qmake \
 -pch \
 -dbus \
 -system-sqlite \
 -qt-libjpeg \
 -qt-zlib \
 -qt-libpng \
 -qt-freetype \
 -qt-kbd-yopy -qt-kbd-usb \
 -little-endian -host-little-endian \
 -no-svg \
 -no-qt3support \
 -no-libtiff -no-libmng \
 -no-opengl \
 -no-mmx -no-sse -no-sse2 \
 -no-3dnow \
 -no-openssl \
 -no-phonon \
 -no-opengl \
 -no-cups \
 -no-qvfb \
 -no-xcursor -no-xfixes -no-xrandr -no-xrender \
 -nomake examples -nomake tools -nomake docs \
 -qt-mouse-tslib -I/usr/local/tslib/include -L/usr/local/tslib/lib \
 -I/usr/arm-linux-gnueabi/include/dbus-1.0 -I/usr/arm-linux-gnueabi/lib/dbus-1.0/include \
 -I/usr/arm-linux-gnu/include -I/usr/arm-linux-gnueabi/include \
 -L/usr/arm-linux-gnueabi/lib -ldbus-1

3. 修改qt源码
src/gui/embedded/qkbdyopy_qws.cpp
terminalName = device.isEmpty()?"/dev/buttons":device.toLatin1().constData();
去掉tcsetpgrp(buttonFD, getpgid(0));
//!<  put tty into "straight through" mode.

if (tcgetattr(buttonFD, &oldT) < 0) {    //!< 保存当前设置到oldtio
   qFatal("Linux-kbd: tcgetattr failed");
}

newT = oldT;
newT.c_lflag &= ~(ICANON | ECHO  | ISIG);
newT.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
newT.c_iflag |= IGNBRK;
newT.c_cc[VMIN]  = 0;
newT.c_cc[VTIME] = 0;    //!< 在规定时间(VTIME)内读取(VMIN)个字符


if (tcsetattr(buttonFD, TCSANOW, &newT) < 0) {
   qFatal("Linux-kbd: TCSANOW tcsetattr failed");
}

if (ioctl(buttonFD, KDSKBMODE, K_MEDIUMRAW) < 0) {
   qFatal("Linux-kbd: KDSKBMODE tcsetattr failed");
}


unsigned int keylen = 6;    //!< 缓存大小
uchar buf[keylen];  //!< 键值缓存
unsigned int keys=0;    //!< key 值总和
char c='1';
int fd;

int n=read(buttonFD,buf,keylen);
if (n<0) {
	qDebug("Keyboard read error %s",strerror(errno));
} 
else {
	uint code = buf[0]&&0xff;//&YPBUTTON_CODE_MASK;
	bool press = !(buf[0]&0x80);

	//printf("lei: Key=%d, %d, %d, %d, %d, %d\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);

	for(int i=1; i<=keylen; i++)
		keys += (buf[i-1]-0x30)*i;

	//printf("lei: Keys=%d\n",keys);

	int k=(-1);
	switch(keys) {
	  case 1:       k=Qt::Key_F1;   break;
	  case 2:       k=Qt::Key_F2;   break;
	  case 3:       k=Qt::Key_F3;   break;
	  case 4:       k=Qt::Key_F4;   break;
	  case 5:       k=Qt::Key_F5;   break;
	  case 6:       k=Qt::Key_F6;   break;
	//case 56:       k=Qt::Key_F1;     break; //windows
	   //case 29:       k=Qt::Key_F2;     break; //cycle
	   //case 24:       k=Qt::Key_F3;     break; //record
	   //case 23:       k=Qt::Key_F4;     break; //mp3
	   //case 4:        k=Qt::Key_F5;     break; // PIMS
	   //case 1:        k=Qt::Key_Escape; break; // Escape

	/*
	   case 35:       if(!press) {
						fd = open("/proc/sys/pm/sleep",O_RDWR,0);
						if(fd >= 0) {
							write(fd,&c,sizeof(c));
							close(fd);
							//
							// Updates all widgets.
							//
							QWidgetList list = QApplication::allWidgets();
							for (int i = 0; i < list.size(); ++i) {
								QWidget *w = list.at(i);
								w->update();
							}
						}
					  }
					  break;
	*/

	   default: k=(-1); break;
	 }

	qDebug("Unrecognised k code %x", k );
	 if (k >= 0) {
		 handler->processKeyEvent(0, k, 0, true, false);
	 }
 }


转载于:https://my.oschina.net/shelllife/blog/54648

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值