]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
Pull cancellation into (most) remaining interfaces
[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 <fcntl.h>
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
37 #include <vlc_osd.h>
38
39 #include <lirc/lirc_client.h>
40
41 #define LIRC_TEXT N_("Change the lirc configuration file.")
42 #define LIRC_LONGTEXT N_( \
43     "Tell lirc to read this configuration file. By default it " \
44     "searches in the users home directory." )
45
46 /*****************************************************************************
47  * intf_sys_t: description and status of FB interface
48  *****************************************************************************/
49 struct intf_sys_t
50 {
51     char *psz_file;
52     struct lirc_config *config;
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open    ( vlc_object_t * );
59 static void Close   ( vlc_object_t * );
60 static void Run     ( intf_thread_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_shortname( N_("Infrared") );
67     set_category( CAT_INTERFACE );
68     set_subcategory( SUBCAT_INTERFACE_CONTROL );
69     set_description( N_("Infrared remote control interface") );
70     set_capability( "interface", 0 );
71     set_callbacks( Open, Close );
72
73     add_string( "lirc-file", NULL, NULL,
74                 LIRC_TEXT, LIRC_LONGTEXT, true );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * Open: initialize interface
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     intf_thread_t *p_intf = (intf_thread_t *)p_this;
83     int i_fd;
84
85     /* Allocate instance and initialize some members */
86     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
87     if( p_intf->p_sys == NULL )
88     {
89         msg_Err( p_intf, "out of memory" );
90         return 1;
91     }
92
93     p_intf->pf_run = Run;
94
95     p_intf->p_sys->psz_file = var_CreateGetNonEmptyString( p_intf, "lirc-file" );
96
97     i_fd = lirc_init( "vlc", 1 );
98     if( i_fd == -1 )
99     {
100         msg_Err( p_intf, "lirc initialisation failed" );
101         free( p_intf->p_sys->psz_file );
102         free( p_intf->p_sys );
103         return 1;
104     }
105
106     /* We want polling */
107     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
108
109     if( lirc_readconfig( p_intf->p_sys->psz_file, &p_intf->p_sys->config, NULL ) != 0 )
110     {
111         msg_Err( p_intf, "failure while reading lirc config" );
112         lirc_deinit();
113         free( p_intf->p_sys->psz_file );
114         free( p_intf->p_sys );
115         return 1;
116     }
117
118     return 0;
119 }
120
121 /*****************************************************************************
122  * Close: destroy interface
123  *****************************************************************************/
124 static void Close( vlc_object_t *p_this )
125 {
126     intf_thread_t *p_intf = (intf_thread_t *)p_this;
127
128     /* Destroy structure */
129     free( p_intf->p_sys->psz_file );
130     lirc_freeconfig( p_intf->p_sys->config );
131     lirc_deinit();
132     free( p_intf->p_sys );
133 }
134
135 /*****************************************************************************
136  * Run: main loop
137  *****************************************************************************/
138 static void Run( intf_thread_t *p_intf )
139 {
140     char *code, *c;
141     int canc = vlc_savecancel();
142
143     for( ;; )
144     {
145         /* Sleep a bit */
146         vlc_restorecancel(canc);
147         msleep( INTF_IDLE_SLEEP );
148         canc = vlc_savecancel();
149
150         /* We poll the lircsocket */
151         if( lirc_nextcode(&code) != 0 )
152         {
153             break;
154         }
155
156         if( code == NULL )
157         {
158             continue;
159         }
160
161         while( !intf_ShouldDie( p_intf )
162                 && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
163                 && (c != NULL) )
164         {
165             vlc_value_t keyval;
166
167             if( !strncmp( "key-", c, 4 ) )
168             {
169                 keyval.i_int = config_GetInt( p_intf, c );
170                 var_Set( p_intf->p_libvlc, "key-pressed", keyval );
171             }
172             else if( !strncmp( "menu ", c, 5)  )
173             {
174                 if( !strncmp( c, "menu on", 7 ) ||
175                     !strncmp( c, "menu show", 9 ))
176                     osd_MenuShow( VLC_OBJECT(p_intf) );
177                 else if( !strncmp( c, "menu off", 8 ) ||
178                          !strncmp( c, "menu hide", 9 ) )
179                     osd_MenuHide( VLC_OBJECT(p_intf) );
180                 else if( !strncmp( c, "menu up", 7 ) )
181                     osd_MenuUp( VLC_OBJECT(p_intf) );
182                 else if( !strncmp( c, "menu down", 9 ) )
183                     osd_MenuDown( VLC_OBJECT(p_intf) );
184                 else if( !strncmp( c, "menu left", 9 ) )
185                     osd_MenuPrev( VLC_OBJECT(p_intf) );
186                 else if( !strncmp( c, "menu right", 10 ) )
187                     osd_MenuNext( VLC_OBJECT(p_intf) );
188                 else if( !strncmp( c, "menu select", 11 ) )
189                     osd_MenuActivate( VLC_OBJECT(p_intf) );
190                 else
191                 {
192                     msg_Err( p_intf, "Please provide one of the following parameters:" );
193                     msg_Err( p_intf, "[on|off|up|down|left|right|select]" );
194                     break;
195                 }
196             }
197             else
198             {
199                 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" );
200                 break;
201             }
202         }
203         free( code );
204     }
205     vlc_restorecancel(canc);
206 }