File: Xinit.c

package info (click to toggle)
xabacus 1-6
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 244 kB
  • ctags: 484
  • sloc: ansic: 746; makefile: 451
file content (275 lines) | stat: -rw-r--r-- 8,800 bytes parent folder | download
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
#include <stdio.h>
#include <X11/cursorfont.h>

#include "icon.bm"

#include "main.h"

Pixel getcolor();
Pixel bgcolor, bdcolor;	/* background & border*/

/*--------------------------------------------------------------------
 * makeWindows, connects to X-server, creates the application windows, 
 *			allocates GC's & sets WM hints, creates cursors & maps windows
 *
 *			passed: argc, argv straight from the commandline
 *			returns: nothing
 *			dep: win struct, a bunch of globals; see maindefs.h
 */
makeWindows(argc, argv, display_name)
int argc; 
char **argv;
char *display_name;
{
unsigned int border_width=1;
char *window_name=APPNAME;
char *icon_name=APPNAME;
/*char *display_name=NULL;*/
Pixmap icon_pixmap;
int i;

#ifdef DEBUG
	fprintf(stderr,"DEBUG: makeWIndows()\n");
#endif

	/* connect to X server */
    if ( (display=XOpenDisplay(display_name)) == NULL ) {
        fprintf(stderr,"%s: cannot connect to X server %s\n", argv[0], 
XDisplayName(display_name));
        exit( -1 );
    }

	/* get screen size from display structure macro */
    screen = DefaultScreen(display);

	bgcolor=getcolor(colors[BACKGROUND]);
	bdcolor=getcolor(BDWINCOLOR);

	/* create opaque window;inherit attributes from root*/
   	base= XCreateSimpleWindow(display, RootWindow(display, screen),
posx, posy, width, height, border_width, bdcolor, bgcolor);

	if(demo){
		demobase=XCreateSimpleWindow(display, RootWindow(display, screen),
posx, posy, demowidth, demoheight, border_width, bdcolor, bgcolor);

	load_font(&font_info);
	}

	/* create separate GC for drawing */
	for(i=0; i<MAXCOLORS; i++) makeGCs(&gc[i], colors[i]);

	/* do special settings*/
    XSetLineAttributes(display, gc[FRAME], framewidth, LineSolid, CapButt, 
JoinMiter);	/* extra thick lines for the frame*/

	if(demo) XSetFont(display, gc[FRAME], font_info->fid);
	XSetFillStyle(display,gc[BEADS],FillSolid);
	XSetFunction(display,gc[BEADS],GXxor);	/* so we don't disturb BG while
												animating beads*/

    /* Create bitmap for icon */
    icon_pixmap= XCreateBitmapFromData(display, base, icon_bits, icon_width,
icon_height);

    if(demo){
    	/* Set resize hints */
		size_hints.flags= PPosition | PMinSize | PMaxSize;
    	size_hints.width= demowidth;	 		
		size_hints.height= demoheight;
    	size_hints.min_width=size_hints.max_width=demowidth;	
		size_hints.min_height=size_hints.max_height=demoheight;
    	size_hints.x= posx+10;			 		size_hints.y= posy+10;

    	/* set Properties for window manager (always before mapping) */
    	XSetStandardProperties(display, demobase, DEMOWINAME, DEMOWINAME, 
			icon_pixmap, argv, argc, &size_hints);

    	/* Select event types wanted */
    	XSelectInput(display, demobase, ExposureMask | KeyPressMask | 
			ButtonPressMask | ButtonReleaseMask);
	}

   	/* Set resize hints re-using size_hints struct for the base-window*/
	size_hints.flags= PPosition | PSize | PMinSize|PMaxSize;
   	size_hints.width=size_hints.min_width=width;	 		
	size_hints.height= height;
	/* the abacus can be made as wide as the screen or MAXCOLS (whichever is
	 * smaller)*/
	size_hints.max_width=DisplayWidth(display, screen);
	/* height is fixed at run-time */
	size_hints.min_height=size_hints.max_height=height;
   	size_hints.x= posx;			 		size_hints.y= posy;

    /* set Properties for window manager (always before mapping) */
    XSetStandardProperties(display, base, window_name, icon_name, 
icon_pixmap, argv, argc, &size_hints);

    /* Select event types wanted */
    XSelectInput(display, base, ExposureMask | KeyPressMask | ButtonPressMask | 
StructureNotifyMask | PointerMotionHintMask | ButtonMotionMask | 
ButtonReleaseMask);

	/* create & map unique cursor to this window*/
	Hand=XCreateFontCursor(display, XC_hand2);
	XDefineCursor(display, base, Hand);

	if (demo) XDefineCursor(display, demobase, Hand);

    XMapWindow(display, base);
    if (demo) XMapWindow(display, demobase);

}/*make Windows*/
/*-----------------------------------------------------------------
 * makeGCs, creates a default GC then sets the color, line &
 *      font attributes; font must have been loaded before this is called
 *
 *      passed: pointer to GC to be set,
 *              colorname to set("red","green"...)
 *      returns: nothing
 *          dep: XGC values, win, display, getcolor()
 */
