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