Make AddMouseRegion's index unsigned
[dockapps.git] / wmhexaclock / src / wmgeneral.c
blobcbd41a057a362ad79c2bb7d775501cb7d82e7517
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <ctype.h>
6 #include <stdarg.h>
7 #include <X11/Xlib.h>
8 #include <X11/xpm.h>
9 #include <X11/extensions/shape.h>
10 #include "wmgeneral.h"
12 /* X11 Variables */
13 int screen;
14 int x_fd;
15 int d_depth;
16 XSizeHints mysizehints;
17 XWMHints mywmhints;
18 Pixel back_pix, fore_pix;
19 char *Geometry = "";
20 Window iconwin, win;
21 Pixmap pixmask;
24 /* Function Prototypes */
25 static void GetXPM(XpmIcon *, char **);
26 static Pixel GetColor(char *);
27 void RedrawWindow(void);
30 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[])
32 XWindowAttributes attributes;
33 int err;
35 /* For the colormap */
36 XGetWindowAttributes(display, Root, &attributes);
38 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
40 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
41 &(wmgen->mask), &(wmgen->attributes));
43 if (err != XpmSuccess) {
44 fprintf(stderr, "Not enough free colorcells.\n");
45 exit(1);
49 static Pixel GetColor(char *name)
51 XColor color;
52 XWindowAttributes attributes;
54 XGetWindowAttributes(display, Root, &attributes);
56 color.pixel = 0;
57 if (!XParseColor(display, attributes.colormap, name, &color)) {
58 fprintf(stderr, "wm.app: can't parse %s.\n", name);
59 } else if (!XAllocColor(display, attributes.colormap, &color)) {
60 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
62 return color.pixel;
65 static int flush_expose(Window w)
67 XEvent dummy;
68 int i=0;
70 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
71 i++;
72 return i;
75 void RedrawWindow(void)
77 flush_expose(iconwin);
78 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
79 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
80 flush_expose(win);
81 XCopyArea(display, wmgen.pixmap, win, NormalGC,
82 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
86 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy)
88 XCopyArea(display, wmnumbers.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
91 void cleanXPMArea()
93 XCopyArea(display, wmempty.pixmap, wmgen.pixmap, NormalGC, 0,0,64,64,0,0);
96 void openXwindow(int argc, char *argv[], char *pixmap_bytes_numbers[], char *pixmap_bytes_background[], char pixmask_bits[], int pixmask_width, int pixmask_height)
98 unsigned int borderwidth = 1;
99 XClassHint classHint;
100 char *display_name = NULL;
101 char *wname = argv[0];
102 XTextProperty name;
103 XGCValues gcv;
104 unsigned long gcm;
105 int dummy = 0;
107 if (!(display = XOpenDisplay(display_name))) {
108 fprintf(stderr, "%s: can't open display %s\n",
109 wname, XDisplayName(display_name));
110 exit(1);
112 screen = DefaultScreen(display);
113 Root = RootWindow(display, screen);
114 d_depth = DefaultDepth(display, screen);
115 x_fd = XConnectionNumber(display);
117 /* Convert XPM to XImage */
118 GetXPM(&wmgen, pixmap_bytes_background);
119 GetXPM(&wmempty, pixmap_bytes_background);
120 GetXPM(&wmnumbers,pixmap_bytes_numbers);
122 /* Create a window to hold the stuff */
123 mysizehints.flags = USSize | USPosition;
124 mysizehints.x = 0;
125 mysizehints.y = 0;
127 back_pix = GetColor("white");
128 fore_pix = GetColor("black");
130 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints, &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
132 mysizehints.width = 64;
133 mysizehints.height = 64;
135 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y, mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
137 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y, mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
139 /* Activate hints */
140 XSetWMNormalHints(display, win, &mysizehints);
141 classHint.res_name = wname;
142 classHint.res_class = wname;
143 XSetClassHint(display, win, &classHint);
145 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
146 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
148 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
149 fprintf(stderr, "%s: can't allocate window name\n", wname);
150 exit(1);
153 XSetWMName(display, win, &name);
155 /* Create GC for drawing */
156 gcm = GCForeground | GCBackground | GCGraphicsExposures;
157 gcv.foreground = fore_pix;
158 gcv.background = back_pix;
159 gcv.graphics_exposures = 0;
160 NormalGC = XCreateGC(display, Root, gcm, &gcv);
162 /* ONLYSHAPE ON */
163 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
164 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
165 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
167 /* ONLYSHAPE OFF */
168 mywmhints.initial_state = WithdrawnState;
169 mywmhints.icon_window = iconwin;
170 mywmhints.icon_x = mysizehints.x;
171 mywmhints.icon_y = mysizehints.y;
172 mywmhints.window_group = win;
173 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
175 XSetWMHints(display, win, &mywmhints);
177 XSetCommand(display, win, argv, argc);
178 XMapWindow(display, win);