wmclockmon: update change-log
[dockapps.git] / wmradio / wmradio.c
bloba5fada96d322a3834e9b8607454c834117277823
1 /*
2 * Copyright (C) 12 Jun 2003 Tomas Cermak
4 * This file is part of wmradio program.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <X11/Xlib.h>
31 #include <X11/xpm.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <locale.h>
35 #include <math.h>
36 #include "config.h"
37 #include "const.h"
38 #include "skin.h"
39 #include "radio.h"
40 #include "wmradio.h"
41 #include "stationnames.h"
42 #include "rc.h"
44 #include "osd.h"
45 #include "fifo.h"
47 #ifdef GNOME_RADIO
48 #include "gnome_applet_envelope.h"
49 #else
50 #include "wm_envelope.h"
51 #endif
52 static int min_freq = 8750, max_freq = 10800;
53 RadioInfo radio_info;
56 RadioInfo *wmradio_radio_info(void)
58 return &radio_info;
61 void wmradio_init_radio_info(void) {
62 radio_info.is_on = 0;
63 radio_info.current_station = 0;
64 radio_info.dont_quit_mode = 0;
67 int open_radio()
69 radio_info.radiofd = open(rc_get_variable(SECTION_CONFIG,"device","/dev/radio"),O_RDONLY);
70 return radio_info.radiofd>=0;
73 void close_radio()
75 if(radio_info.radiofd != -1) close(radio_info.radiofd);
78 void radio_refresh_freq (void) {
79 char str[30];
81 radio_setfreq(radio_info.radiofd,radio_info.current_freq);
82 if(rc_get_variable_as_int(SECTION_CONFIG,"osd",0)) {
83 sprintf(str, "%.2i FM", radio_info.current_freq);
84 osd_print(str);
88 void tuned_to_preset_station(void)
90 int i;
91 int x,y;
93 x = radio_info.current_freq;
94 for(i = 0; i<6; i++) {
95 y = rc_get_freq(i);
96 if(x == y) {
97 skin_select_station(i);
98 return;
103 void tune_plus(int fine_tuning)
105 if(fine_tuning) {
106 radio_info.current_freq += 1;
107 } else {
108 radio_info.current_freq += 10;
110 if(radio_info.current_freq>max_freq) radio_info.current_freq = min_freq;
111 radio_refresh_freq();
114 void tune_minus(int fine_tuning)
116 if(fine_tuning) {
117 radio_info.current_freq -= 1;
118 } else {
119 radio_info.current_freq -= 10;
121 if(radio_info.current_freq<min_freq) radio_info.current_freq = max_freq;
122 radio_refresh_freq();
125 void wmradio_scan(int start)
127 static char starting = 0;
128 int signal;
130 switch(start) {
131 case SCAN_START:
132 starting = radio_info.scan_in_progress = 1;
133 break;
134 case SCAN_STOP:
135 radio_info.scan_in_progress = 0;
136 tuned_to_preset_station();
138 if(!radio_info.scan_in_progress) return;
139 tune_plus(0);
140 usleep(100000);
141 signal = radio_getsignal(radio_info.radiofd);
142 if(starting) {
143 if( signal<2 ) starting = 0;
144 } else {
145 if( signal>2 ) {
146 radio_info.scan_in_progress = 0;
147 tuned_to_preset_station();
152 void wmradio_next_station(void)
154 radio_info.current_freq = station_next_freq(radio_info.current_freq);
155 radio_refresh_freq();
158 void wmradio_prev_station(void)
160 radio_info.current_freq = station_prev_freq(radio_info.current_freq);
161 radio_refresh_freq();
164 #define MOD(A,B) ((A)<0)? (B)+((A)%(B)) : (A)%(B)
166 void wmradio_command(RadioCommand command, int value) {
167 switch (command) {
168 case POWER_SWITCH:
169 if( (value && radio_info.dont_quit_mode)
170 || (!value && !radio_info.dont_quit_mode)){
171 if (radio_info.is_on) {
172 radio_mute(radio_info.radiofd);
173 close_radio();
175 /* rc_save_config(); */
176 video_close();
177 return;
179 if (radio_info.is_on) {
180 if(rc_get_variable_as_int(SECTION_CONFIG,"osd",0)) {
181 osd_print("FM off");
183 radio_mute(radio_info.radiofd);
184 close_radio();
185 skin_switch_radio(SKIN_OFF);
186 radio_info.is_on = 0;
187 /* rc_save_config(); */
188 } else {
189 if(open_radio()) {
190 radio_refresh_freq();
191 radio_unmute(radio_info.radiofd);
192 skin_switch_radio(SKIN_ON);
193 radio_info.is_on = 1;
194 } else {
195 printf("wmradio: can't open radio device\n");
196 #ifdef ONLY_TEST
197 /* those next lines are for tests on my notebook */
198 /* without /dev/rario */
199 printf(" but radio is compiled with ONLY_TEST\n");
200 radio_refresh_freq();
201 radio_unmute(radio_info.radiofd);
202 skin_switch_radio(SKIN_ON);
203 radio_info.is_on = 1;
204 #endif /* ONLY_TEST */
207 break;
208 case TUNE_MINUS:
209 if(radio_info.is_on) {
210 tune_minus(value);
211 skin_unselect_button();
212 tuned_to_preset_station();
214 break;
215 case TUNE_PLUS:
216 if(radio_info.is_on) {
217 tune_plus(value);
218 skin_unselect_button();
219 tuned_to_preset_station();
221 break;
222 case SET_PRESET:
223 if(radio_info.is_on){
224 radio_info.current_station = value;
225 radio_info.current_freq = rc_get_freq(value);
226 radio_refresh_freq();
227 skin_select_station(value);
229 break;
230 case SAVE_PRESET:
231 if(radio_info.is_on){
232 rc_set_freq(value,radio_info.current_freq);
233 skin_select_station(value);
234 rc_save_config();
236 break;
237 case SCAN:
238 if(radio_info.is_on){
239 wmradio_scan(SCAN_START);
240 skin_unselect_button();
242 break;
243 case TUNE_NAME_PREV:
244 if(radio_info.is_on){
245 wmradio_prev_station();
246 skin_unselect_button();
247 tuned_to_preset_station();
249 break;
250 case TUNE_NAME_NEXT:
251 if(radio_info.is_on){
252 wmradio_next_station();
253 skin_unselect_button();
254 tuned_to_preset_station();
256 break;
257 case READ_CONFIG:
258 rc_free_config();
259 rc_read_config();
260 radio_info.dont_quit_mode = rc_get_variable_as_int(SECTION_CONFIG,"dont-quit-mode",0);
261 break;
265 void wmradio_handle_event(RadioEvent *e)
267 int i;
268 int presetindex;
269 /* int stations[] = {5829,5830,5831,5824,5825,5826};*/
271 switch (e->type) {
272 case REVENT_EXPOSE:
273 video_draw(radio_info.current_freq,radio_getstereo(radio_info.radiofd));
274 break;
275 case REVENT_QUIT:
276 if(radio_info.is_on) radio_mute(radio_info.radiofd);
277 close_radio();
278 /* rc_save_config(); */
279 video_close();
280 break;
281 case REVENT_BUTTON_PRESS:
282 skin_mouse_event(e->x,e-> y,e->button,1);
283 video_draw(radio_info.current_freq,radio_getstereo(radio_info.radiofd));
284 break;
285 case REVENT_TIMER:
286 wmradio_scan(SCAN_NO_CHANGE);
287 video_draw(radio_info.current_freq,radio_getstereo(radio_info.radiofd));
288 break;
289 case REVENT_BUTTON_RELEASE:
290 setlocale(LC_ALL,"C");
291 wmradio_scan(SCAN_STOP);
292 i = skin_mouse_event(e->x, e->y,e->button, 0);
293 presetindex = -1;
294 if( (e->button == 4 || e->button == 5) ) {
295 } else {
296 /* this is not mouse wheel */
297 if (i) {
298 switch (i) {
299 case XOR_OFF:
300 wmradio_command(POWER_SWITCH, e->control);
301 break;
302 case XOR_TUNEm:
303 if(e->shift) { wmradio_command(TUNE_NAME_PREV, e->control); }
304 else { wmradio_command(TUNE_MINUS, e->control); }
305 break;
306 case XOR_TUNEp:
307 if(e->shift) { wmradio_command(TUNE_NAME_NEXT, e->control); }
308 else { wmradio_command(TUNE_PLUS, e->control); }
309 break;
310 case XOR_SCAN:
311 wmradio_command(SCAN, 0);
312 break;
313 case XOR_PRESET1:
314 presetindex = 0;
315 break;
316 case XOR_PRESET2:
317 presetindex = 1;
318 break;
319 case XOR_PRESET3:
320 presetindex = 2;
321 break;
322 case XOR_PRESET4:
323 presetindex = 3;
324 break;
325 case XOR_PRESET5:
326 presetindex = 4;
327 break;
328 case XOR_PRESET6:
329 presetindex = 5;
330 break;
332 if(presetindex >= 0) {
333 if( e->control ) {
334 wmradio_command(SAVE_PRESET,presetindex);
335 } else {
336 wmradio_command(SET_PRESET,presetindex);
338 radio_info.current_station = presetindex;
342 video_draw(radio_info.current_freq,
343 radio_getstereo(radio_info.radiofd));
344 break;
345 case REVENT_SCROLL_UP:
346 skin_mouse_event(e->x, e->y,e->button, 1);
347 i = skin_mouse_event(e->x, e->y,e->button, 0);
348 if(i == 4507)
349 if(e->shift) {
350 wmradio_command(TUNE_MINUS, e->control);
351 } else {
352 wmradio_command(TUNE_NAME_PREV, e->control);
354 else
355 wmradio_command(SET_PRESET,
356 MOD(radio_info.current_station-1, 6));
357 break;
358 case REVENT_SCROLL_DOWN:
359 skin_mouse_event(e->x, e->y,e->button, 1);
360 i = skin_mouse_event(e->x, e->y,e->button, 0);
361 if(i == 4507)
362 if(e->shift) {
363 wmradio_command(TUNE_PLUS, e->control);
364 } else {
365 wmradio_command(TUNE_NAME_NEXT, e->control);
367 else
368 wmradio_command(SET_PRESET,
369 MOD(radio_info.current_station+1, 6));
370 break;
372 fifo_parse();
375 int wmradio_init(void)
377 if (rc_get_variable_as_int(SECTION_CONFIG,"osd",0) ){
378 if (! osd_init("wmradio",
379 rc_get_variable(SECTION_CONFIG,"osd-font","*-courier-*"),
380 rc_get_variable(SECTION_CONFIG,"osd-color","green"),
381 rc_get_variable_as_int(SECTION_CONFIG,"osd-position",10),
382 rc_get_variable_as_int(SECTION_CONFIG,"osd-position",10),
383 rc_get_variable_as_int(SECTION_CONFIG,"osd-shadow-offset",5),
384 rc_get_variable_as_int(SECTION_CONFIG,"osd-timeout",3)
387 printf("osd init failed\n");
388 rc_set_variable_as_int(SECTION_CONFIG,"osd",0);
391 fifo_init();
392 skin_switch_radio(SKIN_OFF);
393 radio_info.is_on = 0;
394 radio_info.dont_quit_mode = rc_get_variable_as_int(SECTION_CONFIG,"dont-quit-mode",1);
395 radio_info.current_freq = rc_get_freq(radio_info.current_station);
396 if(! rc_get_variable_as_int(SECTION_CONFIG,"start-muted",1) ) {
397 if(open_radio()) {
398 radio_setfreq(radio_info.radiofd,radio_info.current_freq);
399 radio_unmute(radio_info.radiofd);
400 skin_switch_radio(SKIN_ON);
401 radio_info.is_on = 1;
402 } else {
403 printf("wmradio: can't open radio device\n");
404 radio_info.is_on = 0;
407 return 1;
410 void wmradio_done(void)
412 osd_close();
413 fifo_close();