File: xkeycaps.c

package info (click to toggle)
xkeycaps 2.42-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,504 kB
  • ctags: 2,272
  • sloc: ansic: 35,719; makefile: 523; sh: 125
file content (328 lines) | stat: -rw-r--r-- 9,617 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
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
/* xkeycaps, Copyright (c) 1991, 1992, 1993, 1995, 1996, 1997 
 * Jamie Zawinski <jwz@netscape.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#include "version.h"

#include <stdio.h>
#include <ctype.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xaw/Simple.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xmu/Error.h>

#include "KbdWidgetP.h"
#include "KeyWidgetP.h"

#include "xkeycaps.h"
#include "defaults.h"

char *progname;
char *short_version;

#ifdef __STDC__
# define P(x)x
#else /* !__STDC__ */
# define P(x)()
#endif

static KeyboardWidget make_keyboard P((Widget box, Widget box2,
				       struct info_labels *info,
				       const char *name));
static void maybe_relabel_window P((KeyboardWidget keyboard));
static KeyboardWidget xkeycaps_main_loop P((KeyboardWidget keyboard,
					    XtAppContext app));


Window XSendEvent_BadWindow = 0;

static int
#ifdef __STDC__
xkeycaps_error_handler (Display *dpy, XErrorEvent *error)
#else /* ! __STDC__ */
xkeycaps_error_handler (dpy, error)
     Display *dpy;
     XErrorEvent *error;
#endif /* ! __STDC__ */
{
  switch (error->request_code)
    {
    case X_GetKeyboardMapping:
      return 0;
    case X_SendEvent:
      if (error->error_code == BadWindow && !XSendEvent_BadWindow)
	XSendEvent_BadWindow = error->resourceid;
      else
	XmuPrintDefaultErrorMessage (dpy, error, stderr);
      return 0;
    default:
      XmuPrintDefaultErrorMessage (dpy, error, stderr);
      exit (-1);
    }
}


static KeyboardWidget
#ifdef __STDC__
make_keyboard (Widget box, Widget box2, struct info_labels *info,
	       const char *name)
#else /* ! __STDC__ */
make_keyboard (box, box2, info, name)
     Widget box, box2;
     struct info_labels *info;
     const char *name;
#endif /* ! __STDC__ */
{
  Arg av[20];
  int ac = 0;
  KeyboardWidget keyboard;
  XtSetArg (av[ac], XtNfromVert, box2); ac++;
  XtSetArg (av[ac], XtNleft, XtChainLeft); ac++;
  XtSetArg (av[ac], XtNright, XtChainRight); ac++;
  XtSetArg (av[ac], XtNtop, XtChainTop); ac++;
  XtSetArg (av[ac], XtNbottom, XtChainBottom); ac++;
  if (name) XtSetArg (av[ac], "keyboard", name), ac++;
  keyboard = (KeyboardWidget)
    XtCreateManagedWidget ("keyboard", keyboardWidgetClass, box, av, ac);
  keyboard->keyboard.label_widgets = info;
  keyboard->keyboard.key_menus = make_key_menus ((KeyboardWidget *) &keyboard);
  return keyboard;
}


static void
#ifdef __STDC__
maybe_relabel_window (KeyboardWidget keyboard)
#else /* ! __STDC__ */
maybe_relabel_window (keyboard)
     KeyboardWidget keyboard;
#endif /* ! __STDC__ */
{
  /* If the user hasn't specified the -title option, set the window title
     to be something more informative.
   */
  Widget toplevel = (Widget) keyboard;
  char buf1 [100], buf2 [100];
  XrmValue value;
  char *type;
  XrmDatabase db = XtDatabase (XtDisplay (keyboard));
  char *name, *class;
  while (XtParent (toplevel)) toplevel = XtParent (toplevel);
  XtGetApplicationNameAndClass (XtDisplay (keyboard), &name, &class);
  sprintf (buf1, "%s.title", name);
  sprintf (buf2, "%s.Title", class);
  if (XrmGetResource (db, buf1, buf2, &type, &value)) return;
  sprintf (buf1, "%s; %s%s%s keyboard", class,
	   keyboard->keyboard.vendor,
	   (keyboard->keyboard.kbd_style ? " " : ""),
	   keyboard->keyboard.kbd_style);
  XStoreName (XtDisplay (toplevel), XtWindow (toplevel), buf1);
}


/* Kludge to tell the main loop to reset some variables... */
KeyboardWidget new_kbd_selected = 0;

void
#ifdef __STDC__
replace_keyboard (KeyboardWidget keyboard, const char *name)
#else /* ! __STDC__ */
replace_keyboard (keyboard, name)
     KeyboardWidget keyboard;
     const char *name;
#endif /* ! __STDC__ */
{
  /* Doing this seems to leak about 8k each time, but I really don't care. */
  Widget box, box2;
  KeyboardWidget new_kbd;
  Widget toplevel;
  Arg av[20];
  int ac = 0;
  toplevel = box = XtParent (keyboard);
  while (XtParent (toplevel)) toplevel = XtParent (toplevel);
  box2 = 0;
  XtSetArg (av[ac], XtNfromVert, &box2); ac++;
  XtGetValues ((Widget) keyboard, av, ac);
  /*  if (! box2) abort ();*  #### wtf? */
  new_kbd = make_keyboard (box, box2, keyboard->keyboard.label_widgets, name);
  XtUnmanageChild ((Widget) keyboard);
  XtDestroyWidget ((Widget) keyboard);
  new_kbd_selected = new_kbd; /* kludge... */
  XtSetKeyboardFocus (toplevel, (Widget) new_kbd);
  maybe_relabel_window (new_kbd);
}


