xkbwatch.c revision 010cdda0
1010cdda0Smrg/* $Xorg: xkbwatch.c,v 1.4 2000/08/17 19:54:51 cpqbld Exp $ */
2010cdda0Smrg/************************************************************
3010cdda0Smrg Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.
4010cdda0Smrg
5010cdda0Smrg Permission to use, copy, modify, and distribute this
6010cdda0Smrg software and its documentation for any purpose and without
7010cdda0Smrg fee is hereby granted, provided that the above copyright
8010cdda0Smrg notice appear in all copies and that both that copyright
9010cdda0Smrg notice and this permission notice appear in supporting
10010cdda0Smrg documentation, and that the name of Silicon Graphics not be
11010cdda0Smrg used in advertising or publicity pertaining to distribution
12010cdda0Smrg of the software without specific prior written permission.
13010cdda0Smrg Silicon Graphics makes no representation about the suitability
14010cdda0Smrg of this software for any purpose. It is provided "as is"
15010cdda0Smrg without any express or implied warranty.
16010cdda0Smrg
17010cdda0Smrg SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
18010cdda0Smrg SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19010cdda0Smrg AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
20010cdda0Smrg GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
21010cdda0Smrg DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22010cdda0Smrg DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
23010cdda0Smrg OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
24010cdda0Smrg THE USE OR PERFORMANCE OF THIS SOFTWARE.
25010cdda0Smrg
26010cdda0Smrg ********************************************************/
27010cdda0Smrg/* $XFree86: xc/programs/xkbutils/xkbwatch.c,v 3.4 2001/01/17 23:46:14 dawes Exp $ */
28010cdda0Smrg
29010cdda0Smrg#include <stdlib.h>
30010cdda0Smrg#include <X11/X.h>
31010cdda0Smrg#include <X11/Xlib.h>
32010cdda0Smrg#include <X11/XKBlib.h>
33010cdda0Smrg#include <X11/Intrinsic.h>
34010cdda0Smrg#include <X11/StringDefs.h>
35010cdda0Smrg#include <X11/Shell.h>
36010cdda0Smrg#include <X11/Xaw/Cardinals.h>
37010cdda0Smrg#include <X11/Xaw/Box.h>
38010cdda0Smrg
39010cdda0Smrg#define	OPAQUE_DEFINED
40010cdda0Smrg#define	BOOLEAN_DEFINED
41010cdda0Smrg#define	DEBUG_VAR_NOT_LOCAL
42010cdda0Smrg#define	DEBUG_VAR debugFlags
43010cdda0Smrg#include "utils.h"
44010cdda0Smrg#include "LED.h"
45010cdda0Smrg
46010cdda0Smrg/***====================================================================***/
47010cdda0Smrg
48010cdda0Smrg	Display *	inDpy,*outDpy;
49010cdda0Smrg	int		evBase,errBase;
50010cdda0Smrg	Bool		synch;
51010cdda0Smrg
52010cdda0Smrg/***====================================================================***/
53010cdda0Smrg
54010cdda0Smrg
55010cdda0Smrgstatic XrmOptionDescRec options[] = {
56010cdda0Smrg{"-off",	"*on.on",		XrmoptionNoArg,		"FALSE"},
57010cdda0Smrg{"-on",		"*on.on",		XrmoptionNoArg,		"TRUE"}
58010cdda0Smrg};
59010cdda0Smrg
60010cdda0Smrg/***====================================================================***/
61010cdda0Smrg
62010cdda0Smrgint
63010cdda0Smrgmain(int argc, char *argv[])
64010cdda0Smrg{
65010cdda0SmrgWidget		toplevel;
66010cdda0SmrgXtAppContext	app_con;
67010cdda0SmrgWidget		panel;
68010cdda0SmrgWidget		base[XkbNumModifiers];
69010cdda0SmrgWidget		latched[XkbNumModifiers];
70010cdda0SmrgWidget		locked[XkbNumModifiers];
71010cdda0SmrgWidget		effective[XkbNumModifiers];
72010cdda0SmrgWidget		compat[XkbNumModifiers];
73010cdda0SmrgWidget		baseBox,latchBox,lockBox,effBox,compatBox;
74010cdda0Smrgregister int	i;
75010cdda0Smrgunsigned	bit;
76010cdda0SmrgXkbEvent	ev;
77010cdda0SmrgXkbStateRec	state;
78010cdda0Smrgstatic Arg	hArgs[]= {{ XtNorientation, (XtArgVal)XtorientHorizontal }};
79010cdda0Smrgstatic Arg	vArgs[]= {{ XtNorientation, (XtArgVal)XtorientVertical }};
80010cdda0Smrgstatic Arg	onArgs[]=  {{ XtNon, (XtArgVal)True }};
81010cdda0Smrgstatic Arg	offArgs[]=  {{ XtNon, (XtArgVal)False }};
82010cdda0Smrgstatic char *	fallback_resources[] = {
83010cdda0Smrg    "*Box*background: grey50",
84010cdda0Smrg    "*Box*borderWidth: 0",
85010cdda0Smrg    "*Box*vSpace: 1",
86010cdda0Smrg    NULL
87010cdda0Smrg};
88010cdda0Smrg
89010cdda0Smrg    uSetEntryFile(NullString);
90010cdda0Smrg    uSetDebugFile(NullString);
91010cdda0Smrg    uSetErrorFile(NullString);
92010cdda0Smrg    toplevel = XtOpenApplication(&app_con, "XkbWatch",
93010cdda0Smrg				 options, XtNumber(options), &argc, argv,
94010cdda0Smrg				 fallback_resources,
95010cdda0Smrg				 sessionShellWidgetClass, NULL, ZERO);
96010cdda0Smrg    if (toplevel==NULL) {
97010cdda0Smrg	uFatalError("Couldn't create application top level\n");
98010cdda0Smrg	exit(1);
99010cdda0Smrg    }
100010cdda0Smrg    inDpy= outDpy= XtDisplay(toplevel);
101010cdda0Smrg    if (inDpy) {
102010cdda0Smrg	int i1,mn,mj;
103010cdda0Smrg	mj= XkbMajorVersion;
104010cdda0Smrg	mn= XkbMinorVersion;
105010cdda0Smrg	if (!XkbQueryExtension(inDpy,&i1,&evBase,&errBase,&mj,&mn)) {
106010cdda0Smrg	    uFatalError("Server doesn't support a compatible XKB\n");
107010cdda0Smrg	    exit(1);
108010cdda0Smrg	}
109010cdda0Smrg    }
110010cdda0Smrg    panel= XtCreateManagedWidget("xkbwatch",boxWidgetClass,toplevel,vArgs,1);
111010cdda0Smrg    if (panel==NULL) {
112010cdda0Smrg	uFatalError("Couldn't create top level box\n");
113010cdda0Smrg	exit(1);
114010cdda0Smrg    }
115010cdda0Smrg    baseBox= XtCreateManagedWidget("base",boxWidgetClass,panel,hArgs,1);
116010cdda0Smrg    if (baseBox==NULL)
117010cdda0Smrg	uFatalError("Couldn't create base modifiers box\n");
118010cdda0Smrg    latchBox= XtCreateManagedWidget("latched",boxWidgetClass,panel,hArgs,1);
119010cdda0Smrg    if (latchBox==NULL)
120010cdda0Smrg	uFatalError("Couldn't create latched modifiers box\n");
121010cdda0Smrg    lockBox= XtCreateManagedWidget("locked",boxWidgetClass,panel,hArgs,1);
122010cdda0Smrg    if (lockBox==NULL)
123010cdda0Smrg	uFatalError("Couldn't create locked modifiers box\n");
124010cdda0Smrg    effBox= XtCreateManagedWidget("effective",boxWidgetClass,panel,hArgs,1);
125010cdda0Smrg    if (effBox==NULL)
126010cdda0Smrg	uFatalError("Couldn't create effective modifiers box\n");
127010cdda0Smrg    compatBox= XtCreateManagedWidget("compat",boxWidgetClass,panel,hArgs,1);
128010cdda0Smrg    if (compatBox==NULL)
129010cdda0Smrg	uFatalError("Couldn't create compatibility state box\n");
130010cdda0Smrg    XkbSelectEvents(inDpy,XkbUseCoreKbd,XkbStateNotifyMask,XkbStateNotifyMask);
131010cdda0Smrg    XkbGetState(inDpy,XkbUseCoreKbd,&state);
132010cdda0Smrg    for (i=XkbNumModifiers-1,bit=0x80;i>=0;i--,bit>>=1) {
133010cdda0Smrg	ArgList	list;
134010cdda0Smrg	char	buf[30];
135010cdda0Smrg	sprintf(buf,"base%d",i);
136010cdda0Smrg	if (state.base_mods&bit)	list= onArgs;
137010cdda0Smrg	else				list= offArgs;
138010cdda0Smrg	base[i]= XtCreateManagedWidget(buf,ledWidgetClass,baseBox,list,1);
139010cdda0Smrg	sprintf(buf,"latched%d",i);
140010cdda0Smrg	if (state.latched_mods&bit)	list= onArgs;
141010cdda0Smrg	else				list= offArgs;
142010cdda0Smrg	latched[i]= XtCreateManagedWidget(buf,ledWidgetClass,latchBox,list,1);
143010cdda0Smrg	sprintf(buf,"locked%d",i);
144010cdda0Smrg	if (state.locked_mods&bit)	list= onArgs;
145010cdda0Smrg	else				list= offArgs;
146010cdda0Smrg	locked[i]= XtCreateManagedWidget(buf,ledWidgetClass,lockBox,list,1);
147010cdda0Smrg	sprintf(buf,"effective%d",i);
148010cdda0Smrg	if (state.mods&bit)		list= onArgs;
149010cdda0Smrg	else				list= offArgs;
150010cdda0Smrg	effective[i]= XtCreateManagedWidget(buf,ledWidgetClass,effBox,list,1);
151010cdda0Smrg	sprintf(buf,"compat%d",i);
152010cdda0Smrg	if (state.compat_state&bit)	list= onArgs;
153010cdda0Smrg	else				list= offArgs;
154010cdda0Smrg	compat[i]= XtCreateManagedWidget(buf,ledWidgetClass,compatBox,list,1);
155010cdda0Smrg    }
156010cdda0Smrg    XtRealizeWidget(toplevel);
157010cdda0Smrg    while (1) {
158010cdda0Smrg        XtAppNextEvent(app_con,&ev.core);
159010cdda0Smrg	if (ev.core.type==evBase+XkbEventCode) {
160010cdda0Smrg	    if (ev.any.xkb_type==XkbStateNotify) {
161010cdda0Smrg		unsigned changed;
162010cdda0Smrg		if (ev.state.changed&XkbModifierBaseMask) {
163010cdda0Smrg		    changed= ev.state.base_mods^state.base_mods;
164010cdda0Smrg		    state.base_mods= ev.state.base_mods;
165010cdda0Smrg		    for (i=0,bit=1;i<XkbNumModifiers;i++,bit<<=1) {
166010cdda0Smrg			if (changed&bit) {
167010cdda0Smrg			    ArgList	list;
168010cdda0Smrg			    if (state.base_mods&bit)	list= onArgs;
169010cdda0Smrg			    else			list= offArgs;
170010cdda0Smrg			    XtSetValues(base[i],list,1);
171010cdda0Smrg			}
172010cdda0Smrg		    }
173010cdda0Smrg		}
174010cdda0Smrg		if (ev.state.changed&XkbModifierLatchMask) {
175010cdda0Smrg		    changed= ev.state.latched_mods^state.latched_mods;
176010cdda0Smrg		    state.latched_mods= ev.state.latched_mods;
177010cdda0Smrg		    for (i=0,bit=1;i<XkbNumModifiers;i++,bit<<=1) {
178010cdda0Smrg			if (changed&bit) {
179010cdda0Smrg			    ArgList	list;
180010cdda0Smrg			    if (state.latched_mods&bit)	list= onArgs;
181010cdda0Smrg			    else			list= offArgs;
182010cdda0Smrg			    XtSetValues(latched[i],list,1);
183010cdda0Smrg			}
184010cdda0Smrg		    }
185010cdda0Smrg		}
186010cdda0Smrg		if (ev.state.changed&XkbModifierLockMask) {
187010cdda0Smrg		    changed= ev.state.locked_mods^state.locked_mods;
188010cdda0Smrg		    state.locked_mods= ev.state.locked_mods;
189010cdda0Smrg		    for (i=0,bit=1;i<XkbNumModifiers;i++,bit<<=1) {
190010cdda0Smrg			if (changed&bit) {
191010cdda0Smrg			    ArgList	list;
192010cdda0Smrg			    if (state.locked_mods&bit)	list= onArgs;
193010cdda0Smrg			    else			list= offArgs;
194010cdda0Smrg			    XtSetValues(locked[i],list,1);
195010cdda0Smrg			}
196010cdda0Smrg		    }
197010cdda0Smrg		}
198010cdda0Smrg		if (ev.state.changed&XkbModifierStateMask) {
199010cdda0Smrg		    changed= ev.state.mods^state.mods;
200010cdda0Smrg		    state.mods= ev.state.mods;
201010cdda0Smrg		    for (i=0,bit=1;i<XkbNumModifiers;i++,bit<<=1) {
202010cdda0Smrg			if (changed&bit) {
203010cdda0Smrg			    ArgList	list;
204010cdda0Smrg			    if (state.mods&bit)	list= onArgs;
205010cdda0Smrg			    else		list= offArgs;
206010cdda0Smrg			    XtSetValues(effective[i],list,1);
207010cdda0Smrg			}
208010cdda0Smrg		    }
209010cdda0Smrg		}
210010cdda0Smrg		if (ev.state.changed&XkbCompatStateMask) {
211010cdda0Smrg		    changed= ev.state.compat_state^state.compat_state;
212010cdda0Smrg		    state.compat_state= ev.state.compat_state;
213010cdda0Smrg		    for (i=0,bit=1;i<XkbNumModifiers;i++,bit<<=1) {
214010cdda0Smrg			if (changed&bit) {
215010cdda0Smrg			    ArgList	list;
216010cdda0Smrg			    if (state.compat_state&bit)	list= onArgs;
217010cdda0Smrg			    else			list= offArgs;
218010cdda0Smrg			    XtSetValues(compat[i],list,1);
219010cdda0Smrg			}
220010cdda0Smrg		    }
221010cdda0Smrg		}
222010cdda0Smrg	    }
223010cdda0Smrg	}
224010cdda0Smrg	else XtDispatchEvent(&ev.core);
225010cdda0Smrg    }
226010cdda0Smrg/* BAIL: */
227010cdda0Smrg    if (inDpy)
228010cdda0Smrg	XCloseDisplay(inDpy);
229010cdda0Smrg    if (outDpy!=inDpy)
230010cdda0Smrg	XCloseDisplay(outDpy);
231010cdda0Smrg    inDpy= outDpy= NULL;
232010cdda0Smrg    return 0;
233010cdda0Smrg}
234