summaryrefslogtreecommitdiff
path: root/plugins/hotkeys/hotkeys.c
blob: 415e055c3cac3a8b3169005c5735c6e3c34f3565 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
    Hotkeys plugin for DeaDBeeF
    Copyright (C) 2009 Viktor Semykin <thesame.ml@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <ctype.h>

#include "../../deadbeef.h"

static DB_misc_t plugin;
static DB_functions_t *deadbeef;
static int finished;
static Display *disp;
static intptr_t loop_tid;

#define MAX_COMMAND_COUNT 256

typedef struct {
    char *name;
    KeySym keysym;
} xkey_t;

#define KEY( kname, kcode ) { .name=kname, .keysym=kcode },

static const xkey_t keys[] = {
    #include "keysyms.inc"
};

typedef void (*command_func_t) (void);

typedef struct {
    int keycode;
    int modifier;
    command_func_t func;
} command_t;

static command_t commands [MAX_COMMAND_COUNT];
static int command_count = 0;


typedef struct {
    char* name;
    void (*func) (void);
} known_command_t;

static int
get_keycode( Display *disp, const char* name ) {
    static int first_kk, last_kk;
    static KeySym* syms;
    static int ks_per_kk;
    static int first_time = 1;
    int i, j, ks;

    if ( first_time )
    {
        XDisplayKeycodes( disp, &first_kk, &last_kk );

        syms = XGetKeyboardMapping( disp, first_kk, last_kk - first_kk, &ks_per_kk );
        first_time = 0;
    }

    for ( i = 0; i < last_kk-first_kk; i++ )
    {
        KeySym sym = *(syms + i*ks_per_kk);
        for ( ks = 0; ks < sizeof( keys ) / sizeof( xkey_t ); ks++ )
        {
            if ( (keys[ ks ].keysym == sym) && ( 0 == strcmp(name, keys[ ks ].name) ) )
            {
                return i+first_kk;
            }
        }
    }
    return 0;
}

static char*
trim(char* s)
{
    char *h, *t;
    
    for ( h = s; *h == ' ' || *h == '\t'; h++ );
    for ( t = s + strlen(s); *t == ' ' || *t == '\t'; t-- );
    *(t+1) = 0;
    return h;
}

static void
cmd_seek_fwd() {
    deadbeef->playback_set_pos( deadbeef->playback_get_pos() + 5 );
}

static void
cmd_seek_back() {
    deadbeef->playback_set_pos( deadbeef->playback_get_pos() - 5 );
}

static void
cmd_volume_up() {
    deadbeef->volume_set_db( deadbeef->volume_get_db() + 2 );
}

static void
cmd_volume_down() {
    deadbeef->volume_set_db( deadbeef->volume_get_db() - 2 );
}

static command_func_t
get_command( const char* command )
{
    if ( 0 == strcasecmp( command, "toggle_pause" ) )
        return deadbeef->playback_pause;

    if ( 0 == strcasecmp( command, "play" ) )
        return deadbeef->playback_play;

    if ( 0 == strcasecmp( command, "prev" ) )
        return deadbeef->playback_prev;

    if ( 0 == strcasecmp( command, "next" ) )
        return deadbeef->playback_next;

    if ( 0 == strcasecmp( command, "stop" ) )
        return deadbeef->playback_stop;

    if ( 0 == strcasecmp( command, "play_random" ) )
        return deadbeef->playback_random;

    if ( 0 == strcasecmp( command, "seek_fwd" ) )
        return cmd_seek_fwd;

    if ( 0 == strcasecmp( command, "seek_back" ) )
        return cmd_seek_back;

    if ( 0 == strcasecmp( command, "volume_up" ) )
        return cmd_volume_up;

    if ( 0 == strcasecmp( command, "volume_down" ) )
        return cmd_volume_down;

    return NULL;
}

