File: wmbutton.c

package info (click to toggle)
wmbutton 0.6.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 248 kB
  • ctags: 129
  • sloc: ansic: 988; makefile: 86
file content (560 lines) | stat: -rw-r--r-- 17,546 bytes parent folder | download | duplicates (3)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/***********************************************************************
 *   Code is based on wmppp, wmload, wmtime, wmcp, and asbutton
 *   Author: Edward H. Flora <ehflora@ksu.edu>
 *   Ver 0 Rel 6.1    Jan 23, 2005
 *
 *   Contributors:
 *              Christian 'Greek0' Aichinger <Greek0@gmx.net>
 *                  Did some code cleanup and fixed several memory leaks.
 *              Ralf Horstmann <ralf.horstmann@gmx.de>
 *                  Added ability to load pixmaps at startup,
 *                  without having to re-compile
 *              Michael Cohrs <camico@users.sourceforge.net> 
 *                  Added Tool Tips, and updated graphics
 *              Bruno Essmann <essmann@users.sourceforge.net>)
 *                  Creator of wmpager
 *              Casey Harkins <charkins@cs.wisc.edu> 
 *                  Bug fix reading config file path - 3/6/99
 *                  Added button-presses, and other - denoted by *charkins*
 *              Ben Cohen <buddog@aztec.asu.edu>
 *                  original author of wmcp (et al.)
 *              Thomas Nemeth <tnemeth@multimania.com>
 *                  contributor to wmcp
 *              Casey Harkins <charkins@cs.wisc.edu> 
 *                  Bug fix reading config file path - 3/6/99
 *                  Added button-presses, and other - denoted by *charkins*
 *              Michael Henderson <mghenderson@lanl.gov>
 *                  Application ideas, suggestions
 *              Ryan ?? <pancake@mindspring.com> 
 *                  Modified wmbutton to asbutton.
 *                  Note: asbutton is a seperate program, not associated 
 *                        with wmbutton (just as wmbutton is not associated
 *                        with wmcp)
 *              Jon Bruno
 *                  Web Page Development
 *    The contributors listed above are not necessarily involved with the 
 *    development of wmbutton.  I'm listing them here partially as thanks for 
 *    helping out, catching bugs in the code, etc.
 ***********************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include "wmbutton.h"

#include "backdrop.xpm"           /* background graphic */
#include "buttons.xpm"            /* graphic of 9 buttons */
#include "mask.xbm"               /* Border Graphic */

/*************** Function Prototypes ***********************************/
void redraw(void);                 
void getPixmaps(void);
int  whichButton(int x, int y);   // determine which button has been pressed
void SetWmHints();
void SetClassHints();

/***********************************************************************
 * 		Globals..    OK.. there's too many globals.
 *                           Feel free and fix it, if you'd like.
 ***********************************************************************/
Display *display;
int screen;
Window rootwin, win, iconwin;
GC gc;
int depth;
Pixel bg_pixel, fg_pixel;

struct Config_t Config;

typedef struct _XpmIcon {
  Pixmap pixmap;
  Pixmap mask;
  XpmAttributes attributes;
} XpmIcon;

typedef struct _button_region {
  int x,y;
  int i,j;
} ButtonArea;

ButtonArea button_region[9];

XpmIcon template, visible, buttons;

int border  = 0;
int button_pressed = -1;	/* button to be drawn pressed *charkins*/

char *app_name = "wmbutton";

/***********************************************************************
 * 		Main
 ***********************************************************************/
