LED.c revision af23b0a6
1/* $Xorg: LED.c,v 1.3 2000/08/17 19:54:51 cpqbld Exp $ */
2/************************************************************
3 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.
4
5 Permission to use, copy, modify, and distribute this
6 software and its documentation for any purpose and without
7 fee is hereby granted, provided that the above copyright
8 notice appear in all copies and that both that copyright
9 notice and this permission notice appear in supporting
10 documentation, and that the name of Silicon Graphics not be
11 used in advertising or publicity pertaining to distribution
12 of the software without specific prior written permission.
13 Silicon Graphics makes no representation about the suitability
14 of this software for any purpose. It is provided "as is"
15 without any express or implied warranty.
16
17 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
18 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
20 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
21 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
23 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
24 THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
26 ********************************************************/
27/* $XFree86: xc/programs/xkbutils/LED.c,v 1.4 2001/01/17 23:46:13 dawes Exp $ */
28
29#include <X11/IntrinsicP.h>
30#include <X11/StringDefs.h>
31#include <X11/Xos.h>
32#include <X11/Xaw/XawInit.h>
33#include "LEDP.h"
34#include <stdio.h>
35#include <ctype.h>
36/* needed for abs() */
37#include <stdlib.h>
38
39#define streq(a,b) (strcmp( (a), (b) ) == 0)
40
41#ifdef CRAY
42#define WORD64
43#endif
44
45/****************************************************************
46 *
47 * Full class record constant
48 *
49 ****************************************************************/
50
51/* Private Data */
52
53#define offset(field) XtOffsetOf(LEDRec, field)
54static XtResource resources[] = {
55    {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
56	offset(led.foreground), XtRString, XtDefaultForeground},
57    {XtNon, XtCOn, XtRBoolean, sizeof(Boolean),
58	offset(led.on), XtRImmediate, (XtPointer)False},
59    {XtNtopColor, XtCTopColor, XtRPixel, sizeof(Pixel),
60	offset(led.top_color), XtRString, "black"},
61    {XtNbottomColor, XtCBottomColor, XtRPixel, sizeof(Pixel),
62	offset(led.bottom_color), XtRString, "white"},
63    {XtNonColor, XtCOnColor, XtRPixel, sizeof(Pixel),
64	offset(led.on_color), XtRString, "green"},
65    {XtNoffColor, XtCOffColor, XtRPixel, sizeof(Pixel),
66	offset(led.off_color), XtRString, "#005000"},
67    {XtNbevel, XtCBevel, XtRDimension, sizeof(Dimension),
68	offset(led.bevel), XtRImmediate, (XtPointer)1},
69    {XtNledWidth, XtCLedWidth, XtRDimension, sizeof(Dimension),
70	offset(led.led_width), XtRImmediate, (XtPointer)6},
71    {XtNledHeight, XtCLedHeight, XtRDimension, sizeof(Dimension),
72	offset(led.led_height), XtRImmediate, (XtPointer)12}
73};
74#undef offset
75
76static void ClassInitialize ( void );
77static void Initialize ( Widget request, Widget new, ArgList args,
78			 Cardinal *num_args );
79static void Realize ( Widget w, Mask * mask, XSetWindowAttributes * xswa );
80static void Resize ( Widget w );
81static Boolean SetValues ( Widget current, Widget request, Widget new,
82			   ArgList args, Cardinal *num_args );
83static void Destroy ( Widget w );
84static XtGeometryResult QueryGeometry ( Widget w, XtWidgetGeometry *intended,
85					XtWidgetGeometry *preferred );
86
87LEDClassRec ledClassRec = {
88  {
89/* core_class fields */
90    /* superclass	  	*/	(WidgetClass) &simpleClassRec,
91    /* class_name	  	*/	"LED",
92    /* widget_size	  	*/	sizeof(LEDRec),
93    /* class_initialize   	*/	ClassInitialize,
94    /* class_part_initialize	*/	NULL,
95    /* class_inited       	*/	FALSE,
96    /* initialize	  	*/	Initialize,
97    /* initialize_hook		*/	NULL,
98    /* realize		  	*/	Realize,
99    /* actions		  	*/	NULL,
100    /* num_actions	  	*/	0,
101    /* resources	  	*/	resources,
102    /* num_resources	  	*/	XtNumber(resources),
103    /* xrm_class	  	*/	NULLQUARK,
104    /* compress_motion	  	*/	TRUE,
105    /* compress_exposure  	*/	TRUE,
106    /* compress_enterleave	*/	TRUE,
107    /* visible_interest	  	*/	FALSE,
108    /* destroy		  	*/	Destroy,
109    /* resize		  	*/	Resize,
110    /* expose		  	*/	XtInheritExpose,
111    /* set_values	  	*/	SetValues,
112    /* set_values_hook		*/	NULL,
113    /* set_values_almost	*/	XtInheritSetValuesAlmost,
114    /* get_values_hook		*/	NULL,
115    /* accept_focus	 	*/	NULL,
116    /* version			*/	XtVersion,
117    /* callback_private   	*/	NULL,
118    /* tm_table		   	*/	NULL,
119    /* query_geometry		*/	QueryGeometry,
120    /* display_accelerator	*/	XtInheritDisplayAccelerator,
121    /* extension		*/	NULL
122  },
123/* Simple class fields initialization */
124  {
125    /* change_sensitive		*/	XtInheritChangeSensitive
126  },
127/* LED class fields initialization */
128  {
129    /* ignore 			*/	0
130  }
131};
132WidgetClass ledWidgetClass = (WidgetClass)&ledClassRec;
133/****************************************************************
134 *
135 * Private Procedures
136 *
137 ****************************************************************/
138
139static void
140ClassInitialize(void)
141{
142    XawInitializeWidgetSet();
143}
144
145static void
146GetPixmaps(LEDWidget lw)
147{
148    XGCValues	values;
149    GC		gc;
150    Display *	dpy;
151    Window	root;
152    Pixmap	pix,on_pixmap,off_pixmap;
153    Dimension	bevel,width,height;
154
155    dpy= XtDisplay((Widget)lw);
156    root= RootWindowOfScreen(XtScreen((Widget)lw));
157    if (lw->led.on_pixmap!=None) {
158	XFreePixmap(dpy,lw->led.on_pixmap);
159	lw->led.on_pixmap= None;
160    }
161    if (lw->led.off_pixmap!=None) {
162	XFreePixmap(dpy,lw->led.off_pixmap);
163	lw->led.off_pixmap= None;
164    }
165    lw->led.on_pixmap= on_pixmap= XCreatePixmap(dpy,root,
166				 lw->core.width,lw->core.height,lw->core.depth);
167    lw->led.off_pixmap= off_pixmap= XCreatePixmap(dpy,root,
168				 lw->core.width,lw->core.height,lw->core.depth);
169
170    values.foreground	= lw->led.top_color;
171    gc= XCreateGC(dpy,lw->led.on_pixmap,(unsigned)GCForeground,&values);
172    bevel= lw->led.bevel;
173    width= lw->core.width;
174    height= lw->core.height;
175    XFillRectangle(dpy,on_pixmap,gc,0,0,width,height);
176    XFillRectangle(dpy,off_pixmap,gc,0,0,width,height);
177    XSetForeground(dpy,gc,lw->led.bottom_color);
178    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
179    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
180    XSetForeground(dpy,gc,lw->led.on_color);
181    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
182    XSetForeground(dpy,gc,lw->led.off_color);
183    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
184    XFreeGC(dpy,gc);
185    if (lw->led.on)	pix= on_pixmap;
186    else		pix= off_pixmap;
187    if (XtWindow((Widget)lw)!=None)
188	XSetWindowBackgroundPixmap(dpy,XtWindow((Widget)lw),pix);
189    return;
190}
191
192/* ARGSUSED */
193static void
194Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args)
195{
196    LEDWidget lw = (LEDWidget) new;
197
198    if (lw->core.height == 0)
199        lw->core.height = lw->led.led_height;
200    if (lw->core.width == 0)
201        lw->core.width = lw->led.led_width;
202    lw->core.border_width= 0;
203    if (lw->led.bevel==0)
204	lw->led.bevel= 1;
205    lw->led.on_pixmap= lw->led.off_pixmap= None;
206    (*XtClass(new)->core_class.resize) ((Widget)lw);
207    GetPixmaps(lw);
208} /* Initialize */
209
210static void
211Realize(Widget w, Mask *mask, XSetWindowAttributes *xswa)
212{
213    LEDWidget 	lw = (LEDWidget)w;
214    WidgetClass super = simpleWidgetClass;
215    Pixmap	pix;
216
217    (*super->core_class.realize)(w,mask,xswa);
218    if (lw->led.on)	pix= lw->led.on_pixmap;
219    else		pix= lw->led.off_pixmap;
220    XSetWindowBackgroundPixmap(XtDisplay(w),XtWindow(w),pix);
221    return;
222}
223
224static void
225Resize(Widget w)
226{
227    GetPixmaps((LEDWidget)w);
228    return;
229}
230
231/*
232 * Set specified arguments into widget
233 */
234
235static Boolean
236SetValues(Widget current, Widget request, Widget new,
237	  ArgList args, Cardinal *num_args)
238{
239    LEDWidget curlw = (LEDWidget) current;
240    LEDWidget newlw = (LEDWidget) new;
241    Boolean changed;
242
243    changed= FALSE;
244    if (curlw->led.foreground		!= newlw->led.foreground
245	|| curlw->core.background_pixel != newlw->core.background_pixel
246	|| curlw->led.on_color		!= newlw->led.on_color
247	|| curlw->led.off_color		!= newlw->led.off_color
248	|| curlw->core.width		!= curlw->core.width
249	|| curlw->core.height		!= curlw->core.height) {
250	GetPixmaps(newlw);
251	changed= TRUE;
252    }
253    if (curlw->led.on!=newlw->led.on) {
254	Pixmap pix;
255
256	if (newlw->led.on)	pix= newlw->led.on_pixmap;
257	else			pix= newlw->led.off_pixmap;
258
259	if (XtWindow(newlw)!=None)
260	    XSetWindowBackgroundPixmap(XtDisplay(newlw),XtWindow(newlw),pix);
261	changed= TRUE;
262    }
263    return changed;
264}
265
266static void
267Destroy(Widget w)
268{
269    LEDWidget lw = (LEDWidget)w;
270
271    if (lw->led.on_pixmap!=None) {
272	XFreePixmap(XtDisplay(w),lw->led.on_pixmap);
273	lw->led.on_pixmap= None;
274    }
275    if (lw->led.off_pixmap!=None) {
276	XFreePixmap(XtDisplay(w),lw->led.off_pixmap);
277	lw->led.off_pixmap= None;
278    }
279    return;
280}
281
282
283static XtGeometryResult
284QueryGeometry(Widget w, XtWidgetGeometry *intended,
285	      XtWidgetGeometry *preferred)
286{
287    LEDWidget lw = (LEDWidget)w;
288
289    preferred->request_mode = CWWidth | CWHeight;
290    preferred->width = lw->led.led_height;
291    preferred->height = lw->led.led_width;
292    if (  ((intended->request_mode & (CWWidth | CWHeight))
293	   	== (CWWidth | CWHeight)) &&
294	  intended->width == preferred->width &&
295	  intended->height == preferred->height)
296	return XtGeometryYes;
297    else if (preferred->width == w->core.width &&
298	     preferred->height == w->core.height)
299	return XtGeometryNo;
300    else
301	return XtGeometryAlmost;
302}
303