LED.c revision b3eb03f3
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 <X11/Xmu/Converters.h>
35#include <X11/Xmu/Drawing.h>
36#include <stdio.h>
37#include <ctype.h>
38/* needed for abs() */
39#include <stdlib.h>
40
41#define streq(a,b) (strcmp( (a), (b) ) == 0)
42
43#ifdef CRAY
44#define WORD64
45#endif
46
47/****************************************************************
48 *
49 * Full class record constant
50 *
51 ****************************************************************/
52
53/* Private Data */
54
55#define offset(field) XtOffsetOf(LEDRec, field)
56static XtResource resources[] = {
57    {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
58	offset(led.foreground), XtRString, XtDefaultForeground},
59    {XtNon, XtCOn, XtRBoolean, sizeof(Boolean),
60	offset(led.on), XtRImmediate, (XtPointer)False},
61    {XtNtopColor, XtCTopColor, XtRPixel, sizeof(Pixel),
62	offset(led.top_color), XtRString, "black"},
63    {XtNbottomColor, XtCBottomColor, XtRPixel, sizeof(Pixel),
64	offset(led.bottom_color), XtRString, "white"},
65    {XtNonColor, XtCOnColor, XtRPixel, sizeof(Pixel),
66	offset(led.on_color), XtRString, "green"},
67    {XtNoffColor, XtCOffColor, XtRPixel, sizeof(Pixel),
68	offset(led.off_color), XtRString, "#005000"},
69    {XtNbevel, XtCBevel, XtRDimension, sizeof(Dimension),
70	offset(led.bevel), XtRImmediate, (XtPointer)1},
71    {XtNledWidth, XtCLedWidth, XtRDimension, sizeof(Dimension),
72	offset(led.led_width), XtRImmediate, (XtPointer)6},
73    {XtNledHeight, XtCLedHeight, XtRDimension, sizeof(Dimension),
74	offset(led.led_height), XtRImmediate, (XtPointer)12}
75};
76#undef offset
77
78static void ClassInitialize ( void );
79static void Initialize ( Widget request, Widget new, ArgList args,
80			 Cardinal *num_args );
81static void Realize ( Widget w, Mask * mask, XSetWindowAttributes * xswa );
82static void Resize ( Widget w );
83static Boolean SetValues ( Widget current, Widget request, Widget new,
84			   ArgList args, Cardinal *num_args );
85static void Destroy ( Widget w );
86static XtGeometryResult QueryGeometry ( Widget w, XtWidgetGeometry *intended,
87					XtWidgetGeometry *preferred );
88
89LEDClassRec ledClassRec = {
90  {
91/* core_class fields */
92    /* superclass	  	*/	(WidgetClass) &simpleClassRec,
93    /* class_name	  	*/	"LED",
94    /* widget_size	  	*/	sizeof(LEDRec),
95    /* class_initialize   	*/	ClassInitialize,
96    /* class_part_initialize	*/	NULL,
97    /* class_inited       	*/	FALSE,
98    /* initialize	  	*/	Initialize,
99    /* initialize_hook		*/	NULL,
100    /* realize		  	*/	Realize,
101    /* actions		  	*/	NULL,
102    /* num_actions	  	*/	0,
103    /* resources	  	*/	resources,
104    /* num_resources	  	*/	XtNumber(resources),
105    /* xrm_class	  	*/	NULLQUARK,
106    /* compress_motion	  	*/	TRUE,
107    /* compress_exposure  	*/	TRUE,
108    /* compress_enterleave	*/	TRUE,
109    /* visible_interest	  	*/	FALSE,
110    /* destroy		  	*/	Destroy,
111    /* resize		  	*/	Resize,
112    /* expose		  	*/	XtInheritExpose,
113    /* set_values	  	*/	SetValues,
114    /* set_values_hook		*/	NULL,
115    /* set_values_almost	*/	XtInheritSetValuesAlmost,
116    /* get_values_hook		*/	NULL,
117    /* accept_focus	 	*/	NULL,
118    /* version			*/	XtVersion,
119    /* callback_private   	*/	NULL,
120    /* tm_table		   	*/	NULL,
121    /* query_geometry		*/	QueryGeometry,
122    /* display_accelerator	*/	XtInheritDisplayAccelerator,
123    /* extension		*/	NULL
124  },
125/* Simple class fields initialization */
126  {
127    /* change_sensitive		*/	XtInheritChangeSensitive
128  },
129/* LED class fields initialization */
130  {
131    /* ignore 			*/	0
132  }
133};
134WidgetClass ledWidgetClass = (WidgetClass)&ledClassRec;
135/****************************************************************
136 *
137 * Private Procedures
138 *
139 ****************************************************************/
140
141static void
142ClassInitialize(void)
143{
144    XawInitializeWidgetSet();
145}
146
147static void
148GetPixmaps(LEDWidget lw)
149{
150    XGCValues	values;
151    GC		gc;
152    Display *	dpy;
153    Window	root;
154    Pixmap	pix,on_pixmap,off_pixmap;
155    Dimension	bevel,width,height;
156
157    dpy= XtDisplay((Widget)lw);
158    root= RootWindowOfScreen(XtScreen((Widget)lw));
159    if (lw->led.on_pixmap!=None) {
160	XFreePixmap(dpy,lw->led.on_pixmap);
161	lw->led.on_pixmap= None;
162    }
163    if (lw->led.off_pixmap!=None) {
164	XFreePixmap(dpy,lw->led.off_pixmap);
165	lw->led.off_pixmap= None;
166    }
167    lw->led.on_pixmap= on_pixmap= XCreatePixmap(dpy,root,
168				 lw->core.width,lw->core.height,lw->core.depth);
169    lw->led.off_pixmap= off_pixmap= XCreatePixmap(dpy,root,
170				 lw->core.width,lw->core.height,lw->core.depth);
171
172    values.foreground	= lw->led.top_color;
173    gc= XCreateGC(dpy,lw->led.on_pixmap,(unsigned)GCForeground,&values);
174    bevel= lw->led.bevel;
175    width= lw->core.width;
176    height= lw->core.height;
177    XFillRectangle(dpy,on_pixmap,gc,0,0,width,height);
178    XFillRectangle(dpy,off_pixmap,gc,0,0,width,height);
179    XSetForeground(dpy,gc,lw->led.bottom_color);
180    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
181    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
182    XSetForeground(dpy,gc,lw->led.on_color);
183    XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
184    XSetForeground(dpy,gc,lw->led.off_color);
185    XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
186    XFreeGC(dpy,gc);
187    if (lw->led.on)	pix= on_pixmap;
188    else		pix= off_pixmap;
189    if (XtWindow((Widget)lw)!=None)
190	XSetWindowBackgroundPixmap(dpy,XtWindow((Widget)lw),pix);
191    return;
192}
193
194/* ARGSUSED */
195static void
196Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args)
197{
198    LEDWidget lw = (LEDWidget) new;
199
200    if (lw->core.height == 0)
201        lw->core.height = lw->led.led_height;
202    if (lw->core.width == 0)
203        lw->core.width = lw->led.led_width;
204    lw->core.border_width= 0;
205    if (lw->led.bevel==0)
206	lw->led.bevel= 1;
207    lw->led.on_pixmap= lw->led.off_pixmap= None;
208    (*XtClass(new)->core_class.resize) ((Widget)lw);
209    GetPixmaps(lw);
210} /* Initialize */
211
212static void
213Realize(Widget w, Mask *mask, XSetWindowAttributes *xswa)
214{
215    LEDWidget 	lw = (LEDWidget)w;
216    WidgetClass super = simpleWidgetClass;
217    Pixmap	pix;
218
219    (*super->core_class.realize)(w,mask,xswa);
220    if (lw->led.on)	pix= lw->led.on_pixmap;
221    else		pix= lw->led.off_pixmap;
222    XSetWindowBackgroundPixmap(XtDisplay(w),XtWindow(w),pix);
223    return;
224}
225
226static void
227Resize(Widget w)
228{
229    GetPixmaps((LEDWidget)w);
230    return;
231}
232
233/*
234 * Set specified arguments into widget
235 */
236
237static Boolean
238SetValues(Widget current, Widget request, Widget new,
239	  ArgList args, Cardinal *num_args)
240{
241    LEDWidget curlw = (LEDWidget) current;
242    LEDWidget newlw = (LEDWidget) new;
243    Boolean changed;
244
245    changed= FALSE;
246    if (curlw->led.foreground		!= newlw->led.foreground
247	|| curlw->core.background_pixel != newlw->core.background_pixel
248	|| curlw->led.on_color		!= newlw->led.on_color
249	|| curlw->led.off_color		!= newlw->led.off_color
250	|| curlw->core.width		!= curlw->core.width
251	|| curlw->core.height		!= curlw->core.height) {
252	GetPixmaps(newlw);
253	changed= TRUE;
254    }
255    if (curlw->led.on!=newlw->led.on) {
256	Pixmap pix;
257
258	if (newlw->led.on)	pix= newlw->led.on_pixmap;
259	else			pix= newlw->led.off_pixmap;
260
261	if (XtWindow(newlw)!=None)
262	    XSetWindowBackgroundPixmap(XtDisplay(newlw),XtWindow(newlw),pix);
263	changed= TRUE;
264    }
265    return changed;
266}
267
268static void
269Destroy(Widget w)
270{
271    LEDWidget lw = (LEDWidget)w;
272
273    if (lw->led.on_pixmap!=None) {
274	XFreePixmap(XtDisplay(w),lw->led.on_pixmap);
275	lw->led.on_pixmap= None;
276    }
277    if (lw->led.off_pixmap!=None) {
278	XFreePixmap(XtDisplay(w),lw->led.off_pixmap);
279	lw->led.off_pixmap= None;
280    }
281    return;
282}
283
284
285static XtGeometryResult
286QueryGeometry(Widget w, XtWidgetGeometry *intended,
287	      XtWidgetGeometry *preferred)
288{
289    LEDWidget lw = (LEDWidget)w;
290
291    preferred->request_mode = CWWidth | CWHeight;
292    preferred->width = lw->led.led_height;
293    preferred->height = lw->led.led_width;
294    if (  ((intended->request_mode & (CWWidth | CWHeight))
295	   	== (CWWidth | CWHeight)) &&
296	  intended->width == preferred->width &&
297	  intended->height == preferred->height)
298	return XtGeometryYes;
299    else if (preferred->width == w->core.width &&
300	     preferred->height == w->core.height)
301	return XtGeometryNo;
302    else
303	return XtGeometryAlmost;
304}
305