]> git.sesse.net Git - vlc/blob - plugins/lirc/lirc.c
* ALL: the first libvlc commit.
[vlc] / plugins / lirc / lirc.c
1 /*****************************************************************************
2  * lirc.c : lirc plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: lirc.c,v 1.11 2002/06/01 12:31:59 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_s
42 {
43     struct lirc_config *config;
44     vlc_mutex_t         change_lock;
45 };
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 static void intf_getfunctions( function_list_t * p_function_list );
51
52 static int  intf_Open      ( intf_thread_t *p_intf );
53 static void intf_Close     ( intf_thread_t *p_intf );
54 static void intf_Run       ( intf_thread_t *p_intf );
55
56 /*****************************************************************************
57  * Build configuration tree.
58  *****************************************************************************/
59 MODULE_CONFIG_START
60
61 MODULE_CONFIG_STOP
62
63 MODULE_INIT_START
64     SET_DESCRIPTION( _("infrared remote control module") )
65     ADD_CAPABILITY( INTF, 8 )
66 MODULE_INIT_STOP
67
68 MODULE_ACTIVATE_START
69     intf_getfunctions( &p_module->p_functions->intf );
70 MODULE_ACTIVATE_STOP
71
72 MODULE_DEACTIVATE_START
73 MODULE_DEACTIVATE_STOP
74
75 /*****************************************************************************
76  * Functions exported as capabilities. They are declared as static so that
77  * we don't pollute the namespace too much.
78  *****************************************************************************/
79 static void intf_getfunctions( function_list_t * p_function_list )
80 {
81     p_function_list->functions.intf.pf_open  = intf_Open;
82     p_function_list->functions.intf.pf_close = intf_Close;
83     p_function_list->functions.intf.pf_run   = intf_Run;
84 }
85
86 /*****************************************************************************
87  * intf_Open: initialize dummy interface
88  *****************************************************************************/
89 static int intf_Open( intf_thread_t *p_intf )
90 {
91     int i_fd;
92
93     /* Allocate instance and initialize some members */
94     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
95     if( p_intf->p_sys == NULL )
96     {
97         msg_Err( p_intf, "out of memory" );
98         return 1;
99     }
100
101     i_fd = lirc_init( "vlc", 1 );
102     if( i_fd == -1 )
103     {
104         msg_Err( p_intf, "lirc_init failed" );
105         free( p_intf->p_sys );
106         return 1;
107     }
108
109     /* We want polling */
110     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
111
112     if( lirc_readconfig( NULL, &p_intf->p_sys->config, NULL ) != 0 )
113     {
114         msg_Err( p_intf, "lirc_readconfig failed" );
115         lirc_deinit();
116         free( p_intf->p_sys );
117         return 1;
118     }
119
120     return 0;
121 }
122
123 /*****************************************************************************
124  * intf_Close: destroy dummy interface
125  *****************************************************************************/
126 static void intf_Close( intf_thread_t *p_intf )
127 {
128     /* Destroy structure */
129     lirc_freeconfig( p_intf->p_sys->config );
130     lirc_deinit();
131     free( p_intf->p_sys );
132 }
133
134 /*****************************************************************************
135  * intf_Run: main loop
136  *****************************************************************************/
137 static void intf_Run( intf_thread_t *p_intf )
138 {
139     char *code;
140     char *c;
141
142     /* Manage core vlc functions through the callback */
143     p_intf->pf_manage( p_intf );
144
145     while( !p_intf->p_vlc->b_die )
146     {
147         /* Manage core vlc functions through the callback */
148         p_intf->pf_manage( p_intf );
149         msleep( INTF_IDLE_SLEEP );
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->p_vlc->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 = 1;
169                 continue;
170             }
171
172             if( !strcmp( c, "FULLSCREEN" ) )
173             {
174                 vlc_mutex_lock( &p_intf->p_vlc->p_vout_bank->lock );
175                 /* XXX: only fullscreen the first video output */
176                 if( p_intf->p_vlc->p_vout_bank->i_count )
177                 {
178                     p_intf->p_vlc->p_vout_bank->pp_vout[0]->i_changes
179                         |= VOUT_FULLSCREEN_CHANGE;
180                 }
181                 vlc_mutex_unlock( &p_intf->p_vlc->p_vout_bank->lock );
182                 continue;
183             }
184
185             vlc_mutex_lock( &p_intf->p_vlc->p_input_bank->lock );
186
187             if( !strcmp( c, "PLAY" ) )
188             {
189                 if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
190                 {
191                     input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0],
192                                      INPUT_STATUS_PLAY );
193                     p_intf->p_vlc->p_playlist->b_stopped = 0;
194                 }
195                 else
196                 {
197                     vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
198
199                     if( p_intf->p_vlc->p_playlist->b_stopped 
200                          && p_intf->p_vlc->p_playlist->i_size )
201                     {
202                         vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
203                         intf_PlaylistJumpto( p_intf->p_vlc->p_playlist,
204                                              p_intf->p_vlc->p_playlist->i_index );
205                     }
206                     else
207                     {
208                         vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
209                     }
210                 }
211             }
212             else if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
213             {
214                 if( !strcmp( c, "PAUSE" ) )
215                 {
216                     input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0],
217                                      INPUT_STATUS_PAUSE );
218
219                     vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
220                     p_intf->p_vlc->p_playlist->b_stopped = 0;
221                     vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
222                 }
223                 else if( !strcmp( c, "NEXT" ) )
224                 {
225                     p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
226                 }
227                 else if( !strcmp( c, "LAST" ) )
228                 {
229                     /* FIXME: temporary hack */
230                     intf_PlaylistPrev( p_intf->p_vlc->p_playlist );
231                     intf_PlaylistPrev( p_intf->p_vlc->p_playlist );
232                     p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
233                 }
234                 else if( !strcmp( c, "STOP" ) )
235                 {
236                     /* end playing item */
237                     p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
238     
239                     /* update playlist */
240                     vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
241     
242                     p_intf->p_vlc->p_playlist->i_index--;
243                     p_intf->p_vlc->p_playlist->b_stopped = 1;
244     
245                     vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
246                 }
247                 else if( !strcmp( c, "FAST" ) )
248                 {
249                     input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0],
250                                      INPUT_STATUS_FASTER );
251     
252                     vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
253                     p_intf->p_vlc->p_playlist->b_stopped = 0;
254                     vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
255                 }
256                 else if( !strcmp( c, "SLOW" ) )
257                 {
258                     input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0],
259                                      INPUT_STATUS_SLOWER );
260     
261                     vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
262                     p_intf->p_vlc->p_playlist->b_stopped = 0;
263                     vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
264                 }
265             }
266
267             vlc_mutex_unlock( &p_intf->p_vlc->p_input_bank->lock );
268         }
269
270         free( code );
271     }
272 }
273