Added support for volume up/down buttons.
[vlc.git] / modules / control / lirc / lirc.c
1 /*****************************************************************************
2  * lirc.c : lirc plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: lirc.c,v 1.4 2003/02/16 10:25:57 sigmunau Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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/intf.h>
34 #include <vlc/vout.h>
35 #include <vlc/aout.h>
36
37 #include <lirc/lirc_client.h>
38
39 /*****************************************************************************
40  * intf_sys_t: description and status of FB interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
44     struct lirc_config *config;
45     vlc_mutex_t         change_lock;
46
47     input_thread_t *    p_input;
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_description( _("infrared remote control module") );
62     set_capability( "interface", 0 );
63     set_callbacks( Open, Close );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Open: initialize interface
68  *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
70 {
71     intf_thread_t *p_intf = (intf_thread_t *)p_this;
72     int i_fd;
73
74     /* Allocate instance and initialize some members */
75     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
76     if( p_intf->p_sys == NULL )
77     {
78         msg_Err( p_intf, "out of memory" );
79         return 1;
80     }
81
82     p_intf->pf_run = Run;
83
84     i_fd = lirc_init( "vlc", 1 );
85     if( i_fd == -1 )
86     {
87         msg_Err( p_intf, "lirc_init failed" );
88         free( p_intf->p_sys );
89         return 1;
90     }
91
92     /* We want polling */
93     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
94
95     if( lirc_readconfig( NULL, &p_intf->p_sys->config, NULL ) != 0 )
96     {
97         msg_Err( p_intf, "lirc_readconfig failed" );
98         lirc_deinit();
99         free( p_intf->p_sys );
100         return 1;
101     }
102
103     p_intf->p_sys->p_input = NULL;
104
105     return 0;
106 }
107
108 /*****************************************************************************
109  * Close: destroy interface
110  *****************************************************************************/
111 static void Close( vlc_object_t *p_this )
112 {
113     intf_thread_t *p_intf = (intf_thread_t *)p_this;
114
115     if( p_intf->p_sys->p_input )
116     {
117         vlc_object_release( p_intf->p_sys->p_input );
118     }
119
120     /* Destroy structure */
121     lirc_freeconfig( p_intf->p_sys->config );
122     lirc_deinit();
123     free( p_intf->p_sys );
124 }
125
126 /*****************************************************************************
127  * Run: main loop
128  *****************************************************************************/
129 static void Run( intf_thread_t *p_intf )
130 {
131     char *code, *c;
132     playlist_t *p_playlist;
133     input_thread_t *p_input;
134
135     while( !p_intf->b_die )
136     {
137         /* Sleep a bit */
138         msleep( INTF_IDLE_SLEEP );
139
140         /* Update the input */
141         if( p_intf->p_sys->p_input == NULL )
142         {
143             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
144                                                               FIND_ANYWHERE );
145         }
146         else if( p_intf->p_sys->p_input->b_dead )
147         {
148             vlc_object_release( p_intf->p_sys->p_input );
149             p_intf->p_sys->p_input = NULL;
150         }
151         p_input = p_intf->p_sys->p_input;
152
153         /* We poll the lircsocket */
154         if( lirc_nextcode(&code) != 0 )
155         {
156             break;
157         }
158
159         if( code == NULL )
160         {
161             continue;
162         }
163
164         while( !p_intf->b_die 
165                 && lirc_code2char( p_intf->p_sys->config, code, &c ) == 0
166                 && c != NULL )
167         {
168             if( !strcmp( c, "QUIT" ) )
169             {
170                 p_intf->p_vlc->b_die = VLC_TRUE;
171                 continue;
172             }
173             if( !strcmp( c, "VOL_UP" ) )
174             {
175                 aout_VolumeUp( p_intf, 1, NULL );
176             }
177             if( !strcmp( c, "VOL_DOWN" ) )
178             {
179                 aout_VolumeDown( p_intf, 1, NULL );
180             }
181             if( !strcmp( c, "FULLSCREEN" ) )
182             {
183                 vout_thread_t *p_vout;
184                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
185                                           VLC_OBJECT_VOUT, FIND_CHILD );
186                 if( p_vout )
187                 {
188                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
189                     vlc_object_release( p_vout );
190                 }
191                 continue;
192             }
193             if( !strcmp( c, "ACTIVATE" ) )
194             {
195                 vout_thread_t *p_vout;
196                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
197                                           VLC_OBJECT_VOUT, FIND_CHILD );
198                 if( p_vout )
199                 {
200                     vlc_value_t val;
201                     val.psz_string = "ENTER";
202                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
203                     {
204                         msg_Warn( p_intf, "key-press failed" );
205                     }
206                     vlc_object_release( p_vout );
207                 }
208                 continue;
209             }
210
211             if( !strcmp( c, "LEFT" ) )
212             {
213                 vout_thread_t *p_vout;
214                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
215                                           VLC_OBJECT_VOUT, FIND_CHILD );
216                 if( p_vout )
217                 {
218                     vlc_value_t val;
219                     val.psz_string = "LEFT";
220                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
221                     {
222                         msg_Warn( p_intf, "key-press failed" );
223                     }
224                     vlc_object_release( p_vout );
225                 }
226                 continue;
227             }
228
229             if( !strcmp( c, "RIGHT" ) )
230             {
231                 vout_thread_t *p_vout;
232                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
233                                           VLC_OBJECT_VOUT, FIND_CHILD );
234                 if( p_vout )
235                 {
236                     vlc_value_t val;
237                     val.psz_string = "RIGHT";
238                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
239                     {
240                         msg_Warn( p_intf, "key-press failed" );
241                     }
242                     vlc_object_release( p_vout );
243                 }
244                 continue;
245             }
246
247             if( !strcmp( c, "UP" ) )
248             {
249                 vout_thread_t *p_vout;
250                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
251                                           VLC_OBJECT_VOUT, FIND_CHILD );
252                 if( p_vout )
253                 {
254                     vlc_value_t val;
255                     val.psz_string = "UP";
256                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
257                     {
258                         msg_Warn( p_intf, "key-press failed" );
259                     }
260                     vlc_object_release( p_vout );
261                 }
262                 continue;
263             }
264
265             if( !strcmp( c, "DOWN" ) )
266             {
267                 vout_thread_t *p_vout;
268                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
269                                           VLC_OBJECT_VOUT, FIND_CHILD );
270                 if( p_vout )
271                 {
272                     vlc_value_t val;
273                     val.psz_string = "DOWN";
274                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
275                     {
276                         msg_Warn( p_intf, "key-press failed" );
277                     }
278                     vlc_object_release( p_vout );
279                 }
280                 continue;
281             }
282
283             if( !strcmp( c, "PLAY" ) )
284             {
285                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
286                                                       FIND_ANYWHERE );
287                 if( p_playlist )
288                 {
289                     vlc_mutex_lock( &p_playlist->object_lock );
290                     if( p_playlist->i_size )
291                     {
292                         vlc_mutex_unlock( &p_playlist->object_lock );
293                         playlist_Play( p_playlist );
294                         vlc_object_release( p_playlist );
295                     }
296                 }
297             }
298             if( !strcmp( c, "PLAYPAUSE" ) )
299             {
300                 if( p_input &&
301                     p_input->stream.control.i_status != PAUSE_S )
302                 {
303                     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
304                 }
305                 else
306                 {
307                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
308                                                   FIND_ANYWHERE );
309                     if( p_playlist )
310                     {
311                         vlc_mutex_lock( &p_playlist->object_lock );
312                         if( p_playlist->i_size )
313                         {
314                             vlc_mutex_unlock( &p_playlist->object_lock );
315                             playlist_Play( p_playlist );
316                             vlc_object_release( p_playlist );
317                         }
318                     }
319                 }                    
320             }                
321             else if( p_input )
322             {
323                 if( !strcmp( c, "PAUSE" ) )
324                 {
325                     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
326                 }
327                 else if( !strcmp( c, "NEXT" ) )
328                 {
329                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
330                                                           FIND_ANYWHERE );
331                     if( p_playlist )
332                     {
333                         playlist_Next( p_playlist );
334                         vlc_object_release( p_playlist );
335                     }
336                 }
337                 else if( !strcmp( c, "PREV" ) )
338                 {
339                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
340                                                           FIND_ANYWHERE );
341                     if( p_playlist )
342                     {
343                         playlist_Prev( p_playlist );
344                         vlc_object_release( p_playlist );
345                     }
346                 }
347                 else if( !strcmp( c, "STOP" ) )
348                 {
349                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
350                                                           FIND_ANYWHERE );
351                     if( p_playlist )
352                     {
353                         playlist_Stop( p_playlist );
354                         vlc_object_release( p_playlist );
355                     }
356                 }
357                 else if( !strcmp( c, "FAST" ) )
358                 {
359                     input_SetStatus( p_input, INPUT_STATUS_FASTER );
360                 }
361                 else if( !strcmp( c, "SLOW" ) )
362                 {
363                     input_SetStatus( p_input, INPUT_STATUS_SLOWER );
364                 }
365             }
366         }
367
368         free( code );
369     }
370 }
371