File: kinput2.c

package info (click to toggle)
kinput2 3.0-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,644 kB
  • ctags: 4,647
  • sloc: ansic: 45,508; makefile: 98; sh: 12
file content (614 lines) | stat: -rw-r--r-- 17,463 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
/*
 *	kinput2
 */

/*
 * Copyright (C) 1991 by Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 *		ishisone@sra.co.jp
 */

#ifndef lint
static char	*rcsid = "$Id: kinput2.c,v 1.43 1999/05/26 03:59:51 ishisone Exp $";
#endif

#include <stdio.h>
#include <signal.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include "patchlevel.h"
#include "AsyncErr.h"
#include "MyDispatch.h"
#include "IOECall.h"
#include "ConvMgr.h"
#include "KIProto.h"
#include "XimpProto.h"
#include "IMProto.h"

#if !defined(USE_WNN) && !defined(USE_CANNA) && !defined(USE_SJ3)
#define USE_WNN			/* default */
#endif

#ifdef USE_WNN
#include "CcWnn.h"
#include "WcharDisp.h"
#endif
#ifdef USE_CANNA
#include "Canna.h"
#include "WcharDisp.h"
#endif
#ifdef USE_SJ3
#include "Sj3.h"
#include "WcharDisp.h"
#endif

#include "DebugPrint.h"

int	debug_all;

/* application resource */
typedef struct {
    char *conversionEngine;
    int debugLevel;
    Boolean useKinputProtocol;
    Boolean useXimpProtocol;
    Boolean useXIMProtocol;
    Boolean appdefs_loaded;
} AppResRec, *AppResP;

static AppResRec appres;

static XtResource app_resources[] = {
    { "conversionEngine", "ConversionEngine", XtRString, sizeof(String),
      XtOffset(AppResP, conversionEngine), XtRString, (XtPointer)"" },
    { "debugLevel", "DebugLevel", XtRInt, sizeof(int),
      XtOffset(AppResP, debugLevel), XtRImmediate, (XtPointer)0 },
    { "useKinputProtocol", "UseKinputProtocol", XtRBoolean, sizeof(Boolean),
      XtOffset(AppResP, useKinputProtocol), XtRImmediate, (XtPointer)True },
    { "useXimpProtocol", "UseXimpProtocol", XtRBoolean, sizeof(Boolean),
      XtOffset(AppResP, useXimpProtocol), XtRImmediate, (XtPointer)True },
    { "useXIMProtocol", "UseXIMProtocol", XtRBoolean, sizeof(Boolean),
      XtOffset(AppResP, useXIMProtocol), XtRImmediate, (XtPointer)True },
    { "ki2AppDefsLoaded", "Ki2AppDefsLoaded", XtRBoolean, sizeof(Boolean),
      XtOffset(AppResP, appdefs_loaded), XtRImmediate, (XtPointer)True },
};

static String fallback_resources[] = {
    "*ki2AppDefsLoaded: false",
    "Kinput2.mappedWhenManaged: false",
    "Kinput2.width: 1",
    "Kinput2.height: 1",
    NULL,
};

