Comment remaining unsolved bug.
[ardour.git] / libs / backends / wavesaudio / portmidi / src / pm_win / pmwin.c
1 /* pmwin.c -- PortMidi os-dependent code */
2
3 /* This file only needs to implement:
4        pm_init(), which calls various routines to register the
5            available midi devices,
6        Pm_GetDefaultInputDeviceID(), and
7        Pm_GetDefaultOutputDeviceID().
8    This file must
9    be separate from the main portmidi.c file because it is system
10    dependent, and it is separate from, say, pmwinmm.c, because it
11    might need to register devices for winmm, directx, and others.
12
13  */
14
15 #include "stdlib.h"
16 #include "portmidi.h"
17 #include "pmutil.h"
18 #include "pminternal.h"
19 #include "pmwinmm.h"
20 #ifdef DEBUG
21 #include "stdio.h"
22 #endif
23 #include <windows.h>
24
25 /* pm_exit is called when the program exits.
26    It calls pm_term to make sure PortMidi is properly closed.
27    If DEBUG is on, we prompt for input to avoid losing error messages.
28  */
29 static void pm_exit(void) {
30     pm_term();
31 #ifdef DEBUG
32 #define STRING_MAX 80
33     {
34         char line[STRING_MAX];
35         printf("Type ENTER...\n");
36         /* note, w/o this prompting, client console application can not see one
37            of its errors before closing. */
38         fgets(line, STRING_MAX, stdin);
39     }
40 #endif
41 }
42
43
44 /* pm_init is the windows-dependent initialization.*/
45 void pm_init(void)
46 {
47     atexit(pm_exit);
48 #ifdef DEBUG
49     printf("registered pm_exit with atexit()\n");
50 #endif
51     pm_winmm_init();
52     /* initialize other APIs (DirectX?) here */
53 }
54
55
56 void pm_term(void) {
57     pm_winmm_term();
58 }
59
60
61 static PmDeviceID pm_get_default_device_id(int is_input, char *key) {
62     HKEY hkey;
63 #define PATTERN_MAX 256
64     char pattern[PATTERN_MAX];
65     long pattern_max = PATTERN_MAX;
66     DWORD dwType;
67     /* Find first input or device -- this is the default. */
68     PmDeviceID id = pmNoDevice;
69     int i, j;
70     Pm_Initialize(); /* make sure descriptors exist! */
71     for (i = 0; i < pm_descriptor_index; i++) {
72         if (descriptors[i].pub.input == is_input) {
73             id = i;
74             break;
75         }
76     }
77     /* Look in registry for a default device name pattern. */
78     if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey) !=
79         ERROR_SUCCESS) {
80         return id;
81     }
82     if (RegOpenKeyEx(hkey, "JavaSoft", 0, KEY_READ, &hkey) !=
83         ERROR_SUCCESS) {
84         return id;
85     }
86     if (RegOpenKeyEx(hkey, "Prefs", 0, KEY_READ, &hkey) !=
87         ERROR_SUCCESS) {
88         return id;
89     }
90     if (RegOpenKeyEx(hkey, "/Port/Midi", 0, KEY_READ, &hkey) !=
91         ERROR_SUCCESS) {
92         return id;
93     }
94     if (RegQueryValueEx(hkey, key, NULL, &dwType, (LPBYTE)pattern, (LPDWORD)&pattern_max) !=
95         ERROR_SUCCESS) {
96         return id;
97     }
98
99     /* decode pattern: upper case encoded with "/" prefix */
100     i = j = 0;
101     while (pattern[i]) {
102         if (pattern[i] == '/' && pattern[i + 1]) {
103             pattern[j++] = toupper(pattern[++i]);
104         } else {
105             pattern[j++] = tolower(pattern[i]);
106         }
107         i++;
108     }
109     pattern[j] = 0; /* end of string */
110
111     /* now pattern is the string from the registry; search for match */
112     i = pm_find_default_device(pattern, is_input);
113     if (i != pmNoDevice) {
114         id = i;
115     }
116     return id;
117 }
118
119
120 PmDeviceID Pm_GetDefaultInputDeviceID() {
121     return pm_get_default_device_id(TRUE,
122            "/P/M_/R/E/C/O/M/M/E/N/D/E/D_/I/N/P/U/T_/D/E/V/I/C/E");
123 }
124
125
126 PmDeviceID Pm_GetDefaultOutputDeviceID() {
127   return pm_get_default_device_id(FALSE,
128           "/P/M_/R/E/C/O/M/M/E/N/D/E/D_/O/U/T/P/U/T_/D/E/V/I/C/E");
129 }
130
131
132 #include "stdio.h"
133
134 void *pm_alloc(size_t s) {
135     return malloc(s);
136 }
137
138
139 void pm_free(void *ptr) {
140     free(ptr);
141 }
142