File: init.c

package info (click to toggle)
kdrill 6.5deb2-12
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 1,340 kB
  • sloc: ansic: 7,636; makefile: 47
file content (393 lines) | stat: -rw-r--r-- 10,442 bytes parent folder | download | duplicates (5)
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
#include <stdlib.h>	/* for srand48() */
#include <unistd.h>	/* for "access()" */
#include <stdio.h>

#include <limits.h>	/* for POSIX_PATH_MAX */

#include <Xatom.h>
#include <Xfuncs.h>	/* handles bzero redefine stuff */
#include <Xos.h>
#include <Intrinsic.h>
#include <StringDefs.h>
#include <Shell.h>
#include <Xaw/Command.h>
#include <Xaw/Label.h>
#include <Xaw/Form.h>
#include <Composite.h>


#include "defs.h"
#include "externs.h"
#include "game.h"
#include "grades.h"
#include "options.h"
#include "searchwidgets.h"
#include "widgets.h"
#include "timeout.h"

#include "icon_xbm"

int lowfrequency=0,highfrequency=0;

char *usefile=NULL;	/* global pointer that indicates if usefile is
			 * AVAILABLE, not neccessarily that it is used
			 */
char usefilename[100];

Atom wm_message,delete_message;


/*
 * This array has "fallback resources", in case
 * the normal "KDrill" resource file is not present.
 * Note that it is an either/or situation.
 * If there is a KDrill app-resources file on the system,
 * these fallback resources WILL NOT BE LOOKED AT !!!
 *
 *  If you add something to this list, consider adding it to
 *  SaveConfig() as well!!
 *  And dont forget that you have to use SetXtrmXXXX() routines
 *  to have the values available for SaveConfig() also!!
 *
 */
static char *fallback[] = {
	"KDrill.options_popupx:		0",
	"KDrill.options_popupy:		-200",
	"KDrill.search_popupx:		400", /* should really be precise,
						but... */
	"KDrill.search_popupy:		0",
	"KDrill.notallT:	0",
	"KDrill.noBell:		0",
	"KDrill.nousefile:	1",
	"KDrill.usefile:	.kanjiusefile", 
	DICTLOCATION,	/* change this value in Imakefile !!*/
	EDICTLOCATION,	/* change this value in Imakefile !!*/
	RADLOCATION,	/* change this value in Imakefile !!*/
	"KDrill.kanjifont:	-jis-*--24*jisx0208*",
	"KDrill.smallkanji:	-jis-*--16*jisx0208*",
	"KDrill.englishfont:	fixed",
	"KDrill.gradelevel:	0",
	"KDrill.guessmode:	english",
	"KDrill.questionmode:	kanji",
	"KDrill.romajiswitch:	0",
	"KDrill.lowfrequency:	0",
	"KDrill.highfrequency:	0",
	"KDrill.showinorder:	0",
	"KDrill.timersec:	7",
	"KDrill.logfile:	kdrill.log",
	NULL
};

/* command line arguments should set the appropriate resources.. so we can
 * pull them out when wanted
 */
static XrmOptionDescRec optionDescList[] = {
	{"-notallT",	".notallT",XrmoptionNoArg,"1"},
	{"-usefile",	".usefile",	XrmoptionSepArg,(caddr_t) NULL},
	{"-nousefile",  ".nousefile",	XrmoptionNoArg, "1"},
	{"-kdictfile",	".kdictfile",	XrmoptionSepArg,(caddr_t) NULL},
	{"-edictfile",	".edictfile",	XrmoptionSepArg,(caddr_t) NULL},
	{"-radkfile",	".radkfile",	XrmoptionSepArg,(caddr_t) NULL},
	{"-englishfont",".englishfont",	XrmoptionSepArg,(caddr_t) NULL},
	{"-kanjifont",	".kanjifont",	XrmoptionSepArg,(caddr_t) NULL},
	{"-smallkanji",	".smallkanji",	XrmoptionSepArg,(caddr_t) NULL},
	{"-noBell",     ".noBell",	XrmoptionNoArg, "1"},
	{"-guessmode",	".guessmode",	XrmoptionSepArg,(caddr_t) NULL},
	{"-questionmode",".questionmode",XrmoptionSepArg,(caddr_t) NULL},
	{"-romajiswitch",   ".romajiswitch",    XrmoptionNoArg,"1"},
	{"-showinorder",   ".showinorder",    XrmoptionNoArg,"1"},
	{"-gradelevel", ".gradelevel",  XrmoptionSepArg,(caddr_t) NULL},
	{"-lowfrequency",	".lowfrequency",XrmoptionSepArg,(caddr_t) NULL},
	{"-highfrequency",	".highfrequency",XrmoptionSepArg,(caddr_t) NULL},
	{"-logfile",	".logfile",	XrmoptionSepArg,(caddr_t) NULL},
	
	
};



