File: gvDevice.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (114 lines) | stat: -rw-r--r-- 2,169 bytes parent folder | download | duplicates (2)
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
#include "gvDevice.h"

/***********
** DEVICE **
***********/
GVIDevice * gviNewDevice(GVDeviceID deviceID, GVHardwareType hardwareType, GVDeviceType types, int dataSize)
{
	GVIDevice * device;

	device = (GVIDevice *)gsimalloc(sizeof(GVIDevice));
	if(!device)
		return NULL;

	memset(device, 0, sizeof(GVIDevice));
	memcpy(&device->m_deviceID, &deviceID, sizeof(GVDeviceID));
	device->m_hardwareType = hardwareType;
	device->m_types = types;
	device->m_data = gsimalloc((unsigned int)dataSize);
	if(!device->m_data)
	{
		gsifree(device);
		return NULL;
	}
	memset(device->m_data, 0, (unsigned int)dataSize);

	return device;
}

void gviFreeDevice(GVIDevice * device)
{
	gsifree(device->m_data);
	gsifree(device);
}

void gviDeviceUnplugged(GVIDevice * device)
{
	// if they don't have a callback, we can't free the device without them knowing
	if(!device->m_unpluggedCallback)
		return;

	// let them know it was unplugged...
	device->m_unpluggedCallback(device);

	// ...then free the device
	device->m_methods.m_freeDevice(device);
}

/****************
** DEVICE LIST **
****************/
static int gviFindDeviceIndex(GVIDeviceList devices, GVIDevice * device)
{
	int len;
	int i;

	assert(devices);

	len = ArrayLength(devices);
	for(i = 0 ; i < len ; i++)
	{
		if(device == gviGetDevice(devices, i))
			return i;
	}

	return -1;
}

GVIDeviceList gviNewDeviceList(ArrayElementFreeFn elemFreeFn)
{
	return ArrayNew(sizeof(GVIDevice *), 2, elemFreeFn);
}

void gviFreeDeviceList(GVIDeviceList devices)
{
	assert(devices);

	ArrayFree(devices);
}

void gviAppendDeviceToList(GVIDeviceList devices, GVIDevice * device)
{
	assert(devices);

	ArrayAppend(devices, &device);
}

void gviDeleteDeviceFromList(GVIDeviceList devices, GVIDevice * device)
{
	int index;

	assert(devices);

	// find the device
	index = gviFindDeviceIndex(devices, device);
	if(index == -1)
		return;

	// delete it from the array
	ArrayDeleteAt(devices, index);
}

int gviGetNumDevices(GVIDeviceList devices)
{
	assert(devices);

	return ArrayLength(devices);
}

GVIDevice * gviGetDevice(GVIDeviceList devices, int index)
{
	assert(devices);

	return *(GVIDevice **)ArrayNth(devices, index);
}