makeGCs(gc, colorname)
GC *gc;
char *colorname;
{
unsigned long valuemask = 0; /* use defaults*/
XGCValues values;

    *gc = XCreateGC(display, base, valuemask, &values); /* ...create gc*/

    /* ...set color & other GC attributes*/
    XSetForeground(display, *gc, getcolor(colorname));
    XSetBackground(display, *gc, getcolor(colors[BACKGROUND]));
    XSetLineAttributes(display, *gc, 0, LineSolid, CapButt, JoinMiter);

}/* makeGCs*/

/*-------------------------------------------------------------------
 * closeDisplay, de-allocates GC's, fonts & pixmaps, breaks connection with
 *          X server & de-allocates all data, exits application
 *
 *          passed: nothing
 *          returns:doesn't return
 *          dep: globals in maindefs.h
 */
/* ARGSUSED*/
closeDisplay()
{
int i;
    for(i=0; i<MAXCOLORS; i++) XFreeGC(display, gc[i]);

    if (demo) XUnloadFont(display, font_info->fid);

	XFreeCursor(display, Hand);

    XCloseDisplay(display);

	exit(0);
}

/*------------------------------------------------------------------------
 * getcolor, allocates requested color in the default colormap & returns
 *          a RGB pixel-value for use in allocating a GC.
 *          Automatically detects a monochrome display, & assumes
 *          black-on-white thus returning black instead of requested colors
 *          & white when requested.
 *
 *          passed: colorname to allocate
 *          returns: RGB pixel-value encoided in an unsigned long (I'm
 *                  breaking Rule#1 of Xlib programming by accessing
 *                  the individual  structure elements rather than
 *                  using correct types (Pixel is defined for Xt only))
 *              dep: nothing
 */
unsigned long getcolor(colorname)
char *colorname;
{
int depth;              /* of the screen*/
Colormap cmap;          /* for allocating colors*/
XColor generic;         /* hold pixel-values while making different GC's*/
int i;

    depth=DisplayPlanes(display, screen);   /* is it mono or color*/
    cmap=DefaultColormap(display, screen);

    /* mono screen; ignore colorname; default: black on white */
    if(depth==1){
        if(!strcmp(colorname,"white"))      /* user wants white*/
            return(WhitePixel(display, screen));
        else
            return(BlackPixel(display, screen));
    }
    else{

        /* parse the color & get pixel values to set color... */
        if(!XParseColor(display, cmap, colorname, &generic)){
            fprintf(stdout,"%s: Bad colorname:%s \n", APPNAME, colorname);
            return(BlackPixel(display, screen));    /* send back default*/
        }

        /* allocate color in colormap if space is available*/
        if(!XAllocColor(display, cmap, &generic)){
            fprintf(stdout,"Could not allocate %s, colormap full or \
write-immune\n",colorname);
             return(BlackPixel(display, screen));
        }

        /* color is good*/
        return(generic.pixel);
    }
}/* get color*/

load_font(font_info)
XFontStruct **font_info;
{
    /*char *fontname = "-*-times-*-r-*-*-*-180-*";*/
	char *altfontname = "8x13";

    /* Access font */
    if ((*font_info=XLoadQueryFont(display,demofont)) == NULL) {
		(void) fprintf( stderr, "Cannot open %s font\n",demofont);
		(void) fprintf( stderr, "Attempting %s font as alternate\n",
						altfontname);

		if((*font_info=XLoadQueryFont(display,altfontname)) == NULL) {
        	(void) fprintf( stderr, "Cannot open %s alternate font\n",
						altfontname);
			(void) fprintf( stderr, "use the -demofont option to specify a \
font to use\n");
        	exit( -1 );
		}
    }
}

/*-----------------------------------------------------------------
 * report_button, reports which of the 3 mouse button was pressed
 *          This implementation is *technically* incorrect,
 *  I looked-up the type of data the button-event was (unsigned int);
 *  I don't know how else to do it.
 *  To maintain portability integrity the user must never directly
 *  access the data-types! (I just broke this rule)
 *      -LF
 *
 *          passed: the xevent item with the value of button pressed
 *          returns: 1- if left-button pressed
 *                   2- if middle button
 *                   3- if right button
 */
int report_button(butn)
unsigned int butn;
{

    switch(butn){
        case Button1:
            return(1);

        case Button2:
            return(2);

        case Button3:
            return(3);
    }/* switch*/

}/* report button*/