int main( int argc, char ** argv ) {

  XEvent report;
  XGCValues xgcValues;	
  XTextProperty app_name_atom;
  XSizeHints xsizehints;

  Pixmap pixmask;

  int dummy = 0;
  int N = 1;		        /* Button number pressed to goto app # */
  
  /* Added for Tool Tip Support */
  long nTooltipShowDelay = TOOLTIP_SHOW_DELAY;
  long nTooltipReshowDelay = TOOLTIP_RESHOW_DELAY;
  long nTooltipTimer = -1;
  long nTooltipHideTimer = -1;
  long nNow;
  int nTooltipButton = 0, nTooltipX = 0, nTooltipY = 0;

  /* Parse Command Line Arguments */  
  parseargs(argc, argv);

  /* Open Display */
  if ( (display = XOpenDisplay(Config.Display_str)) == NULL ) 
    err_mess(FAILDISP, Config.Display_str);
  
  screen  = DefaultScreen(display);
  rootwin = RootWindow(display,screen);
  depth   = DefaultDepth(display, screen);
  
  bg_pixel = WhitePixel(display, screen ); 
  fg_pixel = BlackPixel(display, screen ); 
  
  xsizehints.flags  = USSize | USPosition;
  xsizehints.width  = 64;
  xsizehints.height = 64;
  
  /* Parse Geometry string and fill in sizehints fields */
  XWMGeometry(display, screen, 
	      Config.Geometry_str, 
	      NULL, 	
	      border, 
	      &xsizehints,
	      &xsizehints.x, 
	      &xsizehints.y,
	      &xsizehints.width,
	      &xsizehints.height, 
	      &dummy);
  
  if ( (win = XCreateSimpleWindow(display,
				  rootwin,
				  xsizehints.x,
				  xsizehints.y,
				  xsizehints.width,
				  xsizehints.height,
				  border,
				  fg_pixel, bg_pixel) ) == 0 )
    err_mess(FAILSWIN, NULL);

  
  if ( (iconwin = XCreateSimpleWindow(display,
				      win,
				      xsizehints.x,
				      xsizehints.y,
				      xsizehints.width,
				      xsizehints.height,
				      border,
				      fg_pixel, bg_pixel) ) == 0 )
    err_mess(FAILICON, NULL);

  /* Set up shaped windows */
  /*Gives the appicon a border so you can grab and move it. */

  if ( ( pixmask = XCreateBitmapFromData(display, 
					 win,
					 mask_bits,
					 mask_width,
					 mask_height) )  == 0 )
    err_mess(FAILXPM,NULL);

  
  XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet );
  XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);

  /* Convert in pixmaps from .xpm includes. */
  getPixmaps();

  /* Interclient Communication stuff */
  /* Appicons don't work with out this stuff */
  SetWmHints();
  SetClassHints();


  XSetWMNormalHints( display, win, &xsizehints );
  
  /* Tell window manager what the title bar name is. We never see */
  /* this anyways in the WithdrawnState      */
  if ( XStringListToTextProperty(&app_name, 1, &app_name_atom) == 0 )
    err_mess(FAILWNAM,app_name);
  XSetWMName( display, win, &app_name_atom );
  
  /* Create Graphic Context */	
  if (( gc = XCreateGC(display, win,(GCForeground | GCBackground), &xgcValues))
       == NULL )
    err_mess(FAILGC,NULL);

  /* XEvent Masks. We want both window to process X events */
  XSelectInput(display, win,
	       ExposureMask | 
	       ButtonPressMask | 
	       ButtonReleaseMask |    	/* added ButtonReleaseMask *charkins*/
	       PointerMotionMask |
	       StructureNotifyMask |
	       LeaveWindowMask );  
  XSelectInput(display, iconwin,
	       ExposureMask | 
	       ButtonPressMask | 
	       ButtonReleaseMask |	/* added ButtonReleaseMask *charkins*/
	       PointerMotionMask |
	       StructureNotifyMask |
	       LeaveWindowMask );
  
  /* Store the 'state' of the application for restarting */
  XSetCommand( display, win, argv, argc );	

  /* Window won't ever show up until it is mapped.. then drawn after a 	*/
  /* ConfigureNotify */
  XMapWindow( display, win );

  /* Initialize Tooltip Support */
  initTime();
  initTooltip(!Config.bTooltipDisable, Config.szTooltipFont, Config.bTooltipSwapColors);

  /* X Event Loop */
  while (1) {
    while (XPending(display) || nTooltipTimer == -1) {
      XNextEvent(display, &report );
      switch (report.type) {
      case Expose:
	if (report.xexpose.count != 0) {	
	  break;
	}
	if ( Config.Verbose ) fprintf(stdout,"Event: Expose\n");	
	redraw();
	break;      

      case ConfigureNotify:
	if ( Config.Verbose ) fprintf(stdout,"Event: ConfigureNotify\n");	
	redraw();
	break;

      case MotionNotify:
	if (hasTooltipSupport()) {
	  if (!hasTooltip()) {
	    nTooltipTimer= currentTimeMillis();
	    nTooltipX= report.xbutton.x;
	    nTooltipY= report.xbutton.y;
	    nTooltipButton= whichButton(report.xbutton.x, report.xbutton.y);
	  } else {
	    int nButton = whichButton(report.xbutton.x, report.xbutton.y);
	    if (nButton != nTooltipButton) {
	      hideTooltip();
	      nTooltipTimer= -1;
	      nTooltipX = report.xbutton.x;
	      nTooltipY = report.xbutton.y;
	      nTooltipButton = nButton;
	      showTooltip(nTooltipButton, nTooltipX, nTooltipY);
	    }
	  }
	}
	break;
      
      case LeaveNotify:
	if ( Config.Verbose ) fprintf(stdout,"Event: LeaveNotify\n");	
	if (hasTooltip()) {
	  hideTooltip();
	  nTooltipHideTimer= currentTimeMillis();
	}
	nTooltipTimer= -1;
	break;

      case ButtonPress:	/* draw button pressed, don't launch *charkins*/
	if (hasTooltip()) {
	  hideTooltip();
	  nTooltipHideTimer= currentTimeMillis();
	}	switch (report.xbutton.button) {
	case Button1:
	  N = whichButton(report.xbutton.x, report.xbutton.y );
	  if ( (N >= 0) && (N <= NUMB_OF_APPS) ) {
	    button_pressed = N + LMASK;
	    redraw();
	  }

	  if ( Config.Verbose ) 
	    fprintf(stdout,"Button 1:x=%d y=%d N=%d\n", 
		    report.xbutton.x, report.xbutton.y, N+LMASK);	

	  break;
	case Button2:
	  if (Config.mmouse) {
	    N = whichButton(report.xbutton.x, report.xbutton.y );
	    if ( (N >= 0) && (N <= NUMB_OF_APPS) ) {
	      button_pressed = N + MMASK;
	      redraw();
	    }

	    if ( Config.Verbose ) 
	      fprintf(stdout,"Button 2:x=%d y=%d N=%d\n", 
		      report.xbutton.x, report.xbutton.y, N+MMASK);

	  }
	  break;
	case Button3:
	  N = whichButton(report.xbutton.x, report.xbutton.y );
	  if ( (N >= 0) && (N <= NUMB_OF_APPS) ) {
	    button_pressed = N + RMASK;
	    redraw();
	  }

	  if ( Config.Verbose ) 
	    fprintf(stdout,"Button 3:x=%d y=%d N=%d\n", 
		    report.xbutton.x, report.xbutton.y, N+RMASK);

	  break;
	}
	break;
      case ButtonRelease:   /* launch app here if still over button *charkins*/
	switch (report.xbutton.button) {
	case Button1:
	  N = whichButton(report.xbutton.x, report.xbutton.y );
	  if ( (N >= 0) && (N <= NUMB_OF_APPS) && (N == button_pressed))
	    RunAppN( N + LMASK);
	  button_pressed=-1;
	  redraw();

	  if ( Config.Verbose ) 
	    fprintf(stdout,"Button 1:x=%d y=%d N=%d\n", 
		    report.xbutton.x, report.xbutton.y, N+LMASK);	

	  break;
	case Button2:
	  if (Config.mmouse) {
	    N = whichButton(report.xbutton.x, report.xbutton.y );
	    if ( (N >= 0) && (N <= NUMB_OF_APPS) && (N == button_pressed))
	      RunAppN( N + MMASK);
	    button_pressed=-1;
	    redraw();

	    if ( Config.Verbose ) 
	      fprintf(stdout,"Button 2:x=%d y=%d N=%d\n", 
		      report.xbutton.x, report.xbutton.y, N+MMASK);

	  }
	  break;
	case Button3:
	  N = whichButton(report.xbutton.x, report.xbutton.y );
	  if ( (N >= 0) && (N <= NUMB_OF_APPS) && (N == button_pressed))
	    RunAppN( N + RMASK);
	  button_pressed=-1;
	  redraw();

	  if ( Config.Verbose ) 
	    fprintf(stdout,"Button 3:x=%d y=%d N=%d\n", 
		    report.xbutton.x, report.xbutton.y, N+RMASK);

	  break;
	}
	break;
      case DestroyNotify:

	if ( Config.Verbose ) fprintf(stdout, "Bye\n");

	destroyTooltip();
	XFreeGC(display, gc);
	XDestroyWindow(display,win);
	XDestroyWindow(display,iconwin);
	XCloseDisplay(display);
	exit(0);
	break;
      }
    }

    usleep(50000);
    nNow = currentTimeMillis();
    if ( nTooltipTimer != -1 && 
	 ( (nNow > nTooltipTimer + nTooltipShowDelay) || (nNow < nTooltipHideTimer + nTooltipReshowDelay) ) ) {
      showTooltip(nTooltipButton, nTooltipX, nTooltipY);
      nTooltipTimer = -1;
    }
    
  }

  return (0);
}/***********************************************************************/

