~ubuntu-branches/ubuntu/breezy/wmmemload/breezy

« back to all changes in this revision

Viewing changes to src/dockapp.c

  • Committer: Bazaar Package Importer
  • Author(s): Gordon Fraser
  • Date: 2003-11-02 17:32:49 UTC
  • Revision ID: james.westby@ubuntu.com-20031102173249-hlogkg7ndc3hu8sh
Tags: upstream-0.1.5
ImportĀ upstreamĀ versionĀ 0.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1999 Alfredo K. Kojima
 
3
 * Copyright (c) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
 
4
 *
 
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:
 
11
 *
 
12
 * The above copyright notice and this permission notice shall be included in
 
13
 * all copies or substantial portions of the Software.
 
14
 *
 
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.
 
21
 
 
22
 * This code is based on libdockapp-0.4.0
 
23
 * modified by Seiichi SATO <ssato@sh.rim.or.jp>
 
24
 */
 
25
 
 
26
#include "dockapp.h"
 
27
 
 
28
#define WINDOWED_SIZE_W 64
 
29
#define WINDOWED_SIZE_H 64
 
30
 
 
31
/* global */
 
32
Display *display = NULL;
 
33
Bool    dockapp_iswindowed = False;
 
34
Bool    dockapp_isbrokenwm = False;
 
35
 
 
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;
 
44
 
 
45
void
 
46
dockapp_open_window(char *display_specified, char *appname,
 
47
                    unsigned w, unsigned h, int argc, char **argv)
 
48
{
 
49
    XClassHint      *classhint;
 
50
    XWMHints        *wmhints;
 
51
    Status          stat;
 
52
    XTextProperty   title;
 
53
    XSizeHints      sizehints;
 
54
    Window          root;
 
55
    int             ww, wh;
 
56
 
 
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);
 
63
    }
 
64
    root = DefaultRootWindow(display);
 
65
 
 
66
    width = w;
 
67
    height = h;
 
68
 
 
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;
 
78
    }
 
79
 
 
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);
 
86
    }
 
87
 
 
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);
 
93
    }
 
94
    classhint->res_class = "DockApp";
 
95
    classhint->res_name = appname;
 
96
    XSetClassHint(display, window, classhint);
 
97
    XFree(classhint);
 
98
 
 
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);
 
104
    }
 
105
    wmhints->flags = IconWindowHint | WindowGroupHint;
 
106
    if (!dockapp_iswindowed) {
 
107
        wmhints->flags |= StateHint;
 
108
        wmhints->initial_state = WithdrawnState;
 
109
    }
 
110
    wmhints->window_group = window;
 
111
    wmhints->icon_window = icon_window;
 
112
    XSetWMHints(display, window, wmhints);
 
113
    XFree(wmhints);
 
114
 
 
115
    /* Set WM Protocols */
 
116
    delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False);
 
117
    XSetWMProtocols (display, icon_window, &delete_win, 1);
 
118
 
 
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;
 
128
    }
 
129
    sizehints.width = ww;
 
130
    sizehints.height = wh;
 
131
    XSetWMNormalHints(display, icon_window, &sizehints);
 
132
 
 
133
    /* Set WindowTitle for AfterStep Wharf */
 
134
    stat = XStringListToTextProperty(&appname, 1, &title);
 
135
    XSetWMName(display, window, &title);
 
136
    XSetWMName(display, icon_window, &title);
 
137
 
 
138
    /* Set Command to start the app so it can be docked properly */
 
139
    XSetCommand(display, window, argv, argc);
 
140
 
 
141
    depth = DefaultDepth(display, DefaultScreen(display));
 
142
    gc = DefaultGC(display, DefaultScreen(display));
 
143
 
 
144
    XFlush(display);
 
145
}
 
146
 
 
147
 
 
148
void
 
149
dockapp_set_eventmask(long mask)
 
150
{
 
151
    XSelectInput(display, icon_window, mask);
 
152
    XSelectInput(display, window, mask);
 
153
}
 
154
 
 
155
 
 
156
static Pixmap
 
157
create_bg_pixmap(void)
 
