comparison src/General/lirc/lirc.c @ 0:13389e613d67 trunk

[svn] - initial import of audacious-plugins tree (lots to do)
author nenolod
date Mon, 18 Sep 2006 01:11:49 -0700
parents
children 088092a52fea
comparison
equal deleted inserted replaced
-1:000000000000 0:13389e613d67
1 /* Lirc plugin
2
3 Copyright (C) 2005 Audacious development team
4
5 Copyright (c) 1998-1999 Carl van Schaik (carl@leg.uct.ac.za)
6
7 Copyright (C) 2000 Christoph Bartelmus (xmms@bartelmus.de)
8
9 some code was stolen from:
10 IRman plugin for xmms by Charles Sielski (stray@teklabs.net)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38
39 #include <gtk/gtk.h>
40
41 #include <glib.h>
42 #include <glib/gi18n.h>
43
44 #include "audacious/plugin.h"
45 #include "libaudacious/beepctrl.h"
46
47 #include <lirc/lirc_client.h>
48
49 #include "lirc.h"
50
51 const char *plugin_name="LIRC Plugin";
52
53 GeneralPlugin lirc_plugin = {
54 NULL,
55 NULL,
56 -1,
57 NULL,
58 init,
59 about,
60 NULL,
61 cleanup,
62 };
63
64 GeneralPlugin *get_gplugin_info(void)
65 {
66 lirc_plugin.description = g_strdup_printf(_("LIRC Plugin"));
67 return(&lirc_plugin);
68 }
69
70 int lirc_fd=-1;
71 struct lirc_config *config=NULL;
72 gint tracknr=0;
73 gint mute=0; /* mute flag */
74 gint mute_vol=0; /* holds volume before mute */
75
76 gint input_tag;
77
78 void init(void)
79 {
80 int flags;
81
82 if((lirc_fd=lirc_init("audacious",1))==-1)
83 {
84 fprintf(stderr,_("%s: could not init LIRC support\n"),
85 plugin_name);
86 return;
87 }
88 if(lirc_readconfig(NULL,&config,NULL)==-1)
89 {
90 lirc_deinit();
91 fprintf(stderr,
92 _("%s: could not read LIRC config file\n"
93 "%s: please read the documentation of LIRC\n"
94 "%s: how to create a proper config file\n"),
95 plugin_name,plugin_name,plugin_name);
96 return;
97 }
98 input_tag=gdk_input_add(lirc_fd,GDK_INPUT_READ,
99 lirc_input_callback,NULL);
100 fcntl(lirc_fd,F_SETOWN,getpid());
101 flags=fcntl(lirc_fd,F_GETFL,0);
102 if(flags!=-1)
103 {
104 fcntl(lirc_fd,F_SETFL,flags|O_NONBLOCK);
105 }
106 fflush(stdout);
107 }
108
109 void lirc_input_callback(gpointer data,gint source,
110 GdkInputCondition condition)
111 {
112 char *code;
113 char *c;
114 gint playlist_time,playlist_pos,output_time,v;
115 int ret;
116 char *ptr;
117 gint balance;
118 gboolean show_pl;
119 int n;
120
121 while((ret=lirc_nextcode(&code))==0 && code!=NULL)
122 {
123 while((ret=lirc_code2char(config,code,&c))==0 && c!=NULL)
124 {
125 if(strcasecmp("PLAY",c)==0)
126 {
127 xmms_remote_play(lirc_plugin.xmms_session);
128 }
129 else if(strcasecmp("STOP",c)==0)
130 {
131 xmms_remote_stop(lirc_plugin.xmms_session);
132 }
133 else if(strcasecmp("PAUSE",c)==0)
134 {
135 xmms_remote_pause(lirc_plugin.xmms_session);
136 }
137 else if(strcasecmp("PLAYPAUSE",c) == 0)
138 {
139 if(xmms_remote_is_playing(lirc_plugin.
140 xmms_session))
141 xmms_remote_pause
142 (lirc_plugin.xmms_session);
143 else
144 xmms_remote_play
145 (lirc_plugin.xmms_session);
146 }
147 else if(strncasecmp("NEXT",c,4)==0)
148 {
149 ptr=c+4;
150 while(isspace(*ptr)) ptr++;
151 n=atoi(ptr);
152
153 if(n<=0) n=1;
154 for(;n>0;n--)
155 {
156 xmms_remote_playlist_next
157 (lirc_plugin.xmms_session);
158 }
159 }
160 else if(strncasecmp("PREV",c,4)==0)
161 {
162 ptr=c+4;
163 while(isspace(*ptr)) ptr++;
164 n=atoi(ptr);
165
166 if(n<=0) n=1;
167 for(;n>0;n--)
168 {
169 xmms_remote_playlist_prev
170 (lirc_plugin.xmms_session);
171 }
172 }
173 else if(strcasecmp("SHUFFLE",c)==0)
174 {
175 xmms_remote_toggle_shuffle(lirc_plugin.xmms_session);
176 }
177 else if(strcasecmp("REPEAT",c)==0)
178 {
179 xmms_remote_toggle_repeat(lirc_plugin.xmms_session);
180 }
181 else if(strncasecmp("FWD",c,3)==0)
182 {
183 ptr=c+3;
184 while(isspace(*ptr)) ptr++;
185 n=atoi(ptr)*1000;
186
187 if(n<=0) n=5000;
188 output_time=xmms_remote_get_output_time
189 (lirc_plugin.xmms_session);
190 playlist_pos=xmms_remote_get_playlist_pos
191 (lirc_plugin.xmms_session);
192 playlist_time=xmms_remote_get_playlist_time
193 (lirc_plugin.xmms_session,
194 playlist_pos);
195 if(playlist_time-output_time<n)
196 output_time=playlist_time-n;
197 xmms_remote_jump_to_time
198 (lirc_plugin.xmms_session,
199 output_time+n);
200 }
201 else if(strncasecmp("BWD",c,3)==0)
202 {
203 ptr=c+3;
204 while(isspace(*ptr)) ptr++;
205 n=atoi(ptr)*1000;
206
207 if(n<=0) n=5000;
208 output_time=xmms_remote_get_output_time
209 (lirc_plugin.xmms_session);
210 if(output_time<n)
211 output_time=n;
212 xmms_remote_jump_to_time
213 (lirc_plugin.xmms_session,
214 output_time-n);
215 }
216 else if(strncasecmp("VOL_UP",c,6)==0)
217 {
218 ptr=c+6;
219 while(isspace(*ptr)) ptr++;
220 n=atoi(ptr);
221 if(n<=0) n=5;
222
223 v = xmms_remote_get_main_volume
224 (lirc_plugin.xmms_session);
225 if(v > (100-n)) v=100-n;
226 xmms_remote_set_main_volume
227 (lirc_plugin.xmms_session,v+n);
228 }
229 else if(strncasecmp("VOL_DOWN",c,8)==0)
230 {
231 ptr=c+8;
232 while (isspace(*ptr)) ptr++;
233 n=atoi(ptr);
234 if(n<=0) n=5;
235
236 v = xmms_remote_get_main_volume
237 (lirc_plugin.xmms_session);
238 if(v<n) v=n;
239 xmms_remote_set_main_volume
240 (lirc_plugin.xmms_session,v-n);
241 }
242 else if(strcasecmp("QUIT",c)==0)
243 {
244 xmms_remote_quit(lirc_plugin.xmms_session);
245 }
246 else if(strcasecmp("MUTE",c)==0)
247 {
248 if(mute==0)
249 {
250 mute=1;
251 /* store the master volume so
252 we can restore it on unmute. */
253 mute_vol = xmms_remote_get_main_volume
254 (lirc_plugin.xmms_session);
255 xmms_remote_set_main_volume
256 (lirc_plugin.xmms_session, 0);
257 }
258 else
259 {
260 mute=0;
261 xmms_remote_set_main_volume
262 (lirc_plugin.xmms_session,
263 mute_vol);
264 }
265 }
266 else if(strncasecmp("BAL_LEFT",c,8)==0)
267 {
268 ptr=c+8;
269 while(isspace(*ptr)) ptr++;
270 n=atoi(ptr);
271 if(n<=0) n=5;
272
273 balance=xmms_remote_get_balance
274 (lirc_plugin.xmms_session);
275 balance-=n;
276 if(balance<-100) balance=-100;
277 xmms_remote_set_balance
278 (lirc_plugin.xmms_session,balance);
279 }
280 else if(strncasecmp("BAL_RIGHT",c,9)==0)
281 {
282 ptr=c+9;
283 while(isspace(*ptr)) ptr++;
284 n=atoi(ptr);
285 if(n<=0) n=5;
286
287 balance=xmms_remote_get_balance
288 (lirc_plugin.xmms_session);
289 balance+=n;
290 if(balance>100) balance=100;
291 xmms_remote_set_balance
292 (lirc_plugin.xmms_session,balance);
293 }
294 else if(strcasecmp("BAL_CENTER",c)==0)
295 {
296 balance=0;
297 xmms_remote_set_balance
298 (lirc_plugin.xmms_session,balance);
299 }
300 else if(strcasecmp("LIST",c)==0)
301 {
302 show_pl=xmms_remote_is_pl_win
303 (lirc_plugin.xmms_session);
304 show_pl=(show_pl) ? 0:1;
305 xmms_remote_pl_win_toggle
306 (lirc_plugin.xmms_session,show_pl);
307 }
308 else if(strcasecmp("PLAYLIST_CLEAR",c)==0)
309 {
310 gboolean pl_visible;
311
312 pl_visible=xmms_remote_is_pl_win
313 (lirc_plugin.xmms_session);
314 xmms_remote_stop(lirc_plugin.xmms_session);
315 xmms_remote_playlist_clear
316 (lirc_plugin.xmms_session);
317 /* This is to refresh window content */
318 xmms_remote_pl_win_toggle
319 (lirc_plugin.xmms_session,pl_visible);
320 }
321 else if(strncasecmp("PLAYLIST_ADD ",c,13)==0)
322 {
323 gboolean pl_visible;
324 GList list;
325
326 pl_visible=xmms_remote_is_pl_win
327 (lirc_plugin.xmms_session);
328 list.prev=list.next=NULL;
329 list.data=c+13;
330 xmms_remote_playlist_add
331 (lirc_plugin.xmms_session,&list);
332 /* This is to refresh window content */
333 xmms_remote_pl_win_toggle
334 (lirc_plugin.xmms_session,pl_visible);
335 }
336 else
337 {
338 fprintf(stderr,_("%s: unknown command \"%s\"\n"),
339 plugin_name,c);
340 }
341 }
342 free(code);
343 if(ret==-1) break;
344 }
345 if(ret==-1)
346 {
347 /* something went badly wrong */
348 fprintf(stderr,_("%s: disconnected from LIRC\n"),plugin_name);
349 cleanup();
350 return;
351 }
352 }
353
354 void cleanup()
355 {
356 if(config)
357 {
358 if(input_tag)
359 gtk_input_remove(input_tag);
360
361 config=NULL;
362 }
363 if(lirc_fd!=-1)
364 {
365 lirc_deinit();
366 lirc_fd=-1;
367 }
368 }