Initial dockapps git repo
[dockapps.git] / wmmixer-1.5 / xhandler.cc
blob652335d2d64ff07e731839f1536966b406c20acc
1 // wmmixer - A mixer designed for WindowMaker
2 //
3 // Release 1.5
4 // Copyright (C) 1998 Sam Hawker <shawkie@geocities.com>
5 // Copyright (C) 2002 Gordon Fraser <gordon@debian.org>
6 // This software comes with ABSOLUTELY NO WARRANTY
7 // This software is free software, and you are welcome to redistribute it
8 // under certain conditions
9 // See the COPYING file for details.
11 #include "xhandler.h"
13 //--------------------------------------------------------------------
14 XHandler::XHandler()
16 is_wmaker_ = WINDOWMAKER;
17 is_ushape_ = USESHAPE;
18 is_astep_ = AFTERSTEP;
20 strcpy(display_name_, "");
21 strcpy(position_name_, "");
22 strcpy(ledcolor_name_, LEDCOLOR);
23 strcpy(ledcolor_high_name_, LEDCOLOR_HIGH);
24 strcpy(backcolor_name_, BACKCOLOR);
26 button_state_ = 0;
29 //--------------------------------------------------------------------
30 XHandler::~XHandler()
32 XFreeGC(display_default_, graphics_context_);
33 XFreePixmap(display_default_, pixmap_main);
34 XFreePixmap(display_default_, pixmap_tile);
35 XFreePixmap(display_default_, pixmap_disp);
36 XFreePixmap(display_default_, pixmap_mask);
37 XFreePixmap(display_default_, pixmap_icon);
38 XFreePixmap(display_default_, pixmap_nrec);
40 XDestroyWindow(display_default_, window_main_);
42 if(is_wmaker_)
43 XDestroyWindow(display_default_, window_icon_);
45 XCloseDisplay(display_default_);
47 delete[] icon_list_;
51 //--------------------------------------------------------------------
52 void XHandler::init(int argc, char** argv, int num_channels)
54 int display_depth;
56 window_size_=is_astep_ ? ASTEPSIZE : NORMSIZE;
58 if((display_default_ = XOpenDisplay(display_name_))==NULL)
60 std::cerr << NAME << " : Unable to open X display '" << XDisplayName(display_name_) << "'." << std::endl;
61 exit(1);
64 initWindow(argc, argv);
66 initColors();
68 display_depth = DefaultDepth(display_default_, DefaultScreen(display_default_));
69 initPixmaps(display_depth);
71 initGraphicsContext();
73 initMask();
75 initIcons(num_channels);
79 //--------------------------------------------------------------------
80 bool XHandler::isLeftButton(int x, int y)
82 return(x>=BTN_LEFT_X && y>=BTN_LEFT_Y && x<=BTN_LEFT_X + BTN_WIDTH && y<=BTN_LEFT_Y + BTN_HEIGHT);
85 //--------------------------------------------------------------------
86 bool XHandler::isRightButton(int x, int y)
88 return(x>=BTN_RIGHT_X && y>=BTN_RIGHT_Y && x<=BTN_RIGHT_X + BTN_WIDTH && y<=BTN_RIGHT_Y + BTN_HEIGHT);
91 //--------------------------------------------------------------------
92 bool XHandler::isMuteButton(int x, int y)
94 return(x>=BTN_MUTE_X && y>=BTN_MUTE_Y && x<=BTN_MUTE_X + BTN_WIDTH && y<=BTN_MUTE_Y + BTN_HEIGHT);
97 //--------------------------------------------------------------------
98 bool XHandler::isRecButton(int x, int y)
100 return(x>=BTN_REC_X && y>=BTN_REC_Y && x<=BTN_REC_X + BTN_WIDTH && y<=BTN_REC_Y + BTN_HEIGHT);
103 //--------------------------------------------------------------------
104 bool XHandler::isVolumeBar(int x, int y)
106 return(x>=37 && x<=56 && y>=8 && y<=56);
109 //--------------------------------------------------------------------
110 unsigned long XHandler::getColor(char *colorname)
112 XColor color;
113 XWindowAttributes winattr;
115 XGetWindowAttributes(display_default_, window_root_, &winattr);
116 color.pixel=0;
117 XParseColor(display_default_, winattr.colormap, colorname, &color);
118 color.flags=DoRed | DoGreen | DoBlue;
119 XAllocColor(display_default_, winattr.colormap, &color);
121 return color.pixel;
124 //--------------------------------------------------------------------
125 unsigned long XHandler::mixColor(char *colorname1, int prop1, char *colorname2, int prop2)
127 XColor color, color1, color2;
128 XWindowAttributes winattr;
130 XGetWindowAttributes(display_default_, window_root_, &winattr);
132 XParseColor(display_default_, winattr.colormap, colorname1, &color1);
133 XParseColor(display_default_, winattr.colormap, colorname2, &color2);
135 color.pixel = 0;
136 color.red = (color1.red*prop1+color2.red*prop2)/(prop1+prop2);
137 color.green = (color1.green*prop1+color2.green*prop2)/(prop1+prop2);
138 color.blue = (color1.blue*prop1+color2.blue*prop2)/(prop1+prop2);
139 color.flags = DoRed | DoGreen | DoBlue;
141 XAllocColor(display_default_, winattr.colormap, &color);
143 return color.pixel;
146 //--------------------------------------------------------------------
147 void XHandler::repaint()
149 flush_expose(window_icon_);
150 XCopyArea(display_default_, pixmap_disp, window_icon_, graphics_context_, 0, 0, 64, 64, window_size_/2-32, window_size_/2-32);
151 flush_expose(window_main_);
152 XCopyArea(display_default_, pixmap_disp, window_main_, graphics_context_, 0, 0, 64, 64, window_size_/2-32, window_size_/2-32);
154 XEvent xev;
155 while(XCheckTypedEvent(display_default_, Expose, &xev));
158 //--------------------------------------------------------------------
159 void XHandler::update(unsigned channel)
161 if(is_wmaker_ || is_ushape_ || is_astep_)
163 XShapeCombineMask(display_default_, window_icon_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
164 XShapeCombineMask(display_default_, window_main_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
166 else
168 XCopyArea(display_default_, pixmap_tile, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
171 XSetClipMask(display_default_, graphics_context_, pixmap_mask);
172 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
173 XSetClipMask(display_default_, graphics_context_, None);
174 XCopyArea(display_default_, pixmap_icon, pixmap_disp, graphics_context_, icon_list_[channel]*22, 0, 22, 22, 6, 5);
177 //--------------------------------------------------------------------
178 void XHandler::drawLeft(unsigned curleft)
180 XSetForeground(display_default_, graphics_context_, shade_colors_[(curleft*25)/100]);
181 for(unsigned i=0;i<25;i++)
183 if(i >= (curleft*25)/100)
185 XSetForeground(display_default_, graphics_context_, colors_[3]);
187 else
189 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
191 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 37, 55-2*i, 9, 1);
195 //--------------------------------------------------------------------
196 void XHandler::drawRight(unsigned curright)
198 for(unsigned i=0;i<25;i++)
200 if(i >= (curright*25)/100)
202 XSetForeground(display_default_, graphics_context_, colors_[3]);
204 else
206 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
208 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 48, 55-2*i, 9, 1);
212 //--------------------------------------------------------------------
213 // Based on wmsmixer by Damian Kramer <psiren@hibernaculum.demon.co.uk>
214 void XHandler::drawMono(unsigned curright)
216 XSetForeground(display_default_, graphics_context_, colors_[1]);
217 for(unsigned i=0;i<25;i++)
219 if(i >= (curright*25)/100)
221 XSetForeground(display_default_, graphics_context_, colors_[3]);
223 else
225 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
227 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 37, 55-2*i, 20, 1);
232 //--------------------------------------------------------------------
233 void XHandler::drawBtns(int buttons, bool curshowrec)
235 if(buttons & BTNPREV)
236 drawButton(BTN_LEFT_X, BTN_LEFT_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNPREV));
238 if(buttons & BTNNEXT)
239 drawButton(BTN_RIGHT_X, BTN_RIGHT_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNNEXT));
241 if(buttons & BTNMUTE)
242 drawButton(BTN_MUTE_X, BTN_MUTE_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNMUTE));
244 if(buttons & BTNREC){
245 drawButton(BTN_REC_X, BTN_REC_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNREC));
247 if(!curshowrec)
248 XCopyArea(display_default_, pixmap_nrec, pixmap_disp, graphics_context_, 0, 0, 9, 8, 6, 47);
249 else
250 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, 6, 48, 9, 8, 6, 47);
254 //--------------------------------------------------------------------
255 void XHandler::drawButton(int x, int y, int w, int h, bool down)
257 if(!down)
258 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, w, h, x, y);
259 else {
260 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, 1, h-1, x+w-1, y+1);
261 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x+w-1, y+1, 1, h-1, x, y);
262 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, w-1, 1, x+1, y+h-1);
263 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x+1, y+h-1, w-1, 1, x, y);
267 //--------------------------------------------------------------------
268 int XHandler::flush_expose(Window w)
270 XEvent dummy;
271 int i=0;
273 while (XCheckTypedWindowEvent(display_default_, w, Expose, &dummy))
274 i++;
276 return i;
280 //--------------------------------------------------------------------
281 int XHandler::getWindowSize()
283 return window_size_;
286 //--------------------------------------------------------------------
287 // --> inline
288 //Display* XHandler::getDisplay()
290 // return display_default_;
294 //--------------------------------------------------------------------
295 int XHandler::getButtonState()
297 return button_state_;
300 //--------------------------------------------------------------------
301 void XHandler::setButtonState(int button_state)
303 button_state_ = button_state;
306 //--------------------------------------------------------------------
307 void XHandler::setDisplay(char* arg)
309 sprintf(display_name_, "%s", arg);
312 //--------------------------------------------------------------------
313 void XHandler::setPosition(char* arg)
315 sprintf(position_name_, "%s", arg);
318 //--------------------------------------------------------------------
319 void XHandler::setLedColor(char* arg)
321 sprintf(ledcolor_name_, "%s", arg);
324 //--------------------------------------------------------------------
325 void XHandler::setLedHighColor(char* arg)
327 sprintf(ledcolor_high_name_, "%s", arg);
330 //--------------------------------------------------------------------
331 void XHandler::setBackColor(char* arg)
333 sprintf(backcolor_name_, "%s", arg);
336 //--------------------------------------------------------------------
337 void XHandler::setUnshaped()
339 is_ushape_ = 1;
342 //--------------------------------------------------------------------
343 void XHandler::setWindowMaker()
345 is_wmaker_ = 1;
348 //--------------------------------------------------------------------
349 void XHandler::setAfterStep()
351 is_astep_ = 1;
354 //--------------------------------------------------------------------
355 Atom XHandler::getDeleteWin()
357 return deleteWin;
361 //--------------------------------------------------------------------
362 void XHandler::initIcons(int num)
364 if(icon_list_)
365 delete[] icon_list_;
367 icon_list_ = new unsigned[num];
369 icon_list_[0] = 0;
370 icon_list_[1] = 7;
371 icon_list_[2] = 8;
372 icon_list_[3] = 2;
373 icon_list_[4] = 1;
374 icon_list_[5] = 6;
375 icon_list_[6] = 4;
376 icon_list_[7] = 5;
377 icon_list_[8] = 3;
378 for(int counter=9; counter<num; counter++)
379 icon_list_[counter] = 9;
382 //--------------------------------------------------------------------
383 void XHandler::initGraphicsContext()
385 XGCValues gcv;
386 unsigned long gcm;
388 gcm = GCForeground | GCBackground | GCGraphicsExposures;
389 gcv.graphics_exposures = 0;
390 gcv.foreground = fore_pix;
391 gcv.background = back_pix;
392 graphics_context_ = XCreateGC(display_default_, window_root_, gcm, &gcv);
395 //--------------------------------------------------------------------
396 void XHandler::initPixmaps(int display_depth)
398 XpmColorSymbol xpmcsym[4]={{"back_color", NULL, colors_[0]},
399 {"led_color_high", NULL, colors_[1]},
400 {"led_color_med", NULL, colors_[2]},
401 {"led_color_low", NULL, colors_[3]}};
402 XpmAttributes xpmattr;
404 xpmattr.numsymbols = 4;
405 xpmattr.colorsymbols = xpmcsym;
406 xpmattr.exactColors = false;
407 xpmattr.closeness = 40000;
408 xpmattr.valuemask = XpmColorSymbols | XpmExactColors | XpmCloseness;
410 XpmCreatePixmapFromData(display_default_, window_root_, wmmixer_xpm, &pixmap_main, &pixmap_mask, &xpmattr);
411 XpmCreatePixmapFromData(display_default_, window_root_, tile_xpm, &pixmap_tile, NULL, &xpmattr);
412 XpmCreatePixmapFromData(display_default_, window_root_, icons_xpm, &pixmap_icon, NULL, &xpmattr);
413 XpmCreatePixmapFromData(display_default_, window_root_, norec_xpm, &pixmap_nrec, NULL, &xpmattr);
415 pixmap_disp = XCreatePixmap(display_default_, window_root_, 64, 64, display_depth);
419 //--------------------------------------------------------------------
420 void XHandler::initWindow(int argc, char** argv)
422 char *wname = argv[0];
423 int screen, dummy = 0;
424 XWMHints wmhints;
425 XSizeHints shints;
426 XClassHint classHint;
427 XTextProperty name;
429 screen = DefaultScreen(display_default_);
430 _XA_GNUSTEP_WM_FUNC = XInternAtom(display_default_, "_GNUSTEP_WM_FUNCTION", false);
431 deleteWin = XInternAtom(display_default_, "WM_DELETE_WINDOW", false);
434 shints.x = 0;
435 shints.y = 0;
436 // shints.flags = USSize;
437 shints.flags = 0; // Gordon
439 bool pos = (XWMGeometry(display_default_, DefaultScreen(display_default_),
440 position_name_, NULL, 0, &shints, &shints.x, &shints.y,
441 &shints.width, &shints.height, &dummy)
442 & (XValue | YValue));
443 shints.min_width = window_size_;
444 shints.min_height = window_size_;
445 shints.max_width = window_size_;
446 shints.max_height = window_size_;
447 shints.base_width = window_size_;
448 shints.base_height = window_size_;
449 shints.width = window_size_;
450 shints.height = window_size_;
451 shints.flags=PMinSize | PMaxSize | PBaseSize; // Gordon
454 window_root_ = RootWindow(display_default_, screen);
456 back_pix = getColor("white");
457 fore_pix = getColor("black");
459 window_main_ = XCreateSimpleWindow(display_default_, window_root_, shints.x, shints.y,
460 shints.width, shints.height, 0, fore_pix, back_pix);
462 window_icon_ = XCreateSimpleWindow(display_default_, window_root_, shints.x, shints.y,
463 shints.width, shints.height, 0, fore_pix, back_pix);
465 XSetWMNormalHints(display_default_, window_main_, &shints);
468 wmhints.icon_x = shints.x;
469 wmhints.icon_y = shints.y;
471 if(is_wmaker_ || is_astep_ || pos)
472 shints.flags |= USPosition;
474 if(is_wmaker_)
476 wmhints.initial_state = WithdrawnState;
477 wmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
478 wmhints.icon_window = window_icon_;
480 wmhints.icon_x = shints.x;
481 wmhints.icon_y = shints.y;
482 wmhints.window_group = window_main_;
484 else
486 wmhints.initial_state = NormalState;
487 wmhints.flags = WindowGroupHint | StateHint;
490 classHint.res_name=NAME;
491 classHint.res_class=CLASS;
493 XSetClassHint(display_default_, window_main_, &classHint);
494 XSetClassHint(display_default_, window_icon_, &classHint);
497 if (XStringListToTextProperty(&wname, 1, &name) == 0)
499 std::cerr << wname << ": can't allocate window name" << std::endl;
500 exit(1);
503 XSetWMName(display_default_, window_main_, &name);
504 XSetWMHints(display_default_, window_main_, &wmhints);
505 XSetCommand(display_default_, window_main_, argv, argc);
506 XSetWMProtocols(display_default_, window_main_, &deleteWin, 1); // Close
510 //--------------------------------------------------------------------
511 // Initialize main colors and shaded color-array for bars
512 void XHandler::initColors()
514 colors_[0] = mixColor(ledcolor_name_, 0, backcolor_name_, 100);
515 colors_[1] = mixColor(ledcolor_name_, 100, backcolor_name_, 0);
516 colors_[2] = mixColor(ledcolor_name_, 60, backcolor_name_, 40);
517 colors_[3] = mixColor(ledcolor_name_, 25, backcolor_name_, 75);
519 for(int count=0; count<25; count++)
521 shade_colors_[count] = mixColor(ledcolor_high_name_, count*2, ledcolor_name_, 100-count*4);
526 //--------------------------------------------------------------------
527 void XHandler::initMask()
529 XSetClipMask(display_default_, graphics_context_, pixmap_mask);
530 XCopyArea( display_default_, pixmap_main, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
531 XSetClipMask(display_default_, graphics_context_, None);
532 XStoreName( display_default_, window_main_, NAME);
533 XSetIconName(display_default_, window_main_, NAME);
535 if(is_wmaker_ || is_ushape_ || is_astep_)
537 XShapeCombineMask(display_default_, window_icon_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
538 XShapeCombineMask(display_default_, window_main_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
540 else
542 XCopyArea(display_default_, pixmap_tile, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
545 XSelectInput(display_default_, window_main_, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
546 XSelectInput(display_default_, window_icon_, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
547 XMapWindow(display_default_, window_main_);