void
#ifdef __STDC__
xkeycaps_DispatchEvent_hook (KeyboardWidget keyboard, XEvent *event)
#else /* ! __STDC__ */
xkeycaps_DispatchEvent_hook (keyboard, event)
     KeyboardWidget keyboard;
     XEvent *event;
#endif /* ! __STDC__ */
{
  /* MappingNotify and KeymapNotify events don't have an associated
     window, so there's no way to register an event-handler function
     for one of these with Xt.  Lose, lose.
   */
  if (event->xany.type == KeymapNotify)
    keyboard_handle_keymap_notify ((Widget) keyboard, 0, event, 0);
  else if (event->xany.type == MappingNotify)
    keyboard_handle_mapping_notify ((Widget) keyboard, 0, event, 0);
}


static KeyboardWidget
#ifdef __STDC__
xkeycaps_main_loop (KeyboardWidget keyboard, XtAppContext app)
#else /* ! __STDC__ */
xkeycaps_main_loop (keyboard, app)
     KeyboardWidget keyboard;
     XtAppContext app;
#endif /* ! __STDC__ */
{
  XEvent event;
  while (1)
    {
      XtAppNextEvent (app, &event);
      xkeycaps_DispatchEvent_hook (keyboard, &event);
      XtDispatchEvent (&event);

      /* Kludge to prevent us from using a freed keyboard...
	 I think we had actually been allocating the second one out
	 of the *same space* as the first one and not losing?? */
      if (new_kbd_selected)
	return new_kbd_selected;
    }
}

void
#ifdef __STDC__
main (int argc, char **argv)
#else /* ! __STDC__ */
main (argc, argv)
     int argc;
     char **argv;
#endif /* ! __STDC__ */
{
  char *class = "XKeyCaps";
  XtAppContext app;
  Widget toplevel, box, box2, buttons;
  struct info_labels *info;
  KeyboardWidget keyboard = 0;
  Arg av [20];
  int ac = 0;
  char version_buf [255];

  {
    int index;
    int i2 = strlen (version) - 3;
    char *v2 = (char *) malloc (i2);
    memcpy (v2, version+4, i2);
    version = v2;
    for (index = 0;; index++)
      if (!strncmp ("Copyright (c)", version+index, 13)) break;
    for (v2 = version+index+13; *v2; v2++)
      if (*v2 == '(') *v2 = '<';
      else if (*v2 == ')') *v2 = '>';
    memcpy (version_buf, version, i2 = index);
    version_buf [index] = '\251';
    strcpy (version_buf + index + 1, version + index + 13);
    for (;; index++)
      if (isdigit(version[index]) && isdigit(version[index+1]) &&
	  isdigit(version[index+2]) && isdigit(version[index+3]) &&
	  version[index+4] != ',')
	break;
    short_version = (char *) malloc(i2 + 14 + strlen (version + index) + 1);
    memcpy (short_version, version, i2 + 14);
    strcpy (short_version +         i2 + 14, version + index);
  }

  toplevel = XtAppInitialize (&app, class, options, XtNumber (options),
			      &argc, argv, xkeycapsDefaults, NULL, 0);

  XtGetApplicationNameAndClass (XtDisplay (toplevel), &progname, &class);

  if (argc > 1)
    {
      int ok = (!strcmp (argv[1], "-help") ||
		!strcmp (argv[1], "--help") ||
		!strcmp (argv[1], "-version") ||
		!strcmp (argv[1], "--version") ||
		!strcmp (argv[1], "-v") ||
		!strcmp (argv[1], "-h"));
      FILE *out = (ok ? stdout : stderr);
      if (!ok)
	fprintf (out, "%s: unknown option %s\n", progname, argv [1]);

      fprintf (out, "\n%s\n", short_version);
      fprintf (out, "\n\
	Use the -keyboard (or -kbd) option to specify a keyboard,\n\
	or use the `Select Keyboard' dialog box.  Use the -help\n\
	option to list recognised arguments to -kbd.  See the man\n\
	page for more details.\n\n\
	For updates, check http://www.netscape.com/people/jwz/xkeycaps/\n\n");

      if (!ok)
	{
	  exit(-1);
	}
      else
	{
	  fprintf(out, "Recognised keyboard names:\n\n");
	  print_kbd_choices();
	  exit(0);
	}
    }

  create_icon_pixmaps (XtDisplay (toplevel));

  ac = 0;
  box = XtCreateManagedWidget ("vertical", panedWidgetClass, toplevel, av, ac);
  ac = 0;
  XtSetArg (av[ac], XtNorientation, "horizontal"); ac++;
  box2 = XtCreateManagedWidget ("horizontal", panedWidgetClass, box, av, ac);
  buttons = make_command_widgets (box2, (Widget *) &keyboard);
  info = make_info_widget (box2, buttons);

  keyboard = make_keyboard (box, box2, info, 0);

  XtAddEventHandler ((Widget) keyboard, KeymapStateMask, False,
		     keyboard_handle_keymap_notify, 0);
  XtAddEventHandler ((Widget) keyboard, 0, True,
		     keyboard_handle_mapping_notify, 0);
  XtRealizeWidget (toplevel);

  XtSetKeyboardFocus (toplevel, (Widget) keyboard);
  maybe_relabel_window (keyboard);
  XSetErrorHandler (xkeycaps_error_handler);

  message (keyboard, "");
  message2 (keyboard, version_buf);

  if (choose_kbd_dubious_p)
    pop_up_kbd_dbox (toplevel, (void *) &keyboard, 0);

#ifdef HAVE_XTRAP
  if (keyboard->keyboard.trap_data)
    xkeycaps_xtrap_main_loop (keyboard, app);
  else
#endif
    while (1)
      keyboard = xkeycaps_main_loop (keyboard, app);
}