/***********************************************************************
 *   redraw
 *
 *	 Map the button region coordinates.
 *
 *   Draw the appropriate number of buttons on the 'visible' Pixmap 
 *   using data from the 'buttons' pixmap.
 *
 *   Then, copy the 'visible' pixmap to the two windows ( the withdrawn
 *   main window and the icon window which is the main window's icon image.)
 ***********************************************************************/
void redraw() {
  int n;
  int i,j;
  int dest_x, dest_y;
  int space;
  int offset;
  int bsize = 18;

  if ( Config.Verbose ) fprintf(stdout,"In Redraw()\n");	
  space = 0;
  offset = 5;
  XCopyArea(display, template.pixmap, visible.pixmap, gc, 0, 0,
	    template.attributes.width, template.attributes.height, 0, 0 ); 
 
  for ( j=0; j < 3; j++ ) {
    for ( i=0; i < 3; i++ ) {
      n = i + j * 3;
      dest_x = i*(bsize + space) + offset + space;
      dest_y = j*(bsize + space) + offset + space;
      
      /* Define button mouse coords */
      button_region[n].x = dest_x;
      button_region[n].y = dest_y;
      button_region[n].i = dest_x + bsize - 1;
      button_region[n].j = dest_y + bsize - 1; 
      
      /* Copy button images for valid apps */
      if (  (n + 1) <= NUMB_OF_APPS ) {
        XCopyArea(display, buttons.pixmap, visible.pixmap, gc, 
	          i * bsize, j * bsize, bsize, bsize, dest_x, dest_y);
      } 
    }
  }

  if ( button_pressed>0 ) {	/* draw pressed button *charkins*/
    if(button_pressed>RMASK) button_pressed-=RMASK;
    else if(button_pressed>MMASK) button_pressed-=MMASK;
    else if(button_pressed>LMASK) button_pressed-=LMASK;
    i = (button_pressed-1) % 3;	/* get col of button */
    j = (button_pressed-1) / 3;	/* get row of button */
    dest_x = i * (bsize + space) + offset + space;
    dest_y = j * (bsize + space) + offset + space;
    XSetForeground(display, gc, bg_pixel);
    XDrawLine(display, visible.pixmap, gc,
	      dest_x+1, dest_y+bsize-1, dest_x+bsize-1, dest_y+bsize-1);
    XDrawLine(display, visible.pixmap, gc,
	      dest_x+bsize-1, dest_y+bsize-1, dest_x+bsize-1, dest_y+1);
    XSetForeground(display, gc, fg_pixel);
    XDrawLine(display, visible.pixmap, gc,
	      dest_x, dest_y, dest_x+bsize-2, dest_y);
    XDrawLine(display, visible.pixmap, gc,
	      dest_x, dest_y, dest_x, dest_y+bsize-2);
  } /*charkins*/
  
  flush_expose( win ); 
  XCopyArea(display, visible.pixmap, win, gc, 0, 0,
	    visible.attributes.width, visible.attributes.height, 0, 0 ); 
  flush_expose( iconwin ); 
  XCopyArea(display, visible.pixmap, iconwin, gc, 0, 0,
	    visible.attributes.width, visible.attributes.height, 0, 0 );

}/***********************************************************************/

