]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
* lirc: don't use input_ChangeArea.
[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, SOLO_CHAN, _("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, SOLO_CHAN, _("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, SOLO_CHAN, _("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, SOLO_CHAN, _( "Mute" ) );
216                 }
217                 else
218                 {
219                     vout_OSDMessage( p_intf, SOLO_CHAN, _("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), SOLO_CHAN,
309                                      _( "Pause" ) );
310                     val.i_int = PAUSE_S;
311                     var_Set( p_input, "state", val );
312                 }
313                 else
314                 {
315                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
316                                                   FIND_ANYWHERE );
317                     if( p_playlist )
318                     {
319                         vlc_mutex_lock( &p_playlist->object_lock );
320                         if( p_playlist->i_size )
321                         {
322                             vlc_mutex_unlock( &p_playlist->object_lock );
323                             vout_OSDMessage( p_intf, SOLO_CHAN, _( "Play" ) );
324                             playlist_Play( p_playlist );
325                             vlc_object_release( p_playlist );
326                         }
327                     }
328                 }
329                 continue;
330             }
331
332             else if( p_input )
333             {
334                 if( !strcmp( c, "AUDIO_TRACK" ) )
335                 {
336                     vlc_value_t val, list, list2;
337                     int i_count, i;
338                     var_Get( p_input, "audio-es", &val );
339                     var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
340                                 &list, &list2 );
341                     i_count = list.p_list->i_count;
342                     for( i = 0; i < i_count; i++ )
343                     {
344                         if( val.i_int == list.p_list->p_values[i].i_int )
345                         {
346                             break;
347                         }
348                     }
349                     /* value of audio-es was not in choices list */
350                     if( i == i_count )
351                     {
352                         msg_Warn( p_input,
353                                   "invalid current audio track, selecting 0" );
354                         var_Set( p_input, "audio-es",
355                                  list.p_list->p_values[0] );
356                         i = 0;
357                     }
358                     else if( i == i_count - 1 )
359                     {
360                         var_Set( p_input, "audio-es",
361                                  list.p_list->p_values[0] );
362                         i = 0;
363                     }
364                     else
365                     {
366                         var_Set( p_input, "audio-es",
367                                  list.p_list->p_values[i+1] );
368                         i++;
369                     }
370                     vout_OSDMessage( VLC_OBJECT(p_input), SOLO_CHAN,
371                                      _("Audio track: %s"),
372                                      list2.p_list->p_values[i].psz_string );
373                 }
374                 else if( !strcmp( c, "SUBTITLE_TRACK" ) )
375                 {
376                     vlc_value_t val, list, list2;
377                     int i_count, i;
378                     var_Get( p_input, "spu-es", &val );
379                     var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
380                                 &list, &list2 );
381                     i_count = list.p_list->i_count;
382                     for( i = 0; i < i_count; i++ )
383                     {
384                         if( val.i_int == list.p_list->p_values[i].i_int )
385                         {
386                             break;
387                         }
388                     }
389                     /* value of audio-es was not in choices list */
390                     if( i == i_count )
391                     {
392                         msg_Warn( p_input, "invalid current subtitle track, selecting 0" );
393                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
394                         i = 0;
395                     }
396                     else if( i == i_count - 1 )
397                     {
398                         var_Set( p_input, "spu-es", list.p_list->p_values[0] );
399                         i = 0;
400                     }
401                     else
402                     {
403                         var_Set( p_input, "spu-es", list.p_list->p_values[i+1] );
404                         i = i + 1;
405                     }
406                     vout_OSDMessage( VLC_OBJECT(p_input), SOLO_CHAN,
407                                      _("Subtitle track: %s"),
408                                      list2.p_list->p_values[i].psz_string );
409                 }
410                 else if( !strcmp( c, "PAUSE" ) )
411                 {
412                     vlc_value_t val;
413                     vout_OSDMessage( p_intf, SOLO_CHAN, _( "Pause" ) );
414                     val.i_int = PAUSE_S;
415                     var_Set( p_input, "state", val );
416                 }
417                 else if( !strcmp( c, "NEXT" ) )
418                 {
419                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
420                                                           FIND_ANYWHERE );
421                     if( p_playlist )
422                     {
423                         playlist_Next( p_playlist );
424                         vlc_object_release( p_playlist );
425                     }
426                 }
427                 else if( !strcmp( c, "PREV" ) )
428                 {
429                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
430                                                           FIND_ANYWHERE );
431                     if( p_playlist )
432                     {
433                         playlist_Prev( p_playlist );
434                         vlc_object_release( p_playlist );
435                     }
436                 }
437                 else if( !strcmp( c, "STOP" ) )
438                 {
439                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
440                                                           FIND_ANYWHERE );
441                     if( p_playlist )
442                     {
443                         playlist_Stop( p_playlist );
444                         vlc_object_release( p_playlist );
445                     }
446                 }
447                 else if( !strcmp( c, "FAST" ) )
448                 {
449                     vlc_value_t val; val.b_bool = VLC_TRUE;
450                     var_Set( p_input, "rate-faster", val );
451                 }
452                 else if( !strcmp( c, "SLOW" ) )
453                 {
454                     vlc_value_t val; val.b_bool = VLC_TRUE;
455                     var_Set( p_input, "rate-slower", val );
456                 }
457 /* beginning of modifications by stephane Thu Jun 19 15:29:49 CEST 2003 */
458                 else if ( !strcmp(c, "CHAPTER_N" ) ||
459                           !strcmp( c, "CHAPTER_P" ) )
460                 {
461                     unsigned int i_chapter = 0;
462
463                     if( !strcmp( c, "CHAPTER_N" ) )
464                     {
465                         var_SetVoid( p_input, "next-chapter" );
466                     }
467                     else if( !strcmp( c, "CHAPTER_P" ) )
468                     {
469                         var_SetVoid( p_input, "prev-chapter" );
470                     }
471                 }
472 /* end of modification by stephane Thu Jun 19 15:29:49 CEST 2003 */
473             }
474         }
475
476         free( code );
477     }
478 }