]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
Use var_Inherit* instead of var_CreateGet*.
[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 #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, 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
76     int i_fd;
77 };
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static void Run( intf_thread_t * );
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     char *psz_file;
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_intf->pf_run = Run;
101
102     p_sys->i_fd = lirc_init( "vlc", 1 );
103     if( p_sys->i_fd == -1 )
104     {
105         msg_Err( p_intf, "lirc initialisation failed" );
106         goto exit;
107     }
108
109     /* We want polling */
110     fcntl( p_sys->i_fd, F_SETFL, fcntl( p_sys->i_fd, F_GETFL ) | O_NONBLOCK );
111
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 )
115     {
116         msg_Err( p_intf, "failure while reading lirc config" );
117         free( psz_file );
118         goto exit;
119     }
120     free( psz_file );
121
122     return VLC_SUCCESS;
123
124 exit:
125     if( p_sys->i_fd != -1 )
126         lirc_deinit();
127     free( p_sys );
128     return VLC_EGENERIC;
129 }
130
131 /*****************************************************************************
132  * Close: destroy interface
133  *****************************************************************************/
134 static void Close( vlc_object_t *p_this )
135 {
136     intf_thread_t *p_intf = (intf_thread_t *)p_this;
137     intf_sys_t *p_sys = p_intf->p_sys;
138
139     /* Destroy structure */
140     lirc_freeconfig( p_sys->config );
141     lirc_deinit();
142     free( p_sys );
143 }
144
145 /*****************************************************************************
146  * Run: main loop
147  *****************************************************************************/
148 static void Run( intf_thread_t *p_intf )
149 {
150     intf_sys_t *p_sys = p_intf->p_sys;
151
152     for( ;; )
153     {
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;
158
159         /* Process */
160         int canc = vlc_savecancel();
161         Process( p_intf );
162         vlc_restorecancel(canc);
163     }
164 }
165
166 static void Process( intf_thread_t *p_intf )
167 {
168     for( ;; )
169     {
170         char *code, *c;
171         if( lirc_nextcode( &code ) )
172             return;
173
174         if( code == NULL )
175             return;
176
177         while( vlc_object_alive( p_intf )
178                 && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
179                 && (c != NULL) )
180         {
181             if( !strncmp( "key-", c, 4 ) )
182             {
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 );
188             }
189             else if( !strncmp( "menu ", c, 5)  )
190             {
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
208                 {
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;
212                 }
213             }
214             else
215             {
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;
220             }
221         }
222         free( code );
223     }
224 }