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 #include "brightoninternals.h"
23 #include "brightonledstates.h"
24 
25 int
destroyLed(brightonDevice * dev)26 destroyLed(brightonDevice *dev)
27 {
28 	printf("destroyLed()\n");
29 
30 	if (dev->image0)
31 		brightonFreeBitmap(dev->bwin, dev->image0);
32 	if (dev->image1)
33 		brightonFreeBitmap(dev->bwin, dev->image1);
34 	if (dev->image2)
35 		brightonFreeBitmap(dev->bwin, dev->image2);
36 	if (dev->image3)
37 		brightonFreeBitmap(dev->bwin, dev->image3);
38 	if (dev->image4)
39 		brightonFreeBitmap(dev->bwin, dev->image4);
40 
41 	dev->image = NULL;
42 
43 	return(0);
44 }
45 
46 static void
displayled(brightonDevice * dev)47 displayled(brightonDevice *dev)
48 {
49 	int flags = dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags;
50 	brightonBitmap *choice = NULL;
51 
52 	if (dev->bwin->app->resources[dev->panel].flags & BRIGHTON_WITHDRAWN)
53 		return;
54 
55 	if (dev->bwin->app->resources[dev->panel].devlocn[dev->index].flags
56 		& BRIGHTON_WITHDRAWN)
57 		return;
58 
59 	if (((int) dev->value) & BRIGHTON_LED_FLASH_FAST)
60 		choice = dev->image4;
61 	else if (((int) dev->value) & BRIGHTON_LED_FLASH) {
62 		if (((int) dev->value) & BRIGHTON_LED_FLASHING) {
63 			choice = dev->image0;
64 		 	dev->value = ((int) dev->value) & ~BRIGHTON_LED_FLASHING;
65 		} else
66 		 	dev->value = ((int) dev->value) | BRIGHTON_LED_FLASHING;
67 	}
68 
69 	if (choice == NULL)
70 	{
71 		switch (((int) dev->value) & BRIGHTON_LED_MASK) {
72 			default:
73 				choice = dev->image0;
74 				break;
75 			case 1:
76 				choice = dev->image1;
77 				break;
78 			case 2:
79 				choice = dev->image2;
80 				break;
81 			case 3:
82 				choice = dev->image3;
83 				break;
84 			case 4:
85 				choice = dev->image4;
86 				break;
87 		}
88 	}
89 
90 	brightonStretch(dev->bwin, choice,
91 		dev->bwin->dlayer,
92 		dev->x + dev->bwin->app->resources[dev->panel].sx,
93 		dev->y + dev->bwin->app->resources[dev->panel].sy,
94 		dev->width, dev->height, flags);
95 
96 	brightonFinalRender(dev->bwin,
97 		dev->x + dev->bwin->app->resources[dev->panel].sx,
98 		dev->y + dev->bwin->app->resources[dev->panel].sy,
99 		dev->width, dev->height);
100 
101 	dev->lastvalue = dev->value;
102 	dev->lastposition = dev->position;
103 }
104 
105 static void
considercallback(brightonDevice * dev)106 considercallback(brightonDevice *dev)
107 {
108 	brightonIResource *panel = &dev->bwin->app->resources[dev->panel];
109 	int callvalue = 0;
110 
111 	if (((int) dev->value) & BRIGHTON_LED_FLASH_FAST)
112 		callvalue = 1;
113 
114 	if (panel->callback)
115 		panel->callback(dev->bwin, dev->panel, dev->index, callvalue);
116 }
117 
118 static int
configure(brightonDevice * dev,brightonEvent * event)119 configure(brightonDevice *dev, brightonEvent *event)
120 {
121 	if (event->command == -1)
122 		return(-1);
123 
124 //printf("configureLed(%i, %f)\n", event->command, event->value);
125 
126 	if (event->command == BRIGHTON_SLOW_TIMER)
127 	{
128 		/*
129 		 * Change state of display. If we are still configured to flash then
130 		 * request another callback
131 		 */
132 		displayled(dev);
133 
134 		return(0);
135 	}
136 
137 	if (event->command == BRIGHTON_FAST_TIMER)
138 	{
139 //		printf("Fast timer expired\n");
140 
141 		/*
142 		 * We are going on or off, we probably have to know that here from our
143 		 * flag status. In either case we need to set some value and notify a
144 		 * callback value since these are used for note on and note off events.
145 		 */
146 		if (event->value == 0)
147 			dev->value = ((int) dev->value) & ~BRIGHTON_LED_FLASH_FAST;
148 		else
149 			dev->value = ((int) dev->value) | BRIGHTON_LED_FLASH_FAST;
150 
151 		considercallback(dev);
152 		displayled(dev);
153 
154 		return(0);
155 	}
156 
157 	if (event->command == BRIGHTON_RESIZE)
158 	{
159 		dev->originx = event->x;
160 		dev->originy = event->y;
161 
162 		dev->x = event->x;
163 		dev->y = event->y;
164 		dev->width = event->w;
165 		dev->height = event->h;
166 
167 		dev->lastvalue = -1;
168 		displayled(dev);
169 
170 		return(0);
171 	}
172 
173 	if (event->command == BRIGHTON_PARAMCHANGE)
174 	{
175 		int iv = (int) event->value;
176 		/*
177 		 * If we have a flashing option set then request a callback. Do not
178 		 * change the color bits, only the 'flash' bit.
179 		 */
180 		if (((int) event->value) & BRIGHTON_LED_FLASH)
181 		{
182 			brightonSlowTimer(dev->bwin, dev, BRIGHTON_ST_REQ);
183 			if ((iv & BRIGHTON_LED_MASK) == 0)
184 				dev->value = ((int) dev->value) | BRIGHTON_LED_FLASH;
185 			else
186 				dev->value = event->value;
187 		} else {
188 			brightonSlowTimer(dev->bwin, dev, BRIGHTON_ST_CANCEL);
189 			dev->value = event->value;
190 		}
191 
192 		displayled(dev);
193 
194 		return(0);
195 	}
196 	return(0);
197 }
198 
199 int *
createLed(brightonWindow * bwin,brightonDevice * dev,int index,char * bitmap)200 createLed(brightonWindow *bwin, brightonDevice *dev, int index, char *bitmap)
201 {
202 /*	printf("createLed(%s)\n", bitmap); */
203 
204 	dev->destroy = destroyLed;
205 	dev->configure = configure;
206 	dev->index = index;
207 
208 	dev->bwin = bwin;
209 
210 	if (dev->image0)
211 		brightonFreeBitmap(bwin, dev->image0);
212 	if (dev->image1)
213 		brightonFreeBitmap(bwin, dev->image1);
214 	if (dev->image2)
215 		brightonFreeBitmap(bwin, dev->image2);
216 	if (dev->image3)
217 		brightonFreeBitmap(bwin, dev->image3);
218 	if (dev->image4)
219 		brightonFreeBitmap(bwin, dev->image4);
220 
221 	if ((dev->image0
222 		= brightonReadImage(bwin, "bitmaps/images/offled.xpm")) == 0)
223 		printf("could not load offled image\n");
224 	if ((dev->image1
225 		= brightonReadImage(bwin, "bitmaps/images/redled.xpm")) == 0)
226 		printf("could not load redled image\n");
227 	if ((dev->image2
228 		= brightonReadImage(bwin, "bitmaps/images/greenled.xpm")) == 0)
229 		printf("could not load greenled image\n");
230 	if ((dev->image3
231 		= brightonReadImage(bwin, "bitmaps/images/yellowled.xpm")) == 0)
232 		printf("could not load yellowled image\n");
233 	if ((dev->image4
234 		= brightonReadImage(bwin, "bitmaps/images/blueled.xpm")) == 0)
235 		printf("could not load blueled image\n");
236 
237 	dev->value = 0.500001;
238 
239 	dev->lastvalue = -1;
240 	dev->lastposition = 0;
241 
242 	return(0);
243 }
244 
245