static XrmOptionDescRec	options[] = {
    {"-bc",		"*KinputProtocol.backwardCompatible", XrmoptionNoArg,	"True"},
    {"-font",		"*JpWcharDisplay.font",	XrmoptionSepArg,	NULL},
    {"-kanjifont",	"*JpWcharDisplay.kanjiFont", XrmoptionSepArg,	NULL},
    {"-kanafont",	"*JpWcharDisplay.kanaFont", XrmoptionSepArg,	NULL},
    {"-kinput",		".useKinputProtocol",	XrmoptionNoArg,		"on"},
    {"+kinput",		".useKinputProtocol",	XrmoptionNoArg,		"off"},
    {"-ximp",		".useXimpProtocol",	XrmoptionNoArg,		"on"},
    {"+ximp",		".useXimpProtocol",	XrmoptionNoArg,		"off"},
    {"-xim",		".useXIMProtocol",	XrmoptionNoArg,		"on"},
    {"+xim",		".useXIMProtocol",	XrmoptionNoArg,		"off"},
    {"-tbheight",	"*ConversionControl.titlebarHeight",
						XrmoptionSepArg,	NULL},
#ifdef DEBUG
    {"-debug",		".debugLevel",		XrmoptionNoArg,		"1"},
    {"-trace",		".debugLevel",		XrmoptionNoArg,		"10"},
    {"-debuglevel",	".debugLevel",		XrmoptionSepArg,	NULL},
#endif
#ifdef USE_WNN
    {"-wnn",		".conversionEngine",	XrmoptionNoArg,		"wnn"},
    {"-jserver",	"*CcWnn.jserver",	XrmoptionSepArg,	NULL},
    {"-ccdef",		"*CcWnn.ccdef",		XrmoptionSepArg,	NULL},
    {"-wnnenvname",	"*CcWnn.wnnEnvname",	XrmoptionSepArg,	NULL},
    {"-wnnenvrc4",	"*CcWnn.wnnEnvrc4",	XrmoptionSepArg,	NULL},
    {"-wnnenvrc6",	"*CcWnn.wnnEnvrc6",	XrmoptionSepArg,	NULL},
    {"-wnnenvrc",	"*CcWnn.wnnEnvrc",	XrmoptionSepArg,	NULL},
#endif
#ifdef USE_CANNA
    {"-canna",		".conversionEngine",	XrmoptionNoArg,		"canna"},
    {"-cannaserver",	"*Canna.cannahost",	XrmoptionSepArg,	NULL},
    {"-cs",		"*Canna.cannahost",	XrmoptionSepArg,	NULL},
    {"-cannafile",	"*Canna.cannafile",	XrmoptionSepArg,	NULL},
#endif
#ifdef USE_SJ3
    {"-sj3",        ".conversionEngine",    XrmoptionNoArg,     "sj3"},
    {"-sj3serv",    "*Sj3.sj3serv",     XrmoptionSepArg,    NULL},
    {"-sj3serv2",   "*Sj3.sj3serv2",    XrmoptionSepArg,    NULL},
    {"-sj3user",    "*Sj3.sj3user",     XrmoptionSepArg,    NULL},
    {"-rcfile",     "*Sj3.rcfile",      XrmoptionSepArg,    NULL},
    {"-sbfile",     "*Sj3.sbfile",      XrmoptionSepArg,    NULL},
    {"-rkfile",     "*Sj3.rkfile",      XrmoptionSepArg,    NULL},
    {"-hkfile",     "*Sj3.hkfile",  XrmoptionSepArg,    NULL},
    {"-zhfile",     "*Sj3.zhfile",  XrmoptionSepArg,    NULL},
    {"-sjrc",       "*Sj3.rcfile",      XrmoptionSepArg,    NULL},
    {"-sjsb",       "*Sj3.sbfile",      XrmoptionSepArg,    NULL},
    {"-sjrk",       "*Sj3.rkfile",      XrmoptionSepArg,    NULL},
    {"-sjhk",       "*Sj3.hkfile",      XrmoptionSepArg,    NULL},
    {"-sjzh",       "*Sj3.zhfile",      XrmoptionSepArg,    NULL},
#endif
};

XtAppContext	apc;
Widget		toplevel;

static int	numProtocols;
static int	(*DefaultErrorHandler)();

#if XtSpecificationRelease > 5
static XtSignalId	interrupt;
static void	interruptCallback();
#else
static void	exitTimer();
#endif

static WidgetClass	getInputObjClass();
static int	IgnoreBadWindow();
#ifdef SIGNALRETURNSINT
static int	scheduleExit();
#else
static void	scheduleExit();
#endif
static void	Destroyed();
static void	Exit();
static void	realExit();
static void	usage();
static void	print_version();

