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