File: xutils.c

package info (click to toggle)
wmmatrix 0.2-12
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 352 kB
  • ctags: 154
  • sloc: ansic: 753; makefile: 26; sh: 13
file content (393 lines) | stat: -rw-r--r-- 10,410 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 *   xutils.c - A collection of X-windows utilties for creating WindowMAker
 *		DockApps. 
 *
 *     This file contains alot of the lower-level X windows routines. Origins with wmppp
 *     (by  Martijn Pieterse (pieterse@xs4all.nl)), but its been hacked up quite a bit
 *     and passed on from one new DockApp to the next. 
 *
 *
 *
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2, or (at your option)
 *      any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program (see the file COPYING); if not, write to the
 *      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *      Boston, MA  02111-1307, USA
 *
 *
 *
 *
 */  
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>
#include <sys/time.h>
#include <sys/select.h>
#include "xutils.h"



/*
 *   X11 Variables 
 */
int		x_fd;
XSizeHints	mysizehints;
XWMHints	mywmhints;
Pixel		back_pix, fore_pix;
char		*Geometry = "";
GC		NormalGC;
GC		EraseGC;
XpmIcon		wmgen;
Pixmap		pixmask;


/*
 *  Colors for wmCalClock
 */
extern char    TimeColor[30];
extern char    BackgroundColor[30];






/*
 *   flush_expose
 */
static int flush_expose(Window w) {

    XEvent 	dummy;
    int		i=0;

    while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
    i++;

    return i;

}









/*
 *   RedrawWindow
 *   RedrawWindowXY
 */
void RedrawWindow(void) {
	
    flush_expose(iconwin);
    XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);

    flush_expose(win);
    XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);

}

void RedrawWindowXY(int x, int y) {
	
    flush_expose(iconwin);
    XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);

    flush_expose(win);
    XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);

}







/*
 *   copyXPMArea
 *   copyXBMArea
 */
void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
	XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
}

void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {

	XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
}



/*
 *   initXwindow
 */
void initXwindow(int argc, char *argv[]){

    int		 i;
    char	*display_name = NULL;

    for (i=1; argv[i]; ++i) {
        if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
    }


    if (!(display = XOpenDisplay(display_name))) {
        fprintf(stderr, "%s: can't open display %s\n",
                argv[0], XDisplayName(display_name));
        exit(1);
    }


    screen  	 = DefaultScreen(display);
    Root    	 = RootWindow(display, screen);
    DisplayDepth = DefaultDepth(display, screen);
    x_fd    	 = XConnectionNumber(display);

}





/*
 *   openXwindow
 */
