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>
28 #define WINDOWED_SIZE_W 64
29 #define WINDOWED_SIZE_H 64
32 Display
*display
= NULL
;
33 Bool dockapp_iswindowed
= False
;
34 Bool dockapp_isbrokenwm
= False
;
37 static Window window
= None
;
38 static Window icon_window
= None
;
41 static Atom delete_win
;
42 static int width
, height
;
43 static int offset_w
, offset_h
;
46 dockapp_open_window(char *display_specified
, char *appname
,
47 unsigned w
, unsigned h
, int argc
, char **argv
)
49 XClassHint
*classhint
;
57 /* Open Connection to X Server */
58 display
= XOpenDisplay(display_specified
);
60 fprintf(stderr
, "%s: could not open display %s!\n", argv
[0],
61 XDisplayName(display_specified
));
64 root
= DefaultRootWindow(display
);
69 if (dockapp_iswindowed
) {
70 offset_w
= (WINDOWED_SIZE_W
- w
) / 2;
71 offset_h
= (WINDOWED_SIZE_H
- h
) / 2;
75 offset_w
= offset_h
= 0;
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);
85 window
= XCreateSimpleWindow(display
, root
, 0, 0, 1, 1, 0, 0, 0);
89 classhint
= XAllocClassHint();
90 if (classhint
== NULL
) {
91 fprintf(stderr
, "%s: can't allocate memory for wm hints!\n", argv
[0]);
94 classhint
->res_class
= "DockApp";
95 classhint
->res_name
= appname
;
96 XSetClassHint(display
, window
, classhint
);
100 wmhints
= XAllocWMHints();
101 if (wmhints
== NULL
) {
102 fprintf(stderr
, "%s: can't allocate memory for wm hints!\n", argv
[0]);
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
);
115 /* Set WM Protocols */
116 delete_win
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
117 XSetWMProtocols (display
, icon_window
, &delete_win
, 1);
120 sizehints
.flags
= USSize
;
121 if (!dockapp_iswindowed
) {
122 sizehints
.flags
|= USPosition
;
123 sizehints
.x
= sizehints
.y
= 0;
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
));
148 dockapp_set_eventmask()
150 XSelectInput(display
, icon_window
,ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
151 XSelectInput(display
, window
,ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
156 create_bg_pixmap(void)
160 bg
= XCreatePixmap(display
, icon_window
, WINDOWED_SIZE_W
, WINDOWED_SIZE_H
,
162 XSetForeground(display
, gc
, dockapp_getcolor("rgb:ae/aa/ae"));
163 XFillRectangle(display
, bg
, gc
, 0, 0, WINDOWED_SIZE_W
, WINDOWED_SIZE_H
);
164 XSetForeground(display
, gc
, dockapp_getcolor("rgb:ff/ff/ff"));
165 XDrawLine(display
, bg
, gc
, 0, 0, 0, 63);
166 XDrawLine(display
, bg
, gc
, 1, 0, 1, 62);
167 XDrawLine(display
, bg
, gc
, 2, 0, 63, 0);
168 XDrawLine(display
, bg
, gc
, 2, 1, 62, 1);
169 XSetForeground(display
, gc
, dockapp_getcolor("rgb:52/55/52"));
170 XDrawLine(display
, bg
, gc
, 1, 63, 63, 63);
171 XDrawLine(display
, bg
, gc
, 2, 62, 63, 62);
172 XDrawLine(display
, bg
, gc
, 63, 1, 63, 61);
173 XDrawLine(display
, bg
, gc
, 62, 2, 62, 61);
180 dockapp_set_background(Pixmap pixmap
)
182 if (dockapp_iswindowed
) {
184 bg
= create_bg_pixmap();
185 XCopyArea(display
, pixmap
, bg
, gc
, 0, 0, width
, height
,
187 XSetWindowBackgroundPixmap(display
, icon_window
, bg
);
188 XSetWindowBackgroundPixmap(display
, window
, bg
);
189 XFreePixmap(display
, bg
);
191 XSetWindowBackgroundPixmap(display
, icon_window
, pixmap
);
192 XSetWindowBackgroundPixmap(display
, window
, pixmap
);
194 XClearWindow(display
, icon_window
);
202 if (!dockapp_iswindowed
)
203 XMapRaised(display
, window
);
205 XMapRaised(display
, icon_window
);
212 dockapp_xpm2pixmap(char **data
, Pixmap
*pixmap
, Pixmap
*mask
,
213 XpmColorSymbol
* colorSymbol
, unsigned int nsymbols
)
215 XpmAttributes xpmAttr
;
216 xpmAttr
.valuemask
= XpmCloseness
;
217 xpmAttr
.closeness
= 40000;
220 xpmAttr
.colorsymbols
= colorSymbol
;
221 xpmAttr
.numsymbols
= nsymbols
;
222 xpmAttr
.valuemask
|= XpmColorSymbols
;
225 if (XpmCreatePixmapFromData(display
, icon_window
, data
, pixmap
, mask
, &xpmAttr
) != 0)
233 dockapp_XCreatePixmap(int w
, int h
)
235 return (XCreatePixmap(display
, icon_window
, w
, h
, depth
));
240 dockapp_setshape(Pixmap mask
, int x_ofs
, int y_ofs
)
242 XShapeCombineMask(display
, icon_window
, ShapeBounding
, -x_ofs
, -y_ofs
,
244 XShapeCombineMask(display
, window
, ShapeBounding
, -x_ofs
, -y_ofs
,
251 dockapp_copyarea(Pixmap src
, Pixmap dist
, int x_src
, int y_src
, int w
, int h
,
252 int x_dist
, int y_dist
)
254 XCopyArea(display
, src
, dist
, gc
, x_src
, y_src
, w
, h
, x_dist
, y_dist
);
259 dockapp_copy2window (Pixmap src
)
261 if (dockapp_isbrokenwm
) {
262 XCopyArea(display
, src
, window
, gc
, 0, 0, width
, height
, offset_w
,
265 XCopyArea(display
, src
, icon_window
, gc
, 0, 0, width
, height
, offset_w
,
272 dockapp_nextevent_or_timeout(XEvent
*event
, unsigned long miliseconds
)
274 struct timeval timeout
;
277 XSync(display
, False
);
278 if (XPending(display
)) {
279 XNextEvent(display
, event
);
283 timeout
.tv_sec
= miliseconds
/ 1000;
284 timeout
.tv_usec
= (miliseconds
% 1000) * 1000;
287 FD_SET(ConnectionNumber(display
), &rset
);
288 if (select(ConnectionNumber(display
)+1, &rset
, NULL
, NULL
, &timeout
) > 0) {
289 XNextEvent(display
, event
);
290 if (event
->type
== ClientMessage
) {
291 if (event
->xclient
.data
.l
[0] == delete_win
) {
292 XDestroyWindow(display
,event
->xclient
.window
);
293 XCloseDisplay(display
);
297 if (dockapp_iswindowed
) {
298 event
->xbutton
.x
-= offset_w
;
299 event
->xbutton
.y
-= offset_h
;
309 dockapp_getcolor(char *color_name
)
313 if (!XParseColor(display
, DefaultColormap(display
, DefaultScreen(display
)),
315 fprintf(stderr
, "can't parse color %s\n", color_name
), exit(1);
317 if (!XAllocColor(display
, DefaultColormap(display
, DefaultScreen(display
)),
319 fprintf(stderr
, "can't allocate color %s. Using black\n", color_name
);
320 return BlackPixel(display
, DefaultScreen(display
));
328 dockapp_blendedcolor(char *color_name
, int r
, int g
, int b
, float fac
)
332 if ((r
< -255 || r
> 255)||(g
< -255 || g
> 255)||(b
< -255 || b
> 255)){
333 fprintf(stderr
, "r:%d,g:%d,b:%d (r,g,b must be 0 to 255)", r
, g
, b
);
341 if (!XParseColor(display
, DefaultColormap(display
, DefaultScreen(display
)),
343 fprintf(stderr
, "can't parse color %s\n", color_name
), exit(1);
345 if (!XAllocColor(display
, DefaultColormap(display
, DefaultScreen(display
)),
347 fprintf(stderr
, "can't allocate color %s. Using black\n", color_name
);
348 return BlackPixel(display
, DefaultScreen(display
));
351 if (DefaultDepth(display
, DefaultScreen(display
)) < 16)
355 if (color
.red
+ r
> 0xffff) {
357 } else if (color
.red
+ r
< 0) {
360 color
.red
= (unsigned short)(fac
* color
.red
+ r
);
364 if (color
.green
+ g
> 0xffff) {
365 color
.green
= 0xffff;
366 } else if (color
.green
+ g
< 0) {
369 color
.green
= (unsigned short)(fac
* color
.green
+ g
);
373 if (color
.blue
+ b
> 0xffff) {
375 } else if (color
.blue
+ b
< 0) {
378 color
.blue
= (unsigned short)(fac
* color
.blue
+ b
);
381 color
.flags
= DoRed
| DoGreen
| DoBlue
;
383 if (!XAllocColor(display
, DefaultColormap(display
, DefaultScreen(display
)),
385 fprintf(stderr
, "can't allocate color %s. Using black\n", color_name
);
386 return BlackPixel(display
, DefaultScreen(display
));