1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This will emulate a 7 red LED digit block.
24  */
25 
26 #include <math.h>
27 
28 #include "brightoninternals.h"
29 
30 extern int brightonPanelLocation();
31 
32 int
destroyLedblock(brightonDevice * dev)33 destroyLedblock(brightonDevice *dev)
34 {
35 	printf("destroyLedblock()\n");
36 
37 	if (dev->image0)
38 		brightonFreeBitmap(dev->bwin, dev->image0);
39 	if (dev->image1)
40 		brightonFreeBitmap(dev->bwin, dev->image1);
41 	if (dev->image2)
42 		brightonFreeBitmap(dev->bwin, dev->image2);
43 	if (dev->image3)
44 		brightonFreeBitmap(dev->bwin, dev->image3);
45 	if (dev->image4)
46 		brightonFreeBitmap(dev->bwin, dev->image4);
47 	if (dev->image5)
48 		brightonFreeBitmap(dev->bwin, dev->image5);
49 	if (dev->image6)
50 		brightonFreeBitmap(dev->bwin, dev->image6);
51 	if (dev->image7)
52 		brightonFreeBitmap(dev->bwin, dev->image7);
53 	if (dev->image8)
54 		brightonFreeBitmap(dev->bwin, dev->image8);
55 	if (dev->image9)
56 		brightonFreeBitmap(dev->bwin, dev->image9);
57 	if (dev->imagec)
58 		brightonFreeBitmap(dev->bwin, dev->imagec);
59 	if (dev->imagee)
60 		brightonFreeBitmap(dev->bwin, dev->imagee);
61 	dev->image = NULL;
62 	dev->image2 = NULL;
63 
64 	return(0);
65 }
66 
67 static void
displayledblock(brightonDevice * dev)68 displayledblock(brightonDevice *dev)
69 {
70 	int flags = dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags;
71 	brightonBitmap *choice;
72 
73 	if (dev->bwin->app->resources[dev->panel].flags & BRIGHTON_WITHDRAWN)
74 		return;
75 
76 	if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
77 		& BRIGHTON_WITHDRAWN)
78 		return;
79 
80 	switch ((int) dev->value) {
81 		default:
82 			choice = dev->imagec;
83 			break;
84 		case 0:
85 			choice = dev->image0;
86 			break;
87 		case 1:
88 			choice = dev->image1;
89 			break;
90 		case 2:
91 			choice = dev->image2;
92 			break;
93 		case 3:
94 			choice = dev->image3;
95 			break;
96 		case 4:
97 			choice = dev->image4;
98 			break;
99 		case 5:
100 			choice = dev->image5;
101 			break;
102 		case 6:
103 			choice = dev->image6;
104 			break;
105 		case 7:
106 			choice = dev->image7;
107 			break;
108 		case 8:
109 			choice = dev->image8;
110 			break;
111 		case 9:
112 			choice = dev->image9;
113 			break;
114 		case 10:
115 			choice = dev->imagee;
116 			break;
117 	}
118 
119 	brightonStretch(dev->bwin, choice,
120 		/*dev->bwin->app->resources[dev->panel].canvas, */
121 		dev->bwin->dlayer,
122 		dev->x + dev->bwin->app->resources[dev->panel].sx,
123 		dev->y + dev->bwin->app->resources[dev->panel].sy,
124 		dev->width, dev->height, flags);
125 
126 	brightonFinalRender(dev->bwin,
127 		dev->x + dev->bwin->app->resources[dev->panel].sx,
128 		dev->y + dev->bwin->app->resources[dev->panel].sy,
129 		dev->width, dev->height);
130 
131 	dev->lastvalue = dev->value;
132 	dev->lastposition = dev->position;
133 }
134 
considercallback(brightonDevice * dev)135 static int considercallback(brightonDevice *dev)
136 {
137 	brightonIResource *panel = &dev->bwin->app->resources[dev->panel];
138 
139 	if ((dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
140 		& BRIGHTON_WITHDRAWN)
141 		|| (dev->bwin->flags & BRIGHTON_NO_DRAW))
142 		return(0);
143 
144 	if ((dev->lastvalue != dev->value)
145 			|| (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
146 			& BRIGHTON_CHECKBUTTON))
147 	{
148 		if (panel->devlocn[dev->index].callback)
149 		{
150 			panel->devlocn[dev->index].callback(dev->bwin, dev->panel,
151 				dev->index, dev->value);
152 		} else {
153 			if (panel->callback)
154 				panel->callback(dev->bwin, dev->panel, dev->index, dev->value);
155 		}
156 	}
157 	return(0);
158 }
159 
160 static int
configure(brightonDevice * dev,brightonEvent * event)161 configure(brightonDevice *dev, brightonEvent *event)
162 {
163 /* printf("configureLedblock(%i, %f)\n", dev->index, dev->value); */
164 
165 	if (dev->bwin->app->resources[dev->panel].flags & BRIGHTON_WITHDRAWN)
166 		return(0);
167 
168 	if (event->command == -1)
169 		return(-1);
170 
171 	if (event->command == BRIGHTON_RESIZE)
172 	{
173 		dev->x = event->x;
174 		dev->y = event->y;
175 		dev->width = event->w;
176 		dev->height = event->h;
177 		/*
178 		 * We should consider altering the locations structure, so that
179 		 * event dispatching is correct.
180 		 */
181 		if ((dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
182 			& BRIGHTON_CHECKBUTTON) == 0)
183 			considercallback(dev);
184 
185 		brightonPanelLocation(dev->bwin,
186 			dev->panel, dev->index, dev->x, dev->y, dev->width, dev->height);
187 
188 		dev->lastvalue = -1;
189 		displayledblock(dev);
190 
191 		return(0);
192 	}
193 
194 	if (event->command == BRIGHTON_LEAVE)
195 	{
196 		if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
197 			& BRIGHTON_CHECKBUTTON)
198 		{
199 			dev->value = 0;
200 
201 			displayledblock(dev);
202 		}
203 		return(0);
204 	}
205 
206 	if (event->command == BRIGHTON_ENTER)
207 	{
208 		if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
209 			& BRIGHTON_CHECKBUTTON)
210 		{
211 			dev->value = 1;
212 
213 			displayledblock(dev);
214 		}
215 		return(0);
216 	}
217 
218 	if (event->command == BRIGHTON_BUTTONRELEASE)
219 	{
220 		if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
221 			& BRIGHTON_CHECKBUTTON)
222 		{
223 			dev->value = 0;
224 
225 			displayledblock(dev);
226 
227 			considercallback(dev);
228 		}
229 		return(0);
230 	}
231 
232 	if (event->command == BRIGHTON_BUTTONPRESS)
233 	{
234 		brightonIResource *panel = &dev->bwin->app->resources[dev->panel];
235 
236 		if (event->key == BRIGHTON_BUTTON2)
237 		{
238 			brightonRegisterController(dev);
239 
240 			return(0);
241 		}
242 
243 		if (dev->value == 0)
244 			dev->value = panel->devlocn[dev->index].to;
245 		else
246 			dev->value = panel->devlocn[dev->index].from;
247 
248 		if ((dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
249 			& BRIGHTON_CHECKBUTTON) == 0)
250 			considercallback(dev);
251 
252 		displayledblock(dev);
253 
254 		return(0);
255 	}
256 
257 	if (event->command == BRIGHTON_KEYPRESS)
258 	{
259 		brightonIResource *panel = &dev->bwin->app->resources[dev->panel];
260 
261 		if (event->key == 0x20)
262 		{
263 			if (dev->value == 0)
264 				dev->value = panel->devlocn[dev->index].to;
265 			else
266 				dev->value = panel->devlocn[dev->index].from;
267 
268 			considercallback(dev);
269 
270 			displayledblock(dev);
271 
272 			return(0);
273 		}
274 		/*
275 		 * If this was not a space bar which we use to activate and de-activate
276 		 * any arbitrary ledblock then it could be that we pressed some key that
277 		 * can otherwise be interpretted.
278 		 * This is awkward since here we are in a single ledblock and I would
279 		 * like to use keypress to emulate a piano keyboard from the computer.
280 		 * These events would have to be delivered to the parent, not to the
281 		 * device, and the parent would then decide to which device the event
282 		 * should be delivered.
283 		 */
284 	}
285 
286 	if (event->command == BRIGHTON_KEYRELEASE)
287 	{
288 		/*
289 		 * This is just to clear the event and repaint the key, we should not
290 		 * be bothered with the callback.
291 		 */
292 		if (event->key == 0x20)
293 		{
294 			if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
295 				& BRIGHTON_CHECKBUTTON)
296 			{
297 				dev->value = 0;
298 
299 				displayledblock(dev);
300 
301 /*				considercallback(dev); */
302 			}
303 		}
304 		return(0);
305 	}
306 
307 	if (event->command == BRIGHTON_PARAMCHANGE)
308 	{
309 		dev->value = event->value;
310 		dev->lastvalue = -1;
311 
312 		if ((dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
313 			& BRIGHTON_CHECKBUTTON) == 0)
314 			considercallback(dev);
315 
316 		displayledblock(dev);
317 
318 		return(0);
319 	}
320 
321 	return(0);
322 }
323 
324 int *
createLedblock(brightonWindow * bwin,brightonDevice * dev,int index,char * bitmap)325 createLedblock(brightonWindow *bwin, brightonDevice *dev, int index, char *bitmap)
326 {
327 /* printf("createLedblock()\n"); */
328 
329 	dev->destroy = destroyLedblock;
330 	dev->configure = configure;
331 	dev->bwin = bwin;
332 
333 	if (dev->image0)
334 		brightonFreeBitmap(bwin, dev->image0);
335 	if (dev->image1)
336 		brightonFreeBitmap(bwin, dev->image1);
337 	if (dev->image2)
338 		brightonFreeBitmap(bwin, dev->image2);
339 	if (dev->image3)
340 		brightonFreeBitmap(bwin, dev->image3);
341 	if (dev->image4)
342 		brightonFreeBitmap(bwin, dev->image4);
343 	if (dev->image5)
344 		brightonFreeBitmap(bwin, dev->image5);
345 	if (dev->image6)
346 		brightonFreeBitmap(bwin, dev->image6);
347 	if (dev->image7)
348 		brightonFreeBitmap(bwin, dev->image7);
349 	if (dev->image8)
350 		brightonFreeBitmap(bwin, dev->image8);
351 	if (dev->image9)
352 		brightonFreeBitmap(bwin, dev->image9);
353 
354 	if ((dev->image0
355 		= brightonReadImage(bwin, "bitmaps/digits/redled0.xpm")) == 0)
356 		printf("could not load redled image\n");
357 	if ((dev->image1
358 		= brightonReadImage(bwin, "bitmaps/digits/redled1.xpm")) == 0)
359 		printf("could not load redled image\n");
360 	if ((dev->image2
361 		= brightonReadImage(bwin, "bitmaps/digits/redled2.xpm")) == 0)
362 		printf("could not load redled image\n");
363 	if ((dev->image3
364 		= brightonReadImage(bwin, "bitmaps/digits/redled3.xpm")) == 0)
365 		printf("could not load redled image\n");
366 	if ((dev->image4
367 		= brightonReadImage(bwin, "bitmaps/digits/redled4.xpm")) == 0)
368 		printf("could not load redled image\n");
369 	if ((dev->image5
370 		= brightonReadImage(bwin, "bitmaps/digits/redled5.xpm")) == 0)
371 		printf("could not load redled image\n");
372 	if ((dev->image6
373 		= brightonReadImage(bwin, "bitmaps/digits/redled6.xpm")) == 0)
374 		printf("could not load redled image\n");
375 	if ((dev->image7
376 		= brightonReadImage(bwin, "bitmaps/digits/redled7.xpm")) == 0)
377 		printf("could not load redled image\n");
378 	if ((dev->image8
379 		= brightonReadImage(bwin, "bitmaps/digits/redled8.xpm")) == 0)
380 		printf("could not load redled image\n");
381 	if ((dev->image9
382 		= brightonReadImage(bwin, "bitmaps/digits/redled9.xpm")) == 0)
383 		printf("could not load redled image\n");
384 	if ((dev->imagec
385 		= brightonReadImage(bwin, "bitmaps/digits/redledoff.xpm")) == 0)
386 		printf("could not load redled image\n");
387 	if ((dev->imagee
388 		= brightonReadImage(bwin, "bitmaps/digits/redledE.xpm")) == 0)
389 		printf("could not load redled image\n");
390 
391 	/*
392 	 * These will force an update when we first display ourselves.
393 	 */
394 	dev->value = 0;
395 	dev->lastvalue = -1;
396 	dev->lastposition = -1;
397 
398 	return(0);
399 }
400 
401