void
main(ac, av)
int ac;
char **av;
{
    Widget manager, protocol;
    int i;
    WidgetClass inputobjclass, displayobjclass;
    Widget inputobj;

    toplevel = XtAppInitialize(&apc, "Kinput2",
			       options, XtNumber(options),
			       &ac, av,
			       fallback_resources, (ArgList)NULL, 0);

    /* check invalid argument */ 
    if (ac > 1) {
	int do_usage = 0;
	for (i = 1; i < ac; i++) {
	    if (!strcmp(av[i], "-version")) {
		print_version();
	    } else {
		fprintf(stderr, "unknown argument: %s\n", av[i]);
		do_usage = 1;
	    }
	}
	if (do_usage) usage();
    }

    /* initialize asynchronous error handler */
    XAEInit();

    /* initialize I/O error callback handler */
    XIOEInit();

    XtGetApplicationResources(toplevel, &appres,
			      app_resources, XtNumber(app_resources),
			      NULL, 0);

    /*
     * If the application-specific class resource file
     * (the "app-defaults" file) is not found,
     * print a warning message.
     */
    if (!appres.appdefs_loaded) {
	fprintf(stderr, "Warning: Cannot load app-defaults file.\n");
	fprintf(stderr, "  Kinput2 may not work properly without it.\n");
	fprintf(stderr, "  Maybe kinput2 is not installed correctly,\n");
	fprintf(stderr, "  or your file search path (specified by\n");
	fprintf(stderr, "  environment variable 'XFILESEARCHPATH')\n");
	fprintf(stderr, "  is wrong.\n");
    }

    /* set debug level */
    debug_all = appres.debugLevel;

#ifdef RANDOM_ID
    /*
     * one nasty hack here:
     *
     * kinput clients often use server's window ID for the only key
     * value to identify their conversion server (kinput), and they
     * think it is dead and take appropriate action (eg connecting to
     * the new server) when they notice the ID has changed.
     *
     * but it is likely that another kinput has the same resource ID
     * base (because X servers always choose the smallest unused ID
     * base for new clients). and if it is the same, so is the owner
     * window ID, and the clients don't notice the change.
     *
     * to get rid of the problem, we add some small random offset to
     * the resource ID so that every time we get different owner ID
     * even if the resource ID base is the same.
     *
     * of course it heavily depends on the current implementaion of
     * the resource ID allocation in Xlib, so I call it 'nasty'.
     */
    XtDisplay(toplevel)->resource_id += getpid() % 1024;
#endif

    inputobjclass = getInputObjClass();

    inputobj = XtCreateWidget("inputObj", inputobjclass,
				toplevel, 0, 0);
    XtRealizeWidget(inputobj);
    ICRegisterTriggerKeys(inputobj);
    /*
       Destroying the `inputobj' is postponed until all the widgets
       are realized in order to prevent duplicate initialization of
       input object, that is, to prevent making connection twice to
       input conversion server.
     */

    displayobjclass = jpWcharDisplayObjectClass;

    manager = XtVaCreateManagedWidget("convmanager",
				      conversionManagerWidgetClass,
				      toplevel,
				      XtNwidth, 1,
				      XtNheight, 1,
				      NULL);

    numProtocols = 0;

    if (appres.useKinputProtocol) {
	protocol = XtVaCreateWidget("kinputprotocol",
				    kinputProtocolWidgetClass,
				    manager,
				    XtNlanguage, "JAPANESE",
				    XtNinputObjectClass, inputobjclass,
				    XtNdisplayObjectClass, displayobjclass,
				    XtNwidth, 1,
				    XtNheight, 1,
				    NULL);
	XtAddCallback(protocol, XtNdestroyCallback,
		      Destroyed, (XtPointer)NULL);
	numProtocols++;
    }

    if (appres.useXimpProtocol) {
	protocol = XtVaCreateWidget("ximpprotocol",
				    ximpProtocolWidgetClass,
				    manager,
				    XtNlocaleName, "ja_JP",
				    XtNinputObjectClass, inputobjclass,
				    XtNdisplayObjectClass, displayobjclass,
				    XtNwidth, 1,
				    XtNheight, 1,
				    NULL);
	XtAddCallback(protocol, XtNdestroyCallback,
		      Destroyed, (XtPointer)NULL);
	numProtocols++;
    }

    if (appres.useXIMProtocol) {
	protocol = XtVaCreateWidget("improtocol",
				    imProtocolWidgetClass,
				    manager,
				    XtNlanguage, "ja_JP",
				    XtNinputObjectClass, inputobjclass,
				    XtNdisplayObjectClass, displayobjclass,
				    XtNwidth, 1,
				    XtNheight, 1,
				    NULL);
	XtAddCallback(protocol, XtNdestroyCallback,
		      Destroyed, (XtPointer)NULL);
	numProtocols++;
    }

    if (numProtocols == 0) {
	fprintf(stderr, "no protocols activated\n");
	exit(1);
    }

    /* set signal handler */
    if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, scheduleExit);
