File: xinit.c

package info (click to toggle)
mpich 1.1.0-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 22,116 kB
  • ctags: 27,349
  • sloc: ansic: 193,435; sh: 11,172; fortran: 6,545; makefile: 5,801; cpp: 5,020; tcl: 3,548; asm: 3,536; csh: 1,079; java: 614; perl: 183; awk: 168; sed: 70; f90: 62
file content (635 lines) | stat: -rw-r--r-- 17,492 bytes parent folder | download | duplicates (2)
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include <stdio.h>
#include "mpetools.h"
#include "basex11.h"

#if defined(NEEDS_STDLIB_PROTOTYPES)
#include "protofix.h"
#endif

/* 
   This file contains routines to open an X window display and window
   This consists of a number of routines that set the various
   fields in the XBaseWindow structure, which is passed to 
   all of these routines.

   Note that if you use the default visual and colormap, then you
   can use these routines with any X toolkit that will give you the
   Window id of the window that it is managing.  Use that instead of the
   call to XBCreateWindow .  Similarly for the Display.
 */


static void ArgSqueeze ANSI_ARGS(( int *, char ** ));
static int ArgFindName ANSI_ARGS(( int, char **, char * ));
static int ArgGetString ANSI_ARGS(( int *, char **, int, char *, 
				    char *, int ));
/*
   ArgSqueeze - Remove all null arguments from an arg vector; 
   update the number of arguments.
*/
static void ArgSqueeze( Argc, argv )
int  *Argc;
char **argv;
{
    int argc, i, j;
    
/* Compress out the eliminated args */
    argc = *Argc;
    j    = 0;
    i    = 0;
    while (j < argc) {
	while (argv[j] == 0 && j < argc) j++;
	if (j < argc) argv[i++] = argv[j++];
    }
/* Back off the last value if it is null */
    if (!argv[i-1]) i--;
    *Argc = i;
}


/*
   ArgFindName -  Find a name in an argument list.

   Input Parameters:
.  argc - number of arguments
.  argv - argument vector
.  name - name to find

   Returns:
   index in argv of name; -1 if name is not in argv
*/
static int ArgFindName( argc, argv, name )
int  argc;
char **argv;
char *name;
{
    int  i;

    for (i=0; i<argc; i++) {
	if (strcmp( argv[i], name ) == 0) return i;
    }
    return -1;
}


/*
  ArgGetString - Get the value (string) of a named parameter.
  
  Input Parameters:
. Argc  - pointer to argument count
. argv  - argument vector
. rflag - if true, remove the argument and its value from argv
. val   - pointer to buffer to hold value (will be set only if found).
. vallen- length of val
 
  Returns:
  1 on success
*/
static int ArgGetString( Argc, argv, rflag, name, val, vallen )
int  *Argc, rflag, vallen;
char **argv, *name, *val;
{
    int idx;

    idx = ArgFindName( *Argc, argv, name );
    if (idx < 0) return 0;

    if (idx + 1 >= *Argc) {
	SETERRC(1,"Missing value for argument" );
	return 0;
    }

    strncpy( val, argv[idx+1], vallen );
    if (rflag) {
	argv[idx]   = 0;
	argv[idx+1] = 0;
	ArgSqueeze( Argc, argv );
    }
    return 1;
}


/*
  XBWinCreate - Create an XBWin structure that can be used by all other XB
  routines
  */
XBWindow *XBWinCreate()
{
    XBWindow *w;

    w = NEW(XBWindow);
    CHKPTRN(w);

/* Initialize the structure */
    w->disp      = 0;
    w->screen    = 0;
    w->win       = 0;
    w->gc.set    = 0;
    w->vis       = 0;
    w->numcolors = 0;
    w->maxcolors = 0;
    w->cmap      = 0;
    w->foreground= 0;
    w->background= 0;
    w->x         = 0;
    w->y         = 0;
    w->w         = 0;
    w->h         = 0;
    w->drw       = 0;
    return w;
}