/***********************************************************************
 *  whichButton
 *
 *  Return the button that at the x,y coordinates. The button need not
 *  be visible ( drawn ). Return -1 if no button match.
 ***********************************************************************/
int whichButton( int x, int y ) {
  int index;

  for ( index=0; index < NUMB_OF_APPS; index++ ) {
    if ( x >= button_region[index].x &&
	 x <= button_region[index].i &&
	 y >= button_region[index].y &&
	 y <= button_region[index].j  ) {
      return( index + 1);
    }
  }
  return(-1);
}/***********************************************************************/


/***********************************************************************
 *   getPixmaps
 *    
 *   Load XPM data into X Pixmaps.
 *  
 *   Pixmap template contains the untouched window backdrop image.
 *   Pixmap visible is the template pixmap with buttons drawn on it.
 *          -- what is seen by the user.
 *   Pixmap buttons holds the images for individual buttons that are
 *          later copied onto Pixmap visible.
 ***********************************************************************/
void getPixmaps() {
  int loaded = 0;
  template.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
  visible.attributes.valuemask  |= (XpmReturnPixels | XpmReturnExtensions);
  buttons.attributes.valuemask  |= (XpmReturnPixels | XpmReturnExtensions);
  

  if (Config.Verbose) fprintf(stdout, "In getPixmaps\n");
  /* Template Pixmap. Never Drawn To. */
  if ( XpmCreatePixmapFromData(	display, rootwin, backdrop_xpm,
				&template.pixmap, &template.mask, 
				&template.attributes) != XpmSuccess )
    err_mess(FAILTMPL, NULL);

  /* Visible Pixmap. Copied from template Pixmap and then drawn to. */
  if ( XpmCreatePixmapFromData(	display, rootwin, backdrop_xpm,
				&visible.pixmap, &visible.mask, 
				&visible.attributes) != XpmSuccess )
    err_mess(FAILVIS, NULL);
  
  /* Button Pixmap.  */
  if ( access( Config.buttonfile, R_OK  ) == 0 ) {
    /* load buttons from file */
    if ( XpmReadFileToPixmap( display, rootwin, Config.buttonfile,
      			      &buttons.pixmap, &buttons.mask, 
			      &buttons.attributes) != XpmSuccess ) {
      err_mess(FAILBUT, NULL); 
    } else {
      loaded = 1;
    }
  }
  if (! loaded) { 
    /* Use Builtin Button Pixmap.  */
    if (Config.Verbose) fprintf(stdout, "Using builtin buttons pixmap\n");
    if ( XpmCreatePixmapFromData( display, rootwin, buttons_xpm,
				  &buttons.pixmap, &buttons.mask, 
				  &buttons.attributes) != XpmSuccess )
      err_mess(FAILBUT, NULL); 
  }
  if (Config.Verbose) fprintf(stdout, "Leaving getPixmaps\n");

}/*********************************************************************/

void SetWmHints() {
  XWMHints *xwmhints;

  xwmhints = XAllocWMHints();
  xwmhints->flags = WindowGroupHint | IconWindowHint | StateHint;	
  xwmhints->icon_window = iconwin;
  xwmhints->window_group = win;
  xwmhints->initial_state = WithdrawnState;  
  XSetWMHints( display, win, xwmhints );
	XFree(xwmhints);
	xwmhints = NULL;
}

void SetClassHints() {
  XClassHint xclasshint;

  xclasshint.res_name = "wmbutton";
  xclasshint.res_class = "Wmbutton";
  XSetClassHint( display, win, &xclasshint );
}