#if XtSpecificationRelease > 5
    interrupt = XtAppAddSignal(apc, interruptCallback, (XtPointer)NULL);
#endif
    signal(SIGTERM, scheduleExit);
#ifdef USE_WNN
    signal(SIGPIPE, SIG_IGN);
#endif

    /* set my error handler */
    DefaultErrorHandler = XAESetErrorHandler(IgnoreBadWindow);

    XtRealizeWidget(toplevel);

    XtDestroyWidget(inputobj); /* Don't move this before XtRealizeWidget() */

    for (;;) {
	XEvent event;

	XtAppNextEvent(apc, &event);
	XtDispatchEvent(&event);
	MyDispatchEvent(&event); /* additional dispatcher */
    }
    /* NOTREACHED */
}

static WidgetClass
getInputObjClass()
{
    WidgetClass class;

#ifdef USE_WNN
    if (!strcmp(appres.conversionEngine, "wnn")) {
	return ccWnnObjectClass;
    }
#endif
#ifdef USE_CANNA
    if (!strcmp(appres.conversionEngine, "canna") ||
	!strcmp(appres.conversionEngine, "iroha")) {
	return cannaObjectClass;
    }
#endif
#ifdef USE_SJ3
    if (!strcmp(appres.conversionEngine, "sj3")) {
	return sj3ObjectClass;
    }
#endif

    /* set default input object */
#ifdef USE_SJ3
    class = sj3ObjectClass;
#endif
#ifdef USE_CANNA
    class = cannaObjectClass;
#endif
#ifdef USE_WNN
    class = ccWnnObjectClass;
#endif

    return class;
}

static int
IgnoreBadWindow(dpy, error)
Display *dpy;
XErrorEvent *error;
{
    /*
     * BadWindow events will be sent if any of the active clients die
     * during conversion.  Although I select DestroyNotify event on the
     * client windows to detect their destruction and take appropriate
     * actions, and I'm careful when doing critical operations, but still
     * there is a chance of getting unexpecting BadWindow error caused by
     * client death.
     * There are also chances of getting BadDrawable as well.
     * So I set the error handler to ignore BadWindow/BadDrawable errors.
     * Of course I'd better check if the resourceid field of the error
     * event is the window ID of a client, but I'm too lazy to do that...
     * P.S.  We should ignore BadColor also..
     */
    if (error->error_code != BadWindow &&
	error->error_code != BadDrawable &&
	error->error_code != BadColor) {
	/* invoke default error handler */
	(*DefaultErrorHandler)(dpy, error);
    }
    return 0;
}

#ifdef SIGNALRETURNSINT
static int
#else
static void
#endif
scheduleExit()
{
#if XtSpecificationRelease > 5
    XtNoticeSignal(interrupt);
#else
    /*
     * It is unwise to do complex operation (in this case,
     * XtDestroyWidget) in a signal handler.
     * So postpone the real work...
     */
    XtAppAddTimeOut(apc, 1L, exitTimer, (XtPointer)NULL);
#endif
}

#if XtSpecificationRelease > 5
/* ARGSUSED */
static void
interruptCallback(cldata, sigid)
XtPointer cldata;
XtSignalId *sigid;
{
    Exit();
}
#else
/* ARGSUSED */
static void
exitTimer(cldata, timerp)
XtPointer cldata;
XtIntervalId *timerp;
{
    Exit();
}
#endif

/* ARGSUSED */
static void
Destroyed(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    static int cnt = 0;

    /*
     * if all the protocol widgets are dead, kill myself.
     */
    if (++cnt >= numProtocols) Exit();
}

static void
Exit()
{
    static int exiting = 0;

    TRACE(("Exit()\n"));

    if (exiting) return;
    exiting = 1;

    /*
     * Destroy all widgets.
     */
    XtDestroyWidget(toplevel);

    /*
     * Postpone calling exit() until next XtNextEvent(),
     * in order to give each widget time to execute their
     * destroy procedure.
     */
    XtAppAddTimeOut(apc, 1L, realExit, (XtPointer)NULL);
}

