]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
7605510cb63ba7f08bee77582dbf37424921119a
[vlc] / modules / control / lirc.c
1 /*****************************************************************************
2  * lirc.c : lirc module for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Author: 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 #include <osd.h>
37
38 #include <lirc/lirc_client.h>
39
40 /*****************************************************************************
41  * intf_sys_t: description and status of FB interface
42  *****************************************************************************/
43 struct intf_sys_t
44 {
45     struct lirc_config *config;
46     vlc_mutex_t         change_lock;
47
48     input_thread_t *    p_input;
49     vout_thread_t *     p_vout;
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57 static void Run     ( intf_thread_t * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
63     set_description( _("Infrared remote control interface") );
64     set_capability( "interface", 0 );
65     set_callbacks( Open, Close );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * Open: initialize interface
70  *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
72 {
73     intf_thread_t *p_intf = (intf_thread_t *)p_this;
74     int i_fd;
75
76     /* Allocate instance and initialize some members */
77     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
78     if( p_intf->p_sys == NULL )
79     {
80         msg_Err( p_intf, "out of memory" );
81         return 1;
82     }
83
84     p_intf->pf_run = Run;
85
86     i_fd = lirc_init( "vlc", 1 );
87     if( i_fd == -1 )
88     {
89         msg_Err( p_intf, "lirc_init failed" );
90         free( p_intf->p_sys );
91         return 1;
92     }
93
94     /* We want polling */
95     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
96
97     if( lirc_readconfig( NULL, &p_intf->p_sys->config, NULL ) != 0 )
98     {
99         msg_Err( p_intf, "lirc_readconfig failed" );
100         lirc_deinit();
101         free( p_intf->p_sys );
102         return 1;
103     }
104
105     p_intf->p_sys->p_input = NULL;
106
107     return 0;
108 }
109
110 /*****************************************************************************
111  * Close: destroy interface
112  *****************************************************************************/
113 static void Close( vlc_object_t *p_this )
114 {
115     intf_thread_t *p_intf = (intf_thread_t *)p_this;
116
117     if( p_intf->p_sys->p_input )
118     {
119         vlc_object_release( p_intf->p_sys->p_input );
120     }
121     if( p_intf->p_sys->p_vout )
122     {
123         vlc_object_release( p_intf->p_sys->p_vout );
124     }
125     /* Destroy structure */
126     lirc_freeconfig( p_intf->p_sys->config );
127     lirc_deinit();
128     free( p_intf->p_sys );
129 }
130
131 /*****************************************************************************
132  * Run: main loop
133  *****************************************************************************/
134 static void Run( intf_thread_t *p_intf )
135 {
136     char *code, *c;
137     playlist_t *p_playlist;
138     input_thread_t *p_input;
139     vout_thread_t *p_vout = NULL;
140
141     while( !p_intf->b_die )
142     {
143         /* Sleep a bit */
144         msleep( INTF_IDLE_SLEEP );
145
146         /* Update the input */
147         if( p_intf->p_sys->p_input == NULL )
148         {
149             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
150                                                               FIND_ANYWHERE );
151         }
152         else if( p_intf->p_sys->p_input->b_dead )
153         {
154             vlc_object_release( p_intf->p_sys->p_input );
155             p_intf->p_sys->p_input = NULL;
156         }
157         p_input = p_intf->p_sys->p_input;
158
159         /* Update the vout */
160         if( p_vout == NULL )
161         {
162             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
163                                       FIND_ANYWHERE );
164             p_intf->p_sys->p_vout = p_vout;
165         }
166         else if( p_vout->b_die )
167         {
168             vlc_object_release( p_vout );
169             p_vout = NULL;
170             p_intf->p_sys->p_vout = NULL;
171         }
172
173         /* We poll the lircsocket */
174         if( lirc_nextcode(&code) != 0 )
175         {
176             break;
177         }
178
179         if( code == NULL )
180         {
181             continue;
182         }
183
184         while( !p_intf->b_die
185                 && lirc_code2char( p_intf->p_sys->config, code, &c ) == 0
186                 && c != NULL )
187         {
188
189             if( !strcmp( c, "QUIT" ) )
190             {
191                 p_intf->p_vlc->b_die = VLC_TRUE;
192                 vout_OSDMessage( p_intf, _("Quit" ) );
193                 continue;
194             }
195             else if( !strcmp( c, "VOL_UP" ) )
196             {
197                 audio_volume_t i_newvol;
198                 aout_VolumeUp( p_intf, 1, &i_newvol );
199                 vout_OSDMessage( p_intf, _("Vol %%%d"),
200                                  i_newvol * 100 / AOUT_VOLUME_MAX );
201             }
202             else if( !strcmp( c, "VOL_DOWN" ) )
203             {
204                 audio_volume_t i_newvol;
205                 aout_VolumeDown( p_intf, 1, &i_newvol );
206                 vout_OSDMessage( p_intf, _("Vol %%%d"),
207                                  i_newvol * 100 / AOUT_VOLUME_MAX );
208             }
209             else if( !strcmp( c, "MUTE" ) )
210             {
211                 audio_volume_t i_newvol = -1;
212                 aout_VolumeMute( p_intf, &i_newvol );
213                 if( i_newvol == 0 )
214                 {
215                     vout_OSDMessage( p_intf, _( "Mute" ) );
216                 }
217                 else
218                 {
219                     vout_OSDMessage( p_intf, _("Vol %d%%"),
220                                      i_newvol * 100 / AOUT_VOLUME_MAX );
221                 }
222             }
223             if( p_vout )
224             {
225                 if( !strcmp( c, "FULLSCREEN" ) )
226                 {
227                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
228                     continue;
229                 }
230                 if( !strcmp( c, "ACTIVATE" ) )
231                 {
232                     vlc_value_t val;
233                     val.psz_string = "ENTER";
234                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
235                     {
236                         msg_Warn( p_intf, "key-press failed" );
237                     }
238                     continue;
239                 }
240
241                 if( !strcmp( c, "LEFT" ) )
242                 {
243                     vlc_value_t val;
244                     val.psz_string = "LEFT";
245                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
246                     {
247                         msg_Warn( p_intf, "key-press failed" );
248                     }
249                     continue;
250                 }
251
252                 if( !strcmp( c, "RIGHT" ) )
253                 {
254                     vlc_value_t val;
255                     val.psz_string = "RIGHT";
256                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
257                     {
258                         msg_Warn( p_intf, "key-press failed" );
259                     }
260                     continue;
261                 }
262
263                 if( !strcmp( c, "UP" ) )
264                 {
265                     vlc_value_t val;
266                     val.psz_string = "UP";
267                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
268                     {
269                         msg_Warn( p_intf, "key-press failed" );
270                     }
271                     continue;
272                 }
273
274                 if( !strcmp( c, "DOWN" ) )
275                 {
276                     vlc_value_t val;
277                     val.psz_string = "DOWN";
278                     if (var_Set( p_vout, "key-pressed", val ) != VLC_SUCCESS)
279                     {
280                         msg_Warn( p_intf, "key-press failed" );
281                     }
282                     continue;
283                 }
284             }
285
286             if( !strcmp( c, "PLAY" ) )
287             {
288                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
289                                               FIND_ANYWHERE );
290                 if( p_playlist )
291                 {
292                     playlist_Play( p_playlist );
293                     vlc_object_release( p_playlist );
294                 }
295                 continue;
296             }
297
298             if( !strcmp( c, "PLAYPAUSE" ) )
299             {
300                 vlc_value_t val;
301                 val.i_int = PLAYING_S;
302                 if( p_input )
303                 {
304                     var_Get( p_input, "state", &val );
305                 }
306                 if( p_input && val.i_int != PAUSE_S )
307                 {
308                     vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
309                     val.i_int = PAUSE_S;
310                     var_Set( p_input, "state", val );
311                 }
312                 else
313                 {
314                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
315                                                   FIND_ANYWHERE );
316                     if( p_playlist )
317                     {
318                         vlc_mutex_lock( &p_playlist->object_lock );
319                         if( p_playlist->i_size )
320                         {
321                             vlc_mutex_unlock( &p_playlist->object_lock );
322                             vout_OSDMessage( p_intf, _( "Play" ) );
323                             playlist_Play( p_playlist );
324                             vlc_object_release( p_playlist );
325                         }
326                     }
327                 }
328                 continue;
329             }
330
331             else if( p_input )
332             {
333                 if( !strcmp( c, "AUDIO_TRACK" ) )
334                 {
335                     vlc_value_t val, list, list2;
336                     int i_count, i;
337                     var_Get( p_input, "audio-es", &val );
338                     var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
339                                 &list, &list2 );
340                     i_count = list.p_list->i_count;
341                     for( i = 0; i < i_count; i++ )
342                     {
343                         if( val.i_int == list.p_list->p_values[i].i_int )
344                         {
345                             break;
346                         }
347                     }
348                     /* value of audio-es was not in choices list */
349                     if( i == i_count )
350                     {
351                         msg_Warn( p_input,
352                                   "invalid current audio track, selecting 0" );
353                         var_Set( p_input, "audio-es",
354                                  list.p_list->p_values[0] );
355                         i = 0;
356                     }
357                     else if( i == i_count - 1 )
358                     {
359                         var_Set( p_input, "audio-es",
360                                  list.p_list->p_values[0] );
361                         i = 0;
362                     }
363                     else
364                     {
365                         var_Set( p_input, "audio-es",
366                                  list.p_list->p_values[i+1] );
367                         i++;
368                     }
369                     vout_OSDMessage( VLC_OBJECT(p_input), _("Audio track: %s"),
370                                      list2.p_list->p_values[i].psz_string );
371                 }
372                 else if( !strcmp( c, "SUBTITLE_TRACK" ) )
373                 {
374                     vlc_value_t val, list, list2;
375                     int i_count, i;
376                     var_Get( p_input, "spu-es", &val );
377                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
378                                 &list, &list2 );
379                     i_count = list.p_list->i_count;
380                     for( i = 0; i < i_count; i++ )
381                     {
382                         if( val.i_int == list.p_list->p_values[i].i_int )
383                         {
384                             break;
385                         }
386                     }
387                     /* value of audio-es was not in choices list */
388                     if( i == i_count )
389                     {
390                         msg_Warn( p_input, "invalid current subtitle track, selecting 0" );
391                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
392                         i = 0;
393                     }
394                     else if( i == i_count - 1 )
395                     {
396                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
397                         i = 0;
398                     }
399                     else
400                     {
401                         var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
402                         i = i + 1;
403                     }
404                     vout_OSDMessage( VLC_OBJECT(p_input), _("Subtitle track: %s"), list2.p_list->p_values[i].psz_string );
405                 }
406                 else if( !strcmp( c, "PAUSE" ) )
407                 {
408                     vlc_value_t val;
409                     vout_OSDMessage( p_intf, _( "Pause" ) );
410                     val.i_int = PAUSE_S;
411                     var_Set( p_input, "state", val );
412                 }
413                 else if( !strcmp( c, "NEXT" ) )
414                 {
415                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
416                                                           FIND_ANYWHERE );
417                     if( p_playlist )
418                     {
419                         playlist_Next( p_playlist );
420                         vlc_object_release( p_playlist );
421                     }
422                 }
423                 else if( !strcmp( c, "PREV" ) )
424                 {
425                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
426                                                           FIND_ANYWHERE );
427                     if( p_playlist )
428                     {
429                         playlist_Prev( p_playlist );
430                         vlc_object_release( p_playlist );
431                     }
432                 }
433                 else if( !strcmp( c, "STOP" ) )
434                 {
435                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
436                                                           FIND_ANYWHERE );
437                     if( p_playlist )
438                     {
439                         playlist_Stop( p_playlist );
440                         vlc_object_release( p_playlist );
441                     }
442                 }
443                 else if( !strcmp( c, "FAST" ) )
444                 {
445                     vlc_value_t val; val.b_bool = VLC_TRUE;
446                     var_Set( p_input, "rate-faster", val );
447                 }
448                 else if( !strcmp( c, "SLOW" ) )
449                 {
450                     vlc_value_t val; val.b_bool = VLC_TRUE;
451                     var_Set( p_input, "rate-slower", val );
452                 }
453 /* beginning of modifications by stephane Thu Jun 19 15:29:49 CEST 2003 */
454                 else if ( !strcmp(c, "CHAPTER_N" ) ||
455                           !strcmp( c, "CHAPTER_P" ) )
456                 {
457                     unsigned int i_chapter = 0;
458
459                     if( !strcmp( c, "CHAPTER_N" ) )
460                     {
461                         vlc_mutex_lock( &p_input->stream.stream_lock );
462                         i_chapter = p_input->stream.p_selected_area->i_part + 1;
463                         vlc_mutex_unlock( &p_input->stream.stream_lock );
464                     }
465                     else if( !strcmp( c, "CHAPTER_P" ) )
466                     {
467                         vlc_mutex_lock( &p_input->stream.stream_lock );
468                         i_chapter = p_input->stream.p_selected_area->i_part - 1;
469                         vlc_mutex_unlock( &p_input->stream.stream_lock );
470                     }
471
472                     vlc_mutex_lock( &p_input->stream.stream_lock );
473                     if( ( i_chapter > 0 ) && ( i_chapter <
474                                                p_input->stream.p_selected_area->i_part_nb ) )
475                     {
476                         input_area_t *p_area = p_input->stream.p_selected_area;
477                         p_input->stream.p_selected_area->i_part = i_chapter;
478                         vlc_mutex_unlock( &p_input->stream.stream_lock );
479                         input_ChangeArea( p_input, p_area );
480                         input_SetStatus( p_input, INPUT_STATUS_PLAY );
481                         vlc_mutex_lock( &p_input->stream.stream_lock );
482                     }
483                     vlc_mutex_unlock( &p_input->stream.stream_lock );
484                 }
485 /* end of modification by stephane Thu Jun 19 15:29:49 CEST 2003 */
486             }
487         }
488
489         free( code );
490     }
491 }