NxWM::CNxConsole Keyboard Input

I recently decided that I wanted to connect a USB keyboard and use that to drive NxWM::CNxConsole windows. I realized that I had completely forgotten how that work and had to reverse engineer my own code to figure how to do this.

Since I went through that effort to analyze the keyboard path, I decided that I should publish this as a Wiki page. If for no other reason so that I won't have to do this analysis again in the future.

= Players

Let's look at the major players in the keyboard data transfer. This is much more complex than you might initially think:

Special Drivers

Kernel Threads

Application Threads

Now here is the sequence of events to get keyboard input from the stdin device to the correct NxConsole.

1. Application Space / NxWidgets Window Event Handler Thread

Let's start with the initial state of the NX Server Thread. Initially, it will just want for messages from the NX Server.

2. Application Space / Keyboard Listener Thread

Here are the immediate events that happen when the keyboard data is entered. The Keyboard Listener Thread wakes up and forwards the Keyboard data to the the NX Server. Only the NX Server knows which window should get the keyboard input.

3. Kernel Space / NX Server

The NX Server wakes up, receives the keyboard message, and forwards it to the appropriate window.

4. Application Space / NxWidgets Window Event Handler Thread

The Windows client wakes up when the keyboard message is received. It forwards the keyboard data to "/dev/nxcon0/" so that is can be available to the NxConsole window.

NOTE: Here is a violation of the Application and Kernel Space boundaries. nxcon_kbdin.c built into Kernel Space but it is called from Application Space. The solution is to (1) move nxcon_kbdin() to libnx/ and (2) it should then communicate with the /dev/nxcon9 driver via ioctl calls. This will become a problem some day.

5. Kernel Space / NxConsole Thread

Finally,

Mouse Input

Almost everything said here applies to mouse/touchscreen input as well. If we were to replace the names keyboard to mouse, kbdin to mousein, etc. you have a pretty good description of how mouse/touchscreen input works.

The mouse/touchscreen input is a little simpler, however: The main simplication is that the additional complexities of the NxConsole and its special input device do not apply. Mouse/touchscreen inut as always steered to widgets when the callback is received in CCallback::newMouseEvent by an unconditional call to CWidgetControl::newMouseEvent. There is a "fork in the road" at the corresponding point in the logic of CCallback::newKeyboardEvent