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