/* handle_delete
 *	Its sole purpose is to handle WM_DELETE messages.
 *	These days, we want to only CLOSE windows that aren't the
 *	main one.
 *
 */
void
handle_delete(Widget w,XtPointer closure,XEvent *event,Boolean *cont)
{
	XClientMessageEvent *cevent = (XClientMessageEvent *) event;

	if(cevent->type != ClientMessage) return;
	if(cevent->message_type != wm_message) return;
	if(cevent->data.l[0] != delete_message)  return;

	if(w == toplevel){
		quit(NULL, NULL, NULL);
	}

#ifdef DEBUG
	puts("In handle_delete...");
	if(XtIsTopLevelShell(w)){
		puts("   IS a top level shell");
	}
	if(XtIsShell(w)){
		puts("   IS a  shell");
	}
	if(XtIsTransientShell(w)){
		puts("   IS a 'transient' shell");
	}


	if(XtIsTopLevelShell(XtParent(w))){
		puts("   PARENT is a top level shell");
	}
	if(XtIsShell(XtParent(w))){
		puts("   PARENT is a  shell");
	}
	if(XtIsTransientShell(XtParent(w))){
		puts("   PARENT is a 'transient' shell");
	}

	
#endif /* DEBUG */


	/* Problem here: there are three types of windows we have
	   to deal with:
	   "Option window" type  (shell, transient)
	   multi-match window  (toplevel, shell)
	   seach window (toplevel, shell: but DIFFERENT from multi somehow)
	   XtPopdown works for the first two, but only UnmapWidget works
	   for last kind.
	   The only difference I can see, is that the search window
	   has "toplevel" as its parent, which is not strictly
	   speaking just a window. It is what XtVaAppInitialize() returns.
	   It CONTAINS a window, but is not merely a window.
	   searchwindow is made that way, to remain open, when
	   the main window is iconified.
	   I *could* just special-case it. But i hate special cases.
	*/
	XtPopdown(w);
	if(isMapped(w)){
		/* if it is still showing.. try harder! */
		XtUnmapWidget(w);
	}

}

/* initgc:
 *	Initialises global gc values.
 *      We don't really need this any more. But
 *	GC's are good to have, I suppose.
 */
void initgc(){

	gc =  XCreateGC(display,mainwindow,0,NULL);
	cleargc = XCreateGC(display,mainwindow,0,NULL);

	XSetForeground(display,gc,black);
	XSetBackground(display,gc,white);
	XSetForeground(display,cleargc,white);
	XSetBackground(display,cleargc,black);

}

/* GetOptions
 *	get resource and command line options.
 *	(The nice thing is that thanks to Xt, commandline options
 *	 get auto-translated to resources already
 */

void GetOptions(){
	int tmpnumber;
	char tmpstr[100];


	/* set grade bits appropriately */
	GetXtrmString("gradelevel","Gradelevel",tmpstr);
	parsegrades(tmpstr);

	switchKanaEnglish = !GetXtrmBoolean("notallT","NotallT");
	/* default of nousefile is FALSE. which means
	 * useUsefile defaults to TRUE
	 */
	useUsefile = !GetXtrmBoolean("nousefile","Nousefile");

	doBell = !GetXtrmBoolean("noBell","NoBell");
	showinorder = GetXtrmBoolean("showinorder","Showinorder");

	lowfrequency = GetXtrmNumber("lowfrequency","Lowfrequency");
	if((lowfrequency <0) || (lowfrequency >=MAXTRANSLATIONSALLOWED))
		lowfrequency = 0;
	highfrequency = GetXtrmNumber("highfrequency","Highfrequency");
	if((highfrequency <0) || (highfrequency >=MAXTRANSLATIONSALLOWED))
		highfrequency = 0;

	tmpnumber = GetXtrmNumber("timersec","Timersec");
	setTimeoutLen(tmpnumber);
	

	GetXtrmString("guessmode", "Guessmode", tmpstr);
	if(strcmp(tmpstr, "english")==0){
		choicesmode = GUESS_ENGLISH;
	}else if(strcmp(tmpstr, "kanji")==0){
		choicesmode = GUESS_KANJI;
	} else{
		choicesmode = GUESS_KANA;
	}
	GetXtrmString("questionmode", "Questionmode", tmpstr);
	if(strcmp(tmpstr, "english")==0){
		questionmode=GUESS_ENGLISH;
	} else if (strcmp(tmpstr, "kanji")==0){
		questionmode=GUESS_KANJI;
	} else{
		questionmode=GUESS_KANA;
	}

	if( GetXtrmBoolean("romajiswitch","Romajiswitch"))
		romajiswitch = 1;

}


