summaryrefslogtreecommitdiff
path: root/callbacks.c
blob: c0b6dced90e092c870bd8ba972ab2ec9ebca2054 (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
#include <stdio.h>
#include <stdlib.h>

#include "mplayer_server.h"
#include "request.h"

#define CB(_name) int callback_##_name(const byte *buffer __attribute__((unused)), int size __attribute__((unused)))

CB(load_url);
CB(pause);
CB(quit);
CB(snd_down);
CB(snd_up);
CB(fullscreen);
CB(mute);

extern FILE *stream_g;

static callback_t callbacks_g[CALLBACK_COUNT] = {NULL};

int callbacks_init(void)
{
    callbacks_g[CALLBACK_LOAD_URL]      = callback_load_url;
    callbacks_g[CALLBACK_PAUSE]         = callback_pause;
    callbacks_g[CALLBACK_QUIT]          = callback_quit;
    callbacks_g[CALLBACK_SND_DOWN]      = callback_snd_down;
    callbacks_g[CALLBACK_SND_UP]        = callback_snd_up;
    callbacks_g[CALLBACK_FULLSCREEN]    = callback_fullscreen;
    callbacks_g[CALLBACK_MUTE]          = callback_mute;

    return 0;
}

/*
 * returns a new malloced() null terminated escaped string
 * if the string is considered dangerous, then the function returns NULL
 */

char *real_escape_string(const byte *buf, int size);

void *get_assoc_cb(int opcode)
{
    if (opcode >= 0 && opcode < CALLBACK_COUNT) {
        return callbacks_g[opcode];
    }

    return NULL;
}

#define SEND_CMD(_fmt, ...) \
    do { \
	_log(_fmt, ##__VA_ARGS__); \
	fprintf(stream_g, _fmt "\n", ##__VA_ARGS__); \
    } while (0)

/* TODO refuse the execution of commands with arguments when they do not need */
CB(load_url)
{
    /* XXX beware, someone may perform arbitrary code injection */
    char *escaped = real_escape_string(buffer, size);

    if (escaped == NULL) {
	return -1;
    }

    SEND_CMD("loadfile '%s'", escaped);

    free(escaped);
    return 0;
}

CB(pause)
{
    SEND_CMD("pause");
    return 0;
}

CB(quit)
{
    SEND_CMD("quit");
    return 0;
}

CB(snd_down)
{
    SEND_CMD("volume -5");
    return 0;
}

CB(snd_up)
{
    SEND_CMD("volume +5");
    return 0;
}

CB(fullscreen)
{
    SEND_CMD("vo_fullscreen");
    return 0;
}

CB(mute)
{
    SEND_CMD("mute");
    return 0;
}