Currently my keyboard handler processes the scancode and possibly calls a function to figure out what should be done with the input, which possibly calls another function to print something, and so on.
I have a strong feeling this is not the right way to do things, because the keyboard handler code shouldn't run the entire OS.
So how exactly to process the keys properly? Store them in a queue?
More specifically, what should the keyboard handler be doing?
Keyboard handler: what should it be doing?
Re: Keyboard handler: what should it be doing?
Process(es) read(s) from stdin, the keyboard handler sends the bytes to the process, on which the user is focused.Espanish wrote:Currently my keyboard handler processes the scancode and possibly calls a function to figure out what should be done with the input, which possibly calls another function to print something, and so on.
I have a strong feeling this is not the right way to do things, because the keyboard handler code shouldn't run the entire OS.
So how exactly to process the keys properly? Store them in a queue?
More specifically, what should the keyboard handler be doing?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
- Alan Kay
Re: Keyboard handler: what should it be doing?
My keyboard interrupt handler just makes the keyboard thread runnable. They keyboard thread then reads from the controller, decodes the scancode and stores the result in the keyboard buffer (unless it's full), which is a simple circular queue. Doing this on a thread rather than the interrupt handler simplifies the mutex arrangements for the buffer.
Programs can read from the keyboard device (exposed to userspace via the filesystem API, ala UNIX), which dequeues the decoded values from the buffer. The terminal driver provides an additional layer of abstraction, providing "standard input/output" concepts, multiple virtual terminals, etc. Userspace programs generally communicate only with the terminal device and not the keyboard device directly.
Programs can read from the keyboard device (exposed to userspace via the filesystem API, ala UNIX), which dequeues the decoded values from the buffer. The terminal driver provides an additional layer of abstraction, providing "standard input/output" concepts, multiple virtual terminals, etc. Userspace programs generally communicate only with the terminal device and not the keyboard device directly.
- AndrewAPrice
- Member
- Posts: 2303
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: Keyboard handler: what should it be doing?
In my case, it forwards the scancode to the window manager, which first does it's own thing (like detect if the Windows key was pressed), and if there's a focused window, it sends that process a KEY_STATE message.
My OS is Perception.
Re: Keyboard handler: what should it be doing?
My keyboard handler simply stores the key presses on a buffer shared between all keyboards (unless the buffer is full). A shared buffer means my kernel doesn't need to worry about searching each keyboard driver there may be in the future for key press/release events. My shell continuously loops waiting for a keypress to appear on the buffer (unless it is busy running a command).
- mathematician
- Member
- Posts: 437
- Joined: Fri Dec 15, 2006 5:26 pm
- Location: Church Stretton Uk
Re: Keyboard handler: what should it be doing?
You can either place the translated scancode in a circular queue, and wait for an interested application to retrieve it, or you can, like Windows, send the foreground process a message saying, some input from the keyboard has arrived, and here it is.
The continuous image of a connected set is connected.
Re: Keyboard handler: what should it be doing?
Thanks for the suggestions, everybody.
The circular queue idea seems too complex for my needs. If I understand the idea correctly, the handler pushes new scancodes to the queue while the kernel is busy-waiting for the queue to fill up, to pop the scancodes.
(What if the kernel is in the middle of popping and then the user hits a key? Disable interrupts beforehand?)
The message idea seems simpler, but I don't know how a "message" is supposed to be implemented.
I suppose using a global boolean kb_touched that is polled defeats the purpose.
How are messages normally implemented for such purposes?
The circular queue idea seems too complex for my needs. If I understand the idea correctly, the handler pushes new scancodes to the queue while the kernel is busy-waiting for the queue to fill up, to pop the scancodes.
(What if the kernel is in the middle of popping and then the user hits a key? Disable interrupts beforehand?)
The message idea seems simpler, but I don't know how a "message" is supposed to be implemented.
I suppose using a global boolean kb_touched that is polled defeats the purpose.
How are messages normally implemented for such purposes?
- mathematician
- Member
- Posts: 437
- Joined: Fri Dec 15, 2006 5:26 pm
- Location: Church Stretton Uk
Re: Keyboard handler: what should it be doing?
Think about the BIOS's keyboard handler. When int 9 goes off it retrieves the scan code, translates it into an ascii character, and then stores it in a buffer until somebody wants it. A program wanting keyboard input then issues a int 16h, and retrieves the character at the head of the queue; assuming that the queue is not empty.Espanish wrote:Thanks for the suggestions, everybody.
The circular queue idea seems too complex for my needs. If I understand the idea correctly, the handler pushes new scancodes to the queue while the kernel is busy-waiting for the queue to fill up, to pop the scancodes.
Which approach works best will depend upon the design of your operating system.
The continuous image of a connected set is connected.
- AndrewAPrice
- Member
- Posts: 2303
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: Keyboard handler: what should it be doing?
Things start getting complicated when you share mutexes between threads and interrupt handlers, I'm still figuring it out myself too: http://forum.osdev.org/viewtopic.php?f= ... 72#p243472Espanish wrote:(What if the kernel is in the middle of popping and then the user hits a key? Disable interrupts beforehand?)
My OS is Perception.