/* 
  XBWinDestroy - Recover an XBWindow structure

  Input parameter:
. w  - XB window structure to destroy.  
*/
void XBWinDestroy( w )
XBWindow *w;
{
/* Should try to recover X resources ... */
/* Needs to close window ! */
    FREE( w );
}

/* Thanks to Hubertus Franke */
#define MAX_TRY (5)
/*
  XBOpenDisplay - Open a display
  
  Input Parameters:
  XBWin        - pointer to base window structure
  display_name - either null ("") or of the form "host:0"
  */
int XBOpenDisplay( XBWin, display_name )
XBWindow *XBWin;
char     *display_name;
{
    int i;
    if (display_name && display_name[0] == 0)
	display_name = 0;

    for (i=0; i<MAX_TRY; i++) {
	XBWin->disp = XOpenDisplay( display_name );
	if (!XBWin->disp) {
	    if (i < MAX_TRY) { sleep(1); continue; }
	}
	else 
	    break;
    }
    if (!XBWin->disp) 
	return ERR_CAN_NOT_OPEN_DISPLAY;

/* Set the default screen */
    XBWin->screen = DefaultScreen( XBWin->disp );

/* ? should this set defaults? */
    return ERR_NONE;
}

/*
    XBSetVisual - set the visual class for a window and colormap

    Input Parameters:
.   nc - number of colors.  Use the maximum of the visual if
    nc == 0.  Use nc = 2 for black and white displays.
    */
int XBSetVisual( XBWin, q_default_visual, cmap, nc )
XBWindow *XBWin;
int      q_default_visual;
Colormap cmap;
int      nc;
{
if (q_default_visual) {
    XBWin->vis    = DefaultVisual( XBWin->disp, XBWin->screen );
    XBWin->depth  = DefaultDepth(XBWin->disp,XBWin->screen);
    if (!cmap)
        XBWin->cmap  = DefaultColormap( XBWin->disp, XBWin->screen );
    else
        XBWin->cmap  = cmap;
    }
else {
    /* Try to match to some popular types */
    XVisualInfo vinfo;
    if (XMatchVisualInfo( XBWin->disp, XBWin->screen, 24, DirectColor,
			 &vinfo)) {
	XBWin->vis    = vinfo.visual;
	XBWin->depth  = 24;
	}
    else if (XMatchVisualInfo( XBWin->disp, XBWin->screen, 8, PseudoColor,
			 &vinfo)) {
	XBWin->vis    = vinfo.visual;
	XBWin->depth  = 8;
	}
    else if (XMatchVisualInfo( XBWin->disp, XBWin->screen,
			 DefaultDepth(XBWin->disp,XBWin->screen), PseudoColor,
			 &vinfo)) {
	XBWin->vis    = vinfo.visual;
	XBWin->depth  = DefaultDepth(XBWin->disp,XBWin->screen);
	}
    else {
	XBWin->vis    = DefaultVisual( XBWin->disp, XBWin->screen );
	XBWin->depth  = DefaultDepth(XBWin->disp,XBWin->screen);
	}
    /* There are other types; perhaps this routine should accept a 
       "hint" on which types to look for. */
    XBWin->cmap = (Colormap) 0;
    }

/* reset the number of colors from info on the display, the colormap */
XBInitColors( XBWin, cmap, nc );
return ERR_NONE;
}

/* 
   XBSetGC - set the GC structure in the base window
   */
int XBSetGC( XBWin, fg )
XBWindow *XBWin;
PixVal   fg;
{
XGCValues       gcvalues;       /* window graphics context values */

/* Set the graphics contexts */
/* create a gc for the ROP_SET operation (writing the fg value to a pixel) */
/* (do this with function GXcopy; GXset will automatically write 1) */
gcvalues.function   = GXcopy;
gcvalues.foreground = fg;
XBWin->gc.cur_pix   = fg;
XBWin->gc.set = XCreateGC( XBWin->disp, RootWindow(XBWin->disp,XBWin->screen),
                              GCFunction | GCForeground, &gcvalues );
return ERR_NONE;
}

