Read input device in C - c

I'm creating a 2-D platformer game in C that will run in the terminal. I need a way to register "KEY_DOWN" and "KEY_RELEASE" events. As far as I know getchar() reads from stdin and thus can not be used for registering "KEY_RELEASE" events. Neither can it be used for registering if multiple keys are being pressed down at the same time.
Is there a Mac OSX 10.10.5 C library that I can use to solve this problem? Maybe a library which reads input directly from the keyboard instead of the terminal?

None of the potential answers are likely to be simple:
if you are running an application in the terminal, and lacking (as OSX does) a set of system calls for reading the keyboard state directly, it won't work.
the available sources describe non-terminal applications (mostly using Cocoa, the OSX GUI, and mostly using ObjectiveC).
Here are a few:
How can I detect that the Shift key has been pressed?
Showing how to listen to all keypresses in OS X through the Cocoa API using Python and PyObjC
Listening for Global Keypresses in OSX
Mouse button and keypress counter for Mac OS X
OSX: Detect system-wide keyDown events?
For reference (Cocoa Event Handling Guide):
Handling Key Events
Monitoring Events

Related

Receive Signal for System Key using Linux?

I want to register my Application (its a Gtk Application) to receive a signal when the user presses for example the "Next Song" Button, while it is not focused, so the User can change the playback while the Application remains in the background. I have no idea how to do this - will I need to include a specific Library for doing this on Ubuntu 18.04? Just to clarify: I am talking about System-Wide Hotkeys that applications can somehow intercept.
There isn't really a generic mechanism for this in Wayland (the security issues should be pretty obvious); for X, see the XGrabKey function.
For multimedia keys, there is a D-Bus interface you can use at org.gnome.SettingsDaemon.MediaKeys. For an example of how to use it, take a look at plugins/nmkeys/rb-mmkeys-plugin.c in Rhythmbox.

How to get Keyboard inputs into a kernel?

I am writing my own kernel in c. Now I want to code a Console to interact with the Kernel. It should work like the normal Terminal on Linux. How can I get a input over the Keyboard ? Do I have to use Keyboard specific drivers ?
You need to write a driver in your kernel for the keyboard. Assuming a standard PC, the 8042 keyboard controller is pretty well documented (see http://wiki.osdev.org/%228042%22_PS/2_Controller for example). You'll also need to write a driver for the display, and again assuming VGA it is pretty well documented (see http://wiki.osdev.org/VGA_Hardware). Then you'll have to write all of the terminal stuff that sits in between to connect the two.

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. 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. 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(). What's the point of doing all that stuff instead of just reading /dev/input/event0 input_event(s) (same way as mouse)?
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).
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).
For terminal based games it might make sense to use stdin. But then it would probably be better to use GPM instead of reading /dev/input/event1 for the mouse. And possibly even better to use ncurses for everything.
Otherwise you might want to look at SDL, if not for using it directly, at least for learning about different ways to read input. 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.
Expanding on Fabel answer - standard input and event1 are entirely different things. 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. 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).
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.
Finally it only allows for user to play using a evdev subsystem, which probably should be considered Linux/X11 implementation detail. 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.
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).
Reading from /dev/input/eventN works only on GNU/Linux.
Configuring and using stdin works on any system implementing POSIX.1-2008, specifically chapter 11: General Terminal Interface. 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.

Programmatically disabling the screen blanker on X11 [duplicate]

X11 has a screensaver mechanism that can be controlled by xset command. Without having to invoke the external command to disable or heartbeat the screensaver, from inside my X11 application, how can I heartbeat the screensaver mechanism so to prevent it from shutting down the monitor?
If this on a modern Linux system, the preferred FreeDesktop method is to ping the DBus interface, specifically org.freedesktop.ScreenSaver.SimulateUserActivity() or another part of the exposed interface, depending on your needs.
As well as the D-Bus solution suggested by #Kitsune. You can also take the approach that some media players do and simulate the pressing of a key (e.g. Alt) at regular intervals.
This code in totem: http://git.gnome.org/browse/totem/tree/lib/totem-scrsaver.c is designed to be dropped into your code base (it's LGPL v2.1) and will use both the D-Bus interface and also use XTest to synthesize pressing the Alt key every 30s.

How to make a Windows touch pad acts like a Mac touch pad?

I am trying to write something for windows using WINAPI, so I can make the touch pad do whatever the mac touch pad do.
I have checked using Spy++ what WM messages the two finger taps and etc. send to the OS, but figured out it sends only those plus/minus:
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_MOUSEHOVER
WM_MOUSEHWHEEL
WM_MOUSELEAVE
WM_MOUSEMOVE
WM_RBUTTONDOWN
WM_RBUTTONUP
When I tried to see what happend when clicking with 3 or 2 fingers it didn't send any particular message, unless I moved them a bit.
firstly i would like to start with this:
when 5 fingers going down show desktop (as win+D does).
How to write (driver?) something that can diagnose 5 fingers touching simultaneously the touch pad?
Of curse there is no OS messages for this, but I can make some unique combination of existed messages and by that diganose it.
If I need to write a driver can I do it generic for most of the touchpad, can I do it as add-on?
If you can post a good tutorial you are familiar with for writing a driver for windows, pls, cause I have no clue about it.
Do I need anything else to take into account :
1. Diagnose 5 fingers mouse events.
2. Make a thread in Explorer on startup that handle those new mouse messages.
thanks in advance
Mouse Input Notifications
In short, you can't.
First, there are touchpads that can physically detect only 1 finger touch, and for those who can detect many - their drivers do the translation for you.
Windows does not have any inherent support for reading multiple touch inputs - it relies on the touchpad drivers to provide them.
You can achieve your goal for SOME devices by writing your own touchpad driver (probably starting from Linux touchpad drivers and Windows driver development kit), but this is far from being simple.
And, you'll need to do this for each and every touchpad device you want to support (from Synaptics, Alps Electric, Cirque to name the few)
Only after that you can move on to implementing the reaction for the touchpad actions in applications like Explorer.

Resources