wmshutdown: Remove support for deprecated Consolekit.
[dockapps.git] / wmifinfo / xutils.c
blob92050fd18186fe0481f19b8440392c639f5acd4c
1 /*
2 * xutils.c - A collection of X-windows utilties for creating WindowMAker
3 * DockApps.
5 * This file contains alot of the lower-level X windows routines. Origins with wmppp
6 * (by Martijn Pieterse (pieterse@xs4all.nl)), but its been hacked up quite a bit
7 * and passed on from one new DockApp to the next.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program (see the file COPYING); if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
28 * $Id: xutils.c,v 1.2 2002/09/15 14:31:41 ico Exp $
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <ctype.h>
37 #include <stdarg.h>
38 #include <X11/Xlib.h>
39 #include <X11/xpm.h>
40 #include <X11/extensions/shape.h>
41 #include "xutils.h"
46 * X11 Variables
48 int x_fd;
49 XSizeHints mysizehints;
50 XWMHints mywmhints;
51 Pixel back_pix, fore_pix;
52 char *Geometry = "";
53 GC NormalGC;
54 XpmIcon wmgen;
55 Pixmap pixmask;
62 * flush_expose
64 static int flush_expose(Window w) {
66 XEvent dummy;
67 int i=0;
69 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
70 i++;
72 return i;
85 * RedrawWindow
86 * RedrawWindowXY
88 void RedrawWindow(void) {
90 flush_expose(iconwin);
91 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
93 flush_expose(win);
94 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
98 void RedrawWindowXY(int x, int y) {
100 flush_expose(iconwin);
101 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
103 flush_expose(win);
104 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
115 * copyXPMArea
116 * copyXBMArea
118 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
119 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
122 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
124 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
130 * initXwindow
132 void initXwindow(int argc, char *argv[]){
134 int i;
135 char *display_name = NULL;
137 for (i=1; argv[i]; ++i) {
138 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
142 if (!(display = XOpenDisplay(display_name))) {
143 fprintf(stderr, "%s: can't open display %s\n",
144 argv[0], XDisplayName(display_name));
145 exit(1);
149 screen = DefaultScreen(display);
150 Root = RootWindow(display, screen);
151 DisplayDepth = DefaultDepth(display, screen);
152 x_fd = XConnectionNumber(display);
161 * openXwindow
163 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
164 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
165 char *WindGustColor, char *DataColor, char *StationTimeColor) {
167 unsigned int borderwidth = 1;
168 XClassHint classHint;
169 char *wname = argv[0];
170 XTextProperty name;
171 XGCValues gcv;
172 unsigned long gcm;
173 int dummy=0;
174 XpmColorSymbol cols[5]={ {"BackColor", NULL, 0},
175 {"LabelColor", NULL, 0},
176 {"DataColor", NULL, 0},
177 {"WindGustColor", NULL, 0},
178 {"StationTimeColor", NULL, 0} };
184 * Create Pixmap
186 cols[0].pixel = getColor(BackColor, 1.0);
187 cols[1].pixel = getColor(LabelColor, 1.0);
188 cols[2].pixel = getColor(DataColor, 1.0);
189 cols[3].pixel = getColor(WindGustColor, 1.0);
190 cols[4].pixel = getColor(StationTimeColor, 1.0);
191 wmgen.attributes.numsymbols = 5;
192 wmgen.attributes.colorsymbols = cols;
193 wmgen.attributes.exactColors = False;
194 wmgen.attributes.closeness = 40000;
195 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
196 | XpmExactColors | XpmCloseness | XpmSize;
197 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
198 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
199 fprintf(stderr, "Not enough free colorcells.\n");
200 exit(1);
207 * Create a window
209 mysizehints.flags = USSize | USPosition;
210 mysizehints.x = 0;
211 mysizehints.y = 0;
213 back_pix = getColor("white", 1.0);
214 fore_pix = getColor("black", 1.0);
216 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
217 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
219 mysizehints.width = 64;
220 mysizehints.height = 64;
224 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
225 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
227 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
228 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
233 * Activate hints
235 XSetWMNormalHints(display, win, &mysizehints);
236 classHint.res_name = wname;
237 classHint.res_class = wname;
238 XSetClassHint(display, win, &classHint);
243 * Set up the xevents that you want the relevent windows to inherit
244 * Currently, its seems that setting KeyPress events here has no
245 * effect. I.e. for some you will need to Grab the focus and then return
246 * it after you are done...
248 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
249 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
250 | KeyPressMask | KeyReleaseMask);
251 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
252 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
253 | KeyPressMask | KeyReleaseMask);
256 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
257 fprintf(stderr, "%s: can't allocate window name\n", wname);
258 exit(1);
262 XSetWMName(display, win, &name);
265 * Create Graphics Context (GC) for drawing
267 gcm = GCForeground | GCBackground | GCGraphicsExposures;
268 gcv.foreground = fore_pix;
269 gcv.background = back_pix;
270 gcv.graphics_exposures = 0;
271 NormalGC = XCreateGC(display, Root, gcm, &gcv);
275 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
276 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
277 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
280 mywmhints.initial_state = WithdrawnState;
281 mywmhints.icon_window = iconwin;
282 mywmhints.icon_x = mysizehints.x;
283 mywmhints.icon_y = mysizehints.y;
284 mywmhints.window_group = win;
285 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
288 XSetWMHints(display, win, &mywmhints);
291 XSetCommand(display, win, argv, argc);
292 XMapWindow(display, win);
296 unsigned long getColor(char *ColorName, float fac) {
298 XColor Color;
299 XWindowAttributes Attributes;
301 XGetWindowAttributes(display, Root, &Attributes);
302 Color.pixel = 0;
304 XParseColor(display, Attributes.colormap, ColorName, &Color);
305 Color.red = (unsigned short)(Color.red/fac);
306 Color.blue = (unsigned short)(Color.blue/fac);
307 Color.green = (unsigned short)(Color.green/fac);
308 Color.flags = DoRed | DoGreen | DoBlue;
309 XAllocColor(display, Attributes.colormap, &Color);
311 return Color.pixel;