LED.c revision 010cdda0
1010cdda0Smrg/* $Xorg: LED.c,v 1.3 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/LED.c,v 1.4 2001/01/17 23:46:13 dawes Exp $ */
28010cdda0Smrg
29010cdda0Smrg#include <X11/IntrinsicP.h>
30010cdda0Smrg#include <X11/StringDefs.h>
31010cdda0Smrg#include <X11/Xos.h>
32010cdda0Smrg#include <X11/Xaw/XawInit.h>
33010cdda0Smrg#include "LEDP.h"
34010cdda0Smrg#include <X11/Xmu/Converters.h>
35010cdda0Smrg#include <X11/Xmu/Drawing.h>
36010cdda0Smrg#include <stdio.h>
37010cdda0Smrg#include <ctype.h>
38010cdda0Smrg/* needed for abs() */
39010cdda0Smrg#include <stdlib.h>
40010cdda0Smrg
41010cdda0Smrg#define streq(a,b) (strcmp( (a), (b) ) == 0)
42010cdda0Smrg
43010cdda0Smrg#ifdef CRAY
44010cdda0Smrg#define WORD64
45010cdda0Smrg#endif
46010cdda0Smrg
47010cdda0Smrg/****************************************************************
48010cdda0Smrg *
49010cdda0Smrg * Full class record constant
50010cdda0Smrg *
51010cdda0Smrg ****************************************************************/
52010cdda0Smrg
53010cdda0Smrg/* Private Data */
54010cdda0Smrg
55010cdda0Smrg#define offset(field) XtOffsetOf(LEDRec, field)
56010cdda0Smrgstatic XtResource resources[] = {
57010cdda0Smrg    {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
58010cdda0Smrg	offset(led.foreground), XtRString, XtDefaultForeground},
59010cdda0Smrg    {XtNon, XtCOn, XtRBoolean, sizeof(Boolean),
60010cdda0Smrg	offset(led.on), XtRImmediate, (XtPointer)False},
61010cdda0Smrg    {XtNtopColor, XtCTopColor, XtRPixel, sizeof(Pixel),
62010cdda0Smrg	offset(led.top_color), XtRString, "black"},
63010cdda0Smrg    {XtNbottomColor, XtCBottomColor, XtRPixel, sizeof(Pixel),
64010cdda0Smrg	offset(led.bottom_color), XtRString, "white"},
65010cdda0Smrg    {XtNonColor, XtCOnColor, XtRPixel, sizeof(Pixel),
66010cdda0Smrg	offset(led.on_color), XtRString, "green"},
67010cdda0Smrg    {XtNoffColor, XtCOffColor, XtRPixel, sizeof(Pixel),
68010cdda0Smrg	offset(led.off_color), XtRString, "#005000"},
69010cdda0Smrg    {XtNbevel, XtCBevel, XtRDimension, sizeof(Dimension),
70010cdda0Smrg	offset(led.bevel), XtRImmediate, (XtPointer)1},
71010cdda0Smrg    {XtNledWidth, XtCLedWidth, XtRDimension, sizeof(Dimension),
72010cdda0Smrg	offset(led.led_width), XtRImmediate, (XtPointer)6},
73010cdda0Smrg    {XtNledHeight, XtCLedHeight, XtRDimension, sizeof(Dimension),
74010cdda0Smrg	offset(led.led_height), XtRImmediate, (XtPointer)12}
75010cdda0Smrg};
76010cdda0Smrg#undef offset
77010cdda0Smrg
78010cdda0Smrgstatic void ClassInitialize ( void );
79010cdda0Smrgstatic void Initialize ( Widget request, Widget new, ArgList args,
80010cdda0Smrg			 Cardinal *num_args );
81010cdda0Smrgstatic void Realize ( Widget w, Mask * mask, XSetWindowAttributes * xswa );
82010cdda0Smrgstatic void Resize ( Widget w );
83010cdda0Smrgstatic Boolean SetValues ( Widget current, Widget request, Widget new,
84010cdda0Smrg			   ArgList args, Cardinal *num_args );
85010cdda0Smrgstatic void Destroy ( Widget w );
86010cdda0Smrgstatic XtGeometryResult QueryGeometry ( Widget w, XtWidgetGeometry *intended,
87010cdda0Smrg					XtWidgetGeometry *preferred );
88010cdda0Smrg
89010cdda0SmrgLEDClassRec ledClassRec = {
90010cdda0Smrg  {
91010cdda0Smrg/* core_class fields */
92010cdda0Smrg    /* superclass	  	*/	(WidgetClass) &simpleClassRec,
93010cdda0Smrg    /* class_name	  	*/	"LED",
94010cdda0Smrg    /* widget_size	  	*/	sizeof(LEDRec),
95010cdda0Smrg    /* class_initialize   	*/	ClassInitialize,
96010cdda0Smrg    /* class_part_initialize	*/	NULL,
97010cdda0Smrg    /* class_inited       	*/	FALSE,
98010cdda0Smrg    /* initialize	  	*/	Initialize,
99010cdda0Smrg    /* initialize_hook		*/	NULL,
100010cdda0Smrg    /* realize		  	*/	Realize,
101010cdda0Smrg    /* actions		  	*/	NULL,
102010cdda0Smrg    /* num_actions	  	*/	0,
103010cdda0Smrg    /* resources	  	*/	resources,
104010cdda0Smrg    /* num_resources	  	*/	XtNumber(resources),
105010cdda0Smrg    /* xrm_class	  	*/	NULLQUARK,
106010cdda0Smrg    /* compress_motion	  	*/	TRUE,
107010cdda0Smrg    /* compress_exposure  	*/	TRUE,
108010cdda0Smrg    /* compress_enterleave	*/	TRUE,
109010cdda0Smrg    /* visible_interest	  	*/	FALSE,
110010cdda0Smrg    /* destroy		  	*/	Destroy,
111010cdda0Smrg    /* resize		  	*/	Resize,
112010cdda0Smrg    /* expose		  	*/	XtInheritExpose,
113010cdda0Smrg    /* set_values	  	*/	SetValues,
114010cdda0Smrg    /* set_values_hook		*/	NULL,
115010cdda0Smrg    /* set_values_almost	*/	XtInheritSetValuesAlmost,
116010cdda0Smrg    /* get_values_hook		*/	NULL,
117010cdda0Smrg    /* accept_focus	 	*/	NULL,
118010cdda0Smrg    /* version			*/	XtVersion,
119010cdda0Smrg    /* callback_private   	*/	NULL,
120010cdda0Smrg    /* tm_table		   	*/	NULL,
121010cdda0Smrg    /* query_geometry		*/	QueryGeometry,
122010cdda0Smrg    /* display_accelerator	*/	XtInheritDisplayAccelerator,
123010cdda0Smrg    /* extension		*/	NULL
124010cdda0Smrg  },
125010cdda0Smrg/* Simple class fields initialization */
126010cdda0Smrg  {
127010cdda0Smrg    /* change_sensitive		*/	XtInheritChangeSensitive
128010cdda0Smrg  },
129010cdda0Smrg/* LED class fields initialization */
130010cdda0Smrg  {
131010cdda0Smrg    /* ignore 			*/	0
132010cdda0Smrg  }
133010cdda0Smrg};
134010cdda0SmrgWidgetClass ledWidgetClass = (WidgetClass)&ledClassRec;
135010cdda0Smrg/****************************************************************
136010cdda0Smrg *
137010cdda0Smrg * Private Procedures
138010cdda0Smrg *
139010cdda0Smrg ****************************************************************/
140010cdda0Smrg
141010cdda0Smrgstatic void
142010cdda0SmrgClassInitialize(void)
143010cdda0Smrg{
144010cdda0Smrg    XawInitializeWidgetSet();
145010cdda0Smrg}
146010cdda0Smrg
147010cdda0Smrgstatic void
148010cdda0SmrgGetPixmaps(LEDWidget lw)
149010cdda0Smrg{
150010cdda0Smrg    XGCValues	values;
151010cdda0Smrg    GC		gc;
152010cdda0Smrg    Display *	dpy;
153010cdda0Smrg    Window	root;
154010cdda0Smrg    Pixmap	pix,on_pixmap,off_pixmap;
155010cdda0Smrg    Dimension	bevel,width,height;
156010cdda0Smrg
157010cdda0Smrg    dpy= XtDisplay((Widget)lw);
158010cdda0Smrg    root= RootWindowOfScreen(XtScreen((Widget)lw));
159010cdda0Smrg    if (lw->led.on_pixmap!=None) {
160010cdda0Smrg	XFreePixmap(dpy,lw->led.on_pixmap);
161010cdda0Smrg	lw->led.on_pixmap= None;
162010cdda0Smrg    }
163010cdda0Smrg    if (lw->led.off_pixmap!=None) {
164010cdda0Smrg	XFreePixmap(dpy,lw->led.off_pixmap);
165010cdda0Smrg	lw->led.off_pixmap= None;
166010cdda0Smrg    }
167010cdda0Smrg    lw->led.on_pixmap= on_pixmap= XCreatePixmap(dpy,root,
168010cdda0Smrg				 lw->core.width,lw->core.height,lw->core.depth);
169010cdda0Smrg    lw->led.off_pixmap= off_pixmap= XCreatePixmap(dpy,root,
170010cdda0Smrg				 lw->core.width,lw->core.height,lw->core.depth);
171010cdda0Smrg
172010cdda0Smrg    values.foreground	= lw->led.top_color;
173010cdda0Smrg    gc= XCreateGC(dpy,lw->led.on_pixmap,(unsigned)GCForeground,&values);
174010cdda0Smrg    bevel= lw->led.bevel;
175010cdda0Smrg    width= lw->core.width;
176010cdda0Smrg    height= lw->core.height;
177010cdda0Smrg    XFillRectangle(dpy,on_pixmap,gc,0,0,width,height);
178010cdda0Smrg    XFillRectangle(dpy,off_pixmap,gc,0,0,width,height);
179010cdda0Smrg    XSetForeground(dpy,gc,lw->led.bottom_color);
180010cdda0Smrg    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
181010cdda0Smrg    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
182010cdda0Smrg    XSetForeground(dpy,gc,lw->led.on_color);
183010cdda0Smrg    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
184010cdda0Smrg    XSetForeground(dpy,gc,lw->led.off_color);
185010cdda0Smrg    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
186010cdda0Smrg    XFreeGC(dpy,gc);
187010cdda0Smrg    if (lw->led.on)	pix= on_pixmap;
188010cdda0Smrg    else		pix= off_pixmap;
189010cdda0Smrg    if (XtWindow((Widget)lw)!=None)
190010cdda0Smrg	XSetWindowBackgroundPixmap(dpy,XtWindow((Widget)lw),pix);
191010cdda0Smrg    return;
192010cdda0Smrg}
193010cdda0Smrg
194010cdda0Smrg/* ARGSUSED */
195010cdda0Smrgstatic void Initialize(request, new, args, num_args)
196010cdda0Smrg    Widget request, new;
197010cdda0Smrg    ArgList args;
198010cdda0Smrg    Cardinal *num_args;
199010cdda0Smrg{
200010cdda0Smrg    LEDWidget lw = (LEDWidget) new;
201010cdda0Smrg
202010cdda0Smrg    if (lw->core.height == 0)
203010cdda0Smrg        lw->core.height = lw->led.led_height;
204010cdda0Smrg    if (lw->core.width == 0)
205010cdda0Smrg        lw->core.width = lw->led.led_width;
206010cdda0Smrg    lw->core.border_width= 0;
207010cdda0Smrg    if (lw->led.bevel==0)
208010cdda0Smrg	lw->led.bevel= 1;
209010cdda0Smrg    lw->led.on_pixmap= lw->led.off_pixmap= None;
210010cdda0Smrg    (*XtClass(new)->core_class.resize) ((Widget)lw);
211010cdda0Smrg    GetPixmaps(lw);
212010cdda0Smrg} /* Initialize */
213010cdda0Smrg
214010cdda0Smrgstatic void
215010cdda0SmrgRealize(Widget w, Mask *mask, XSetWindowAttributes *xswa)
216010cdda0Smrg{
217010cdda0Smrg    LEDWidget 	lw = (LEDWidget)w;
218010cdda0Smrg    WidgetClass super = simpleWidgetClass;
219010cdda0Smrg    Pixmap	pix;
220010cdda0Smrg
221010cdda0Smrg    (*super->core_class.realize)(w,mask,xswa);
222010cdda0Smrg    if (lw->led.on)	pix= lw->led.on_pixmap;
223010cdda0Smrg    else		pix= lw->led.off_pixmap;
224010cdda0Smrg    XSetWindowBackgroundPixmap(XtDisplay(w),XtWindow(w),pix);
225010cdda0Smrg    return;
226010cdda0Smrg}
227010cdda0Smrg
228010cdda0Smrgstatic void
229010cdda0SmrgResize(Widget w)
230010cdda0Smrg{
231010cdda0Smrg    GetPixmaps((LEDWidget)w);
232010cdda0Smrg    return;
233010cdda0Smrg}
234010cdda0Smrg
235010cdda0Smrg/*
236010cdda0Smrg * Set specified arguments into widget
237010cdda0Smrg */
238010cdda0Smrg
239010cdda0Smrgstatic Boolean
240010cdda0SmrgSetValues(Widget current, Widget request, Widget new,
241010cdda0Smrg	  ArgList args, Cardinal *num_args)
242010cdda0Smrg{
243010cdda0Smrg    LEDWidget curlw = (LEDWidget) current;
244010cdda0Smrg    LEDWidget newlw = (LEDWidget) new;
245010cdda0Smrg    Boolean changed;
246010cdda0Smrg
247010cdda0Smrg    changed= FALSE;
248010cdda0Smrg    if (curlw->led.foreground		!= newlw->led.foreground
249010cdda0Smrg	|| curlw->core.background_pixel != newlw->core.background_pixel
250010cdda0Smrg	|| curlw->led.on_color		!= newlw->led.on_color
251010cdda0Smrg	|| curlw->led.off_color		!= newlw->led.off_color
252010cdda0Smrg	|| curlw->core.width		!= curlw->core.width
253010cdda0Smrg	|| curlw->core.height		!= curlw->core.height) {
254010cdda0Smrg	GetPixmaps(newlw);
255010cdda0Smrg	changed= TRUE;
256010cdda0Smrg    }
257010cdda0Smrg    if (curlw->led.on!=newlw->led.on) {
258010cdda0Smrg	Pixmap pix;
259010cdda0Smrg
260010cdda0Smrg	if (newlw->led.on)	pix= newlw->led.on_pixmap;
261010cdda0Smrg	else			pix= newlw->led.off_pixmap;
262010cdda0Smrg
263010cdda0Smrg	if (XtWindow(newlw)!=None)
264010cdda0Smrg	    XSetWindowBackgroundPixmap(XtDisplay(newlw),XtWindow(newlw),pix);
265010cdda0Smrg	changed= TRUE;
266010cdda0Smrg    }
267010cdda0Smrg    return changed;
268010cdda0Smrg}
269010cdda0Smrg
270010cdda0Smrgstatic void
271010cdda0SmrgDestroy(Widget w)
272010cdda0Smrg{
273010cdda0Smrg    LEDWidget lw = (LEDWidget)w;
274010cdda0Smrg
275010cdda0Smrg    if (lw->led.on_pixmap!=None) {
276010cdda0Smrg	XFreePixmap(XtDisplay(w),lw->led.on_pixmap);
277010cdda0Smrg	lw->led.on_pixmap= None;
278010cdda0Smrg    }
279010cdda0Smrg    if (lw->led.off_pixmap!=None) {
280010cdda0Smrg	XFreePixmap(XtDisplay(w),lw->led.off_pixmap);
281010cdda0Smrg	lw->led.off_pixmap= None;
282010cdda0Smrg    }
283010cdda0Smrg    return;
284010cdda0Smrg}
285010cdda0Smrg
286010cdda0Smrg
287010cdda0Smrgstatic XtGeometryResult
288010cdda0SmrgQueryGeometry(Widget w, XtWidgetGeometry *intended,
289010cdda0Smrg	      XtWidgetGeometry *preferred)
290010cdda0Smrg{
291010cdda0Smrg    LEDWidget lw = (LEDWidget)w;
292010cdda0Smrg
293010cdda0Smrg    preferred->request_mode = CWWidth | CWHeight;
294010cdda0Smrg    preferred->width = lw->led.led_height;
295010cdda0Smrg    preferred->height = lw->led.led_width;
296010cdda0Smrg    if (  ((intended->request_mode & (CWWidth | CWHeight))
297010cdda0Smrg	   	== (CWWidth | CWHeight)) &&
298010cdda0Smrg	  intended->width == preferred->width &&
299010cdda0Smrg	  intended->height == preferred->height)
300010cdda0Smrg	return XtGeometryYes;
301010cdda0Smrg    else if (preferred->width == w->core.width &&
302010cdda0Smrg	     preferred->height == w->core.height)
303010cdda0Smrg	return XtGeometryNo;
304010cdda0Smrg    else
305010cdda0Smrg	return XtGeometryAlmost;
306010cdda0Smrg}
307