File: brightonDevice.c

package info (click to toggle)
bristol 0.60.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,652 kB
  • sloc: ansic: 124,457; sh: 10,579; makefile: 111
file content (357 lines) | stat: -rw-r--r-- 8,503 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

/*
 *  Diverse Bristol audio routines.
 *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This is the generic device creator. It will generate the device and give it
 * all the generic handlers and tables. The device init is then also called to
 * provide its specifics.
 */

#include "brightoninternals.h"
#include "brightonDevtable.h"

extern int BSendEvent(brightonDisplay *, brightonWindow *, brightonEvent *);

static int
brightonInitDevice(brightonWindow *bwin, brightonDevice *device, int index,
char *bitmap)
{
	if (device->device == -1)
		return(0);

	/*
	 * Then call the generic inheritances.
	 */ 
	if ((brightonDevTable[device->device].from >= 0)
		&& (brightonDevTable[device->device].from != index))
	{
		int orgdev = device->device;

		device->device = brightonDevTable[device->device].from;

		brightonInitDevice(bwin, device, index, bitmap);

		device->device = orgdev;
	}

	/*
	 * And finally the specific instances
	 */
	brightonDevTable[device->device].create(bwin, device, index, bitmap);

	return(0);
}

brightonDevice *
brightonCreateDevice(brightonWindow *bwin, int type, int panel,
	int index, char *bitmap)
{
	brightonDevice *device;

/*	printf("brightonCreateDevice(%x, %i, %i, %s)\n", */
/*		bwin, type, index, bitmap); */

	if ((type < 0) || (type >= BRIGHTON_DEVTABLE_SIZE))
		return(0);

	device = brightonmalloc(sizeof(brightonDevice));
	device->device = type;
	device->panel = panel;
	device->index = index;
	device->flags |= BRIGHTON_DEV_INITED;

	if (brightonInitDevice(bwin, device, index, bitmap) == 0)
		return(device);
	else {
		brightonDestroyDevice(device);
		return(0);
	}
}

static void
brightonDestroyDevices(brightonDevice *device)
{
#warning we return early here, that may have to be resolved
printf("destroying %p\n", device);
return;
	if ((device == 0) || (device->device == -1))
		return;

	if ((brightonDevTable[device->device].from >= 0)
		&& (brightonDevTable[device->device].from != device->device))
	{
		int index = device->device;

		device->device = brightonDevTable[device->device].from;
		brightonDestroyDevices(device);
		device->device = index;
	}
	brightonDevTable[device->device].destroy(device);
}

void
brightonDestroyDevice(brightonDevice *device)
{
	printf("destroyDevice()\n");

	brightonDestroyDevices(device);

	if (device->destroy)
		device->destroy(device);

	if (device->next)
		device->next->last = device->last;
	if (device->last)
		device->last->next = device->next;

	if (device->shadow.coords)
		brightonfree(device->shadow.coords);
	if (device->shadow.mask)
		brightonfree(device->shadow.mask);

	brightonfree(device);
}

void
brightonConfigureDevice(brightonDevice *dev, brightonEvent *event)
{
}

/*
 * Event can be motion, keypress, varchange, buttonpress, etc.
 * Alterations to world are handled in configure - ie, changes in position,
 * size, etc. We may, however, collapse these into a single call?
 */
void
brightonDeviceEvent(brightonDevice *dev, brightonEvent *event)
{
}

int
brightonSendEvent(brightonWindow* bwin, int panel, int index, brightonEvent *event)
{
	/*
	 * This needs to go to BSendEvent(), this will evaluate global x/y location
	 * so that the correct device receives the even.
	 */
	event->x = bwin->app->resources[panel].devlocn[index].x
		+ bwin->app->resources[panel].x + 1;

	event->y = bwin->app->resources[panel].devlocn[index].y
		+ bwin->app->resources[panel].y;

	return(BSendEvent(bwin->display, bwin, event));
}

/*
 * This will make the GUI reflect the new value being given.
 */