/* ARGSUSED */
static void
realExit(cldata, timerp)
XtPointer cldata;
XtIntervalId *timerp;
{
    exit(0);
}

static void
usage()
{
    char **p;
    static char *syntaxtable[] = {
#ifdef USE_WNN
	"-wnn",			"use Wnn as the conversion engine",
	"-jserver <hostname>",	"specify jserver host",
	"-ccdef <ccdeffile>",	"specify character conversion def. file",
	"-wnnenvname <name>",	"specify Wnn environment name",
	"-wnnenvrc4 <wnnenvrcfile>", "specify Wnn environment file for Wnn4",
	"-wnnenvrc6 <wnnenvrcfile>", "specify Wnn environment file for Wnn6",
	"-wnnenvrc <wnnenvrcfile>", "specify Wnn environment file",
#endif
#ifdef USE_CANNA
	"-canna",		"use Canna (Iroha) as the conversion engine",
	"{-cannaserver|-cs} <hostname>[:n]", "specify cannaserver host",
	"-cannafile <cannafile>", "specify canna customize file",
#endif
#ifdef USE_SJ3
	"-sj3",         	"use SJ3 as the conversion engine",
	"-sj3serv <hostname>",  "specify first sj3serv host",
	"-sj3serv2 <hostname>", "specify second sj3serv host",
	"-sj3user <user>",      "specify user name connect to sj3serv",
	"{-rcfile|-sjrc} <file>",       "specify resource definition file",
	"{-sbfile|-sjsb} <file>",       "specify symbol table file",
	"{-rkfile|-sjrk} <file>",       "specify roma-kana coversion definition file",
	"{-hkfile|-sjhk} <file>",       "specify hira-kata coversion definition file",
	"{-zhfile|-sjzh} <file>",       "specify zen/han coversion definition file",
#endif
	"-bc",			"backward compatible mode",
	"-font <font>",		"ASCII font to be used",
	"-kanjifont <font>",	"KANJI font to be used",
	"-kanafont <font>",	"KANA font to be used",
	"-/+kinput",		"activate/deactivate kinput protocol family",
	"-/+ximp",		"activate/deactivate Ximp protocol",
	"-/+xim",		"activate/deactivate X Input Method protocol",
	"-tbheight <number>",	"specify pop-up shell's titlebar height",
	"-background <color>",	"background color",
	"-foreground <color>",	"foreground color",
	"-rv",			"reverse video mode",
	"-display <display>",	"specify display",
	"-version",		"print version information and exit",
#ifdef DEBUG
	"-debug",		"print debug messages (debug level 1)",
	"-trace",		"print trace messages (debug level 10)",
	"-debuglevel <level>",	"set debug level",
#endif
	NULL, NULL,
    };

    fprintf(stderr, "options are:\n");
    for (p = syntaxtable; *p != NULL; p += 2) {
	fprintf(stderr, "    %-30s %s\n", *p, *(p + 1));
    }
    exit(1);
}

static void
print_version()
{
    char *p;

    printf("kinput2 %s ", KINPUT2_VERSION);
    if (PATCHLEVEL > 0) printf("fix %d ", PATCHLEVEL);
#ifdef STATUS
    printf("-%s- ", STATUS);
#endif
    printf(" (");
    p = DATE + 7;				/* skip '$Date: ' */
    while (*p != '\0' && *p != ' ') {
	putchar(*p);	/* print date */
	p++;
    }
    printf(")\n");

    printf("\toptions: ");
#ifdef USE_WNN
#ifdef USE_WNN6
    printf("[Wnn6] ");
#else
    printf("[Wnn] ");
#endif
#endif
#ifdef USE_CANNA
    printf("[Canna2] ");
#endif
#ifdef USE_SJ3
    printf("[Sj3] ");
#endif
#ifdef DEBUG
    printf("[DEBUG] ");
#endif
    printf("\n");
    exit(0);
}

#if defined(USE_WNN) && defined(NEED_Strlen)
/*
 * Wnn/jlib/js.c should have this function...
 */
int
Strlen(s)
unsigned short *s;
{
    int n = 0;

    while (*s++) n++;
    return n;
}
#endif