* ./debian/rules: moved the gtk_main and gnome_main plugins to the gvlc
[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.1 2002/08/04 17:23:42 sam 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
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     vlc_mutex_t         change_lock;
45
46     input_thread_t *    p_input;
47 };
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Open    ( vlc_object_t * );
53 static void Close   ( vlc_object_t * );
54 static void Run     ( intf_thread_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     set_description( _("infrared remote control module") );
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_init 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, "lirc_readconfig failed" );
97         lirc_deinit();
98         free( p_intf->p_sys );
99         return 1;
100     }
101
102     p_intf->p_sys->p_input = NULL;
103
104     return 0;
105 }
106
107 /*****************************************************************************
108  * Close: destroy interface
109  *****************************************************************************/
110 static void Close( vlc_object_t *p_this )
111 {
112     intf_thread_t *p_intf = (intf_thread_t *)p_this;
113
114     if( p_intf->p_sys->p_input )
115     {
116         vlc_object_release( p_intf->p_sys->p_input );
117     }
118
119     /* Destroy structure */
120     lirc_freeconfig( p_intf->p_sys->config );
121     lirc_deinit();
122     free( p_intf->p_sys );
123 }
124
125 /*****************************************************************************
126  * Run: main loop
127  *****************************************************************************/
128 static void Run( intf_thread_t *p_intf )
129 {
130     char *code, *c;
131     playlist_t *p_playlist;
132     input_thread_t *p_input;
133
134     while( !p_intf->b_die )
135     {
136         /* Sleep a bit */
137         msleep( INTF_IDLE_SLEEP );
138
139         /* Update the input */
140         if( p_intf->p_sys->p_input == NULL )
141         {
142             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
143                                                               FIND_ANYWHERE );
144         }
145         else if( p_intf->p_sys->p_input->b_dead )
146         {
147             vlc_object_release( p_intf->p_sys->p_input );
148             p_intf->p_sys->p_input = NULL;
149         }
150
151         /* We poll the lircsocket */
152         if( lirc_nextcode(&code) != 0 )
153         {
154             break;
155         }
156
157         if( code == NULL )
158         {
159             continue;
160         }
161
162         while( !p_intf->b_die 
163                 && lirc_code2char( p_intf->p_sys->config, code, &c ) == 0
164                 && c != NULL )
165         {
166             if( !strcmp( c, "QUIT" ) )
167             {
168                 p_intf->p_vlc->b_die = VLC_TRUE;
169                 continue;
170             }
171
172             if( !strcmp( c, "FULLSCREEN" ) )
173             {
174                 vout_thread_t *p_vout;
175                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
176                                           VLC_OBJECT_VOUT, FIND_CHILD );
177                 if( p_vout )
178                 {
179                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
180                     vlc_object_release( p_vout );
181                 }
182                 continue;
183             }
184
185             if( !strcmp( c, "PLAY" ) )
186             {
187                 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
188                                                       FIND_ANYWHERE );
189                 if( p_playlist )
190                 {
191                     vlc_mutex_lock( &p_playlist->object_lock );
192                     if( p_playlist->i_size )
193                     {
194                         vlc_mutex_unlock( &p_playlist->object_lock );
195                         playlist_Play( p_playlist );
196                         vlc_object_release( p_playlist );
197                     }
198                 }
199             }
200             else if( p_intf->p_sys->p_input )
201             {
202                 p_input = p_intf->p_sys->p_input;
203
204                 if( !strcmp( c, "PAUSE" ) )
205                 {
206                     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
207                 }
208                 else if( !strcmp( c, "NEXT" ) )
209                 {
210                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
211                                                           FIND_ANYWHERE );
212                     if( p_playlist )
213                     {
214                         playlist_Next( p_playlist );
215                         vlc_object_release( p_playlist );
216                     }
217                 }
218                 else if( !strcmp( c, "PREV" ) )
219                 {
220                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
221                                                           FIND_ANYWHERE );
222                     if( p_playlist )
223                     {
224                         playlist_Prev( p_playlist );
225                         vlc_object_release( p_playlist );
226                     }
227                 }
228                 else if( !strcmp( c, "STOP" ) )
229                 {
230                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
231                                                           FIND_ANYWHERE );
232                     if( p_playlist )
233                     {
234                         playlist_Stop( p_playlist );
235                         vlc_object_release( p_playlist );
236                     }
237                 }
238                 else if( !strcmp( c, "FAST" ) )
239                 {
240                     input_SetStatus( p_input, INPUT_STATUS_FASTER );
241                 }
242                 else if( !strcmp( c, "SLOW" ) )
243                 {
244                     input_SetStatus( p_input, INPUT_STATUS_SLOWER );
245                 }
246             }
247         }
248
249         free( code );
250     }
251 }
252