/* this routine is solely for internalizing various
 *  "Atoms" neccessary to handle the stupid WM_DELETE_WINDOW
 *  message
 */
void setup_deletewindow(Widget winwidget)
{

	/* Must wait for the window to actually appear. sigh.*/
	XFlush(display);
	XSync(display,False); /* This should be redundant, but... */

	if(wm_message==(Atom)NULL){
		wm_message=XInternAtom(display,"WM_PROTOCOLS",False);
		delete_message=XInternAtom(display,"WM_DELETE_WINDOW",False);
		if (wm_message == (Atom) NULL) {
			perror("unable to create wm_message property");
			exit(1);
		}
		if (delete_message == (Atom) NULL) {
			perror("unable to create delete property");
			exit(1);
		}
	}
	XtAddEventHandler(winwidget, NoEventMask, True,
			     handle_delete, (XtPointer)NULL);

	XtAugmentTranslations(winwidget, AllAccel);

	/* I haven't figured out why we would be called when this is 0.
	 * Seems to be a strange bug in X. sigh.
	 */ 
	if(XtWindow(winwidget)==0){
		/*printf("XtWindow is %d\n",XtWindow(winwidget));*/
		return;
	}

        XChangeProperty(display, XtWindow(winwidget),
			wm_message, XA_ATOM, 32, PropModeReplace, 
                        (unsigned char *) &delete_message, 1);


}

#ifndef _POSIX_PATH_MAX
#define _POSIX_PATH_MAX 1024
#endif

/* If we have the appropriate functions (X11R6?)
 * load up our own special user-specific xresources file
 * We need the global 'display' set
 */
void init_xdb()
{
	char XFileName[_POSIX_PATH_MAX];
	XrmDatabase globaldb;

	sprintf(XFileName,"%s/.kdrill", homedir);
	globaldb=XrmGetDatabase(display);

	/* override "global" prefs with user specific ones */
	/* That way, we try to keep working, even if prefs file
	 * is screwed up
	 */
	XrmCombineFileDatabase(XFileName, &globaldb, True);
}

/* initstuffs:
 *	calls the various init routines to setup
 *	GCs, fonts, dictionaries, and widgets
 *	(But initializing the translations is done later)
 */

static Pixmap iconpixmap;

void initstuffs(int *argc,char *argv[])
{
	srand48 (time(NULL));

	bzero(translations, sizeof(struct translationstruct *) * MAXTRANSLATIONSALLOWED);
	lowestkanji = highestkanji = 0;

	homedir = (char *) getenv("HOME");
	if(homedir == NULL){
		puts("WARNING: no 'HOME' environment variable");
		puts("Faking with '.' instead");
		homedir=".";
	}

	toplevel = XtVaAppInitialize(&Context,"KDrill",
 				   optionDescList,XtNumber(optionDescList),
 				   argc,argv,fallback,
 				   NULL,NULL);

	if(*argc >1){
		usage();
	}

	display = XtDisplay(toplevel);

	if(display == NULL) perror("NULL DISPLAY");

#ifndef NOXRMSAVE
	init_xdb();
#endif /*NOXRMSAVE*/


	/* Get command line options... yeah, we should do this in a
	 *  struct, in one go... but some things need to be initalized
	 * later
	 */

	white = XWhitePixel(display,0);
	black = XBlackPixel(display,0);

	GetOptions();

	MakeWidgets();

	XtRealizeWidget(toplevel);

	mainwindow = XtWindow(toplevel);
	if(mainwindow == 0) perror("NULL WINDOW");
	
	setup_deletewindow(toplevel);

	iconpixmap = XCreateBitmapFromData(display,mainwindow,
			   (char *)icon_xbm_bits,
			   icon_xbm_width,icon_xbm_height);

	XtVaSetValues(toplevel,XtNiconName,"kdrill",
			XtNiconPixmap,iconpixmap,NULL);

	/* Why doesnt this work? It should... ? */
	XtVaSetValues(search_popup,XtNiconName,"kdrill_search",
			XtNiconPixmap,iconpixmap,NULL);


	initgc();

}