wmbattery: avoid truncating ACPI file paths
[dockapps.git] / wmradio / x_envelope.c
blob01d5c14c82f752c626a6d31dad83810c38308cf2
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, buffer;
42 GC NormalGC;
43 unsigned long gcm;
44 XGCValues gcv;
45 Atom wm_delete_window_atom, wm_protocol_atom;
47 char radio_continue;
49 Pixel GetColor(char *ColorName, Display * disp, Window win)
51 XColor Color;
52 XWindowAttributes Attributes;
54 XGetWindowAttributes(disp, win, &Attributes);
55 Color.pixel = 0;
57 if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
58 printf("wmradio: can't parse %s\n", ColorName);
59 else if (!XAllocColor(disp, Attributes.colormap, &Color))
60 printf("wmradio: can't allocate %s\n", ColorName);
62 return Color.pixel;
65 int parse_command_line(int argc, char *argv [],RadioInfo *info)
67 static struct option long_options[] = {
68 {"dont-quit-mode",0,0,0}, /* 0 */
69 {"start-muted",0,0,0},
70 {"help",0,0,0},
71 {"version",0,0,0},
73 {"osd",0,0,0},
74 {"osd-font",1,0,0}, /* 5 */
75 {"osd-color",1,0,0},
76 {"osd-position",1,0,0},
77 {"osd-shadow-offset",1,0,0},
78 {"osd-timeout",1,0,0},
80 {"skin",1,0,0},
82 {0,0,0,0}
84 int option_index = 0;
85 int opt;
86 int x,y,offset,timeout;
88 while(1) {
89 opt = getopt_long(argc,argv,"qmhvof:c:p:s:t:k:",long_options, &option_index);
90 if( opt == -1 ) break;
91 if( opt == 0 ) opt = option_index;
92 switch(opt) {
93 case 0:
94 case 'q':
95 info->dont_quit_mode = 1;
96 rc_set_variable_as_int(SECTION_CONFIG,"dont-quit-mode",1);
97 break;
98 case 1:
99 case 'm':
100 rc_set_variable_as_int(SECTION_CONFIG,"start-muted",1);
101 break;
102 case 2:
103 case 'h':
104 printf("wmradio [options]\n"
105 " options are:\n"
106 " -h|--help print this help\n"
107 " -m|--start-muted program starts, but doesn't open radio device\n"
108 " -q|--dont-quit-mode program doesn't quit, just close radio device\n"
109 " -v|--version print version and quit\n"
110 " -o|--osd use osd\n"
111 " -f|--osd-font font osd font\n"
112 " -c|--osd-color font color\n"
113 " -p|--osd-position display position (-p 10x30 for example)\n"
114 " -s|--osd-shadow-offset shadow offset\n"
115 " -t|--osd-timeout osd timeout\n"
116 " -k|--skin skin\n"
118 return 0;
119 case 3:
120 case 'v':
121 printf("This is %s %s\n", PACKAGE,VERSION);
122 return 0;
123 case 4:
124 case 'o':
125 rc_set_variable_as_int(SECTION_CONFIG,"osd",1);
126 break;
127 case 5:
128 case 'f':
129 rc_set_variable(SECTION_CONFIG,"osd-font",optarg);
130 break;
131 case 6:
132 case 'c':
133 rc_set_variable(SECTION_CONFIG,"osd-color",optarg);
134 break;
135 case 7:
136 case 'p':
137 if (sscanf(optarg,"%ix%i", &x, &y) < 2) {
138 fprintf(stderr, "%s: incorrect syntax in OSD position\n", argv[0]);
139 } else {
140 rc_set_variable_as_int(SECTION_CONFIG,"osd-position",x);
141 /* rc_set_variable_as_int(SECTION_CONFIG,"osd-position",y); */
143 break;
144 case 8:
145 case 's':
146 if (sscanf(optarg,"%i", &offset) < 1) {
147 fprintf(stderr, "%s: incorrect syntax in OSD shadow offset\n", argv[0]);
148 } else {
149 rc_set_variable_as_int(SECTION_CONFIG,"osd-shadow-offset",offset);
151 break;
152 case 9:
153 case 't':
154 if (sscanf(optarg,"%i", &timeout) < 1) {
155 fprintf(stderr, "%s: incorrect syntax in OSD timeout\n", argv[0]);
156 } else {
157 rc_set_variable_as_int(SECTION_CONFIG,"osd-timeout",timeout);
159 break;
160 case 10:
161 case 'k':
162 rc_set_variable(SECTION_CONFIG,"skin",optarg);
163 break;
166 return 1;
169 void video_mainloop(void)
171 XEvent xe;
172 RadioEvent re;
173 int i;
175 radio_continue = 1;
176 while (radio_continue) {
177 /* X Events */
178 while (XPending(main_display)) {
179 XNextEvent(main_display, &xe);
180 /* printf("xe.type = %i\n", xe.type); */
181 switch (xe.type) {
182 case Expose:
183 re.type = REVENT_EXPOSE;
184 wmradio_handle_event(&re);
185 break;
186 case ClientMessage:
187 if(xe.xclient.message_type == wm_protocol_atom) {
188 Atom a = (xe.xclient.data.l)[0];
189 if( a == wm_delete_window_atom ) {
190 re.type = REVENT_QUIT;
191 wmradio_handle_event(&re);
194 break;
195 case DestroyNotify:
196 re.type = REVENT_QUIT;
197 wmradio_handle_event(&re);
198 return;
199 case ButtonPress:
200 if(xe.xbutton.button < 4) {
201 re.type = REVENT_BUTTON_PRESS;
202 re.x = xe.xbutton.x;
203 re.y = xe.xbutton.y;
204 re.button = xe.xbutton.button;
205 re.control = xe.xbutton.state & ControlMask ?
206 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
207 re.shift = xe.xbutton.state & ShiftMask ?
208 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
209 wmradio_handle_event(&re);
211 break;
212 case ButtonRelease:
213 re.type = REVENT_BUTTON_RELEASE;
214 re.x = xe.xbutton.x;
215 re.y = xe.xbutton.y;
216 re.button = xe.xbutton.button;
217 re.control = xe.xbutton.state & ControlMask ?
218 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
219 re.shift = xe.xbutton.state & ShiftMask ?
220 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
221 if(re.button == 4) re.type = REVENT_SCROLL_UP;
222 if(re.button == 5) re.type = REVENT_SCROLL_DOWN;
223 wmradio_handle_event(&re);
224 break;
226 } /* XPending */
227 if(radio_continue) {
228 re.type = REVENT_TIMER;
229 wmradio_handle_event(&re);
230 for(i = 0; i<10; i++){
231 usleep(100000);
232 if(XPending(main_display)) i=1000;
236 XCloseDisplay(main_display);
239 void video_close(void)
241 radio_continue = 0;
244 void video_draw(float freq,int stereo)
246 skin_to_window(main_display,buffer, NormalGC,freq,stereo);
247 XCopyArea(main_display,buffer, applet, NormalGC,0,0,skin_width(),skin_height(),0,0);
250 int main(int argc, char *argv [])
252 Pixel foreground,background;
253 Status status;
254 XClassHint classhint;
255 XTextProperty title;
256 int screen;
257 char * appletname = "WmRadio";
259 XSizeHints sh;
261 wmradio_init_radio_info();
262 rc_read_config();
263 parse_command_line(argc,argv,wmradio_radio_info());
264 main_display = XOpenDisplay(NULL);
265 if (!main_display) {
266 printf("wmradio: can't open display %s.\n",XDisplayName(NULL));
267 return 0;
269 screen = DefaultScreen(main_display);
270 root = RootWindow(main_display,screen);
272 background = GetColor("black", main_display, root);
273 foreground = GetColor("white", main_display, root);
274 create_skin(rc_get_variable(SECTION_CONFIG,"skin","default.skin"),main_display,root);
275 wmradio_init();
276 applet = XCreateSimpleWindow(main_display,
277 root,
278 0,0,skin_width(),skin_height(),
280 foreground,background);
281 buffer = XCreatePixmap(main_display,
282 root,
283 skin_width(),skin_height(),
284 DefaultDepth(main_display,screen)/*16 color_depth */);
286 status = XStringListToTextProperty(&appletname, 1, &title);
287 XSetWMName(main_display, applet, &title);
288 classhint.res_name = "wmradio" ;
289 classhint.res_class = "WMRADIO";
290 XSetClassHint(main_display, applet, &classhint);
291 sh.width_inc = 0;
292 sh.height_inc = 0;
293 sh.min_width = sh.max_width =skin_width();
294 sh.min_height = sh.max_height = skin_height();
295 sh.flags = PResizeInc | PMinSize | PMaxSize;
296 XSetWMNormalHints(main_display,applet,&sh);
297 XStoreName(main_display, applet, "WmRadio");
298 XSetIconName(main_display, applet, "WmRadio");
300 status = XMapWindow(main_display, applet);
301 gcm = GCForeground | GCBackground | GCGraphicsExposures;
302 gcv.foreground = foreground;
303 gcv.background = background;
304 gcv.graphics_exposures = 0;
305 NormalGC = XCreateGC(main_display, root, gcm, &gcv);
306 wm_delete_window_atom = XInternAtom(main_display, "WM_DELETE_WINDOW", 0);
307 wm_protocol_atom = XInternAtom(main_display, "WM_PROTOCOLS", 0);
308 XSetWMProtocols(main_display, applet, &wm_delete_window_atom, 1);
309 XSelectInput(main_display, applet,
310 ButtonPressMask | ExposureMask |
311 ButtonReleaseMask | PointerMotionMask |
312 StructureNotifyMask);
313 video_mainloop();
314 wmradio_done();
315 return 0;