/*
   XBOpenWindow - Open the window data structure 

   Note that before a window can be opened, the Visual class and the
   (initial) colormap should be set.
    Opening a window comes in two parts:
    Creating most of the local window structure, and
    actually creating and mapping the window.
    This allows us to defer the choice of the window size until
    the various tools have been set (note that many tools have
    minimum sizes determined by things like the current font size).

    This needs to know a few more things inorder to co-exist with
    other tools and applications.  In particular, it needs to know
    whether to use the default visual or another window's colormap.
  */
int XBOpenWindow( XBWin )
XBWindow *XBWin;
{
return ERR_NONE;
}

/*
    Actually display a window at [x,y] with sizes (w,h)
    If w and/or h are 0, use the sizes in the fields of XBWin
    (which may have been set by, for example, XBSetWindowSize)
 */
int XBDisplayWindow( XBWin, label, x, y, w, h, backgnd_pixel )
XBWindow *XBWin;
char     *label;
int      w, h, x, y;
PixVal   backgnd_pixel;
{
unsigned int    wavail, havail;
XSizeHints      size_hints;
int             q_user_pos;
XWindowAttributes       in_window_attributes;
XSetWindowAttributes    window_attributes;
int                     depth, border_width;
unsigned long           wmask;

/* get the available widths */
wavail              = DisplayWidth(  XBWin->disp, XBWin->screen );
havail              = DisplayHeight( XBWin->disp, XBWin->screen );

if (w <= 0 || h <= 0) return ERR_ILLEGAL_SIZE;

if (w > wavail)
    w   = wavail;
if (h > havail)
    h   = havail;

/* Find out if the user specified the position */
q_user_pos  = (x >= 0) && (y >= 0);

border_width   = 0;
if (x < 0) x   = 0;
if (y < 0) y   = 0;
x   = (x + w > wavail) ? wavail - w : x;
y   = (y + h > havail) ? havail - h : y;

/* We need XCreateWindow since we may need an visual other than
   the default one */
XGetWindowAttributes( XBWin->disp, RootWindow(XBWin->disp,XBWin->screen),
                      &in_window_attributes );
window_attributes.background_pixmap = None;
window_attributes.background_pixel  = backgnd_pixel;
/* No border for now */
window_attributes.border_pixmap     = None;
/* 
window_attributes.border_pixel      = border_pixel; 
 */
window_attributes.bit_gravity       = in_window_attributes.bit_gravity;
window_attributes.win_gravity       = in_window_attributes.win_gravity;
        /* Backing store is too slow in color systems */
window_attributes.backing_store     = 0;
window_attributes.backing_pixel     = backgnd_pixel;
window_attributes.save_under        = 1;
window_attributes.event_mask        = 0;
window_attributes.do_not_propagate_mask = 0;
window_attributes.override_redirect = 0;
window_attributes.colormap          = XBWin->cmap;
/* None for cursor does NOT mean none, it means Parent's cursor */
window_attributes.cursor            = None; 
wmask   = CWBackPixmap | CWBackPixel | CWBorderPixmap | CWBitGravity |
          CWWinGravity | CWBackingStore | CWBackingPixel | CWOverrideRedirect |
          CWSaveUnder  | CWEventMask    | CWDontPropagate |
          CWCursor     | CWColormap ;
/* depth should really be the depth of the visual in use */
depth       = DefaultDepth( XBWin->disp, XBWin->screen );
XBWin->win  = XCreateWindow( XBWin->disp, 
			     RootWindow(XBWin->disp,XBWin->screen),
                             x, y, w, h, border_width,
                             depth, InputOutput, XBWin->vis,
                             wmask, &window_attributes );

if (!XBWin->win) 
    return ERR_CAN_NOT_OPEN_WINDOW;

/* set resize hints (prohibit?) */
size_hints.x            = x;
size_hints.y            = y;
size_hints.min_width    = 4*border_width;
size_hints.min_height   = 4*border_width;
size_hints.width        = w;
size_hints.height       = h;
if (q_user_pos)
    size_hints.flags        = USPosition | USSize | PMinSize; /*  | PAspect; */
else
    size_hints.flags        = PPosition  | PSize  | PMinSize; /*  | PAspect; */

/* Set the standard properties */
XSetStandardProperties( XBWin->disp, XBWin->win, label, label, 0,
                        (char **)0, 0, &size_hints );

/* make the window visible */
XSelectInput( XBWin->disp, XBWin->win, ExposureMask | StructureNotifyMask );
XMapWindow( XBWin->disp, XBWin->win );

/* some window systems are cruel and interfere with the placement of
   windows.  We wait here for the window to be created or to die */
if (XB_wait_map( XBWin, 
		 (void (*)ANSI_ARGS(( XBWindow *, int, int, int, int )))0 )) {
    XBWin->win    = (Window)0;
    return 0;
    }
/* Initial values for the upper left corner */
XBWin->x = 0;
XBWin->y = 0;
return 0;
}



