Ticket #4463: devlist.c

File devlist.c, 2.9 KB (added by Pete, 13 years ago)
Line 
1/*
2 * midi usb driver
3 * devlist.c
4 *
5 * Copyright 2006-2011 Haiku Inc. All rights reserved.
6 * Distributed under the terms of the MIT Licence.
7 *
8 * Authors:
9 * Jérôme Duval
10 * Pete Goodeve, pete.goodeve@computer.org
11 *
12 * Some portions of this code were originally derived from
13 * USB Joystick driver for BeOS R5
14 * Copyright 2000 (C) ITO, Takayuki
15 * All rights reserved
16 *
17 */
18
19
20#include "usb_midi.h"
21
22#include <stdlib.h>
23#include <string.h>
24
25
26sem_id usbmidi_port_list_lock = -1;
27bool usbmidi_port_list_changed = true; /* added or removed */
28
29static usbmidi_port_info* usbmidi_port_list = NULL;
30static int usbmidi_port_count = 0;
31
32
33void
34add_port_info(usbmidi_port_info* port)
35{
36 assert(port != NULL);
37 acquire_sem(usbmidi_port_list_lock);
38 port->next = usbmidi_port_list;
39 usbmidi_port_list = port;
40 usbmidi_port_count++;
41 usbmidi_port_list_changed = true;
42 release_sem(usbmidi_port_list_lock);
43}
44
45
46void
47remove_port_info(usbmidi_port_info* port)
48{
49 assert(port != NULL);
50 acquire_sem(usbmidi_port_list_lock);
51 if (usbmidi_port_list == port) {
52 usbmidi_port_list = port->next;
53 --usbmidi_port_count;
54 usbmidi_port_list_changed = true;
55 } else {
56 usbmidi_port_info* d;
57 for (d = usbmidi_port_list; d != NULL; d = d->next) {
58 if (d->next == port) {
59 d->next = port->next;
60 --usbmidi_port_count;
61 usbmidi_port_list_changed = true;
62 break;
63 }
64 }
65 assert(d != NULL);
66 }
67 release_sem(usbmidi_port_list_lock);
68}
69
70
71usbmidi_port_info*
72search_port_info(const char* name)
73{
74 usbmidi_port_info* port;
75
76 acquire_sem(usbmidi_port_list_lock);
77 for (port = usbmidi_port_list; port != NULL; port = port->next) {
78 if (strcmp(port->name, name) == 0)
79 break;
80 }
81 release_sem(usbmidi_port_list_lock);
82 return port;
83}
84
85int
86find_free_device_number(void)
87{
88 usbmidi_port_info* port;
89 int number = 0;
90
91 acquire_sem(usbmidi_port_list_lock);
92 do {
93 for (port = usbmidi_port_list; port != NULL; port = port->next) {
94 if (port->device->devnum == number) {
95 number++;
96 break; /* try next higher */
97 }
98 }
99 } while (port);
100 release_sem(usbmidi_port_list_lock);
101 return number;
102}
103
104
105
106/*
107 device names
108*/
109
110/* dynamically generated */
111char** usbmidi_port_names = NULL;
112
113
114void
115alloc_port_names(void)
116{
117 assert(usbmidi_port_names == NULL);
118 usbmidi_port_names = malloc(sizeof(char*) * (usbmidi_port_count + 1));
119}
120
121
122void
123free_port_names(void)
124{
125 if (usbmidi_port_names != NULL) {
126 int i;
127 for (i = 0; usbmidi_port_names[i] != NULL; i++)
128 free(usbmidi_port_names[i]);
129 free(usbmidi_port_names);
130 usbmidi_port_names = NULL;
131 }
132}
133
134
135void
136rebuild_port_names(void)
137{
138 int i;
139 usbmidi_port_info* port;
140
141 assert(usbmidi_port_names != NULL);
142 acquire_sem(usbmidi_port_list_lock);
143 for (i = 0, port = usbmidi_port_list; port != NULL; port = port->next) {
144 usbmidi_port_names[i++] = strdup(port->name);
145 DPRINTF_INFO((MY_ID "publishing %s\n", port->name));
146 }
147 usbmidi_port_names[i] = NULL;
148 release_sem(usbmidi_port_list_lock);
149}