void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, 
    int pixmask_width, int pixmask_height) {

    unsigned int	borderwidth = 1;
    XClassHint		classHint;
    char		*wname = argv[0];
    XTextProperty	name;
    XGCValues		gcv;
    unsigned long	gcm;
    int			dummy=0, red, grn, blu;
    XpmColorSymbol 	cols[10]={	{"Back", NULL, 0},
    					{"Color1", NULL, 0},
    					{"Color2", NULL, 0},
    					{"Color3", NULL, 0},
    					{"Color4", NULL, 0},
    					{"Color5", NULL, 0},
    					{"Color6", NULL, 0},
    					{"Color7", NULL, 0},
    					{"Color8", NULL, 0},
    					{"Color9", NULL, 0}};


    

    /* 
     *  Create Pixmap
     */
    cols[0].pixel		  = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
    cols[1].pixel		  = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
    cols[2].pixel		  = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
    cols[3].pixel		  = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
    cols[4].pixel		  = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
    cols[5].pixel		  = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
    cols[6].pixel		  = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
    cols[7].pixel		  = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
    cols[8].pixel		  = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
    cols[9].pixel		  = getBlendedColor(TimeColor, 1.0000, red, grn, blu);

    wmgen.attributes.numsymbols   = 10;
    wmgen.attributes.colorsymbols = cols;
    wmgen.attributes.exactColors  = False;
    wmgen.attributes.closeness    = 40000;
    wmgen.attributes.valuemask    = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols 
							| XpmExactColors | XpmCloseness | XpmSize;
    if (XpmCreatePixmapFromData(display, Root, pixmap_bytes, 
      &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
	fprintf(stderr, "Not enough free colorcells.\n");
	exit(1);
    }




    /* 
     *  Create a window 
     */
    mysizehints.flags = USSize | USPosition;
    mysizehints.x = 0;
    mysizehints.y = 0;

    back_pix = getColor("black", 1.0, &red, &grn, &blu);
    fore_pix = getColor("white", 1.0, &red, &grn, &blu);

    XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
				&mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);

    mysizehints.width  = 64;
    mysizehints.height = 64;
		


    win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
	
    iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);



    /* 
     *  Activate hints 
     */
    XSetWMNormalHints(display, win, &mysizehints);
    classHint.res_name = wname;
    classHint.res_class = wname;
    XSetClassHint(display, win, &classHint);



    /*
     *  Set up the xevents that you want the relevent windows to inherit
     *  Currently, its seems that setting KeyPress events here has no
     *  effect. I.e. for some you will need to Grab the focus and then return
     *  it after you are done...
     */
    XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask 
		| PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask 
						| KeyPressMask | KeyReleaseMask);
    XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask 
		| PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask 
						| KeyPressMask | KeyReleaseMask);


    if (XStringListToTextProperty(&wname, 1, &name) == 0) {
        fprintf(stderr, "%s: can't allocate window name\n", wname);
        exit(1);
    }


    XSetWMName(display, win, &name);

    /* 
     *   Create Graphics Context (GC) for drawing 
     */
    gcm = GCForeground | GCBackground | GCGraphicsExposures;
    gcv.foreground = fore_pix;
    gcv.background = back_pix;
    gcv.graphics_exposures = 0;
    NormalGC = XCreateGC(display, Root, gcm, &gcv);

    /* 
     *   Create Graphics Context (GC) for erasing 
     */
    gcm = GCForeground | GCBackground | GCGraphicsExposures;
    gcv.foreground = back_pix;
    gcv.background = back_pix;
    gcv.graphics_exposures = 0;
    EraseGC = XCreateGC(display, Root, gcm, &gcv);



    pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
    XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
    XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);


    mywmhints.initial_state = WithdrawnState;
    mywmhints.icon_window = iconwin;
    mywmhints.icon_x = mysizehints.x;
    mywmhints.icon_y = mysizehints.y;
    mywmhints.window_group = win;
    mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;


    XSetWMHints(display, win, &mywmhints);


    XSetCommand(display, win, argv, argc);
    XMapWindow(display, win);

}

unsigned long getColor(char *ColorName, float fac, int *red, int *grn, int *blu) {

     XColor 		Color;
     XWindowAttributes 	Attributes;

     XGetWindowAttributes(display, Root, &Attributes);
     Color.pixel = 0;

     XParseColor(display, Attributes.colormap, ColorName, &Color);
     Color.red 	 = (unsigned short)(fac*(Color.red-24) + 24);
     Color.blue  = (unsigned short)(fac*(Color.blue-24) + 24);
     Color.green = (unsigned short)(fac*(Color.green-24) + 24);
     Color.flags = DoRed | DoGreen | DoBlue;
     XAllocColor(display, Attributes.colormap, &Color);

     
     *red = Color.red;
     *grn = Color.green;
     *blu = Color.blue;
     return Color.pixel;

}

unsigned long getBlendedColor(char *ColorName, float fac, int red, int grn, int blu) {

     XColor 		Color;
     XWindowAttributes 	Attributes;

     XGetWindowAttributes(display, Root, &Attributes);
     Color.pixel = 0;

     XParseColor(display, Attributes.colormap, ColorName, &Color);
     Color.red 	 = (unsigned short)(fac*(Color.red-red) + red);
     Color.blue  = (unsigned short)(fac*(Color.blue-grn) + grn);
     Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
     Color.flags = DoRed | DoGreen | DoBlue;
     XAllocColor(display, Attributes.colormap, &Color);

     return Color.pixel;

}


/*
 *  Here is a faster version of usleep using select()
 */
void uusleep(unsigned long usecs) {

    struct timeval tv;
    tv.tv_sec  = usecs / 1000000UL;
    tv.tv_usec = usecs % 1000000UL;
    select(1, NULL, NULL, NULL, &tv);

}

/*
 *  This version assumes the tv_sec value will be zero. I.e. the delay
 *  will be less than 1 second. This allows us to save on a div operation.
 *
 */
void short_uusleep(unsigned long usecs) {

    struct timeval tv;
    tv.tv_sec  = 0;
    tv.tv_usec = usecs;
    select(1, NULL, NULL, NULL, &tv);

}