lirc.c 7.55 KB
Newer Older
1
/*****************************************************************************
Anil Daoud's avatar
Anil Daoud committed
2
 * lirc.c : lirc module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2003-2005 the VideoLAN team
5
 * $Id$
6
 *
7
 * Author: Sigmund Augdal Helberg <dnumgis@videolan.org>
8
9
10
11
12
 *
 * 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.
13
 *
14
15
16
17
18
19
20
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

Pierre Ynard's avatar
Pierre Ynard committed
28
#include <errno.h>
29
30
#include <fcntl.h>

31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
37
#include <vlc_interface.h>
38
#include <vlc_osd.h>
dionoea's avatar
dionoea committed
39
#include <vlc_keys.h>
40

41
42
43
44
#ifdef HAVE_POLL
# include <poll.h>
#endif

45
46
#include <lirc/lirc_client.h>

47
#define LIRC_TEXT N_("Change the lirc configuration file")
48
49
50
51
#define LIRC_LONGTEXT N_( \
    "Tell lirc to read this configuration file. By default it " \
    "searches in the users home directory." )

52
/*****************************************************************************
53
 * Module descriptor
54
55
56
57
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );

58
59
60
61
62
63
64
vlc_module_begin ()
    set_shortname( N_("Infrared") )
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
    set_description( N_("Infrared remote control interface") )
    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
65

66
    add_string( "lirc-file", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
67
                LIRC_TEXT, LIRC_LONGTEXT, true )
68
vlc_module_end ()
69

70
71
72
73
74
75
/*****************************************************************************
 * intf_sys_t: description and status of FB interface
 *****************************************************************************/
struct intf_sys_t
{
    struct lirc_config *config;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
    vlc_thread_t thread;
77
78
79
80
81
82
    int i_fd;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
83
static void *Run( void * );
84

ivoire's avatar
ivoire committed
85
static void Process( intf_thread_t * );
86

87
88
89
90
91
92
/*****************************************************************************
 * Open: initialize interface
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
93
    intf_sys_t *p_sys;
94
95

    /* Allocate instance and initialize some members */
96
97
98
    p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_sys == NULL )
        return VLC_ENOMEM;
99

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
100
    p_intf->pf_run = NULL;
101

102
103
    p_sys->i_fd = lirc_init( "vlc", 1 );
    if( p_sys->i_fd == -1 )
104
    {
105
        msg_Err( p_intf, "lirc initialisation failed" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
106
        goto error;
107
108
109
    }

    /* We want polling */
110
    fcntl( p_sys->i_fd, F_SETFL, fcntl( p_sys->i_fd, F_GETFL ) | O_NONBLOCK );
111

112
    /* Read the configuration file */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113
114
115
116
    char *psz_file = var_InheritString( p_intf, "lirc-file" );
    int val = lirc_readconfig( psz_file, &p_sys->config, NULL );
    free( psz_file );
    if( val != 0 )
117
    {
118
        msg_Err( p_intf, "failure while reading lirc config" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
119
120
121
122
123
124
125
126
127
        lirc_deinit();
        goto error;
    }

    if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
    {
        lirc_freeconfig( p_sys->config );
        lirc_deinit();
        goto error;
128
129
    }

130
131
    return VLC_SUCCESS;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
132
error:
133
134
    free( p_sys );
    return VLC_EGENERIC;
135
136
137
138
139
140
141
142
}

/*****************************************************************************
 * Close: destroy interface
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
143
    intf_sys_t *p_sys = p_intf->p_sys;
144

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
145
146
147
    vlc_cancel( p_sys->thread );
    vlc_join( p_sys->thread, NULL );

148
    /* Destroy structure */
149
    lirc_freeconfig( p_sys->config );
150
    lirc_deinit();
151
    free( p_sys );
152
153
154
155
156
}

/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
157
static void *Run( void *data )
158
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
159
    intf_thread_t *p_intf = data;
160
    intf_sys_t *p_sys = p_intf->p_sys;
161

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
162
163
164
165
    struct pollfd ufd;
    ufd.fd = p_sys->i_fd;
    ufd.events = POLLIN;

166
    for( ;; )
167
    {
168
169
        /* Wait for data */
        if( poll( &ufd, 1, -1 ) == -1 )
Pierre Ynard's avatar
Pierre Ynard committed
170
        {
171
172
            if( errno == EINTR )
                continue;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
173
            break;
Pierre Ynard's avatar
Pierre Ynard committed
174
        }
175
176
177
178

        /* Process */
        int canc = vlc_savecancel();
        Process( p_intf );
179
        vlc_restorecancel(canc);
180
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
181
    return NULL;
182
}
183

ivoire's avatar
ivoire committed
184
static void Process( intf_thread_t *p_intf )
185
186
187
188
{
    for( ;; )
    {
        char *code, *c;
ivoire's avatar
ivoire committed
189
190
        if( lirc_nextcode( &code ) )
            return;
191
192

        if( code == NULL )
ivoire's avatar
ivoire committed
193
            return;
194

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
        while( vlc_object_alive( p_intf )
196
197
                && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
                && (c != NULL) )
198
        {
199
200
            if( !strncmp( "key-", c, 4 ) )
            {
201
                vlc_action_t i_key = vlc_GetActionId( c );
dionoea's avatar
dionoea committed
202
203
204
                if( i_key )
                    var_SetInteger( p_intf->p_libvlc, "key-action", i_key );
                else
205
                    msg_Err( p_intf, "Unknown hotkey '%s'", c );
206
207
208
209
210
211
212
213
214
215
216
217
218
            }
            else if( !strncmp( "menu ", c, 5)  )
            {
                if( !strncmp( c, "menu on", 7 ) ||
                    !strncmp( c, "menu show", 9 ))
                    osd_MenuShow( VLC_OBJECT(p_intf) );
                else if( !strncmp( c, "menu off", 8 ) ||
                         !strncmp( c, "menu hide", 9 ) )
                    osd_MenuHide( VLC_OBJECT(p_intf) );
                else if( !strncmp( c, "menu up", 7 ) )
                    osd_MenuUp( VLC_OBJECT(p_intf) );
                else if( !strncmp( c, "menu down", 9 ) )
                    osd_MenuDown( VLC_OBJECT(p_intf) );
219
                else if( !strncmp( c, "menu left", 9 ) )
220
221
222
223
224
225
226
                    osd_MenuPrev( VLC_OBJECT(p_intf) );
                else if( !strncmp( c, "menu right", 10 ) )
                    osd_MenuNext( VLC_OBJECT(p_intf) );
                else if( !strncmp( c, "menu select", 11 ) )
                    osd_MenuActivate( VLC_OBJECT(p_intf) );
                else
                {
227
                    msg_Err( p_intf, "Please provide one of the following parameters:" );
228
229
230
231
232
                    msg_Err( p_intf, "[on|off|up|down|left|right|select]" );
                    break;
                }
            }
            else
233
            {
ivoire's avatar
ivoire committed
234
235
236
                msg_Err( p_intf, "this doesn't appear to be a valid keycombo "
                                 "lirc sent us. Please look at the "
                                 "doc/lirc/example.lirc file in VLC" );
237
                break;
238
239
240
241
242
            }
        }
        free( code );
    }
}