QWSLinuxInputKbPrivate::QWSLinuxInputKbPrivate(QWSLinuxInputKeyboardHandler *h, const QString &device)
: m_handler(h), m_fd(-1), m_tty_fd(-1), m_orig_kbmode(K_XLATE)
{
setObjectName(QLatin1String("LinuxInputSubsystem Keyboard Handler"));
QString dev = QLatin1String("/dev/input/event1");
int repeat_delay = -1;
int repeat_rate = -1;
int grab = 0;
QStringList args = device.split(QLatin1Char(':'));
foreach (const QString &arg, args) {
if (arg.startsWith(QLatin1String("repeat-delay=")))
repeat_delay = arg.mid(13).toInt();
else if (arg.startsWith(QLatin1String("repeat-rate=")))
repeat_rate = arg.mid(12).toInt();
else if (arg.startsWith(QLatin1String("grab=")))
grab = arg.mid(5).toInt();
else if (arg.startsWith(QLatin1String("/dev/")))
dev = arg;
}
m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0);
if (m_fd >= 0) {
::ioctl(m_fd, EVIOCGRAB, grab);
if (repeat_delay > 0 && repeat_rate > 0) {
int kbdrep[2] = { repeat_delay, repeat_rate };
::ioctl(m_fd, EVIOCSREP, kbdrep);
}
QSocketNotifier *notifier;
notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode()));
// play nice in case we are started from a shell (e.g. for debugging)
m_tty_fd = isatty(0) ? 0 : -1;
if (m_tty_fd >= 0) {
// save tty config for restore.
tcgetattr(m_tty_fd, &m_tty_attr);
struct ::termios termdata;
tcgetattr(m_tty_fd, &termdata);
// record the original mode so we can restore it again in the destructor.
::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode);
// setting this translation mode is even needed in INPUT mode to prevent
// the shell from also interpreting codes, if the process has a tty
// attached: e.g. Ctrl+C wouldn't copy, but kill the application.
::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW);
// set the tty layer to pass-through
termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
termdata.c_oflag = 0;
termdata.c_cflag = CREAD | CS8;
termdata.c_lflag = 0;
termdata.c_cc[VTIME]=0;
termdata.c_cc[VMIN]=1;
cfsetispeed(&termdata, 9600);
cfsetospeed(&termdata, 9600);
tcsetattr(m_tty_fd, TCSANOW, &termdata);
}
} else {
qWarning("Cannot open keyboard input device '%s': %s", qPrintable(dev), strerror(errno));
return;
}
}
调用流程是 app -> QWSServer::initServer -> QWSServer::openKeyboard() -> QKbdDriverFactory::create()
这是Qt的LinuxInput鼠标键盘打开过程,这是在QWSServer初始化的时候执行的,也就是在应用程序一开始就执行了,如果没有设备节点初始化鼠标键盘设备就会失败。因此不会支持热插拔。其它地方没有发现打开鼠标键盘设备的操作。你可以按照这个思路再跟一下,有时间我也看看,如果能改进一下QWSServer就更好了