/* 
   There should also be a routine that gets this data from a widget, so
   that this structure can be used with a widget set 
 */

/*
  XBGetArgs - Get the X related arguments (geometry for now)

  Input Parameters:
. Argc - pointer to argument count
. argv - argument vector
. flag - 1 if the "found" arguments should be stripped from the argument list

  Output Parameters:
. px,py - position of window
. pw,ph - width and height of window

  Note:
  The output parameters are unchanged if they are not set.  The form of the
  argument is
$ -geometry width x height + xoffset + yoffset
$ with no spaces.  For example
$ -geometry 400x400
$ for a 400 x 400 window, and
$ -geometry +100+200
$ for a window located at (100,200).
  */
void XBGetArgs( Argc, argv, flag, px, py, pw, ph )
int  *Argc, flag;
char **argv;
int  *px, *py, *pw, *ph;
{
    char         val[128];
    int          vallen;
    int          st, xx, yy;
    unsigned int ww, hh;

    vallen = 128;
    if (ArgGetString( Argc, argv, flag, "-geometry", val, vallen )) {
	/* value is of form wxh+x+y */
	st  = XParseGeometry( val, &xx, &yy, &ww, &hh );
	if (st & XValue)        *px = xx;
	if (st & YValue)        *py = yy;
	if (st & WidthValue)    *pw = (int)ww;
	if (st & HeightValue)   *ph = (int)hh;
    }
}

/*
  XBGetArgsDisplay - Get the X display name from the argument list

  Input Parameters:
. Argc - pointer to argument count
. argv - argument vector
. flag - 1 if the "found" arguments should be stripped from the argument list
. dlen - length of dname

  Output Parameters:
. dname - Name of display

  Note:
  The output parameter is unchanged if they are not set.  The form of the
  argument is
$ -display name
  */
void XBGetArgsDisplay( Argc, argv, flag, dlen, dname )
int  *Argc, flag;
char **argv, *dname;
int  dlen;
{
    ArgGetString( Argc, argv, flag, "-display", dname, dlen );
}


int XBiQuickWindow( mywindow, host, name, x, y, nx, ny, nc )
XBWindow *mywindow;
char     *host, *name;
int      x, y, nx, ny, nc;
{
    if (XBOpenDisplay( mywindow, host )) {
	fprintf( stderr, "Could not open display\n" );
	return 1;
    }
    if (XBSetVisual( mywindow, 1, (Colormap)0, nc )) {
	fprintf( stderr, "Could not set visual to default\n" );
	return 1;
    }
    if (XBOpenWindow( mywindow )) {
	fprintf( stderr, "Could not open the window\n" );
	return 1;
    }
/* Use cmapping[0] for the background */
    if (XBDisplayWindow( mywindow, name, x, y, nx, ny, 
			 mywindow->cmapping[0])) {
	fprintf( stderr, "Could not display window\n" );
	return 1;
    }
/* Set the foreground */
    XBSetGC( mywindow, mywindow->cmapping[1] );
    XBClearWindow(mywindow,0,0,mywindow->w,mywindow->h);
    return 0;
}

