wmclockmon: update debug flags to reflect what I've been using
[dockapps.git] / wmradio / wm_envelope.c
blobfa8676805c33997e547e4f4f7e4ed5bddfa26080
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 <getopt.h>
33 #include "config.h"
34 #include "wm_envelope.h"
35 #include "wmradio.h"
36 #include "skin.h"
37 #include "rc.h"
39 Display *main_display = 0;
40 Window root, applet, icon, buffer;
42 GC NormalGC;
43 unsigned long gcm;
44 XGCValues gcv;
46 char radio_continue;
48 Atom wm_delete_window_atom, wm_protocol_atom;
50 Pixel GetColor(char *ColorName, Display * disp, Window win)
52 XColor Color;
53 XWindowAttributes Attributes;
55 XGetWindowAttributes(disp, win, &Attributes);
56 Color.pixel = 0;
58 if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
59 printf("wmradio: can't parse %s\n", ColorName);
60 else if (!XAllocColor(disp, Attributes.colormap, &Color))
61 printf("wmradio: can't allocate %s\n", ColorName);
63 return Color.pixel;
66 int parse_command_line(int argc, char *argv [],RadioInfo *info)
68 static struct option long_options[] = {
69 {"dont-quit-mode",0,0,0}, /* 0 */
70 {"start-muted",0,0,0},
71 {"help",0,0,0},
72 {"version",0,0,0},
74 {"osd",0,0,0},
75 {"osd-font",1,0,0}, /* 5 */
76 {"osd-color",1,0,0},
77 {"osd-position",1,0,0},
78 {"osd-shadow-offset",1,0,0},
79 {"osd-timeout",1,0,0},
81 {"skin",1,0,0},
83 {0,0,0,0}
85 int option_index = 0;
86 int opt;
87 int x,y,offset,timeout;
89 while(1) {
90 opt = getopt_long(argc,argv,"qmhvof:c:p:s:t:k:",long_options, &option_index);
91 if( opt == -1 ) break;
92 if( opt == 0 ) opt = option_index;
93 switch(opt) {
94 case 0:
95 case 'q':
96 info->dont_quit_mode = 1;
97 rc_set_variable_as_int(SECTION_CONFIG,"dont-quit-mode",1);
98 break;
99 case 1:
100 case 'm':
101 rc_set_variable_as_int(SECTION_CONFIG,"start-muted",1);
102 break;
103 case 2:
104 case 'h':
105 printf("wmradio [options]\n"
106 " options are:\n"
107 " -h|--help print this help\n"
108 " -m|--start-muted program starts, but doesn't open radio device\n"
109 " -q|--dont-quit-mode program doesn't quit, just close radio device\n"
110 " -v|--version print version and quit\n"
111 " -o|--osd use osd\n"
112 " -f|--osd-font font osd font\n"
113 " -c|--osd-color font color\n"
114 " -p|--osd-position display position (-p 10x30 for example)\n"
115 " -s|--osd-shadow-offset shadow offset\n"
116 " -t|--osd-timeout osd timeout\n"
117 " -k|--skin skin\n"
119 return 0;
120 case 3:
121 case 'v':
122 printf("This is %s %s\n", PACKAGE,VERSION);
123 return 0;
124 case 4:
125 case 'o':
126 rc_set_variable_as_int(SECTION_CONFIG,"osd",1);
127 break;
128 case 5:
129 case 'f':
130 rc_set_variable(SECTION_CONFIG,"osd-font",optarg);
131 break;
132 case 6:
133 case 'c':
134 rc_set_variable(SECTION_CONFIG,"osd-color",optarg);
135 break;
136 case 7:
137 case 'p':
138 if (sscanf(optarg,"%ix%i", &x, &y) < 2) {
139 fprintf(stderr, "%s: incorrect syntax in OSD position\n", argv[0]);
140 } else {
141 rc_set_variable_as_int(SECTION_CONFIG,"osd-position",x);
142 /* rc_set_variable_as_int(SECTION_CONFIG,"osd-position",y); */
144 break;
145 case 8:
146 case 's':
147 if (sscanf(optarg,"%i", &offset) < 1) {
148 fprintf(stderr, "%s: incorrect syntax in OSD shadow offset\n", argv[0]);
149 } else {
150 rc_set_variable_as_int(SECTION_CONFIG,"osd-shadow-offset",offset);
152 break;
153 case 9:
154 case 't':
155 if (sscanf(optarg,"%i", &timeout) < 1) {
156 fprintf(stderr, "%s: incorrect syntax in OSD timeout\n", argv[0]);
157 } else {
158 rc_set_variable_as_int(SECTION_CONFIG,"osd-timeout",timeout);
160 break;
161 case 10:
162 case 'k':
163 rc_set_variable(SECTION_CONFIG,"skin",optarg);
164 break;
167 return 1;
170 void video_mainloop(void)
172 XEvent xe;
173 RadioEvent re;
175 radio_continue = 1;
176 while (radio_continue) {
177 /* X Events */
178 while (XPending(main_display)) {
179 XNextEvent(main_display, &xe);
180 switch (xe.type) {
181 case Expose:
182 re.type = REVENT_EXPOSE;
183 wmradio_handle_event(&re);
184 break;
185 case ClientMessage:
186 if(xe.xclient.message_type == wm_protocol_atom) {
187 Atom a = (xe.xclient.data.l)[0];
188 if( a == wm_delete_window_atom ) {
189 re.type = REVENT_QUIT;
190 printf("quit\n");
191 wmradio_handle_event(&re);
194 break;
195 case DestroyNotify:
196 re.type = REVENT_QUIT;
197 wmradio_handle_event(&re);
198 printf("quit\n");
199 return;
200 case ButtonPress:
201 if(xe.xbutton.button < 4) {
202 re.type = REVENT_BUTTON_PRESS;
203 if(xe.xbutton.button == 4) re.type = REVENT_SCROLL_UP;
204 if(xe.xbutton.button == 5) re.type = REVENT_SCROLL_DOWN;
205 re.x = xe.xbutton.x;
206 re.y = xe.xbutton.y;
207 re.button = xe.xbutton.button;
208 re.control = xe.xbutton.state & ControlMask ?
209 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
210 re.shift = xe.xbutton.state & ShiftMask ?
211 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
212 wmradio_handle_event(&re);
214 break;
215 case ButtonRelease:
216 re.type = REVENT_BUTTON_RELEASE;
217 re.x = xe.xbutton.x;
218 re.y = xe.xbutton.y;
219 re.button = xe.xbutton.button;
220 re.control = xe.xbutton.state & ControlMask ?
221 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
222 re.shift = xe.xbutton.state & ShiftMask ?
223 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
224 if(re.button == 4) re.type = REVENT_SCROLL_UP;
225 if(re.button == 5) re.type = REVENT_SCROLL_DOWN;
226 wmradio_handle_event(&re);
227 break;
229 } /* XPending */
230 usleep(100000);
231 re.type = REVENT_TIMER;
232 wmradio_handle_event(&re);
234 XCloseDisplay(main_display);
237 void video_close(void)
239 radio_continue = 0;
242 void video_draw(float freq,int stereo)
244 skin_to_window(main_display,buffer, NormalGC,freq,stereo);
245 XCopyArea(main_display,buffer, icon, NormalGC,0,0,skin_width(),skin_height(),0,0);
246 XCopyArea(main_display,buffer, applet, NormalGC,0,0,skin_width(),skin_height(),0,0);
249 int main(int argc, char *argv [])
251 Pixel foreground,background;
252 XWMHints WmHints;
253 Status status;
254 XClassHint classhint;
255 XTextProperty title;
256 int screen;
257 char * appletname = "WmRadio";
259 wmradio_init_radio_info();
260 rc_read_config();
261 parse_command_line(argc,argv,wmradio_radio_info());
263 main_display = XOpenDisplay(NULL);
264 if (!main_display) {
265 printf("wmradio: can't open display %s.\n",XDisplayName(NULL));
266 return 0;
268 screen = DefaultScreen(main_display);
269 root = RootWindow(main_display,screen);
271 background = GetColor("black", main_display, root);
272 foreground = GetColor("white", main_display, root);
273 create_skin(rc_get_variable(SECTION_CONFIG,"skin","default.skin"),main_display,root);
274 wmradio_init();
275 applet = XCreateSimpleWindow(main_display,
276 root,
277 0,0,skin_width(),skin_height(),
279 foreground,background);
280 icon = XCreateSimpleWindow(main_display,
281 root,
282 0,0,skin_width(),skin_height(),
284 foreground,background);
285 WmHints.flags = StateHint | IconWindowHint;
286 WmHints.initial_state = WithdrawnState;
287 WmHints.icon_window = icon;
288 WmHints.window_group = applet;
289 WmHints.flags |= WindowGroupHint;
291 XSetWMHints(main_display,
292 applet,
293 &WmHints);
295 buffer = XCreatePixmap(main_display,
296 root,
297 skin_width(),skin_height(),
298 DefaultDepth(main_display,screen)/*16 color_depth */);
300 status = XStringListToTextProperty(&appletname, 1, &title);
301 XSetWMName(main_display, applet, &title);
302 XSetWMName(main_display, icon, &title);
303 classhint.res_name = "wmradio" ;
304 classhint.res_class = "WMRADIO";
305 XSetClassHint(main_display, applet, &classhint);
306 XStoreName(main_display, applet, "WmRadio");
307 XSetIconName(main_display, applet, "WmRadio");
309 wm_delete_window_atom = XInternAtom(main_display, "WM_DELETE_WINDOW", 0);
310 wm_protocol_atom = XInternAtom(main_display, "WM_PROTOCOLS", 0);
311 XSetWMProtocols(main_display, applet, &wm_delete_window_atom, 1);
313 status = XMapWindow(main_display, applet);
314 gcm = GCForeground | GCBackground | GCGraphicsExposures;
315 gcv.foreground = foreground;
316 gcv.background = background;
317 gcv.graphics_exposures = 0;
318 NormalGC = XCreateGC(main_display, root, gcm, &gcv);
319 XSelectInput(main_display, applet,
320 ButtonPressMask | ExposureMask |
321 ButtonReleaseMask | PointerMotionMask |
322 StructureNotifyMask);
323 XSelectInput(main_display, icon,
324 ButtonPressMask | ExposureMask |
325 ButtonReleaseMask | PointerMotionMask |
326 StructureNotifyMask);
327 video_mainloop();
328 wmradio_done();
329 return 0;