]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
94841071398aea3b5dbdcd66589bfad41938f37c
[vlc] / modules / control / lirc.c
1 /*****************************************************************************
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  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <errno.h>
29 #include <fcntl.h>
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_keys.h>
39
40 #ifdef HAVE_POLL
41 # include <poll.h>
42 #endif
43
44 #include <lirc/lirc_client.h>
45
46 #define LIRC_TEXT N_("Change the lirc configuration file")
47 #define LIRC_LONGTEXT N_( \
48     "Tell lirc to read this configuration file. By default it " \
49     "searches in the users home directory." )
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open    ( vlc_object_t * );
55 static void Close   ( vlc_object_t * );
56
57 vlc_module_begin ()
58     set_shortname( N_("Infrared") )
59     set_category( CAT_INTERFACE )
60     set_subcategory( SUBCAT_INTERFACE_CONTROL )
61     set_description( N_("Infrared remote control interface") )
62     set_capability( "interface", 0 )
63     set_callbacks( Open, Close )
64
65     add_string( "lirc-file", NULL,
66                 LIRC_TEXT, LIRC_LONGTEXT, true )
67 vlc_module_end ()
68
69 /*****************************************************************************
70  * intf_sys_t: description and status of FB interface
71  *****************************************************************************/
72 struct intf_sys_t
73 {
74     struct lirc_config *config;
75     vlc_thread_t thread;
76     int i_fd;
77 };
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static void *Run( void * );
83
84 static void Process( intf_thread_t * );
85
86 /*****************************************************************************
87  * Open: initialize interface
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t *)p_this;
92     intf_sys_t *p_sys;
93
94     /* Allocate instance and initialize some members */
95     p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
96     if( p_sys == NULL )
97         return VLC_ENOMEM;
98
99     p_sys->i_fd = lirc_init( "vlc", 1 );
100     if( p_sys->i_fd == -1 )
101     {
102         msg_Err( p_intf, "lirc initialisation failed" );
103         goto error;
104     }
105
106     /* We want polling */
107     fcntl( p_sys->i_fd, F_SETFL, fcntl( p_sys->i_fd, F_GETFL ) | O_NONBLOCK );
108
109     /* Read the configuration file */
110     char *psz_file = var_InheritString( p_intf, "lirc-file" );
111     int val = lirc_readconfig( psz_file, &p_sys->config, NULL );
112     free( psz_file );
113     if( val != 0 )
114     {
115         msg_Err( p_intf, "failure while reading lirc config" );
116         lirc_deinit();
117         goto error;
118     }
119
120     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
121     {
122         lirc_freeconfig( p_sys->config );
123         lirc_deinit();
124         goto error;
125     }
126
127     return VLC_SUCCESS;
128
129 error:
130     free( p_sys );
131     return VLC_EGENERIC;
132 }
133
134 /*****************************************************************************
135  * Close: destroy interface
136  *****************************************************************************/
137 static void Close( vlc_object_t *p_this )
138 {
139     intf_thread_t *p_intf = (intf_thread_t *)p_this;
140     intf_sys_t *p_sys = p_intf->p_sys;
141
142     vlc_cancel( p_sys->thread );
143     vlc_join( p_sys->thread, NULL );
144
145     /* Destroy structure */
146     lirc_freeconfig( p_sys->config );
147     lirc_deinit();
148     free( p_sys );
149 }
150
151 /*****************************************************************************
152  * Run: main loop
153  *****************************************************************************/
154 static void *Run( void *data )
155 {
156     intf_thread_t *p_intf = data;
157     intf_sys_t *p_sys = p_intf->p_sys;
158
159     struct pollfd ufd;
160     ufd.fd = p_sys->i_fd;
161     ufd.events = POLLIN;
162
163     for( ;; )
164     {
165         /* Wait for data */
166         if( poll( &ufd, 1, -1 ) == -1 )
167         {
168             if( errno == EINTR )
169                 continue;
170             break;
171         }
172
173         /* Process */
174         int canc = vlc_savecancel();
175         Process( p_intf );
176         vlc_restorecancel(canc);
177     }
178     return NULL;
179 }
180
181 static void Process( intf_thread_t *p_intf )
182 {
183     for( ;; )
184     {
185         char *code, *c;
186         if( lirc_nextcode( &code ) )
187             return;
188
189         if( code == NULL )
190             return;
191
192         while( (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
193                 && (c != NULL) )
194         {
195             if( !strncmp( "key-", c, 4 ) )
196             {
197                 vlc_action_t i_key = vlc_GetActionId( c );
198                 if( i_key )
199                     var_SetInteger( p_intf->p_libvlc, "key-action", i_key );
200                 else
201                     msg_Err( p_intf, "Unknown hotkey '%s'", c );
202             }
203             else
204             {
205                 msg_Err( p_intf, "this doesn't appear to be a valid keycombo "
206                                  "lirc sent us. Please look at the "
207                                  "doc/lirc/example.lirc file in VLC" );
208                 break;
209             }
210         }
211         free( code );
212     }
213 }