Make AddMouseRegion's index unsigned
[dockapps.git] / wmmixer / xhandler.cc
blob95d76a2a9504d7b396ab22d6c533051ee9e74788
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);
25 strcpy(window_class_, CLASS);
27 button_state_ = 0;
28 icon_list_ = NULL;
31 //--------------------------------------------------------------------
32 XHandler::~XHandler()
34 XFreeGC(display_default_, graphics_context_);
35 XFreePixmap(display_default_, pixmap_main);
36 XFreePixmap(display_default_, pixmap_tile);
37 XFreePixmap(display_default_, pixmap_disp);
38 XFreePixmap(display_default_, pixmap_mask);
39 XFreePixmap(display_default_, pixmap_icon);
40 XFreePixmap(display_default_, pixmap_nrec);
42 XDestroyWindow(display_default_, window_main_);
44 if(is_wmaker_)
45 XDestroyWindow(display_default_, window_icon_);
47 XCloseDisplay(display_default_);
49 delete[] icon_list_;
53 //--------------------------------------------------------------------
54 void XHandler::init(int argc, char** argv, int num_channels)
56 int display_depth;
58 window_size_=is_astep_ ? ASTEPSIZE : NORMSIZE;
60 if((display_default_ = XOpenDisplay(display_name_))==NULL)
62 std::cerr << NAME << " : Unable to open X display '" << XDisplayName(display_name_) << "'." << std::endl;
63 exit(1);
66 initWindow(argc, argv);
68 initColors();
70 display_depth = DefaultDepth(display_default_, DefaultScreen(display_default_));
71 initPixmaps(display_depth);
73 initGraphicsContext();
75 initMask();
77 initIcons(num_channels);
81 //--------------------------------------------------------------------
82 bool XHandler::isLeftButton(int x, int y)
84 return(x>=BTN_LEFT_X && y>=BTN_LEFT_Y && x<=BTN_LEFT_X + BTN_WIDTH && y<=BTN_LEFT_Y + BTN_HEIGHT);
87 //--------------------------------------------------------------------
88 bool XHandler::isRightButton(int x, int y)
90 return(x>=BTN_RIGHT_X && y>=BTN_RIGHT_Y && x<=BTN_RIGHT_X + BTN_WIDTH && y<=BTN_RIGHT_Y + BTN_HEIGHT);
93 //--------------------------------------------------------------------
94 bool XHandler::isMuteButton(int x, int y)
96 return(x>=BTN_MUTE_X && y>=BTN_MUTE_Y && x<=BTN_MUTE_X + BTN_WIDTH && y<=BTN_MUTE_Y + BTN_HEIGHT);
99 //--------------------------------------------------------------------
100 bool XHandler::isRecButton(int x, int y)
102 return(x>=BTN_REC_X && y>=BTN_REC_Y && x<=BTN_REC_X + BTN_WIDTH && y<=BTN_REC_Y + BTN_HEIGHT);
105 //--------------------------------------------------------------------
106 bool XHandler::isVolumeBar(int x, int y)
108 return(x>=37 && x<=56 && y>=8 && y<=56);
111 //--------------------------------------------------------------------
112 unsigned long XHandler::getColor(char *colorname)
114 XColor color;
115 XWindowAttributes winattr;
117 XGetWindowAttributes(display_default_, window_root_, &winattr);
118 color.pixel=0;
119 XParseColor(display_default_, winattr.colormap, colorname, &color);
120 color.flags=DoRed | DoGreen | DoBlue;
121 XAllocColor(display_default_, winattr.colormap, &color);
123 return color.pixel;
126 //--------------------------------------------------------------------
127 unsigned long XHandler::mixColor(char *colorname1, int prop1, char *colorname2, int prop2)
129 XColor color, color1, color2;
130 XWindowAttributes winattr;
132 XGetWindowAttributes(display_default_, window_root_, &winattr);
134 XParseColor(display_default_, winattr.colormap, colorname1, &color1);
135 XParseColor(display_default_, winattr.colormap, colorname2, &color2);
137 color.pixel = 0;
138 color.red = (color1.red*prop1+color2.red*prop2)/(prop1+prop2);
139 color.green = (color1.green*prop1+color2.green*prop2)/(prop1+prop2);
140 color.blue = (color1.blue*prop1+color2.blue*prop2)/(prop1+prop2);
141 color.flags = DoRed | DoGreen | DoBlue;
143 XAllocColor(display_default_, winattr.colormap, &color);
145 return color.pixel;
148 //--------------------------------------------------------------------
149 void XHandler::repaint()
151 flush_expose(window_icon_);
152 XCopyArea(display_default_, pixmap_disp, window_icon_, graphics_context_, 0, 0, 64, 64, window_size_/2-32, window_size_/2-32);
153 flush_expose(window_main_);
154 XCopyArea(display_default_, pixmap_disp, window_main_, graphics_context_, 0, 0, 64, 64, window_size_/2-32, window_size_/2-32);
156 XEvent xev;
157 while(XCheckTypedEvent(display_default_, Expose, &xev));
160 //--------------------------------------------------------------------
161 void XHandler::update(unsigned channel)
163 if(is_wmaker_ || is_ushape_ || is_astep_)
165 XShapeCombineMask(display_default_, window_icon_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
166 XShapeCombineMask(display_default_, window_main_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
168 else
170 XCopyArea(display_default_, pixmap_tile, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
173 XSetClipMask(display_default_, graphics_context_, pixmap_mask);
174 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
175 XSetClipMask(display_default_, graphics_context_, None);
176 XCopyArea(display_default_, pixmap_icon, pixmap_disp, graphics_context_, icon_list_[channel]*22, 0, 22, 22, 6, 5);
179 //--------------------------------------------------------------------
180 void XHandler::drawLeft(unsigned curleft)
182 XSetForeground(display_default_, graphics_context_, shade_colors_[(curleft*25)/100]);
183 for(unsigned i=0;i<25;i++)
185 if(i >= (curleft*25)/100)
187 XSetForeground(display_default_, graphics_context_, colors_[3]);
189 else
191 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
193 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 37, 55-2*i, 9, 1);
197 //--------------------------------------------------------------------
198 void XHandler::drawRight(unsigned curright)
200 for(unsigned i=0;i<25;i++)
202 if(i >= (curright*25)/100)
204 XSetForeground(display_default_, graphics_context_, colors_[3]);
206 else
208 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
210 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 48, 55-2*i, 9, 1);
214 //--------------------------------------------------------------------
215 // Based on wmsmixer by Damian Kramer <psiren@hibernaculum.demon.co.uk>
216 void XHandler::drawMono(unsigned curright)
218 XSetForeground(display_default_, graphics_context_, colors_[1]);
219 for(unsigned i=0;i<25;i++)
221 if(i >= (curright*25)/100)
223 XSetForeground(display_default_, graphics_context_, colors_[3]);
225 else
227 XSetForeground(display_default_, graphics_context_, shade_colors_[i]);
229 XFillRectangle(display_default_, pixmap_disp, graphics_context_, 37, 55-2*i, 20, 1);
234 //--------------------------------------------------------------------
235 void XHandler::drawBtns(int buttons, bool curshowrec)
237 if(buttons & BTNPREV)
238 drawButton(BTN_LEFT_X, BTN_LEFT_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNPREV));
240 if(buttons & BTNNEXT)
241 drawButton(BTN_RIGHT_X, BTN_RIGHT_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNNEXT));
243 if(buttons & BTNMUTE)
244 drawButton(BTN_MUTE_X, BTN_MUTE_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNMUTE));
246 if(buttons & BTNREC){
247 drawButton(BTN_REC_X, BTN_REC_Y, BTN_WIDTH, BTN_HEIGHT, (button_state_ & BTNREC));
249 if(!curshowrec)
250 XCopyArea(display_default_, pixmap_nrec, pixmap_disp, graphics_context_, 0, 0, 9, 8, 6, 47);
251 else
252 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, 6, 48, 9, 8, 6, 47);
256 //--------------------------------------------------------------------
257 void XHandler::drawButton(int x, int y, int w, int h, bool down)
259 if(!down)
260 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, w, h, x, y);
261 else {
262 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, 1, h-1, x+w-1, y+1);
263 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x+w-1, y+1, 1, h-1, x, y);
264 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x, y, w-1, 1, x+1, y+h-1);
265 XCopyArea(display_default_, pixmap_main, pixmap_disp, graphics_context_, x+1, y+h-1, w-1, 1, x, y);
269 //--------------------------------------------------------------------
270 int XHandler::flush_expose(Window w)
272 XEvent dummy;
273 int i=0;
275 while (XCheckTypedWindowEvent(display_default_, w, Expose, &dummy))
276 i++;
278 return i;
282 //--------------------------------------------------------------------
283 int XHandler::getWindowSize()
285 return window_size_;
288 //--------------------------------------------------------------------
289 // --> inline
290 //Display* XHandler::getDisplay()
292 // return display_default_;
296 //--------------------------------------------------------------------
297 int XHandler::getButtonState()
299 return button_state_;
302 //--------------------------------------------------------------------
303 void XHandler::setButtonState(int button_state)
305 button_state_ = button_state;
308 //--------------------------------------------------------------------
309 void XHandler::setDisplay(char* arg)
311 sprintf(display_name_, "%s", arg);
314 //--------------------------------------------------------------------
315 void XHandler::setPosition(char* arg)
317 sprintf(position_name_, "%s", arg);
320 //--------------------------------------------------------------------
321 void XHandler::setLedColor(char* arg)
323 sprintf(ledcolor_name_, "%s", arg);
326 //--------------------------------------------------------------------
327 void XHandler::setLedHighColor(char* arg)
329 sprintf(ledcolor_high_name_, "%s", arg);
332 //--------------------------------------------------------------------
333 void XHandler::setBackColor(char* arg)
335 sprintf(backcolor_name_, "%s", arg);
338 //--------------------------------------------------------------------
339 void XHandler::setWindowClass(char* arg)
341 snprintf(window_class_, 254, "%s", arg);
344 //--------------------------------------------------------------------
345 void XHandler::setUnshaped()
347 is_ushape_ = 1;
350 //--------------------------------------------------------------------
351 void XHandler::setWindowMaker()
353 is_wmaker_ = 1;
356 //--------------------------------------------------------------------
357 void XHandler::setAfterStep()
359 is_astep_ = 1;
362 //--------------------------------------------------------------------
363 Atom XHandler::getDeleteWin()
365 return deleteWin;
369 //--------------------------------------------------------------------
370 void XHandler::initIcons(int num)
372 if(icon_list_)
373 delete[] icon_list_;
375 icon_list_ = new unsigned[num];
377 icon_list_[0] = 0;
378 icon_list_[1] = 7;
379 icon_list_[2] = 8;
380 icon_list_[3] = 2;
381 icon_list_[4] = 1;
382 icon_list_[5] = 6;
383 icon_list_[6] = 4;
384 icon_list_[7] = 5;
385 icon_list_[8] = 3;
386 for(int counter=9; counter<num; counter++)
387 icon_list_[counter] = 9;
390 //--------------------------------------------------------------------
391 void XHandler::initGraphicsContext()
393 XGCValues gcv;
394 unsigned long gcm;
396 gcm = GCForeground | GCBackground | GCGraphicsExposures;
397 gcv.graphics_exposures = 0;
398 gcv.foreground = fore_pix;
399 gcv.background = back_pix;
400 graphics_context_ = XCreateGC(display_default_, window_root_, gcm, &gcv);
403 //--------------------------------------------------------------------
404 void XHandler::initPixmaps(int display_depth)
406 XpmColorSymbol const xpmcsym[4]={{(char *)"back_color", NULL, colors_[0]},
407 {(char *)"led_color_high", NULL, colors_[1]},
408 {(char *)"led_color_med", NULL, colors_[2]},
409 {(char *)"led_color_low", NULL, colors_[3]}};
410 XpmAttributes xpmattr;
412 xpmattr.numsymbols = 4;
413 xpmattr.colorsymbols = (XpmColorSymbol *)xpmcsym;
414 xpmattr.exactColors = false;
415 xpmattr.closeness = 40000;
416 xpmattr.valuemask = XpmColorSymbols | XpmExactColors | XpmCloseness;
418 XpmCreatePixmapFromData(display_default_, window_root_, (char **)wmmixer_xpm, &pixmap_main, &pixmap_mask, &xpmattr);
419 XpmCreatePixmapFromData(display_default_, window_root_, (char **)tile_xpm, &pixmap_tile, NULL, &xpmattr);
420 XpmCreatePixmapFromData(display_default_, window_root_, (char **)icons_xpm, &pixmap_icon, NULL, &xpmattr);
421 XpmCreatePixmapFromData(display_default_, window_root_, (char **)norec_xpm, &pixmap_nrec, NULL, &xpmattr);
423 pixmap_disp = XCreatePixmap(display_default_, window_root_, 64, 64, display_depth);
427 //--------------------------------------------------------------------
428 void XHandler::initWindow(int argc, char** argv)
430 char *wname = argv[0];
431 int screen, dummy = 0;
432 XWMHints wmhints;
433 XSizeHints shints;
434 XClassHint classHint;
435 XTextProperty name;
437 screen = DefaultScreen(display_default_);
438 _XA_GNUSTEP_WM_FUNC = XInternAtom(display_default_, "_GNUSTEP_WM_FUNCTION", false);
439 deleteWin = XInternAtom(display_default_, "WM_DELETE_WINDOW", false);
442 shints.x = 0;
443 shints.y = 0;
444 // shints.flags = USSize;
445 shints.flags = 0; // Gordon
447 bool pos = (XWMGeometry(display_default_, DefaultScreen(display_default_),
448 position_name_, NULL, 0, &shints, &shints.x, &shints.y,
449 &shints.width, &shints.height, &dummy)
450 & (XValue | YValue));
451 shints.min_width = window_size_;
452 shints.min_height = window_size_;
453 shints.max_width = window_size_;
454 shints.max_height = window_size_;
455 shints.base_width = window_size_;
456 shints.base_height = window_size_;
457 shints.width = window_size_;
458 shints.height = window_size_;
459 shints.flags=PMinSize | PMaxSize | PBaseSize; // Gordon
462 window_root_ = RootWindow(display_default_, screen);
464 back_pix = getColor((char *)"white");
465 fore_pix = getColor((char *)"black");
467 window_main_ = XCreateSimpleWindow(display_default_, window_root_, shints.x, shints.y,
468 shints.width, shints.height, 0, fore_pix, back_pix);
470 window_icon_ = XCreateSimpleWindow(display_default_, window_root_, shints.x, shints.y,
471 shints.width, shints.height, 0, fore_pix, back_pix);
473 XSetWMNormalHints(display_default_, window_main_, &shints);
476 wmhints.icon_x = shints.x;
477 wmhints.icon_y = shints.y;
479 if(is_wmaker_ || is_astep_ || pos)
480 shints.flags |= USPosition;
482 if(is_wmaker_)
484 wmhints.initial_state = WithdrawnState;
485 wmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
486 wmhints.icon_window = window_icon_;
488 wmhints.icon_x = shints.x;
489 wmhints.icon_y = shints.y;
490 wmhints.window_group = window_main_;
492 else
494 wmhints.initial_state = NormalState;
495 wmhints.flags = WindowGroupHint | StateHint;
498 classHint.res_name=(char *)NAME;
499 classHint.res_class = window_class_;
501 XSetClassHint(display_default_, window_main_, &classHint);
502 XSetClassHint(display_default_, window_icon_, &classHint);
505 if (XStringListToTextProperty(&wname, 1, &name) == 0)
507 std::cerr << wname << ": can't allocate window name" << std::endl;
508 exit(1);
511 XSetWMName(display_default_, window_main_, &name);
512 XSetWMHints(display_default_, window_main_, &wmhints);
513 XSetCommand(display_default_, window_main_, argv, argc);
514 XSetWMProtocols(display_default_, window_main_, &deleteWin, 1); // Close
518 //--------------------------------------------------------------------
519 // Initialize main colors and shaded color-array for bars
520 void XHandler::initColors()
522 colors_[0] = mixColor(ledcolor_name_, 0, backcolor_name_, 100);
523 colors_[1] = mixColor(ledcolor_name_, 100, backcolor_name_, 0);
524 colors_[2] = mixColor(ledcolor_name_, 60, backcolor_name_, 40);
525 colors_[3] = mixColor(ledcolor_name_, 25, backcolor_name_, 75);
527 for(int count=0; count<25; count++)
529 shade_colors_[count] = mixColor(ledcolor_high_name_, count*2, ledcolor_name_, 100-count*4);
534 //--------------------------------------------------------------------
535 void XHandler::initMask()
537 XSetClipMask(display_default_, graphics_context_, pixmap_mask);
538 XCopyArea( display_default_, pixmap_main, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
539 XSetClipMask(display_default_, graphics_context_, None);
540 XStoreName( display_default_, window_main_, NAME);
541 XSetIconName(display_default_, window_main_, NAME);
543 if(is_wmaker_ || is_ushape_ || is_astep_)
545 XShapeCombineMask(display_default_, window_icon_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
546 XShapeCombineMask(display_default_, window_main_, ShapeBounding, window_size_/2-32, window_size_/2-32, pixmap_mask, ShapeSet);
548 else
550 XCopyArea(display_default_, pixmap_tile, pixmap_disp, graphics_context_, 0, 0, 64, 64, 0, 0);
553 XSelectInput(display_default_, window_main_, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
554 XSelectInput(display_default_, window_icon_, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
555 XMapWindow(display_default_, window_main_);