new audio engine backend for native CoreAudio audio I/O, and PortMIDI for MIDI.
[ardour.git] / libs / backends / wavesaudio / portmidi / src / pm_mac / finddefault.c
1 /* finddefault.c -- find_default_device() implementation
2    Roger Dannenberg, June 2008
3 */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "portmidi.h"
8 #include "pmutil.h"
9 #include "pminternal.h"
10 #include "pmmacosxcm.h"
11 #include "readbinaryplist.h"
12
13 /* Parse preference files, find default device, search devices --
14    This parses the preference file(s) once for input and once for
15    output, which is inefficient but much simpler to manage. Note
16    that using the readbinaryplist.c module, you cannot keep two
17    plist files (user and system) open at once (due to a simple
18    memory management scheme).
19 */
20 PmDeviceID find_default_device(char *path, int input, PmDeviceID id)
21 /* path -- the name of the preference we are searching for
22    input -- true iff this is an input device
23    id -- current default device id
24    returns matching device id if found, otherwise id
25 */
26 {
27     static char *pref_file = "com.apple.java.util.prefs.plist";
28     char *pref_str = NULL;
29     // read device preferences
30     value_ptr prefs = bplist_read_user_pref(pref_file);
31     if (prefs) {
32         value_ptr pref_val = value_dict_lookup_using_path(prefs, path);
33         if (pref_val) {
34             pref_str = value_get_asciistring(pref_val);
35         }
36     }
37     if (!pref_str) {
38         bplist_free_data(); /* look elsewhere */
39         prefs = bplist_read_system_pref(pref_file);
40         if (prefs) {
41             value_ptr pref_val = value_dict_lookup_using_path(prefs, path);
42             if (pref_val) {
43                 pref_str = value_get_asciistring(pref_val);
44             }
45         }
46     }
47     if (pref_str) { /* search devices for match */
48         int i = pm_find_default_device(pref_str, input);
49         if (i != pmNoDevice) {
50             id = i;
51         }
52     }
53     if (prefs) {
54         bplist_free_data();
55     }
56     return id;
57 }