File: learnkeys.c

package info (click to toggle)
esekeyd 1.2.7-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 544 kB
  • ctags: 55
  • sloc: ansic: 1,355; sh: 1,129; makefile: 76
file content (166 lines) | stat: -rw-r--r-- 4,182 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * learnkeys.c --- ESE Key Daemon --- Keycode Daemon for Funny/Function Keys.
 *
 * Taps /dev/input/event<number> and makes config file for ESE Key Daemon.
 *
 * $Id: learnkeys.c 63 2010-07-04 20:23:51Z kb $
 *
 * Based on code from funky.c released under the GNU Public License
 * by Rick van Rein.
 *
 * (c) 2000 Rick van Rein.
 * (c) 2004 Krzysztof Burghardt.
 *
 * Released under the GNU Public License.
 */

#include "esekey.h"

FILE *funkey = NULL;
FILE *config = NULL;

void cleanup ()
{
    fclose (funkey);
    fclose (config);
}

void signal_handler (int x)
{
    printf ("\nCaught signal %d, writing config file and exiting...\n", x);
    cleanup ();
    exit(x);
}

void register_signal_handlers (void)
{
    signal (SIGHUP,  signal_handler);
    signal (SIGINT,  signal_handler);
    signal (SIGQUIT, signal_handler);
    signal (SIGILL,  signal_handler);
    signal (SIGTRAP, signal_handler);
    signal (SIGABRT, signal_handler);
    signal (SIGIOT,  signal_handler);
    signal (SIGFPE,  signal_handler);
    signal (SIGKILL, signal_handler);
    signal (SIGSEGV, signal_handler);
    signal (SIGPIPE, signal_handler);
    signal (SIGTERM, signal_handler);
    signal (SIGSTOP, signal_handler);
    signal (SIGUSR1, SIG_IGN);
}

int main (int argc, char *argv[])
{
    short int device = 0;
    char *device_name = NULL;
    char *key = NULL;
    struct input_event ev;

    printf ("learnkeys (%s, %s)\n", PACKAGE_STRING, PACKAGE_VERSION_SVN_REV);

    if (argc < 2)
    {
        printf ("\nUsage:\n");
        printf ("%s config_file_name [input_device_name]\n\n", argv[0]);
        printf ("config_file_name  - location of esekeyd config file\n");
        printf ("input_device_name - input (event) device; if given turns off\n");
        printf ("                    of 1st keyboard device\n");
        printf ("\nExample:\n");
        printf ("%s ~/.esekeyd.conf /dev/input/event3\n", argv[0]);
        exit (1);
    }

    if (argc > 2)
    {
        device_name = argv[2];
    }
    else
    {
        switch (check_handlers ())
        {
            case -1:
                printf ("%s: cannot open %s\n", argv[0], INPUT_HANDLERS);
                return -1;
            case -2:
                printf ("%s: evdev handler not found in %s\n", argv[0],
                        INPUT_HANDLERS);
                return -2;
        }

        switch (device = find_input_dev ())
        {
            case -1:
                printf ("%s: evdev for keyboard not found in %s\n", argv[0],
                        INPUT_HANDLERS);
                return -3;
            default:
                asprintf (&device_name, "%s%hu", EVENT_DEVICE, device);
        }
    }

    if (!(funkey = fopen (device_name, "r")))
    {
        printf ("%s: can`t open %s\n", argv[0], device_name);
        return -4;
    }

    if (!(config = fopen (argv[1], "w")))
    {
        printf ("%s: can`t open %s\n", argv[0], argv[1]);
        return -5;
    }

    fprintf (config, 
"#\n"
"# %s config file\n"
"#\n"
"\n"
"#\n"
"# example 1: to run mutt in xterm we must set DISPLAY\n"
"#            so the command line will be as follows:\n"
"#MAIL:/bin/sh -c \"DISPLAY=:0 xterm -e mutt\"\n"
"#\n"
"# example 2: turn on/off GPS reciever when lid is open/closed\n"
"#RADIO(press):echo 1 >/sys/device/platform/gps/gps_power\n"
"#RADIO(release):echo 0 >/sys/device/platform/gps/gps_power\n"
"#\n"
"# example 3: run nautilus when both left meta and e keys are press\n"
"#LEFTMETA+E:nautilus\n"
"#\n"
"\n", PACKAGE_STRING);

    printf ("\nPres ANY (fun)key... or Ctrl-C to exit...\n\n");

    register_signal_handlers();

    while (fread (&ev, sizeof (struct input_event), 1, funkey))
    {

        if (ev.code == 28 && ev.type == EV_KEY && ev.value == 1)
        {

            printf ("\n");
            fclose (funkey);
            return 0;

        }

        if ((key = parse (ev)) != NULL)
        {

            printf ("key %s stored in config file\n", key);
            if (ev.value == 1)
                fprintf (config, "#%s(press):\n", key);
            else
                fprintf (config, "#%s(release):\n", key);

        }

    }

    cleanup ();

    return 0;

}