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 #undef UNICODE
24 #include <windows.h>
25 
26 /* pm_exit is called when the program exits.
27    It calls pm_term to make sure PortMidi is properly closed.
28    If DEBUG is on, we prompt for input to avoid losing error messages.
29  */
pm_exit(void)30 static void pm_exit(void) {
31     pm_term();
32 #ifdef DEBUG
33 #define STRING_MAX 80
34     {
35         char line[STRING_MAX];
36         printf("Type ENTER...\n");
37         /* note, w/o this prompting, client console application can not see one
38            of its errors before closing. */
39         fgets(line, STRING_MAX, stdin);
40     }
41 #endif
42 }
43 
44 
45 /* pm_init is the windows-dependent initialization.*/
pm_init(void)46 void pm_init(void)
47 {
48     atexit(pm_exit);
49 #ifdef DEBUG
50     printf("registered pm_exit with atexit()\n");
51 #endif
52     pm_winmm_init();
53     /* initialize other APIs (DirectX?) here */
54 }
55 
56 
pm_term(void)57 void pm_term(void) {
58     pm_winmm_term();
59 }
60 
61 
pm_get_default_device_id(int is_input,char * key)62 static PmDeviceID pm_get_default_device_id(int is_input, char *key) {
63     HKEY hkey;
64 #define PATTERN_MAX 256
65     char pattern[PATTERN_MAX];
66     DWORD pattern_max = PATTERN_MAX;
67     DWORD dwType;
68     /* Find first input or device -- this is the default. */
69     PmDeviceID id = pmNoDevice;
70     int i, j;
71     Pm_Initialize(); /* make sure descriptors exist! */
72     for (i = 0; i < pm_descriptor_index; i++) {
73         if (descriptors[i].pub.input == is_input) {
74             id = i;
75             break;
76         }
77     }
78     /* Look in registry for a default device name pattern. */
79     if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey) !=
80         ERROR_SUCCESS) {
81         return id;
82     }
83     if (RegOpenKeyExA(hkey, "JavaSoft", 0, KEY_READ, &hkey) !=
84         ERROR_SUCCESS) {
85         return id;
86     }
87     if (RegOpenKeyExA(hkey, "Prefs", 0, KEY_READ, &hkey) !=
88         ERROR_SUCCESS) {
89         return id;
90     }
91     if (RegOpenKeyExA(hkey, "/Port/Midi", 0, KEY_READ, &hkey) !=
92         ERROR_SUCCESS) {
93         return id;
94     }
95     if (RegQueryValueExA(hkey, key, NULL, &dwType, (BYTE *)pattern, &pattern_max) !=
96 	ERROR_SUCCESS) {
97         return id;
98     }
99 
100     /* decode pattern: upper case encoded with "/" prefix */
101     i = j = 0;
102     while (pattern[i]) {
103         if (pattern[i] == '/' && pattern[i + 1]) {
104             pattern[j++] = toupper(pattern[++i]);
105 	} else {
106             pattern[j++] = tolower(pattern[i]);
107 	}
108         i++;
109     }
110     pattern[j] = 0; /* end of string */
111 
112     /* now pattern is the string from the registry; search for match */
113     i = pm_find_default_device(pattern, is_input);
114     if (i != pmNoDevice) {
115         id = i;
116     }
117     return id;
118 }
119 
120 
Pm_GetDefaultInputDeviceID()121 PmDeviceID Pm_GetDefaultInputDeviceID() {
122     return pm_get_default_device_id(TRUE,
123            (char *)"/P/M_/R/E/C/O/M/M/E/N/D/E/D_/I/N/P/U/T_/D/E/V/I/C/E");
124 }
125 
126 
Pm_GetDefaultOutputDeviceID()127 PmDeviceID Pm_GetDefaultOutputDeviceID() {
128   return pm_get_default_device_id(FALSE,
129           (char *)"/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");
130 }
131 
132 
133 #include "stdio.h"
134 
pm_alloc(size_t s)135 void *pm_alloc(size_t s) {
136     return malloc(s);
137 }
138 
139 
pm_free(void * ptr)140 void pm_free(void *ptr) {
141     free(ptr);
142 }
143 
144 
145