]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
vlc_plugin: fix non-LGPL plugins meta infos
[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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_interface.h>
39 #include <vlc_keys.h>
40
41 #ifdef HAVE_POLL
42 # include <poll.h>
43 #endif
44
45 #include <lirc/lirc_client.h>
46
47 #define LIRC_TEXT N_("Change the lirc configuration file")
48 #define LIRC_LONGTEXT N_( \
49     "Tell lirc to read this configuration file. By default it " \
50     "searches in the users home directory." )
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57
58 vlc_module_begin ()
59     set_shortname( N_("Infrared") )
60     set_category( CAT_INTERFACE )
61     set_subcategory( SUBCAT_INTERFACE_CONTROL )
62     set_description( N_("Infrared remote control interface") )
63     set_capability( "interface", 0 )
64     set_callbacks( Open, Close )
65
66     add_string( "lirc-file", NULL,
67                 LIRC_TEXT, LIRC_LONGTEXT, true )
68 vlc_module_end ()
69
70 /*****************************************************************************
71  * intf_sys_t: description and status of FB interface
72  *****************************************************************************/
73 struct intf_sys_t
74 {
75     struct lirc_config *config;
76     vlc_thread_t thread;
77     int i_fd;
78 };
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static void *Run( void * );
84
85 static void Process( intf_thread_t * );
86
87 /*****************************************************************************
88  * Open: initialize interface
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     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     p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
97     if( p_sys == NULL )
98         return VLC_ENOMEM;
99
100     p_sys->i_fd = lirc_init( "vlc", 1 );
101     if( p_sys->i_fd == -1 )
102     {
103         msg_Err( p_intf, "lirc initialisation failed" );
104         goto error;
105     }
106
107     /* We want polling */
108     fcntl( p_sys->i_fd, F_SETFL, fcntl( p_sys->i_fd, F_GETFL ) | O_NONBLOCK );
109
110     /* Read the configuration file */
111     char *psz_file = var_InheritString( p_intf, "lirc-file" );
112     int val = lirc_readconfig( psz_file, &p_sys->config, NULL );
113     free( psz_file );
114     if( val != 0 )
115     {
116         msg_Err( p_intf, "failure while reading lirc config" );
117         lirc_deinit();
118         goto error;
119     }
120
121     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
122     {
123         lirc_freeconfig( p_sys->config );
124         lirc_deinit();
125         goto error;
126     }
127
128     return VLC_SUCCESS;
129
130 error:
131     free( p_sys );
132     return VLC_EGENERIC;
133 }
134
135 /*****************************************************************************
136  * Close: destroy interface
137  *****************************************************************************/
138 static void Close( vlc_object_t *p_this )
139 {
140     intf_thread_t *p_intf = (intf_thread_t *)p_this;
141     intf_sys_t *p_sys = p_intf->p_sys;
142
143     vlc_cancel( p_sys->thread );
144     vlc_join( p_sys->thread, NULL );
145
146     /* Destroy structure */
147     lirc_freeconfig( p_sys->config );
148     lirc_deinit();
149     free( p_sys );
150 }
151
152 /*****************************************************************************
153  * Run: main loop
154  *****************************************************************************/
155 static void *Run( void *data )
156 {
157     intf_thread_t *p_intf = data;
158     intf_sys_t *p_sys = p_intf->p_sys;
159
160     struct pollfd ufd;
161     ufd.fd = p_sys->i_fd;
162     ufd.events = POLLIN;
163
164     for( ;; )
165     {
166         /* Wait for data */
167         if( poll( &ufd, 1, -1 ) == -1 )
168         {
169             if( errno == EINTR )
170                 continue;
171             break;
172         }
173
174         /* Process */
175         int canc = vlc_savecancel();
176         Process( p_intf );
177         vlc_restorecancel(canc);
178     }
179     return NULL;
180 }
181
182 static void Process( intf_thread_t *p_intf )
183 {
184     for( ;; )
185     {
186         char *code, *c;
187         if( lirc_nextcode( &code ) )
188             return;
189
190         if( code == NULL )
191             return;
192
193         while( (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
194                 && (c != NULL) )
195         {
196             if( !strncmp( "key-", c, 4 ) )
197             {
198                 vlc_action_t i_key = vlc_GetActionId( c );
199                 if( i_key )
200                     var_SetInteger( p_intf->p_libvlc, "key-action", i_key );
201                 else
202                     msg_Err( p_intf, "Unknown hotkey '%s'", c );
203             }
204             else
205             {
206                 msg_Err( p_intf, "this doesn't appear to be a valid keycombo "
207                                  "lirc sent us. Please look at the "
208                                  "doc/lirc/example.lirc file in VLC" );
209                 break;
210             }
211         }
212         free( code );
213     }
214 }