]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
Potential memleak.
[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
142     while( !intf_ShouldDie( p_intf ) )
143     {
144         /* Sleep a bit */
145         msleep( INTF_IDLE_SLEEP );
146
147         /* We poll the lircsocket */
148         if( lirc_nextcode(&code) != 0 )
149         {
150             break;
151         }
152
153         if( code == NULL )
154         {
155             continue;
156         }
157
158         while( !intf_ShouldDie( p_intf )
159                 && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
160                 && (c != NULL) )
161         {
162             vlc_value_t keyval;
163
164             if( !strncmp( "key-", c, 4 ) )
165             {
166                 keyval.i_int = config_GetInt( p_intf, c );
167                 var_Set( p_intf->p_libvlc, "key-pressed", keyval );
168             }
169             else if( !strncmp( "menu ", c, 5)  )
170             {
171                 if( !strncmp( c, "menu on", 7 ) ||
172                     !strncmp( c, "menu show", 9 ))
173                     osd_MenuShow( VLC_OBJECT(p_intf) );
174                 else if( !strncmp( c, "menu off", 8 ) ||
175                          !strncmp( c, "menu hide", 9 ) )
176                     osd_MenuHide( VLC_OBJECT(p_intf) );
177                 else if( !strncmp( c, "menu up", 7 ) )
178                     osd_MenuUp( VLC_OBJECT(p_intf) );
179                 else if( !strncmp( c, "menu down", 9 ) )
180                     osd_MenuDown( VLC_OBJECT(p_intf) );
181                 else if( !strncmp( c, "menu left", 9 ) )
182                     osd_MenuPrev( VLC_OBJECT(p_intf) );
183                 else if( !strncmp( c, "menu right", 10 ) )
184                     osd_MenuNext( VLC_OBJECT(p_intf) );
185                 else if( !strncmp( c, "menu select", 11 ) )
186                     osd_MenuActivate( VLC_OBJECT(p_intf) );
187                 else
188                 {
189                     msg_Err( p_intf, _("Please provide one of the following parameters:") );
190                     msg_Err( p_intf, "[on|off|up|down|left|right|select]" );
191                     break;
192                 }
193             }
194             else
195             {
196                 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" );
197                 break;
198             }
199         }
200         free( code );
201     }
202 }