int
brightonParamChange(brightonWindow *bwin, int panel, int index,
brightonEvent *event)
{
	char *image = "bitmaps/images/cable.xpm";

	if (((index < 0) && (event->type != BRIGHTON_EXPOSE))
		|| (panel < 0)
		|| (bwin == NULL))
		return(-1);

	if (panel >= bwin->app->nresources)
	{
		if (bwin->flags & BRIGHTON_DEBUG)
			printf("panel count %i over %i\n", panel, bwin->app->nresources);
		return(-1);
	}

	if (index >= bwin->app->resources[panel].ndevices)
	{
		/*
		 * Drop this debug
		printf("index %i over panel count %i (transposed keyboard?)\n",
			index, bwin->app->resources[panel].ndevices);
		 */
		return(-1);
	}

/*	if (bwin->flags & BRIGHTON_BUSY) */
/*		return(-1); */

/*	printf("brightonParamChange(%x, %i): link\n", bwin, index); */

	if (event->type == BRIGHTON_LINK)
	{
		int i2, x1, y1, x2, y2, xh;

		/*
		 * The main reason to be here is to adjust the location of the place
		 * request. We do not really mind what the device is, only the two 
		 * indices, source and dest. These are taken from the resource list
		 * and scaled according to the window geometry, then placed on the
		 * device bitmaps.
		 *
		 * We have the brightonWindow which gives us all our resources, and
		 * we should know which devices we are linking together.
		 */
 		if ((i2 = event->intvalue) < 0)
			return(0);

 		x1 = bwin->app->resources[panel].devlocn[index].x
			* bwin->app->resources[panel].sw / 1000
			+ bwin->app->resources[panel].sx;
 		y1 = bwin->app->resources[panel].devlocn[index].y
			* bwin->app->resources[panel].sh / 1000
			+ bwin->app->resources[panel].sy;
 		x2 = bwin->app->resources[panel].devlocn[i2].x
			* bwin->app->resources[panel].sw / 1000
			+ bwin->app->resources[panel].sx;
 		y2 = bwin->app->resources[panel].devlocn[i2].y
			* bwin->app->resources[panel].sh / 1000
			+ bwin->app->resources[panel].sy;

		if (x2 < x1) {
			xh = x2;
			x2 = x1;
			x1 = xh;
			xh = y2;
			y2 = y1;
			y1 = xh;
			image = "bitmaps/images/cablered.xpm";
		} else
			image = "bitmaps/images/cableyellow.xpm";

		/*
		 * We should correct positioning now: the aspect - if height is greater
		 * then width then we are going to use a different bitmap and it will
		 * be slid rather than shifted, and the endpoints need to have some
		 * constants added. Plus we want to give organised coord (x1 < x2).
		 */
		if (x2 < x1) {
			int w, h, xh = x2, yh = y2;

			image = "bitmaps/images/cableyellow.xpm";

			x2 = x1;
			y2 = y1;
			x1 = xh;
			y1 = yh;

			w = x2 - x1;
			if ((h = y2 - y1) < 0)
				h = -h;

			if (w < h) {
				x1 += 1;
				y1 -= 0;
				x2 += 3;
				y2 += 8;
				image = "bitmaps/images/cableV.xpm";
			} else {
				x1 += 2;
				y1 += 0;
				x2 += 9;
				y2 -= 1;
			}
		} else {
			int w, h;

			w = x2 - x1;

			if ((h = y2 - y1) < 0)
				h = -h;

			if (w < h) {
 				if (bwin->app->resources[panel].devlocn[i2].x <
 					bwin->app->resources[panel].devlocn[index].x)
					image = "bitmaps/images/cableVred.xpm";
				else
					image = "bitmaps/images/cableVyellow.xpm";
				if (y1 < y2) {
					x1 += 2;
					y1 += 1;
					x2 += 3;
					y2 += 7;
				} else {
					x1 += 1;
					y1 += 7;
					x2 += 2;
					y2 += 1;
				}
			} else {
				if (y1 < y2) {
					x1 += 2;
					y1 += 0;
					x2 += 8;
					y2 += 2;
				} else {
					x1 += 2;
					y1 -= 0;
					x2 += 8;
					y2 -= 1;
				}
			}
		}

		return(brightonPlace(bwin, image, x1, y1, x2, y2));
	}

	if (event->type == BRIGHTON_UNLINK)
	{
		brightonRemove(bwin, event->intvalue);
		return(-1);
	}

	if ((event->command != BRIGHTON_SLOW_TIMER)
		&& (event->command != BRIGHTON_FAST_TIMER))
		event->command = BRIGHTON_PARAMCHANGE;

/*
	if (type == 0)
		param.args.f = value;
	else if (type == 1)
		param.args.m = (void *) &value;
*/

	if (panel >= bwin->app->nresources)
		return(-1);

	if (index >= bwin->app->resources[panel].ndevices)
		return(-1);

	if (bwin->app->resources[panel].devlocn[index].type == -1)
		return(-1);

	/*
	 * See if this is a panel configuration event.
	 */
	if (index == -1)
	{
		if (bwin->app->resources[panel].configure)
			bwin->app->resources[panel].configure
				(bwin, &bwin->app->resources[panel], event);

	} else
		((brightonDevice *) bwin->app->resources[panel].devlocn[index].dev)
			->configure(bwin->app->resources[panel].devlocn[index].dev, event);

	return(0);
}