158
{
 
159
    Pixmap bg;
 
160
 
 
161
    bg = XCreatePixmap(display, icon_window, WINDOWED_SIZE_W, WINDOWED_SIZE_H,
 
162
                       depth);
 
163
    XSetForeground(display, gc, dockapp_getcolor("rgb:ae/aa/ae"));
 
164
    XFillRectangle(display, bg, gc, 0, 0, WINDOWED_SIZE_W, WINDOWED_SIZE_H);
 
165
    XSetForeground(display, gc, dockapp_getcolor("rgb:ff/ff/ff"));
 
166
    XDrawLine(display, bg, gc, 0, 0, 0, 63);
 
167
    XDrawLine(display, bg, gc, 1, 0, 1, 62);
 
168
    XDrawLine(display, bg, gc, 2, 0, 63, 0);
 
169
    XDrawLine(display, bg, gc, 2, 1, 62, 1);
 
170
    XSetForeground(display, gc, dockapp_getcolor("rgb:52/55/52"));
 
171
    XDrawLine(display, bg, gc, 1, 63, 63, 63);
 
172
    XDrawLine(display, bg, gc, 2, 62, 63, 62);
 
173
    XDrawLine(display, bg, gc, 63, 1, 63, 61);
 
174
    XDrawLine(display, bg, gc, 62, 2, 62, 61);
 
175
 
 
176
    return bg;
 
177
}
 
178
 
 
179
 
 
180
void
 
181
dockapp_set_background(Pixmap pixmap)
 
182
{
 
183
    if (dockapp_iswindowed) {
 
184
        Pixmap bg;
 
185
        bg = create_bg_pixmap();
 
186
        XCopyArea(display, pixmap, bg, gc, 0, 0, width, height,
 
187
                  offset_w, offset_w);
 
188
        XSetWindowBackgroundPixmap(display, icon_window, bg);
 
189
        XSetWindowBackgroundPixmap(display, window, bg);
 
190
        XFreePixmap(display, bg);
 
191
    } else {
 
192
        XSetWindowBackgroundPixmap(display, icon_window, pixmap);
 
193
        XSetWindowBackgroundPixmap(display, window, pixmap);
 
194
    }
 
195
    XClearWindow(display, icon_window);
 
196
    XFlush(display);
 
197
}
 
198
 
 
199
 
 
200
void
 
201
dockapp_show(void)
 
202
{
 
203
    if (!dockapp_iswindowed)
 
204
        XMapRaised(display, window);
 
205
    else
 
206
        XMapRaised(display, icon_window);
 
207
 
 
208
    XFlush(display);
 
209
}
 
210
 
 
211
 
 
212
Bool
 
213
dockapp_xpm2pixmap(char **data, Pixmap *pixmap, Pixmap *mask,
 
214
                   XpmColorSymbol * colorSymbol, unsigned int nsymbols)
 
215
{
 
216
    XpmAttributes xpmAttr;
 
217
    xpmAttr.valuemask = XpmCloseness;
 
218
    xpmAttr.closeness = 40000;
 
219
 
 
220
    if (nsymbols) {
 
221
        xpmAttr.colorsymbols = colorSymbol;
 
222
        xpmAttr.numsymbols = nsymbols;
 
223
        xpmAttr.valuemask |= XpmColorSymbols;
 
224
    }
 
225
 
 
226
    if (XpmCreatePixmapFromData(display, icon_window, data, pixmap, mask, &xpmAttr) != 0)
 
227
        return False;
 
228
 
 
229
    return True;
 
230
}
 
231
 
 
232
 
 
233
Pixmap
 
234
dockapp_XCreatePixmap(int w, int h)
 
235
{
 
236
    return (XCreatePixmap(display, icon_window, w, h, depth));
 
237
}
 
238
 
 
239
 
 
240
void
 
241
dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs)
 
242
{
 
243
    XShapeCombineMask(display, icon_window, ShapeBounding, -x_ofs, -y_ofs,
 
244
                      mask, ShapeSet);
 
245
    XShapeCombineMask(display, window, ShapeBounding, -x_ofs, -y_ofs,
 
246
                      mask, ShapeSet);
 
247
    XFlush(display);
 
248
}
 
249
 
 
250
 
 
251
void
 
252
dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src, int w, int h,
 
253
                 int x_dist, int y_dist)
 
254
{
 
255
    XCopyArea(display, src, dist, gc, x_src, y_src, w, h, x_dist, y_dist);
 
256
}
 
257
 
 
258
 
 
259
void
 
260
dockapp_copy2window (Pixmap src)
 
