简体   繁体   English

Linux键盘原始读取,什么更好,从/ dev / input / event0读取或从stdin读取?

[英]Linux keyboard raw reading, what's better, reading from /dev/input/event0 or reading from stdin?

I'm working on a small C videogames library for the Raspberry Pi. 我正在为Raspberry Pi开发一个小型C视频游戏库。 I'm coding the input system from scratch and after reading and seeing some examples about raw input reading, I got some doubts. 我正在从头开始编写输入系统,在阅读并看到一些关于原始输入读数的例子后,我有些疑惑。

For mouse reading, I just use /dev/input/event1, I open() it as O_NONBLOCK, I read() input_event(s) and I also put the mouse reading in a separate pthread. 对于鼠标读取,我只使用/ dev / input / event1,我打开()它作为O_NONBLOCK,我读取()input_event(s),我也将鼠标读取放在一个单独的pthread中。 Easy. 简单。

For keyboard reading, I saw some examples that reconfigure stdin to O_NONBLOCK (using fcntl()), then save and reconfigure keyboard termios attibutes (ICANON, ECHO), and some example also save and reconfigure keyboard mode with ioctl(). 对于键盘读取,我看到一些示例将stdin重新配置为O_NONBLOCK(使用fcntl()),然后保存并重新配置键盘termios attibutes(ICANON,ECHO),还有一些示例还使用ioctl()保存和重新配置键盘模式。 What's the point of doing all that stuff instead of just reading /dev/input/event0 input_event(s) (same way as mouse)? 做所有这些事情的重点是什么而不仅仅是读取/ dev / input / event0 input_event(s)(与鼠标相同)?

Note that I undestand what those functions do, I just don't undestand why should be better to do all that stuff instead of just reading input_event(s). 请注意,我没有明确这些函数的作用,我只是不明白为什么应该更好地做所有这些事情,而不是只读取input_event(s)。

Reading stdin is not limited to reading locally connected keyboards (but has other limitation making it mostly unsuitable for games). 阅读标准输入不仅限于阅读本地连接的键盘(但还有其他限制,使其大多不适合游戏)。 Then reading stdin you could be reading keystrokes from a user logged in remotely, using a locally connected serial terminal or a terminal emulator (possibly operated from a remote X server). 然后读取stdin,您可以使用本地连接的串行终端或终端仿真器(可能从远程X服务器操作)读取远程登录用户的击键。

For terminal based games it might make sense to use stdin. 对于基于终端的游戏,使用stdin可能是有意义的。 But then it would probably be better to use GPM instead of reading /dev/input/event1 for the mouse. 但是,使用GPM而不是为鼠标读取/ dev / input / event1可能会更好。 And possibly even better to use ncurses for everything. 甚至可能更好地使用ncurses来处理所有事情。

Otherwise you might want to look at SDL , if not for using it directly, at least for learning about different ways to read input. 否则,您可能希望查看SDL ,如果不是直接使用它,至少是为了了解不同的读取输入方法。 For example, SDL has full support for network transparency using X. Which means a game can execute on one computer but be played on another. 例如,SDL完全支持使用X的网络透明性。这意味着游戏可以在一台计算机上执行,但可以在另一台计算机上播放。

Expanding on Fabel answer - standard input and event1 are entirely different things. 扩展在费伯尔的答案-标准输入和event1是完全不同的事情。 Starting from that the event1 does not have to be mouse but can be an other device depending on udev, kernel version, phase of moon etc. - on my (non Raspberry Pi) system it's input device Sleep Button - which has a keyboard interface. 从那开始, event1不一定是鼠标,但可以是其他设备,具体取决于udev,内核版本,月相等。 - 在我的(非Raspberry Pi)系统上,它是input device Sleep Button - 它有一个键盘接口。 In short you cannot and should not assume it's keyboard - or the only keyboard for that matter (for example also YubiKey emulates the keyboard for it's function but it's rather useless as gaming device, and while 2 keyboards are rarely connected to same Raspberry Pi I don't think it's good idea to assume such setup never happens). 简而言之,你不能也不应该假设它的键盘 - 或唯一的键盘(例如YubiKey也模仿键盘的功能,但它作为游戏设备相当无用,而且2个键盘很少连接到相同的Raspberry Pi我不喜欢我认为假设这种设置永远不会发生是个好主意。

Furthermore typically input devices can be read only by privileged user (on older/current systems) or by user holding a 'seat' (on rootless X systems) - assuming you're not using bleeding edge system it means your game can only by used by root user which is usually considered a bad idea . 此外,通常输入设备只能由特权用户(在旧系统/当前系统上)或持有“座位”的用户(在无根X系统上)读取 - 假设您没有使用前沿系统,则意味着您的游戏只能使用由root用户通常被认为是一个主意

Finally it only allows for user to play using a evdev subsystem, which probably should be considered Linux/X11 implementation detail. 最后它只允许用户使用evdev子系统进行播放,这可能应该被视为Linux / X11实现细节。 If you try to connect via network (X11, vnc or whaterver), try to use on-screen keyboard or any accessible input program - you might run into problems. 如果您尝试通过网络(X11,vnc或whaterver)进行连接,请尝试使用屏幕键盘或任何可访问的输入程序 - 您可能会遇到问题。

On the other hand what you can do is either use standard input directly, use some more portable libraries like readline (which should be sufficient for rougulike ) or use graphics server (probably indirectly via library like QT or SDL as X11 is not the nicest protocol to program against). 另一方面,你可以做的是直接使用标准输入,使用一些更可移植的库,如readline (这应该足够粗糙 )或使用图形服务器(可能间接通过QTSDL等库,因为X11不是最好的协议编程反对)。

Reading from /dev/input/eventN works only on GNU/Linux. /dev/input/eventN仅适用于GNU / Linux。

Configuring and using stdin works on any system implementing POSIX.1-2008 , specifically chapter 11: General Terminal Interface . 配置和使用stdin适用于任何实现POSIX.1-2008的系统,特别是第11:通用终端接口 So if you want portable code, like say to work on Linux and OS X without rewriting the input system, you'd do it this way. 因此,如果您想要可移植代码,比如说在Linux和OS X上工作而不重写输入系统,那么您就是这样做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM