LED.c revision 010cdda0
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 Initialize(request, new, args, num_args)
196    Widget request, new;
197    ArgList args;
198    Cardinal *num_args;
199{
200    LEDWidget lw = (LEDWidget) new;
201
202    if (lw->core.height == 0)
203        lw->core.height = lw->led.led_height;
204    if (lw->core.width == 0)
205        lw->core.width = lw->led.led_width;
206    lw->core.border_width= 0;
207    if (lw->led.bevel==0)
208	lw->led.bevel= 1;
209    lw->led.on_pixmap= lw->led.off_pixmap= None;
210    (*XtClass(new)->core_class.resize) ((Widget)lw);
211    GetPixmaps(lw);
212} /* Initialize */
213
214static void
215Realize(Widget w, Mask *mask, XSetWindowAttributes *xswa)
216{
217    LEDWidget 	lw = (LEDWidget)w;
218    WidgetClass super = simpleWidgetClass;
219    Pixmap	pix;
220
221    (*super->core_class.realize)(w,mask,xswa);
222    if (lw->led.on)	pix= lw->led.on_pixmap;
223    else		pix= lw->led.off_pixmap;
224    XSetWindowBackgroundPixmap(XtDisplay(w),XtWindow(w),pix);
225    return;
226}
227
228static void
229Resize(Widget w)
230{
231    GetPixmaps((LEDWidget)w);
232    return;
233}
234
235/*
236 * Set specified arguments into widget
237 */
238
239static Boolean
240SetValues(Widget current, Widget request, Widget new,
241	  ArgList args, Cardinal *num_args)
242{
243    LEDWidget curlw = (LEDWidget) current;
244    LEDWidget newlw = (LEDWidget) new;
245    Boolean changed;
246
247    changed= FALSE;
248    if (curlw->led.foreground		!= newlw->led.foreground
249	|| curlw->core.background_pixel != newlw->core.background_pixel
250	|| curlw->led.on_color		!= newlw->led.on_color
251	|| curlw->led.off_color		!= newlw->led.off_color
252	|| curlw->core.width		!= curlw->core.width
253	|| curlw->core.height		!= curlw->core.height) {
254	GetPixmaps(newlw);
255	changed= TRUE;
256    }
257    if (curlw->led.on!=newlw->led.on) {
258	Pixmap pix;
259
260	if (newlw->led.on)	pix= newlw->led.on_pixmap;
261	else			pix= newlw->led.off_pixmap;
262
263	if (XtWindow(newlw)!=None)
264	    XSetWindowBackgroundPixmap(XtDisplay(newlw),XtWindow(newlw),pix);
265	changed= TRUE;
266    }
267    return changed;
268}
269
270static void
271Destroy(Widget w)
272{
273    LEDWidget lw = (LEDWidget)w;
274
275    if (lw->led.on_pixmap!=None) {
276	XFreePixmap(XtDisplay(w),lw->led.on_pixmap);
277	lw->led.on_pixmap= None;
278    }
279    if (lw->led.off_pixmap!=None) {
280	XFreePixmap(XtDisplay(w),lw->led.off_pixmap);
281	lw->led.off_pixmap= None;
282    }
283    return;
284}
285
286
287static XtGeometryResult
288QueryGeometry(Widget w, XtWidgetGeometry *intended,
289	      XtWidgetGeometry *preferred)
290{
291    LEDWidget lw = (LEDWidget)w;
292
293    preferred->request_mode = CWWidth | CWHeight;
294    preferred->width = lw->led.led_height;
295    preferred->height = lw->led.led_width;
296    if (  ((intended->request_mode & (CWWidth | CWHeight))
297	   	== (CWWidth | CWHeight)) &&
298	  intended->width == preferred->width &&
299	  intended->height == preferred->height)
300	return XtGeometryYes;
301    else if (preferred->width == w->core.width &&
302	     preferred->height == w->core.height)
303	return XtGeometryNo;
304    else
305	return XtGeometryAlmost;
306}
307