wmclockmon: update change-log
[dockapps.git] / wmthrottle / src / dockapp.c
blob90feaff3f21eb40f75aaa081bbe4e398ce98ec4a
1 /*
2 * Copyright (c) 1999 Alfredo K. Kojima
3 * Copyright (c) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * This code is based on libdockapp-0.4.0
23 * modified by Seiichi SATO <ssato@sh.rim.or.jp>
26 #include "dockapp.h"
28 #define WINDOWED_SIZE_W 64
29 #define WINDOWED_SIZE_H 64
31 /* global */
32 Display *display = NULL;
33 Bool dockapp_iswindowed = False;
34 Bool dockapp_isbrokenwm = False;
36 /* private */
37 static Window window = None;
38 static Window icon_window = None;
39 static GC gc = NULL;
40 static int depth = 0;
41 static Atom delete_win;
42 static int width, height;
43 static int offset_w, offset_h;
45 void
46 dockapp_open_window(char *display_specified, char *appname,
47 unsigned w, unsigned h, int argc, char **argv)
49 XClassHint *classhint;
50 XWMHints *wmhints;
51 Status stat;
52 XTextProperty title;
53 XSizeHints sizehints;
54 Window root;
55 int ww, wh;
57 /* Open Connection to X Server */
58 display = XOpenDisplay(display_specified);
59 if (!display) {
60 fprintf(stderr, "%s: could not open display %s!\n", argv[0],
61 XDisplayName(display_specified));
62 exit(1);
64 root = DefaultRootWindow(display);
66 width = w;
67 height = h;
69 if (dockapp_iswindowed) {
70 offset_w = (WINDOWED_SIZE_W - w) / 2;
71 offset_h = (WINDOWED_SIZE_H - h) / 2;
72 ww = WINDOWED_SIZE_W;
73 wh = WINDOWED_SIZE_H;
74 } else {
75 offset_w = offset_h = 0;
76 ww = w;
77 wh = h;
80 /* Create Windows */
81 icon_window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0);
82 if (dockapp_isbrokenwm) {
83 window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0);
84 } else {
85 window = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
88 /* Set ClassHint */
89 classhint = XAllocClassHint();
90 if (classhint == NULL) {
91 fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
92 exit(1);
94 classhint->res_class = "DockApp";
95 classhint->res_name = appname;
96 XSetClassHint(display, window, classhint);
97 XFree(classhint);
99 /* Set WMHints */
100 wmhints = XAllocWMHints();
101 if (wmhints == NULL) {
102 fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
103 exit(1);
105 wmhints->flags = IconWindowHint | WindowGroupHint;
106 if (!dockapp_iswindowed) {
107 wmhints->flags |= StateHint;
108 wmhints->initial_state = WithdrawnState;
110 wmhints->window_group = window;
111 wmhints->icon_window = icon_window;
112 XSetWMHints(display, window, wmhints);
113 XFree(wmhints);
115 /* Set WM Protocols */
116 delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False);
117 XSetWMProtocols (display, icon_window, &delete_win, 1);
119 /* Set Size Hints */
120 sizehints.flags = USSize;
121 if (!dockapp_iswindowed) {
122 sizehints.flags |= USPosition;
123 sizehints.x = sizehints.y = 0;
124 } else {
125 sizehints.flags |= PMinSize | PMaxSize;
126 sizehints.min_width = sizehints.max_width = WINDOWED_SIZE_W;
127 sizehints.min_height = sizehints.max_height = WINDOWED_SIZE_H;
129 sizehints.width = ww;
130 sizehints.height = wh;
131 XSetWMNormalHints(display, icon_window, &sizehints);
133 /* Set WindowTitle for AfterStep Wharf */
134 stat = XStringListToTextProperty(&appname, 1, &title);
135 XSetWMName(display, window, &title);
136 XSetWMName(display, icon_window, &title);
138 /* Set Command to start the app so it can be docked properly */
139 XSetCommand(display, window, argv, argc);
141 depth = DefaultDepth(display, DefaultScreen(display));
142 gc = DefaultGC(display, DefaultScreen(display));
144 XFlush(display);
148 void
149 dockapp_set_eventmask(long mask)
151 XSelectInput(display, icon_window, mask);
152 XSelectInput(display, window, mask);
155 void init_dock(void) {
156 AddMouseRegion(0, 6, 4, 16, 15);
157 AddMouseRegion(1, 18, 4, 28, 15);
158 AddMouseRegion(2, 30, 4, 40, 15);
159 AddMouseRegion(3, 42, 4, 52, 15);
160 AddMouseRegion(4, 6, 16, 16, 27);
161 AddMouseRegion(5, 18, 16, 28, 27);
162 AddMouseRegion(6, 30, 16, 40, 27);
163 AddMouseRegion(7, 42, 16, 52, 27);
164 AddMouseRegion(8, 42, 44, 53, 55);
165 EnableMouseRegion(0);
166 EnableMouseRegion(1);
167 EnableMouseRegion(2);
168 EnableMouseRegion(3);
169 EnableMouseRegion(4);
170 EnableMouseRegion(5);
171 EnableMouseRegion(6);
172 EnableMouseRegion(7);
173 EnableMouseRegion(8);
176 static Pixmap
177 create_bg_pixmap(void)
179 Pixmap bg;
181 bg = XCreatePixmap(display, icon_window, WINDOWED_SIZE_W, WINDOWED_SIZE_H,
182 depth);
183 XSetForeground(display, gc, dockapp_getcolor("rgb:ae/aa/ae"));
184 XFillRectangle(display, bg, gc, 0, 0, WINDOWED_SIZE_W, WINDOWED_SIZE_H);
185 XSetForeground(display, gc, dockapp_getcolor("rgb:ff/ff/ff"));
186 XDrawLine(display, bg, gc, 0, 0, 0, 63);
187 XDrawLine(display, bg, gc, 1, 0, 1, 62);
188 XDrawLine(display, bg, gc, 2, 0, 63, 0);
189 XDrawLine(display, bg, gc, 2, 1, 62, 1);
190 XSetForeground(display, gc, dockapp_getcolor("rgb:52/55/52"));
191 XDrawLine(display, bg, gc, 1, 63, 63, 63);
192 XDrawLine(display, bg, gc, 2, 62, 63, 62);
193 XDrawLine(display, bg, gc, 63, 1, 63, 61);
194 XDrawLine(display, bg, gc, 62, 2, 62, 61);
196 return bg;
200 void
201 dockapp_set_background(Pixmap pixmap)
203 if (dockapp_iswindowed) {
204 Pixmap bg;
205 bg = create_bg_pixmap();
206 XCopyArea(display, pixmap, bg, gc, 0, 0, width, height,
207 offset_w, offset_w);
208 XSetWindowBackgroundPixmap(display, icon_window, bg);
209 XSetWindowBackgroundPixmap(display, window, bg);
210 XFreePixmap(display, bg);
211 } else {
212 XSetWindowBackgroundPixmap(display, icon_window, pixmap);
213 XSetWindowBackgroundPixmap(display, window, pixmap);
215 XClearWindow(display, icon_window);
216 XFlush(display);
220 void
221 dockapp_show(void)
223 if (!dockapp_iswindowed)
224 XMapRaised(display, window);
225 else
226 XMapRaised(display, icon_window);
228 XFlush(display);
232 Bool
233 dockapp_xpm2pixmap(char **data, Pixmap *pixmap, Pixmap *mask,
234 XpmColorSymbol * colorSymbol, unsigned int nsymbols)
236 XpmAttributes xpmAttr;
237 xpmAttr.valuemask = XpmCloseness;
238 xpmAttr.closeness = 40000;
240 if (nsymbols) {
241 xpmAttr.colorsymbols = colorSymbol;
242 xpmAttr.numsymbols = nsymbols;
243 xpmAttr.valuemask |= XpmColorSymbols;
246 if (XpmCreatePixmapFromData(display, icon_window, data, pixmap, mask, &xpmAttr) != 0)
247 return False;
249 return True;
253 Pixmap
254 dockapp_XCreatePixmap(int w, int h)
256 return (XCreatePixmap(display, icon_window, w, h, depth));
260 void
261 dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs)
263 XShapeCombineMask(display, icon_window, ShapeBounding, -x_ofs, -y_ofs,
264 mask, ShapeSet);
265 XShapeCombineMask(display, window, ShapeBounding, -x_ofs, -y_ofs,
266 mask, ShapeSet);
267 XFlush(display);
271 void
272 dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src, int w, int h,
273 int x_dist, int y_dist)
275 XCopyArea(display, src, dist, gc, x_src, y_src, w, h, x_dist, y_dist);
279 void
280 dockapp_copy2window (Pixmap src)
282 if (dockapp_isbrokenwm) {
283 XCopyArea(display, src, window, gc, 0, 0, width, height, offset_w,
284 offset_h);
285 } else {
286 XCopyArea(display, src, icon_window, gc, 0, 0, width, height, offset_w,
287 offset_h);
292 Bool dockapp_nextevent_or_timeout(XEvent *event, unsigned long miliseconds)
294 struct timeval timeout;
295 fd_set rset;
297 XSync(display, False);
298 if (XPending(display)) {
299 XNextEvent(display, event);
300 return True;
303 timeout.tv_sec = miliseconds / 1000;
304 timeout.tv_usec = (miliseconds % 1000) * 1000;
306 FD_ZERO(&rset);
307 FD_SET(ConnectionNumber(display), &rset);
308 if (select(ConnectionNumber(display)+1, &rset, NULL, NULL, &timeout) > 0) {
309 XNextEvent(display, event);
310 if (event->type == ClientMessage) {
311 if (event->xclient.data.l[0] == delete_win) {
312 XDestroyWindow(display,event->xclient.window);
313 XCloseDisplay(display);
314 exit(0);
317 if (dockapp_iswindowed) {
318 event->xbutton.x -= offset_w;
319 event->xbutton.y -= offset_h;
321 return True;
324 return False;
328 unsigned long
329 dockapp_getcolor(char *color_name)
331 XColor color;
333 if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
334 color_name, &color))
335 fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
337 if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
338 &color)) {
339 fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
340 return BlackPixel(display, DefaultScreen(display));
343 return color.pixel;
347 unsigned long
348 dockapp_blendedcolor(char *color_name, int r, int g, int b, float fac)
350 XColor color;
352 if ((r < -255 || r > 255)||(g < -255 || g > 255)||(b < -255 || b > 255)){
353 fprintf(stderr, "r:%d,g:%d,b:%d (r,g,b must be 0 to 255)", r, g, b);
354 exit(1);
357 r *= 255;
358 g *= 255;
359 b *= 255;
361 if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
362 color_name, &color))
363 fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
365 if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
366 &color)) {
367 fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
368 return BlackPixel(display, DefaultScreen(display));
371 if (DefaultDepth(display, DefaultScreen(display)) < 16)
372 return color.pixel;
374 /* red */
375 if (color.red + r > 0xffff) {
376 color.red = 0xffff;
377 } else if (color.red + r < 0) {
378 color.red = 0;
379 } else {
380 color.red = (unsigned short)(fac * color.red + r);
383 /* green */
384 if (color.green + g > 0xffff) {
385 color.green = 0xffff;
386 } else if (color.green + g < 0) {
387 color.green = 0;
388 } else {
389 color.green = (unsigned short)(fac * color.green + g);
392 /* blue */
393 if (color.blue + b > 0xffff) {
394 color.blue = 0xffff;
395 } else if (color.blue + b < 0) {
396 color.blue = 0;
397 } else {
398 color.blue = (unsigned short)(fac * color.blue + b);
401 color.flags = DoRed | DoGreen | DoBlue;
403 if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
404 &color)) {
405 fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
406 return BlackPixel(display, DefaultScreen(display));
409 return color.pixel;