summaryrefslogtreecommitdiff
path: root/src/unix/lirc.c
blob: a68118dbd1cd2471c25212818a89f437f5a82644 (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
/*
Copyright (C) 2010 Andrey Nazarov

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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "shared/shared.h"
#include "common/cmd.h"
#include "common/common.h"
#include "common/cvar.h"
#include "common/net/net.h"
#include "client/input.h"
#include "client/keys.h"
#include "system/lirc.h"
#include "system/system.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

#include <lirc/lirc_client.h>

static cvar_t   *lirc_enable;
static cvar_t   *lirc_config;

static struct {
    qboolean initialized;
    struct lirc_config *config;
    int fd;
    ioentry_t *io;
} lirc;

void Lirc_GetEvents(void)
{
    char *code, *str;
    int ret, key;
    unsigned time;

    if (!lirc.initialized || !lirc.io->canread) {
        return;
    }

    while ((ret = lirc_nextcode(&code)) == 0 && code) {
        time = Sys_Milliseconds();
        Com_DPrintf("%s: %u %s\n", __func__, time, code);
        while ((ret = lirc_code2char(lirc.config, code, &str)) == 0 && str) {
            if (*str == '@') {
                key = Key_StringToKeynum(str + 1);
                if (key > 0) {
                    Key_Event(key, qtrue, time);
                    Key_Event(key, qfalse, time);
                }
            } else {
                Cbuf_AddText(&cmd_buffer, str);
                Cbuf_AddText(&cmd_buffer, "\n");
            }
        }
        free(code);
        if (ret) {
            goto error;
        }
    }

    if (ret) {
error:
        Com_EPrintf("Error reading from LIRC.\n");
        Cvar_Reset(lirc_enable);
        Lirc_Shutdown();
    }
}

void Lirc_Shutdown(void)
{
    if (!lirc.initialized) {
        return;
    }
    lirc_freeconfig(lirc.config);
    NET_RemoveFd(lirc.fd);
    lirc_deinit();
    memset(&lirc, 0, sizeof(lirc));
}

static void lirc_param_changed(cvar_t *self)
{
    Lirc_Shutdown();
    Lirc_Init();
}

qboolean Lirc_Init(void)
{
    int ret;

    lirc_enable = Cvar_Get("lirc_enable", "0", 0);
    lirc_enable->changed = lirc_param_changed;
    lirc_config = Cvar_Get("lirc_config", "", CVAR_NOSET);
    //lirc_config->changed = lirc_param_changed;

    if (!lirc_enable->integer) {
        return qfalse;
    }

    lirc.fd = lirc_init(APPLICATION, 0);
    if (lirc.fd == -1) {
        Com_EPrintf("Failed to initialize LIRC.\n");
        Cvar_Reset(lirc_enable);
        return qfalse;
    }

    if (lirc_readconfig(lirc_config->string[0] ? lirc_config->string : NULL,
                        &lirc.config, NULL) != 0) {
        Com_EPrintf("Failed to read LIRC config.\n");
        lirc_deinit();
        Cvar_Reset(lirc_enable);
        return qfalse;
    }

    // change it to non-blocking
    ret = fcntl(lirc.fd, F_GETFL, 0);
    if (!(ret & O_NONBLOCK))
        fcntl(lirc.fd, F_SETFL, ret | O_NONBLOCK);

    lirc.io = NET_AddFd(lirc.fd);
    lirc.io->wantread = qtrue;

    Com_Printf("LIRC interface initialized.\n");
    lirc.initialized = qtrue;

    return qtrue;
}