CNxConsole Keyboard Input
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
NxConsole Device. The NX console input comes through a special device driver that
is registered at /dev/nxcon0 as part of early initialization.
Kernel Threads
NX Server Thread The NX Server is the graphics server command. It receives messages from various sources, performs graphics actions, and forwards graphic event messages to the correct window. Most of the time, the NX Server Thread was waiting on a message queue to receive the next graphics event.
NxConsole Threads Each NxConsole has a thread that was started when each
NxWM::CNxConsoleinstance was created by NxWM. EachNxWM::CNxConsolethread opens the NxConsole driver at/dev/nxcon0and redirects stdin, stdout, and stderr to/from that special device. Normally, theNxWM::CNxConsolethread is stopped, just waiting on read for keyboard input to complete.
Application Threads
NxWidgets Window Event Handler Thread
CNxServer::listener()is an application thread started by NxWidgets each time a new window is opened. It receives window messages from the NX server and dispatches the messages accordingly.Keyboard Listener Thread
CKeyboard::listener()is an application thread that is started by NxWM. It just listens for keyboard input and forwards it through the graphics routing system.
Now here is the sequence of events to get keyboard input from the stdin device to the correct NxConsole.
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.
NxWidgets/libnxwidgets/src/cnxserver.cxxCNxServer::listener()is it window listener thread. It just callsnx_eventhandler()to receive and process server events. There is one such listener thread per window.nuttx/libnx/nxwm/nx_eventhandlernx_eventhandler()waits to receive a message from the NX server. Each window has its own message queue; each window instance has its ownnx_eventhandler()waiting for messages.
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.
NxWidgets\nxwm\src\ckeyboard.cxxCKeyboard::listener()is a tiny thread that is started by NxWM that just listens for keyboard input. It opens the keyboard device and waits on aread()from the keyboard device to receive the next keyboard input. When data is returned by reading from the keyboard device,CKeyboard::listener()callsnx_kbdin()nuttx\libnx\nxmu\nx_kbdin.cThis library function just hides the NX server messaging implementation. It sends theNX_SVRMSG_KBDINto the NX server thread.
Kernel Space / NX Server
The NX Server wakes up, receives the keyboard message, and forwards it to the appropriate window.
nuttx/graphics/nxmu/nxmu/nxmu_server.cThe receipt of theNX_SVRMSG_KBDINmessage wakes up the NX server thread. The NX server thread decodes the message and callsnxmu_kbdin().nuttx/graphics/nxconsole/nxmu_kbdin.cnxmu_kbdin()simply sends theNX_CLIMSG_KBDINto the appropriate windows client via the client message queue associated with the window.
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.nuttx/libnx/nxwm/nx_eventhandlerThenx_eventhandler()logic running in theCNxServer::listener()thread receives theNX_CLIMSG_KBDINmessage and dispatches it to the kbdin callback method. In this case that callback method maps toCCallback::newKeyboardEvent().NxWidgets/libnxwidgets/src/ccallback.cxxFor normal keyboard input,CCallback::newKeyboardEvent()directs the Keyboard to the widget with focus via theCWidgetControl::newKeyboardEvent()method. But the story is different for the NxConsole window. This case,CCallback::newKeyboardEvent(), callsnxcon_kbdin().nuttx/graphics/nxconsole/nxcon_kbdin.cnxcon_kbdin()adds the keyboard data to a circular buffer and wakes up any reads on the/dev/nxcon0input device.
NOTE: Here is a violation of the Application and Kernel Space boundaries.
nxcon_kbdin.cbuilt into Kernel Space but it is called from Application Space. The solution is to (1) movenxcon_kbdin()tolibnx/and (2) it should then communicate with the/dev/nxcon9driver via ioctl calls. This will become a problem some day.Kernel Space / NxConsole Thread
- Finally,
nuttx/graphics/nxconsole/nxcon_kbdin.cThe receipt and enqueuing of keyboard data bynxcon_kbdin()wakes up any threads waiting in thenxcon_read()method. This is how the NxConsole gets its keyboard input.
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 simplification
is that the additional complexities of the NxConsole and its special input device
do not apply. Mouse/touchscreen input 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