summaryrefslogtreecommitdiff
path: root/source/in_lirc.c
blob: b88c8bd4f12b93961e70f08cab76301f9097fe7f (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
/*
Copyright (C) 2010 skuller.net

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "com_local.h"
#include "key_public.h"
#include "in_public.h"
#include "cl_public.h"
#include "sys_public.h"
#include "io_sleep.h"
#include "in_lirc.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 ) {
            key = Key_StringToKeynum( str );
            if( key > 0 ) {
               Key_Event( key, qtrue, time );
               Key_Event( key, qfalse, time );
            }
        }
        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 );
    IO_Remove( 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 ) {
    lirc_enable = Cvar_Get( "lirc_enable", "0", 0 );
    lirc_enable->changed = lirc_param_changed;
    lirc_config = Cvar_Get( "lirc_config", "", 0 );
    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;
    }
 
    fcntl( lirc.fd, F_SETFL, fcntl( lirc.fd, F_GETFL, 0 ) | FNDELAY );
    lirc.io = IO_Add( lirc.fd );
    lirc.io->wantread = qtrue;

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

    return qtrue;
}