LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Keyboard interrupt handler and more (https://www.linuxquestions.org/questions/programming-9/keyboard-interrupt-handler-and-more-549672/)

john_crichton 04-28-2007 04:39 AM

Keyboard interrupt handler and more
 
I need to serve keyboard interrupts in my application. I have done fair share of searching and I generally know how to register interrupt handler with kernel. But I don't know how to save/restore previous interrupt handler so when my application finishes computer remains usable. Also I need to have a function called on regular time intervals. I heard there is a kernel function for that but I don't know which one it is. Thank you in advance for any help.

taylor_venable 04-29-2007 03:44 PM

Quote:

Originally Posted by john_crichton
I need to serve keyboard interrupts in my application. ... Also I need to have a function called on regular time intervals.

Hope this helps:
Code:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void handler(int); /* signal handler */

int main(int argc, char* argv[]) {
    int x = 0;
    /* register the handler for keyboard interrupt */
    if (signal(SIGINT, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGINT.\n");
    /* register the handler for alarm signal */
    if (signal(SIGALRM, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGALRM.\n");
    else
        /* set the alarm to go off in three seconds */
        alarm(3);
    while (1) {
        fprintf(stderr, "%d ", x++);
        sleep(1);
    }
    return 0;
}

static void handler(int signum) {
    if (signum == SIGINT) {
        printf("Got an interrupt signal.\n");
        exit(0);
    }
    else if (signum == SIGALRM) {
        fprintf(stderr, "ALARM! ");
        alarm(3);
    }
    return;
}


Mara 04-29-2007 03:47 PM

Register an interrupt handler from a kernel module - that's doable, but from an user-space application? I rather does not work this way (of course, depending on the OS).

In general, you would need to store old handler address and then restore it later (if you know where to write the address, it should be rather easy).

I don't know what you're doing and what's the reason you need your own interrupt handler (what with different types of keyboards?), but it may be a much better idea to get raw access to keyboard from the application.

Mara 04-29-2007 03:48 PM

One more note: the code taylor_venable posted is not about interrupt handling, but signal handling, what is a different issue. The original poster may be, however, making the same mistake.

kingraja84 04-30-2007 01:17 AM

i can tell how i have implemented my keypad to get detected

first;
request kernel for an interrupt in ur driver for keyboard
otherwise kernel will ignore the interrupt
request_irq(interrupt number,function to get executed on irq...)

second:
write an irq routine that will get executed on recieve of interrupt (function what i have mentioned above) write it as short as possible

read the buffer on recieve of interrupt "copy_to_user" on completion of line

abhinavg 07-16-2009 10:30 AM

Request keyboard interrupt handler for application
 
Can anyone send me the code for key board interrupt handling. I need to open the keyboard device and handle the keyboard interrupts and send my application the received key information. Thanks a lot.


All times are GMT -5. The time now is 10:08 AM.