261
{
 
262
    if (dockapp_isbrokenwm) {
 
263
        XCopyArea(display, src, window, gc, 0, 0, width, height, offset_w,
 
264
                  offset_h);
 
265
    } else {
 
266
        XCopyArea(display, src, icon_window, gc, 0, 0, width, height, offset_w,
 
267
                  offset_h);
 
268
    }
 
269
}
 
270
 
 
271
 
 
272
Bool
 
273
dockapp_nextevent_or_timeout(XEvent *event, unsigned long miliseconds)
 
274
{
 
275
    struct timeval timeout;
 
276
    fd_set rset;
 
277
 
 
278
    XSync(display, False);
 
279
    if (XPending(display)) {
 
280
        XNextEvent(display, event);
 
281
        return True;
 
282
    }
 
283
 
 
284
    timeout.tv_sec = miliseconds / 1000;
 
285
    timeout.tv_usec = (miliseconds % 1000) * 1000;
 
286
 
 
287
    FD_ZERO(&rset);
 
288
    FD_SET(ConnectionNumber(display), &rset);
 
289
    if (select(ConnectionNumber(display)+1, &rset, NULL, NULL, &timeout) > 0) {
 
290
        XNextEvent(display, event);
 
291
        if (event->type == ClientMessage) {
 
292
            if (event->xclient.data.l[0] == delete_win) {
 
293
                XDestroyWindow(display,event->xclient.window);
 
294
                XCloseDisplay(display);
 
295
                exit(0);
 
296
            }
 
297
        }
 
298
        if (dockapp_iswindowed) {
 
299
                event->xbutton.x -= offset_w;
 
300
                event->xbutton.y -= offset_h;
 
301
        }
 
302
        return True;
 
303
    }
 
304
 
 
305
    return False;
 
306
}
 
307
 
 
308
 
 
309
unsigned long
 
310
dockapp_getcolor(char *color_name)
 
311
{
 
312
    XColor color;
 
313
 
 
314
    if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
 
315
                     color_name, &color))
 
316
        fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
 
317
 
 
318
    if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
 
319
                     &color)) {
 
320
        fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
 
321
        return BlackPixel(display, DefaultScreen(display));
 
322
    }
 
323
 
 
324
    return color.pixel;
 
325
}
 
326
 
 
327
 
 
328
unsigned long
 
329
dockapp_blendedcolor(char *color_name, int r, int g, int b, float fac)
 
330
{
 
331
    XColor color;
 
332
 
 
333
    if ((r < -255 || r > 255)||(g < -255 || g > 255)||(b < -255 || b > 255)){
 
334
        fprintf(stderr, "r:%d,g:%d,b:%d (r,g,b must be 0 to 255)", r, g, b);
 
335
        exit(1);
 
336
    }
 
337
 
 
338
    r *= 255;
 
339
    g *= 255;
 
340
    b *= 255;
 
341
 
 
342
    if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
 
343
                     color_name, &color))
 
344
        fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
 
345
 
 
346
    if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
 
347
                     &color)) {
 
348
        fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
 
349
        return BlackPixel(display, DefaultScreen(display));
 
350
    }
 
351
 
 
352
    if (DefaultDepth(display, DefaultScreen(display)) < 16)
 
353
        return color.pixel;
 
354
 
 
355
    /* red */
 
356
    if (color.red + r > 0xffff) {
 
357
        color.red = 0xffff;
 
358
    } else if (color.red + r < 0) {
 
359
        color.red = 0;
 
360
    } else {
 
361
        color.red = (unsigned short)(fac * color.red + r);
 
362
    }
 
363
 
 
364
    /* green */
 
365
    if (color.green + g > 0xffff) {
 
366
        color.green = 0xffff;
 
367
    } else if (color.green + g < 0) {
 
368
        color.green = 0;
 
369
    } else {
 
370
        color.green = (unsigned short)(fac * color.green + g);
 
371
    }
 
372
 
 
373
    /* blue */
 
374
    if (color.blue + b > 0xffff) {
 
375
        color.blue = 0xffff;
 
376
    } else if (color.blue + b < 0) {
 
377
        color.blue = 0;
 
378
    } else {
 
379
        color.blue = (unsigned short)(fac * color.blue + b);
 
380
    }
 
381
 
 
382
    color.flags = DoRed | DoGreen | DoBlue;
 
383
 
 
384
    if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
 
385
                     &color)) {
 
386
        fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
 
387
        return BlackPixel(display, DefaultScreen(display));
 
388
    }
 
389
 
 
390
    return color.pixel;
 
391
}