]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
1951c1751a495d5e5dcba6e129621ffd53e65b5b
[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 );
102         return 1;
103     }
104
105     /* We want polling */
106     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
107
108     if( lirc_readconfig( p_intf->p_sys->psz_file, &p_intf->p_sys->config, NULL ) != 0 )
109     {
110         msg_Err( p_intf, "failure while reading lirc config" );
111         lirc_deinit();
112         free( p_intf->p_sys );
113         return 1;
114     }
115
116     return 0;
117 }
118
119 /*****************************************************************************
120  * Close: destroy interface
121  *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
123 {
124     intf_thread_t *p_intf = (intf_thread_t *)p_this;
125
126     /* Destroy structure */
127     free( p_intf->p_sys->psz_file );
128     lirc_freeconfig( p_intf->p_sys->config );
129     lirc_deinit();
130     free( p_intf->p_sys );
131 }
132
133 /*****************************************************************************
134  * Run: main loop
135  *****************************************************************************/
136 static void Run( intf_thread_t *p_intf )
137 {
138     char *code, *c;
139
140     while( !intf_ShouldDie( p_intf ) )
141     {
142         /* Sleep a bit */
143         msleep( INTF_IDLE_SLEEP );
144
145         /* We poll the lircsocket */
146         if( lirc_nextcode(&code) != 0 )
147         {
148             break;
149         }
150
151         if( code == NULL )
152         {
153             continue;
154         }
155
156         while( !intf_ShouldDie( p_intf )
157                 && (lirc_code2char( p_intf->p_sys->config, code, &c ) == 0)
158                 && (c != NULL) )
159         {
160             vlc_value_t keyval;
161
162             if( !strncmp( "key-", c, 4 ) )
163             {
164                 keyval.i_int = config_GetInt( p_intf, c );
165                 var_Set( p_intf->p_libvlc, "key-pressed", keyval );
166             }
167             else if( !strncmp( "menu ", c, 5)  )
168             {
169                 if( !strncmp( c, "menu on", 7 ) ||
170                     !strncmp( c, "menu show", 9 ))
171                     osd_MenuShow( VLC_OBJECT(p_intf) );
172                 else if( !strncmp( c, "menu off", 8 ) ||
173                          !strncmp( c, "menu hide", 9 ) )
174                     osd_MenuHide( VLC_OBJECT(p_intf) );
175                 else if( !strncmp( c, "menu up", 7 ) )
176                     osd_MenuUp( VLC_OBJECT(p_intf) );
177                 else if( !strncmp( c, "menu down", 9 ) )
178                     osd_MenuDown( VLC_OBJECT(p_intf) );
179                 else if( !strncmp( c, "menu left", 9 ) )
180                     osd_MenuPrev( VLC_OBJECT(p_intf) );
181                 else if( !strncmp( c, "menu right", 10 ) )
182                     osd_MenuNext( VLC_OBJECT(p_intf) );
183                 else if( !strncmp( c, "menu select", 11 ) )
184                     osd_MenuActivate( VLC_OBJECT(p_intf) );
185                 else
186                 {
187                     msg_Err( p_intf, _("Please provide one of the following parameters:") );
188                     msg_Err( p_intf, "[on|off|up|down|left|right|select]" );
189                     break;
190                 }
191             }
192             else
193             {
194                 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" );
195                 break;
196             }
197         }
198         free( code );
199     }
200 }