1/*
2 * Copyright 2004-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Jérôme Duval
7 *
8 * Some portions of code are copyrighted by
9 * USB Joystick driver for BeOS R5
10 * Copyright 2000 (C) ITO, Takayuki. All rights reserved
11 */
12
13
14#include "driver.h"
15
16#include <stdlib.h>
17#include <string.h>
18
19
20sem_id gDeviceListLock = -1;
21bool gDeviceListChanged = true;	/* added or removed */
22/* dynamically generated */
23char **gDeviceNames = NULL;
24
25
26static pegasus_dev *sDeviceList = NULL;
27static int sDeviceCount = 0;
28
29
30void
31add_device_info(pegasus_dev *device)
32{
33	ASSERT(device != NULL);
34
35	acquire_sem(gDeviceListLock);
36	device->next = sDeviceList;
37	sDeviceList = device;
38	sDeviceCount++;
39	gDeviceListChanged = true;
40	release_sem(gDeviceListLock);
41}
42
43
44void
45remove_device_info(pegasus_dev *device)
46{
47	ASSERT(device != NULL);
48
49	acquire_sem(gDeviceListLock);
50
51	if (sDeviceList == device) {
52		sDeviceList = device->next;
53		--sDeviceCount;
54		gDeviceListChanged = true;
55	} else {
56		pegasus_dev *previous;
57		for (previous = sDeviceList; previous != NULL; previous = previous->next) {
58			if (previous->next == device) {
59				previous->next = device->next;
60				--sDeviceCount;
61				gDeviceListChanged = true;
62				break;
63			}
64		}
65		ASSERT(previous != NULL);
66	}
67	release_sem(gDeviceListLock);
68}
69
70
71pegasus_dev *
72search_device_info(const char* name)
73{
74	pegasus_dev *device;
75
76	acquire_sem(gDeviceListLock);
77	for (device = sDeviceList; device != NULL; device = device->next) {
78		if (strcmp(device->name, name) == 0)
79			break;
80	}
81
82	release_sem(gDeviceListLock);
83	return device;
84}
85
86
87//	#pragma mark - device names
88
89
90void
91alloc_device_names(void)
92{
93	ASSERT(gDeviceNames == NULL);
94	gDeviceNames = malloc(sizeof(char *) * (sDeviceCount + 1));
95}
96
97
98void
99free_device_names(void)
100{
101	if (gDeviceNames != NULL) {
102		int i;
103		for (i = 0; gDeviceNames [i] != NULL; i++) {
104			free(gDeviceNames[i]);
105		}
106
107		free(gDeviceNames);
108		gDeviceNames = NULL;
109	}
110}
111
112
113void
114rebuild_device_names(void)
115{
116	int i;
117	pegasus_dev *device;
118
119	ASSERT(gDeviceNames != NULL);
120	acquire_sem(gDeviceListLock);
121	for (i = 0, device = sDeviceList; device != NULL; device = device->next) {
122		gDeviceNames[i++] = strdup(device->name);
123		DPRINTF_INFO(MY_ID "publishing %s\n", device->name);
124	}
125	gDeviceNames[i] = NULL;
126	release_sem(gDeviceListLock);
127}
128
129