wmacpi: Bump to version 2.2.
[dockapps.git] / wmweather+ / wmgeneral / wmgeneral-x11.c
blob7d60af6a6a592e7739238ab5c0b80734ba1a6269
1 #include "../config.h"
3 /*
4 Best viewed with vim5, using ts=4
6 wmgeneral was taken from wmppp.
8 It has a lot of routines which most of the wm* programs use.
10 ------------------------------------------------------------
12 Author: Martijn Pieterse (pieterse@xs4all.nl)
14 ---
15 CHANGES:
16 ---
17 15/08/2002 (Brad Jorsch, anomie@users.sourceforge.net)
18 * Pulled createXBMfromXPM into its own file, because it's the same in
19 both -gtk and -x11.
21 11/08/2002 (Brad Jorsch, anomie@users.sourceforge.net)
22 * Removed the rc-file and mouse region stuff to their own files.
23 * Renamed this file to "wmgeneral-x11.c"
24 * Renamed a few of the functions
26 28/08/2001 (Brad Jorsch, anomie@users.sourceforge.net)
27 * Added EnableMouseRegion and DisableMouseRegion
28 * Got annoyed with the 81-character lines. Fixed it. If you don't like
29 it, find a different copy of wmgeneral.c ;)
30 * GraphicsExpose events are enabled here.
31 * GetXPM is exported. It optionally takes an XpmColorSymbol array.
32 * GetColor is exported.
34 30/09/2000 (Brad Jorsch, anomie@users.sourceforge.net)
35 * You know, wmgen.mask sounds like a much nicer place to store the
36 mask... why don't we do that?
38 21/09/1999 (Brad Jorsch, anomie@users.sourceforge.net)
39 * Changed openXwindow to use only the filename, sans path,
40 as the name and class properties of the app.
42 14/09/1998 (Dave Clark, clarkd@skyia.com)
43 * Updated createXBMfromXPM routine
44 * Now supports >256 colors
45 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
46 * Removed a bug from parse_rcfile. You could
47 not use "start" in a command if a label was
48 also start.
49 * Changed the needed geometry string.
50 We don't use window size, and don't support
51 negative positions.
52 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
53 * Added parse_rcfile2
54 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
55 * Added -geometry support (untested)
56 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
57 * Added createXBMfromXPM routine
58 * Saves a lot of work with changing xpm's.
59 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
60 * changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
61 * debugged the parse_rc file.
62 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
63 * Ripped similar code from all the wm* programs,
64 and put them in a single file.
68 #include <stdlib.h>
69 #include <stdio.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <ctype.h>
73 #include <stdarg.h>
75 #include <X11/Xlib.h>
76 #include <X11/xpm.h>
77 #include <X11/extensions/shape.h>
79 #include "wmgeneral-x11.h"
81 /*****************/
82 /* X11 Variables */
83 /*****************/
85 Window Root;
86 int screen;
87 int x_fd;
88 int d_depth;
89 XSizeHints mysizehints;
90 XWMHints mywmhints;
91 Pixel back_pix, fore_pix;
92 char *Geometry = "";
93 Window iconwin, win;
94 GC NormalGC;
95 GC RedrawGC;
96 XpmIcon wmgen;
98 /***********************/
99 /* Function Prototypes */
100 /***********************/
102 void RedrawWindow(void);
104 /******************************************************************************\
105 |* GetXPM *|
106 \******************************************************************************/
108 void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
110 XWindowAttributes attributes;
111 int err;
113 /* For the colormap */
114 XGetWindowAttributes(display, Root, &attributes);
116 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
117 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
118 &(wmgen->mask), &(wmgen->attributes));
120 if (err != XpmSuccess) {
121 fprintf(stderr, "Not enough free colorcells.\n");
122 exit(1);
126 /******************************************************************************\
127 |* GetColor *|
128 \******************************************************************************/
130 Pixel GetColor(char *name) {
132 XColor color;
133 XWindowAttributes attributes;
135 XGetWindowAttributes(display, Root, &attributes);
137 color.pixel = 0;
138 if (!XParseColor(display, attributes.colormap, name, &color)) {
139 fprintf(stderr, "wm.app: can't parse %s.\n", name);
140 } else if (!XAllocColor(display, attributes.colormap, &color)) {
141 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
143 return color.pixel;
146 /******************************************************************************\
147 |* flush_expose *|
148 \******************************************************************************/
150 static int flush_expose(Window w) {
152 XEvent dummy;
153 int i=0;
155 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
156 i++;
158 return i;
161 /******************************************************************************\
162 |* RedrawWindow *|
163 \******************************************************************************/
165 void RedrawWindow(void) {
167 flush_expose(iconwin);
168 XCopyArea(display, wmgen.pixmap, iconwin, RedrawGC,
169 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
170 flush_expose(win);
171 XCopyArea(display, wmgen.pixmap, win, RedrawGC,
172 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
175 /******************************************************************************\
176 |* RedrawWindowXY *|
177 \******************************************************************************/
179 void RedrawWindowXY(int x, int y) {
181 flush_expose(iconwin);
182 XCopyArea(display, wmgen.pixmap, iconwin, RedrawGC,
183 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
184 flush_expose(win);
185 XCopyArea(display, wmgen.pixmap, win, RedrawGC,
186 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
189 /******************************************************************************\
190 |* copyXPMArea *|
191 \******************************************************************************/
193 void copyPixmapArea(int x, int y, int sx, int sy, int dx, int dy) {
195 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
199 /******************************************************************************\
200 |* copyXBMArea *|
201 \******************************************************************************/
203 void copyMaskArea(int x, int y, int sx, int sy, int dx, int dy) {
205 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
209 /******************************************************************************\
210 |* setMaskXY *|
211 \******************************************************************************/
213 void setMaskXY(int x, int y) {
215 XShapeCombineMask(display, win, ShapeBounding, x, y, wmgen.mask, ShapeSet);
216 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, wmgen.mask, ShapeSet);
219 /******************************************************************************\
220 |* openXwindow *|
221 \******************************************************************************/
222 void openDockWindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
224 unsigned int borderwidth = 1;
225 XClassHint classHint;
226 char *display_name = NULL;
227 char *wname;
228 XTextProperty name;
230 XGCValues gcv;
231 unsigned long gcm;
233 char *geometry = NULL;
235 int dummy=0;
236 int i, wx, wy;
238 wname=strrchr(argv[0], '/');
239 if(wname==NULL) wname=argv[0];
240 else wname++;
242 for (i=1; argv[i]; i++) {
243 if (!strcmp(argv[i], "-display")) {
244 display_name = argv[i+1];
245 i++;
247 if (!strcmp(argv[i], "-geometry")) {
248 geometry = argv[i+1];
249 i++;
253 if (!(display = XOpenDisplay(display_name))) {
254 fprintf(stderr, "%s: can't open display %s\n",
255 wname, XDisplayName(display_name));
256 exit(1);
258 screen = DefaultScreen(display);
259 Root = RootWindow(display, screen);
260 d_depth = DefaultDepth(display, screen);
261 x_fd = XConnectionNumber(display);
263 /* Convert XPM to XImage */
264 GetXPM(&wmgen, pixmap_bytes);
266 /* Create a window to hold the stuff */
267 mysizehints.flags = USSize | USPosition;
268 mysizehints.x = 0;
269 mysizehints.y = 0;
271 back_pix = GetColor("white");
272 fore_pix = GetColor("black");
274 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
275 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
277 mysizehints.width = 64;
278 mysizehints.height = 64;
280 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
281 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
283 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
284 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
286 /* Activate hints */
287 XSetWMNormalHints(display, win, &mysizehints);
288 classHint.res_name = wname;
289 classHint.res_class = wname;
290 XSetClassHint(display, win, &classHint);
292 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
293 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
295 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
296 fprintf(stderr, "%s: can't allocate window name\n", wname);
297 exit(1);
300 XSetWMName(display, win, &name);
302 /* Create GC for drawing */
304 gcm = GCForeground | GCBackground | GCGraphicsExposures;
305 gcv.foreground = fore_pix;
306 gcv.background = back_pix;
307 gcv.graphics_exposures = True;
308 NormalGC = XCreateGC(display, Root, gcm, &gcv);
309 gcv.graphics_exposures = False;
310 RedrawGC = XCreateGC(display, Root, gcm, &gcv);
312 /* ONLYSHAPE ON */
314 if(pixmask_bits!=NULL){
315 XFreePixmap(display, wmgen.mask);
316 wmgen.mask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
319 XShapeCombineMask(display, win, ShapeBounding, 0, 0, wmgen.mask, ShapeSet);
320 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, wmgen.mask, ShapeSet);
322 /* ONLYSHAPE OFF */
324 mywmhints.initial_state = WithdrawnState;
325 mywmhints.icon_window = iconwin;
326 mywmhints.icon_x = mysizehints.x;
327 mywmhints.icon_y = mysizehints.y;
328 mywmhints.window_group = win;
329 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
331 XSetWMHints(display, win, &mywmhints);
333 XSetCommand(display, win, argv, argc);
334 XMapWindow(display, win);
336 if (geometry) {
337 if (sscanf(geometry, "+%d+%d", &wx, &wy) != 2) {
338 fprintf(stderr, "Bad geometry string.\n");
339 exit(1);
341 XMoveWindow(display, win, wx, wy);