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.
27 #include "stationnames.h"
29 #define ACTION_COUNT 16
31 #define SKIN_DIR "/usr/local/lib/wmradio/"
34 ButtonInfo buttons
[ACTION_COUNT
];
39 ButtonInfo StereoInfo
;
40 char radio_is_off
= 0;
41 int icon_width
= DEFAULTICONSIZE
;
42 int icon_height
= DEFAULTICONSIZE
;
44 int in_region(int x
, int y
, int btx
,int bty
,int btw
,int bth
)
46 if( (x
>=btx
) && (x
< btx
+ btw
) && (y
>=bty
) && (y
<bty
+bth
) ) return 1;
50 int xor_string(char *s
)
55 for(a
=0; a
<strlen(s
); a
++) {
57 if( r
>0xFFFF ) r
= (r
& 0xFFFF) + 1;
63 /* void add_action(char *name, region reg) */
65 /* if( action_count < ACTION_COUNT ) { */
66 /* actions[action_count].action = xor_string(name); */
67 /* actions[action_count].r = reg; */
72 int skin_read_num(char *p
)
87 void skin_def_line(char *line
, Display
*display
, Drawable drawable
, char *skin_desc_dir
)
100 case 5829: /* preset1-6 */
101 buttons
[button_count
].status
= BS_SELECTED
;
107 case 1457: /* tune+ */
108 case 1463: /* tune- */
111 case 4507: /* display */
112 buttons
[button_count
].action
= x
;
113 buttons
[button_count
].destx
= skin_read_num(p
);
114 buttons
[button_count
].desty
= skin_read_num(p
);
115 buttons
[button_count
].srcx
= skin_read_num(p
);
116 buttons
[button_count
].srcy
= skin_read_num(p
);
117 buttons
[button_count
].width
= skin_read_num(p
);
118 buttons
[button_count
].height
= skin_read_num(p
);
123 if( ! strstr(p
,"/") ) {
124 strcpy(buffer
,skin_desc_dir
);
127 if(XpmReadFileToPixmap(display
,drawable
,buffer
,
130 &(RadioSkin
.attributes
)) != XpmSuccess
) {
131 printf("wmradio: can't load pixmap %s\n",buffer
);
134 case 2071: /* digits */
135 digits
.destx
= skin_read_num(p
);
136 digits
.desty
= skin_read_num(p
);
137 digits
.srcx
= skin_read_num(p
);
138 digits
.srcy
= skin_read_num(p
);
139 for(i
=0; i
<12; i
++) {
140 digits
.w
[i
] = skin_read_num(p
);
142 digits
.h
= skin_read_num(p
);
144 case 4675: /* letters */
145 letters
.destx
= skin_read_num(p
);
146 letters
.desty
= skin_read_num(p
);
147 letters
.srcx
= skin_read_num(p
);
148 letters
.srcy
= skin_read_num(p
);
149 for(i
=0; i
<26; i
++) {
150 letters
.w
[i
] = skin_read_num(p
);
152 letters
.h
= skin_read_num(p
);
154 case 2917: /* stereo */
155 StereoInfo
.destx
= skin_read_num(p
);
156 StereoInfo
.desty
= skin_read_num(p
);
157 StereoInfo
.srcx
= skin_read_num(p
);
158 StereoInfo
.srcy
= skin_read_num(p
);
159 StereoInfo
.width
= skin_read_num(p
);
160 StereoInfo
.height
= skin_read_num(p
);
162 case 19368: /* iconwidth */
163 icon_width
= atoi(p
);
164 case 37920: /* iconheight */
165 icon_height
= atoi(p
);
169 void create_skin(char *skin_def_file
, Display
*display
, Drawable drawable
)
172 char buffer
[256], *p
, dir
[256];
175 memset(&letters
,0,sizeof(letters
));
177 if(!strstr(skin_def_file
,"/")) {
178 strcpy(buffer
,SKIN_DIR
);
180 strcat(buffer
,skin_def_file
);
181 strncpy(dir
,buffer
,sizeof(dir
));
182 p
= &dir
[strlen(dir
)-1];
183 while( *p
!= '/' ) p
--;
186 f
= fopen(buffer
,"r");
188 printf("wmradio: Skin %s not found\n", skin_def_file
);
189 if( strcmp(skin_def_file
,"default.skin") != 0 ) {
190 create_skin("default.skin",display
,drawable
);
195 fgets(buffer
,sizeof(buffer
),f
);
196 p
= strstr(buffer
,"#");
198 p
= strstr(buffer
,"\n");
200 while(buffer
[0] == ' ') strcpy(buffer
,&buffer
[1]);
201 skin_def_line(buffer
,display
,drawable
,dir
);
206 int find_action(int x
, int y
)
210 for(a
=0; a
< button_count
; a
++) {
218 ) return buttons
[a
].action
;
223 int skin_button_index(int x
, int y
)
226 for(i
=0; i
< button_count
; i
++) {
231 buttons
[i
].height
) ) {
238 void skin_unselect_button(void)
242 for(a
=0; a
< ACTION_COUNT
; a
++) {
243 if(buttons
[a
].status
== BS_SELECTED
) buttons
[a
].status
= BS_RELEASED
;
247 int skin_mouse_event(int x
, int y
, int mousebutton
, int press
)
250 static int last_press
= -1;
254 last_press
= skin_button_index(x
,y
);
255 buttons
[last_press
].status
= BS_PRESSED
;
258 if(last_press
== skin_button_index(x
,y
)) {
259 r
= buttons
[last_press
].action
;
260 buttons
[last_press
].status
= BS_RELEASED
;
262 for(i
= 0; i
< button_count
; i
++) buttons
[i
].status
= BS_RELEASED
;
263 buttons
[last_press
].status
= BS_SELECTED
;
266 if(last_press
>= 0) {
267 buttons
[last_press
].status
= BS_RELEASED
;
275 int digit_index(char digit
) {
278 if(isdigit(digit
)) i
= digit
- '0';
279 if(digit
== ' ') i
= 10;
280 if(digit
== '.') i
= 11;
284 int digit_source_x(int index
)
289 for(i
=0; i
<index
; i
++) {
295 int letter_index(char letter
) {
297 if(isalpha(letter
) && isascii(letter
)) return tolower(letter
) - 'a';
301 int letter_source_x(int index
)
306 for(i
=0; i
<index
; i
++) {
312 int have_letters(void)
314 return letters
.h
!= 0;
317 void char_to_window(Display
*display
, Window win
, GC gc
, char c
, int *x
, int y
)
322 idx
= letter_index(c
);
323 sourcex
= letter_source_x(idx
);
324 XCopyArea(display
,RadioSkin
.pixmap
,win
, gc
,
325 letters
.srcx
+ sourcex
,
331 *x
+= letters
.w
[idx
];
333 idx
= digit_index(c
);
334 sourcex
= digit_source_x(idx
);
335 XCopyArea(display
,RadioSkin
.pixmap
,win
, gc
,
336 digits
.srcx
+ sourcex
,
347 void freq_to_window(Display
*display
, Window win
, GC gc
, int freq
)
349 char freqs
[10], temp
[10], *stn_name
;
352 stn_name
= station_get_freq_name(freq
);
353 if(stn_name
&& have_letters()) {
354 strncpy(freqs
,stn_name
,4);
356 while(strlen(freqs
)<3) strcat(freqs
," ");
359 snprintf(freqs
,sizeof(freqs
),"%i.%i",freq
/100, (freq
% 100) / 10);
360 while(strlen(freqs
)<5) {
366 /* freq now contain right text */
370 char_to_window(display
,win
,gc
,freqs
[i
],&x
,digits
.desty
);
375 void skin_to_window(Display
*display
, Window win
, GC gc
, int freq
, char stereo
)
381 setlocale(LC_ALL
,"C");
382 XCopyArea(display
,RadioSkin
.pixmap
,win
, gc
, 0, 0, skin_width(), skin_height(), 0, 0);
383 for(i
=0; i
<button_count
; i
++) {
384 xs
= buttons
[i
].srcx
;
385 bs
= buttons
[i
].status
;
386 if( radio_is_off
) bs
= BS_PRESSED
;
389 xs
= buttons
[i
].srcx
;
392 xs
= buttons
[i
].srcx
+ buttons
[i
].width
;
395 xs
= buttons
[i
].srcx
+ buttons
[i
].width
* 2;
398 if( buttons
[i
].action
!= 4507 ) { /* 4507 is display */
399 XCopyArea(display
,RadioSkin
.pixmap
,win
, gc
,
409 freq_to_window(display
,win
,gc
,freq
);
411 xs
= StereoInfo
.srcx
;
412 if(stereo
) xs
+= StereoInfo
.width
;
413 XCopyArea(display
,RadioSkin
.pixmap
,win
, gc
,
423 void skin_select_button(int action
) {
426 for(a
=0; a
< ACTION_COUNT
; a
++) {
427 if(buttons
[a
].action
== action
) buttons
[a
].status
= BS_SELECTED
;
431 void skin_switch_radio(char status
)
433 radio_is_off
= status
;
436 int skin_width(void) {
440 int skin_height(void) {
444 void skin_select_station(int station
)
448 for(i
=0; i
<ACTION_COUNT
; i
++) {
449 switch(buttons
[i
].action
) {
451 buttons
[i
].status
= station
== 0 ? BS_SELECTED
: BS_RELEASED
;
454 buttons
[i
].status
= station
== 1 ? BS_SELECTED
: BS_RELEASED
;
457 buttons
[i
].status
= station
== 2 ? BS_SELECTED
: BS_RELEASED
;
460 buttons
[i
].status
= station
== 3 ? BS_SELECTED
: BS_RELEASED
;
463 buttons
[i
].status
= station
== 4 ? BS_SELECTED
: BS_RELEASED
;
466 buttons
[i
].status
= station
== 5 ? BS_SELECTED
: BS_RELEASED
;