1 /*
2  **LIBS: -lXm -lXt -lX11
3  ** keysym handling
4  */
5 
6 #include <X11/Xatom.h>
7 #include <X11/Intrinsic.h>
8 #include <X11/Shell.h>
9 
10 #include <Xm/Xm.h>
11 #include <Xm/BulletinB.h>
12 #include <Xm/DialogS.h>
13 #include <Xm/Label.h>
14 #include <stdio.h>
15 
16 Widget shell = (Widget) NULL;
17 
18 void
keys(widget,event,params,num_params)19 keys(widget, event, params, num_params)
20 	Widget          widget;
21 	XEvent         *event;
22 	String         *params;
23 	Cardinal       *num_params;
24 {
25 	char            strbuf[128];
26 	int             num_bytes;
27 	KeySym          ks;
28 	XComposeStatus  stat;
29 	Modifiers       mods;
30 
31 	printf("Keycode 0x%x modifiers 0x%x\n", event->xkey.keycode,
32 	       event->xkey.state);
33 	num_bytes = XLookupString(&(event->xkey), strbuf, 127, &ks, &stat);
34 	printf("X keysym 0x%x (%s)\n", (unsigned int)ks, XKeysymToString(ks));
35 	strbuf[num_bytes] = '\0';
36 	printf("X keystring %s\n", strbuf);
37 	XtTranslateKeycode(XtDisplay(widget), event->xkey.keycode,
38 			   event->xkey.state, &mods, &ks);
39 	printf("Xt keysym 0x%x (%s)\n", (unsigned int)ks, XKeysymToString(ks));
40 	printf("\n");
41 }
42 
43 static XtActionsRec keys_actions[] = {
44 	{"keys", keys}
45 };
46 
47 void
reg_keys(w)48 reg_keys(w)
49 	Widget          w;
50 {
51 	XtAppAddActions(XtWidgetToApplicationContext(w), keys_actions,
52 			XtNumber(keys_actions));
53 }
54 
55 void
create_shell(display,app_name,app_argc,app_argv)56 create_shell(display, app_name, app_argc, app_argv)
57 	Display        *display;
58 	char           *app_name;
59 	int             app_argc;
60 	char          **app_argv;
61 {
62 	Arg             al[64];	/* Arg List */
63 	register int    ac = 0;	/* Arg Count */
64 	XmString        xmstring;
65 	Widget          bullboard = (Widget) NULL;
66 	Widget          label = (Widget) NULL;
67 
68 	XtSetArg(al[ac], XmNallowShellResize, TRUE); ac++;
69 	XtSetArg(al[ac], XmNargc, app_argc); ac++;
70 	XtSetArg(al[ac], XmNargv, app_argv); ac++;
71 	shell = XtAppCreateShell(app_name, "XApplication", applicationShellWidgetClass, display, al, ac);
72 	ac = 0;
73 	XtSetArg(al[ac], XmNautoUnmanage, FALSE); ac++;
74 	bullboard = XmCreateBulletinBoard(shell, "bullboard", al, ac);
75 	ac = 0;
76 	xmstring = XmStringCreateLtoR("Key entry", (XmStringCharSet) XmFONTLIST_DEFAULT_TAG);
77 	XtSetArg(al[ac], XmNlabelString, xmstring); ac++;
78 	label = XmCreateLabel(bullboard, "label", al, ac);
79 	ac = 0;
80 	XmStringFree(xmstring);
81 	/*
82 	 * Register action proc for all keystrokes and set up translations on
83 	 * XmLabel to invoke it.
84 	 */
85 	reg_keys(shell);
86 	XtUninstallTranslations(label);
87 	XtOverrideTranslations(label, XtParseTranslationTable("<Key>: keys()"));
88 	XtManageChild(label);
89 	XtManageChild(bullboard);
90 }
91 
92 XtAppContext    app_context;
93 Display        *display;	/* Display             */
94 
95 int
main(argc,argv)96 main(argc, argv)
97 	int             argc;
98 	char          **argv;
99 {
100 	XtSetLanguageProc((XtAppContext) NULL, (XtLanguageProc) NULL, (XtPointer) NULL);
101 	XtToolkitInitialize();
102 	app_context = XtCreateApplicationContext();
103 	display = XtOpenDisplay(app_context, NULL, argv[0], "XApplication",
104 				NULL, 0, &argc, argv);
105 	if (!display) {
106 		printf("%s: can't open display, exiting...\n", argv[0]);
107 		exit(-1);
108 	}
109 	create_shell(display, argv[0], argc, argv);
110 	XtRealizeWidget(shell);
111 
112 {
113     static XtWidgetGeometry Expected[] = {
114    CWWidth | CWHeight            ,   50,   50,   79,   38, 0,0,0, /* bullboard */
115    CWWidth | CWHeight | CWX | CWY,   10,   10,   58,   17, 0,0,0, /* label */
116     };
117     PrintDetails(shell,Expected);
118 };
119 	LessTifTestMainLoop(shell);
120 	exit(0);
121 }
122