static int
read_config( Display *disp )
{
    DB_conf_item_t *item = deadbeef->conf_find ("hotkeys.", NULL);
    while (item) {
//        fprintf (stderr, "hotkeys: adding %s %s\n", item->key, item->value);
        if ( command_count == MAX_COMMAND_COUNT )
        {
            fprintf( stderr, "hotkeys: maximum number (%d) of commands exceeded\n", MAX_COMMAND_COUNT );
            break;
        }

        command_t *cmd_entry = &commands[ command_count ];
        cmd_entry->modifier = 0;
        cmd_entry->keycode = 0;

        size_t l = strlen (item->value);
        char param[l+1];
        memcpy (param, item->value, l+1);
        
        char* colon = strchr( param, ':' );
        if ( !colon )
        {
            fprintf( stderr, "hotkeys: bad config option %s %s\n", item->key, item->value);
            continue;
        }
        char* command = colon+1;
        *colon = 0;

        int modifier = 0;
        char* key = NULL;

        int done = 0;
        char* p;
        char* space = param - 1;
        do {
            p = space+1;
            space = strchr( p, ' ' );
            if ( space )
                *space = 0;
            else
                done = 1;

            if ( 0 == strcasecmp( p, "Ctrl" ) )
                cmd_entry->modifier |= ControlMask;

            else if ( 0 == strcasecmp( p, "Alt" ) )
                cmd_entry->modifier |= Mod1Mask;

            else if ( 0 == strcasecmp( p, "Shift" ) )
                cmd_entry->modifier |= ShiftMask;

            else if ( 0 == strcasecmp( p, "Super" ) ) {
                cmd_entry->modifier |= Mod2Mask;
            }

            else {
                if (p[0] == '0' && p[1] == 'x') {
                    // parse hex keycode
                    int r = sscanf (p, "0x%x", &cmd_entry->keycode);
                    if (!r) {
                        cmd_entry->keycode = 0;
                    }
                }
                else {
                    // lookup name table
                    cmd_entry->keycode = get_keycode( disp, p );
                }
                if ( !cmd_entry->keycode )
                {
                    fprintf( stderr, "hotkeys: Unknown key: <%s> while parsing %s %s\n", key, item->key, item->value );
                    break;
                }
            }
        } while ( !done );

        if (done) {
            if ( cmd_entry->keycode == 0 ) {
                fprintf( stderr, "hotkeys: Key not found while parsing %s %s\n", item->key, item->value);
            }
            else {
                command = trim (command);
                cmd_entry->func = get_command( command );
                if ( !cmd_entry->func )
                {
                    fprintf( stderr, "hotkeys: Unknown command <%s> while parsing %s %s\n", command,  item->key, item->value);
                }
                else {
                    command_count++;
                }
            }
        }
        item = deadbeef->conf_find ("hotkeys.", item);
    }
}

DB_plugin_t *
hotkeys_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

static void
cleanup() {
    command_count = 0;
    XCloseDisplay( disp );
}

static void
hotkeys_event_loop( uintptr_t unused ) {
    int i;

    while (!finished) {
        XEvent event;
        while ( XPending( disp ) )
        {
            XNextEvent( disp, &event );

            if ( event.xkey.type == KeyPress )
            {
                for ( i = 0; i < command_count; i++ )
                    if ( (event.xkey.keycode == commands[ i ].keycode) &&
                         ((event.xkey.state & commands[ i ].modifier) == commands[ i ].modifier) )
                    {
                        commands[i].func();
                        break;
                    }
            }
        }
        usleep( 200 * 1000 );
    }
}

static int
x_err_handler (Display *d, XErrorEvent *evt) {
    char buffer[1024];
    XGetErrorText(d, evt->error_code, buffer, sizeof (buffer));
    fprintf( stderr, "hotkeys: xlib error: %s\n", buffer);
}

static int
hotkeys_start (void) {
    finished = 0;
    loop_tid = 0;
    disp = XOpenDisplay( NULL );
    if ( !disp )
    {
        fprintf( stderr, "hotkeys: could not open display\n" );
        return -1;
    }
    XSetErrorHandler( x_err_handler );

    read_config( disp );
    int i;
    // need to grab it here to prevent gdk_x_error from being called while we're
    // doing it on other thread
    for (i = 0; i < command_count; i++) {
        XGrabKey (disp, commands[i].keycode, commands[i].modifier, DefaultRootWindow (disp), False, GrabModeAsync, GrabModeAsync);
    }
    XSync (disp, 0);
    if (command_count > 0) {
        loop_tid = deadbeef->thread_start( hotkeys_event_loop, 0 );
    }
    else {
        cleanup();
    }
}

static int
hotkeys_stop (void) {
    if (loop_tid) {
        finished = 1;
        deadbeef->thread_join( loop_tid );
        cleanup();
    }
}

// define plugin interface
static DB_misc_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.type = DB_PLUGIN_MISC,
    .plugin.name = "Global hotkeys support",
    .plugin.descr = "Allows to control player with global hotkeys",
    .plugin.author = "Viktor Semykin",
    .plugin.email = "thesame.ml@gmail.com",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = hotkeys_start,
    .plugin.stop = hotkeys_stop
};