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