]> git.sesse.net Git - vlc/blob - modules/control/lirc.c
Copyright fixes
[vlc] / modules / control / lirc.c
1 /*****************************************************************************
2  * lirc.c : lirc module for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2005 VideoLAN (Centrale Réseaux) and its contributors
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
35 #include <lirc/lirc_client.h>
36
37 /*****************************************************************************
38  * intf_sys_t: description and status of FB interface
39  *****************************************************************************/
40 struct intf_sys_t
41 {
42     struct lirc_config *config;
43 };
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  Open    ( vlc_object_t * );
49 static void Close   ( vlc_object_t * );
50 static void Run     ( intf_thread_t * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 vlc_module_begin();
56     set_category( CAT_INTERFACE );
57     set_subcategory( SUBCAT_INTERFACE_CONTROL );
58     set_description( _("Infrared remote control interface") );
59     set_capability( "interface", 0 );
60     set_callbacks( Open, Close );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Open: initialize interface
65  *****************************************************************************/
66 static int Open( vlc_object_t *p_this )
67 {
68     intf_thread_t *p_intf = (intf_thread_t *)p_this;
69     int i_fd;
70
71     /* Allocate instance and initialize some members */
72     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
73     if( p_intf->p_sys == NULL )
74     {
75         msg_Err( p_intf, "out of memory" );
76         return 1;
77     }
78
79     p_intf->pf_run = Run;
80
81     i_fd = lirc_init( "vlc", 1 );
82     if( i_fd == -1 )
83     {
84         msg_Err( p_intf, "lirc_init failed" );
85         free( p_intf->p_sys );
86         return 1;
87     }
88
89     /* We want polling */
90     fcntl( i_fd, F_SETFL, fcntl( i_fd, F_GETFL ) | O_NONBLOCK );
91
92     if( lirc_readconfig( NULL, &p_intf->p_sys->config, NULL ) != 0 )
93     {
94         msg_Err( p_intf, "lirc_readconfig failed" );
95         lirc_deinit();
96         free( p_intf->p_sys );
97         return 1;
98     }
99
100     return 0;
101 }
102
103 /*****************************************************************************
104  * Close: destroy interface
105  *****************************************************************************/
106 static void Close( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109
110     /* Destroy structure */
111     lirc_freeconfig( p_intf->p_sys->config );
112     lirc_deinit();
113     free( p_intf->p_sys );
114 }
115
116 /*****************************************************************************
117  * Run: main loop
118  *****************************************************************************/
119 static void Run( intf_thread_t *p_intf )
120 {
121     char *code, *c;
122
123     while( !p_intf->b_die )
124     {
125         /* Sleep a bit */
126         msleep( INTF_IDLE_SLEEP );
127
128         /* We poll the lircsocket */
129         if( lirc_nextcode(&code) != 0 )
130         {
131             break;
132         }
133
134         if( code == NULL )
135         {
136             continue;
137         }
138
139         while( !p_intf->b_die
140                 && lirc_code2char( p_intf->p_sys->config, code, &c ) == 0
141                 && c != NULL )
142         {
143             vlc_value_t keyval;
144
145             if( strncmp( "key-", c, 4 ) )
146             {
147                 msg_Err( p_intf, "This doesn't appear to be a valid keycombo lirc sent us. Please look at the doc/lirc/example.lirc file in VLC" );
148                 break;
149             }
150             keyval.i_int = config_GetInt( p_intf, c );
151             var_Set( p_intf->p_vlc, "key-pressed", keyval );
152         }
153         free( code );
154     }
155 }
156