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 is the generic device creator. It will generate the device and give it
24 * all the generic handlers and tables. The device init is then also called to
25 * provide its specifics.
26 */
27
28 #include "brightoninternals.h"
29 #include "brightonDevtable.h"
30
31 extern int BSendEvent(brightonDisplay *, brightonWindow *, brightonEvent *);
32
33 static int
brightonInitDevice(brightonWindow * bwin,brightonDevice * device,int index,char * bitmap)34 brightonInitDevice(brightonWindow *bwin, brightonDevice *device, int index,
35 char *bitmap)
36 {
37 if (device->device == -1)
38 return(0);
39
40 /*
41 * Then call the generic inheritances.
42 */
43 if ((brightonDevTable[device->device].from >= 0)
44 && (brightonDevTable[device->device].from != index))
45 {
46 int orgdev = device->device;
47
48 device->device = brightonDevTable[device->device].from;
49
50 brightonInitDevice(bwin, device, index, bitmap);
51
52 device->device = orgdev;
53 }
54
55 /*
56 * And finally the specific instances
57 */
58 brightonDevTable[device->device].create(bwin, device, index, bitmap);
59
60 return(0);
61 }
62
63 brightonDevice *
brightonCreateDevice(brightonWindow * bwin,int type,int panel,int index,char * bitmap)64 brightonCreateDevice(brightonWindow *bwin, int type, int panel,
65 int index, char *bitmap)
66 {
67 brightonDevice *device;
68
69 /* printf("brightonCreateDevice(%x, %i, %i, %s)\n", */
70 /* bwin, type, index, bitmap); */
71
72 if ((type < 0) || (type >= BRIGHTON_DEVTABLE_SIZE))
73 return(0);
74
75 device = brightonmalloc(sizeof(brightonDevice));
76 device->device = type;
77 device->panel = panel;
78 device->index = index;
79 device->flags |= BRIGHTON_DEV_INITED;
80
81 if (brightonInitDevice(bwin, device, index, bitmap) == 0)
82 return(device);
83 else {
84 brightonDestroyDevice(device);
85 return(0);
86 }
87 }
88
89 static void
brightonDestroyDevices(brightonDevice * device)90 brightonDestroyDevices(brightonDevice *device)
91 {
92 #warning we return early here, that may have to be resolved
93 printf("destroying %p\n", device);
94 return;
95 if ((device == 0) || (device->device == -1))
96 return;
97
98 if ((brightonDevTable[device->device].from >= 0)
99 && (brightonDevTable[device->device].from != device->device))
100 {
101 int index = device->device;
102
103 device->device = brightonDevTable[device->device].from;
104 brightonDestroyDevices(device);
105 device->device = index;
106 }
107 brightonDevTable[device->device].destroy(device);
108 }
109
110 void
brightonDestroyDevice(brightonDevice * device)111 brightonDestroyDevice(brightonDevice *device)
112 {
113 printf("destroyDevice()\n");
114
115 brightonDestroyDevices(device);
116
117 if (device->destroy)
118 device->destroy(device);
119
120 if (device->next)
121 device->next->last = device->last;
122 if (device->last)
123 device->last->next = device->next;
124
125 if (device->shadow.coords)
126 brightonfree(device->shadow.coords);
127 if (device->shadow.mask)
128 brightonfree(device->shadow.mask);
129
130 brightonfree(device);
131 }
132
133 void
brightonConfigureDevice(brightonDevice * dev,brightonEvent * event)134 brightonConfigureDevice(brightonDevice *dev, brightonEvent *event)
135 {
136 }
137
138 /*
139 * Event can be motion, keypress, varchange, buttonpress, etc.
140 * Alterations to world are handled in configure - ie, changes in position,
141 * size, etc. We may, however, collapse these into a single call?
142 */
143 void
brightonDeviceEvent(brightonDevice * dev,brightonEvent * event)144 brightonDeviceEvent(brightonDevice *dev, brightonEvent *event)
145 {
146 }
147
148 int
brightonSendEvent(brightonWindow * bwin,int panel,int index,brightonEvent * event)149 brightonSendEvent(brightonWindow* bwin, int panel, int index, brightonEvent *event)
150 {
151 /*
152 * This needs to go to BSendEvent(), this will evaluate global x/y location
153 * so that the correct device receives the even.
154 */
155 event->x = bwin->app->resources[panel].devlocn[index].x
156 + bwin->app->resources[panel].x + 1;
157
158 event->y = bwin->app->resources[panel].devlocn[index].y
159 + bwin->app->resources[panel].y;
160
161 return(BSendEvent(bwin->display, bwin, event));
162 }
163
164 /*
165 * This will make the GUI reflect the new value being given.
166 */
167 int
brightonParamChange(brightonWindow * bwin,int panel,int index,brightonEvent * event)168 brightonParamChange(brightonWindow *bwin, int panel, int index,
169 brightonEvent *event)
170 {
171 char *image = "bitmaps/images/cable.xpm";
172
173 if (((index < 0) && (event->type != BRIGHTON_EXPOSE))
174 || (panel < 0)
175 || (bwin == NULL))
176 return(-1);
177
178 if (panel >= bwin->app->nresources)
179 {
180 if (bwin->flags & BRIGHTON_DEBUG)
181 printf("panel count %i over %i\n", panel, bwin->app->nresources);
182 return(-1);
183 }
184
185 if (index >= bwin->app->resources[panel].ndevices)
186 {
187 /*
188 * Drop this debug
189 printf("index %i over panel count %i (transposed keyboard?)\n",
190 index, bwin->app->resources[panel].ndevices);
191 */
192 return(-1);
193 }
194
195 /* if (bwin->flags & BRIGHTON_BUSY) */
196 /* return(-1); */
197
198 /* printf("brightonParamChange(%x, %i): link\n", bwin, index); */
199
200 if (event->type == BRIGHTON_LINK)
201 {
202 int i2, x1, y1, x2, y2, xh;
203
204 /*
205 * The main reason to be here is to adjust the location of the place
206 * request. We do not really mind what the device is, only the two
207 * indices, source and dest. These are taken from the resource list
208 * and scaled according to the window geometry, then placed on the
209 * device bitmaps.
210 *
211 * We have the brightonWindow which gives us all our resources, and
212 * we should know which devices we are linking together.
213 */
214 if ((i2 = event->intvalue) < 0)
215 return(0);
216
217 x1 = bwin->app->resources[panel].devlocn[index].x
218 * bwin->app->resources[panel].sw / 1000
219 + bwin->app->resources[panel].sx;
220 y1 = bwin->app->resources[panel].devlocn[index].y
221 * bwin->app->resources[panel].sh / 1000
222 + bwin->app->resources[panel].sy;
223 x2 = bwin->app->resources[panel].devlocn[i2].x
224 * bwin->app->resources[panel].sw / 1000
225 + bwin->app->resources[panel].sx;
226 y2 = bwin->app->resources[panel].devlocn[i2].y
227 * bwin->app->resources[panel].sh / 1000
228 + bwin->app->resources[panel].sy;
229
230 if (x2 < x1) {
231 xh = x2;
232 x2 = x1;
233 x1 = xh;
234 xh = y2;
235 y2 = y1;
236 y1 = xh;
237 image = "bitmaps/images/cablered.xpm";
238 } else
239 image = "bitmaps/images/cableyellow.xpm";
240
241 /*
242 * We should correct positioning now: the aspect - if height is greater
243 * then width then we are going to use a different bitmap and it will
244 * be slid rather than shifted, and the endpoints need to have some
245 * constants added. Plus we want to give organised coord (x1 < x2).
246 */
247 if (x2 < x1) {
248 int w, h, xh = x2, yh = y2;
249
250 image = "bitmaps/images/cableyellow.xpm";
251
252 x2 = x1;
253 y2 = y1;
254 x1 = xh;
255 y1 = yh;
256
257 w = x2 - x1;
258 if ((h = y2 - y1) < 0)
259 h = -h;
260
261 if (w < h) {
262 x1 += 1;
263 y1 -= 0;
264 x2 += 3;
265 y2 += 8;
266 image = "bitmaps/images/cableV.xpm";
267 } else {
268 x1 += 2;
269 y1 += 0;
270 x2 += 9;
271 y2 -= 1;
272 }
273 } else {
274 int w, h;
275
276 w = x2 - x1;
277
278 if ((h = y2 - y1) < 0)
279 h = -h;
280
281 if (w < h) {
282 if (bwin->app->resources[panel].devlocn[i2].x <
283 bwin->app->resources[panel].devlocn[index].x)
284 image = "bitmaps/images/cableVred.xpm";
285 else
286 image = "bitmaps/images/cableVyellow.xpm";
287 if (y1 < y2) {
288 x1 += 2;
289 y1 += 1;
290 x2 += 3;
291 y2 += 7;
292 } else {
293 x1 += 1;
294 y1 += 7;
295 x2 += 2;
296 y2 += 1;
297 }
298 } else {
299 if (y1 < y2) {
300 x1 += 2;
301 y1 += 0;
302 x2 += 8;
303 y2 += 2;
304 } else {
305 x1 += 2;
306 y1 -= 0;
307 x2 += 8;
308 y2 -= 1;
309 }
310 }
311 }
312
313 return(brightonPlace(bwin, image, x1, y1, x2, y2));
314 }
315
316 if (event->type == BRIGHTON_UNLINK)
317 {
318 brightonRemove(bwin, event->intvalue);
319 return(-1);
320 }
321
322 if ((event->command != BRIGHTON_SLOW_TIMER)
323 && (event->command != BRIGHTON_FAST_TIMER))
324 event->command = BRIGHTON_PARAMCHANGE;
325
326 /*
327 if (type == 0)
328 param.args.f = value;
329 else if (type == 1)
330 param.args.m = (void *) &value;
331 */
332
333 if (panel >= bwin->app->nresources)
334 return(-1);
335
336 if (index >= bwin->app->resources[panel].ndevices)
337 return(-1);
338
339 if (bwin->app->resources[panel].devlocn[index].type == -1)
340 return(-1);
341
342 /*
343 * See if this is a panel configuration event.
344 */
345 if (index == -1)
346 {
347 if (bwin->app->resources[panel].configure)
348 bwin->app->resources[panel].configure
349 (bwin, &bwin->app->resources[panel], event);
350
351 } else
352 ((brightonDevice *) bwin->app->resources[panel].devlocn[index].dev)
353 ->configure(bwin->app->resources[panel].devlocn[index].dev, event);
354
355 return(0);
356 }
357
358