Contrib: upnp/win32, remove strerror use, and other small hacks...
[vlc/asuraparaju-public.git] / modules / control / lirc.c
blob5688c97ab106c2e64038f3d05faf42d113b62ff3
1 /*****************************************************************************
2 * lirc.c : lirc module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2005 the VideoLAN team
5 * $Id$
7 * Author: Sigmund Augdal Helberg <dnumgis@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #include <fcntl.h>
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
37 #include <vlc_osd.h>
38 #include <vlc_keys.h>
40 #ifdef HAVE_POLL
41 # include <poll.h>
42 #endif
44 #include <lirc/lirc_client.h>
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." )
51 /*****************************************************************************
52 * Module descriptor
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
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 )
65 add_string( "lirc-file", NULL, NULL,
66 LIRC_TEXT, LIRC_LONGTEXT, true )
67 vlc_module_end ()
69 /*****************************************************************************
70 * intf_sys_t: description and status of FB interface
71 *****************************************************************************/
72 struct intf_sys_t
74 struct lirc_config *config;
76 int i_fd;
79 /*****************************************************************************
80 * Local prototypes
81 *****************************************************************************/
82 static void Run( intf_thread_t * );
84 static void Process( intf_thread_t * );
86 /*****************************************************************************
87 * Open: initialize interface
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t *)p_this;
92 intf_sys_t *p_sys;
93 char *psz_file;
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;
100 p_intf->pf_run = Run;
102 p_sys->i_fd = lirc_init( "vlc", 1 );
103 if( p_sys->i_fd == -1 )
105 msg_Err( p_intf, "lirc initialisation failed" );
106 goto exit;
109 /* We want polling */
110 fcntl( p_sys->i_fd, F_SETFL, fcntl( p_sys->i_fd, F_GETFL ) | O_NONBLOCK );
112 /* Read the configuration file */
113 psz_file = var_CreateGetNonEmptyString( p_intf, "lirc-file" );
114 if( lirc_readconfig( psz_file, &p_sys->config, NULL ) != 0 )
116 msg_Err( p_intf, "failure while reading lirc config" );
117 free( psz_file );
118 goto exit;
120 free( psz_file );
122 return VLC_SUCCESS;
124 exit:
125 if( p_sys->i_fd != -1 )
126 lirc_deinit();
127 free( p_sys );
128 return VLC_EGENERIC;
131 /*****************************************************************************
132 * Close: destroy interface
133 *****************************************************************************/
134 static void Close( vlc_object_t *p_this )
136 intf_thread_t *p_intf = (intf_thread_t *)p_this;
137 intf_sys_t *p_sys = p_intf->p_sys;
139 /* Destroy structure */
140 lirc_freeconfig( p_sys->config );
141 lirc_deinit();
142 free( p_sys );
145 /*****************************************************************************
146 * Run: main loop
147 *****************************************************************************/
148 static void Run( intf_thread_t *p_intf )
150 intf_sys_t *p_sys = p_intf->p_sys;
152 for( ;; )
154 /* Wait for data */
155 struct pollfd ufd = { .fd = p_sys->i_fd, .events = POLLIN, .revents = 0 };
156 if( poll( &ufd, 1, -1 ) == -1 )
157 break;
159 /* Process */
160 int canc = vlc_savecancel();
161 Process( p_intf );
162 vlc_restorecancel(canc);
166 static void Process( intf_thread_t *p_intf )
168 for( ;; )
170 char *code, *c;
171 if( lirc_nextcode( &code ) )
172 return;
174 if( code == NULL )
175 return;
177 while( vlc_object_alive( p_intf )
178 && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
179 && (c != NULL) )
181 if( !strncmp( "key-", c, 4 ) )
183 vlc_key_t i_key = vlc_GetActionId( c );
184 if( i_key )
185 var_SetInteger( p_intf->p_libvlc, "key-action", i_key );
186 else
187 msg_Err( p_intf, "Unknown hotkey '%s'", c );
189 else if( !strncmp( "menu ", c, 5) )
191 if( !strncmp( c, "menu on", 7 ) ||
192 !strncmp( c, "menu show", 9 ))
193 osd_MenuShow( VLC_OBJECT(p_intf) );
194 else if( !strncmp( c, "menu off", 8 ) ||
195 !strncmp( c, "menu hide", 9 ) )
196 osd_MenuHide( VLC_OBJECT(p_intf) );
197 else if( !strncmp( c, "menu up", 7 ) )
198 osd_MenuUp( VLC_OBJECT(p_intf) );
199 else if( !strncmp( c, "menu down", 9 ) )
200 osd_MenuDown( VLC_OBJECT(p_intf) );
201 else if( !strncmp( c, "menu left", 9 ) )
202 osd_MenuPrev( VLC_OBJECT(p_intf) );
203 else if( !strncmp( c, "menu right", 10 ) )
204 osd_MenuNext( VLC_OBJECT(p_intf) );
205 else if( !strncmp( c, "menu select", 11 ) )
206 osd_MenuActivate( VLC_OBJECT(p_intf) );
207 else
209 msg_Err( p_intf, "Please provide one of the following parameters:" );
210 msg_Err( p_intf, "[on|off|up|down|left|right|select]" );
211 break;
214 else
216 msg_Err( p_intf, "this doesn't appear to be a valid keycombo "
217 "lirc sent us. Please look at the "
218 "doc/lirc/example.lirc file in VLC" );
219 break;
222 free( code );