/* 
   XBQuickWindow - Create an X window

   Input parameters:
.  mywindow - A pointer to an XBWindow structure that will be used to hold
              information on the window.  This should be acquired with
	      XBWinCreate.
.  host     - name of the display
.  name     - title (name) of the window
.  x,y      - coordinates of the upper left corner of the window.  If <0,
              use user-positioning (normally, the window manager will
	      ask the user where to put the window)
.  nx,ny    - width and height of the window

   Note:
   This creates a window with various defaults (visual, colormap, etc)

   A small modification to this routine would allow Black and White windows
   to be used on color displays; this would be useful for testing codes.
   */
int XBQuickWindow( mywindow, host, name, x, y, nx, ny )
XBWindow *mywindow;
char     *host, *name;
int      x, y, nx, ny;
{
/* Just to be careful, clear mywindow */
    MEMSET( mywindow, 0, sizeof(XBWindow) );
    return XBiQuickWindow( mywindow, host, name, x, y, nx, ny, 0 );
}

/* 
   And a quick version (from an already defined window) 
 */
int XBQuickWindowFromWindow( mywindow, host, win )
XBWindow *mywindow;
char     *host;
Window   win;
{
    Window       root;
    int          d;
    unsigned int ud;

    if (XBOpenDisplay( mywindow, host )) {
	fprintf( stderr, "Could not open display\n" );
	return 1;
    }
    if (XBSetVisual( mywindow, 1, (Colormap)0, 0 )) {
	fprintf( stderr, "Could not set visual to default\n" );
	return 1;
    }

    mywindow->win = win;
    XGetGeometry( mywindow->disp, mywindow->win, &root, 
		  &d, &d, 
		  (unsigned int *)&mywindow->w, (unsigned int *)&mywindow->h,
		  &ud, &ud );
    mywindow->x = mywindow->y = 0;

    XBSetGC( mywindow, mywindow->cmapping[1] );
    return 0;
}

/* 
    XBFlush - Flush all X 11 requests.

    Input parameter:
.   XBWin - window

    Note:  Using an X drawing routine does not necessarily cause the
    the server to receive and draw the requests.  Use this routine
    if necessary to force the server to draw (doing so may slow down
    the program, so don't insert unnecessary XBFlush calls).

    If double-buffering is enabled, this routine copies from the buffer
    to the window before flushing the requests.  This is the appropriate
    action for animation.
 */
void XBFlush( XBWin )
XBWindow *XBWin;
{
    if (XBWin->drw) {
	XCopyArea( XBWin->disp, XBWin->drw, XBWin->win, XBWin->gc.set, 0, 0, 
		   XBWin->w, XBWin->h, XBWin->x, XBWin->y );
    }
    XFlush( XBWin->disp );
}

/* 
      XBSetWindowLabel - Sets new label in open window.

  Input Parameters:
.  window - Window to set label for
.  label  - Label to give window
  */
void XBSetWindowLabel( XBwin, label )
XBWindow *XBwin;
char     *label;
{
    XTextProperty prop;
    XGetWMName(XBwin->disp,XBwin->win,&prop);
    prop.value = (unsigned char *)label; prop.nitems = (long) strlen(label);
    XSetWMName(XBwin->disp,XBwin->win,&prop);
}

/*
  XBCaptureWindowToFile - Capture a window and write it in xwd format to a file

  Input Parameters:
. XBWin - XB Window to write out
. fname - name of file

  Notes:
  This command uses XGetImage; thus, the window must be unobscured.  The
  current implementation actually uses xwd called by spawning a process.  Later
  implementations may write the file directly.

  Be sure that you have XBFlush()'ed the output; X11 does not require that
  the image be current until a flush is executed.
 */
void XBCaptureWindowToFile( XBWin, fname )
XBWindow *XBWin;
char     *fname;
{
#ifdef HAVE_SYSTEM
    char cmdbuf[1024];
    sprintf( cmdbuf, "xwd -id %ld > %s\n", (long)XBWin->win, fname );
    system( cmdbuf );
#else
    fprintf( stderr, "This machine does not support the system call\n\
which is needed by XBCaptureWindowToFile\n" );
#endif
}