* stderr.printf -> debug etc
[jackpanel.git] / jackpanel / Configuration.vala
blobd82d932c907dad66141a10b373f0635cb89cc1b3
1 using GLib;
3 namespace Jackpanel {
5 public class Configuration {
6 GConf.Client gconf;
8 private static string jackpanel_root = "/apps/jackpanel";
9 public static string root () { return jackpanel_root; }
10 public void set_test_mode () { jackpanel_root = "/apps/jackpanel-test"; }
12 public Configuration () {
13 gconf = GConf.Client.get_default ();
16 public void init () {
17 config_root = "default";
18 server_command = "";
19 sample_rate = -1;
20 buffer_size = -1;
21 periods = -1;
22 realtime_priority = -1;
25 public bool exists () {
26 return gconf.dir_exists (root ()) && server_command != "";
29 public static bool nonempty (string str) { return str != null && str != "";}
31 public void sweep () {
32 if (exists ()) {
33 gconf.recursive_unset (root (), GConf.UnsetFlags.NAMES);
34 init ();
38 private string _config_root;
39 public string config_root {
40 get { return _config_root; }
41 set {
42 _config_root = root () + "/configs/";
43 if ("/" in value) {
44 var parts = value.split ("/");
45 active_config = parts[parts.length - 1];
46 } else {
47 active_config = value;
49 _config_root += active_config;
53 void append_if_nonnegative (StringBuilder b, string option, int val) {
54 if (val >= 0) b.append (" %s %d".printf (option, val));
57 void append_if_nonempty (StringBuilder b, string option, string? val) {
58 if (val != null && val != "") b.append (" %s %s".printf (option, val));
61 public string to_command_line () {
62 var b = new StringBuilder ();
63 b.append (server_command);
64 if (realtime) b.append (" --realtime");
65 append_if_nonempty (b, "", jack_options);
67 append_if_nonnegative (b, "--realtime-priority", realtime_priority);
69 append_if_nonempty (b, "--driver", driver);
70 if (driver == "alsa") {
71 append_if_nonnegative (b, "--rate", sample_rate);
72 append_if_nonnegative (b, "--period", buffer_size);
73 append_if_nonnegative (b, "--nperiods", periods);
74 append_if_nonempty (b, "--capture", input_device);
75 append_if_nonempty (b, "--playback", output_device);
76 append_if_nonempty (b, "--midi", midi_driver);
77 if (soft_mode) b.append (" --softmode");
78 if (16bit) b.append (" --shorts");
79 if (hw_monitoring) b.append (" --hwmon");
80 if (monitor_ports) b.append (" --monitor");
83 return b.str;
86 private delegate void SetterDelegate (string str);
88 private void set_for_prefixes (string[] prefixes, string part, SetterDelegate set_value) {
89 foreach (string prefix in prefixes) {
90 if (part.has_prefix (prefix)) {
91 set_value (part.replace (prefix, ""));
96 private bool after_driver;
97 public void from_command_line (string commandline) {
98 debug ("got commandline: %s\n", commandline);
99 Regex args_with_spaces = new Regex ("(--?[A-Za-z]+)[ \t]+([^- \t]+)");
100 string normalized = commandline.strip();
101 normalized = args_with_spaces.replace (normalized, -1, 0, "\\1\\2");
102 debug ("commandline (normalized): %s\n", normalized);
104 after_driver = false;
105 string[] parts = normalized.split (" ");
106 debug ("config-root: %s, number of parts: %d\n", config_root, parts.length);
107 server_command = parts[0];
108 foreach (string part in parts) {
109 debug ("analyzing part: %s\n", part);
110 if (part == "-R" || part == "--realtime") realtime = true;
111 if (!after_driver) {
112 set_for_prefixes ({"-P", "--realtime-priority"}, part, (str) => { realtime_priority = str.to_int (); });
113 set_for_prefixes ({"-d", "--driver"}, part, (str) => { driver = str; after_driver = true; });
115 if (after_driver && driver == "alsa") {
116 set_for_prefixes ({"-r", "--rate"}, part, (str) => { sample_rate = str.to_int (); });
117 set_for_prefixes ({"-p", "--period"}, part, (str) => { buffer_size = str.to_int (); });
118 set_for_prefixes ({"-n", "--nperiods"}, part, (str) => { periods = str.to_int (); });
119 set_for_prefixes ({"-C", "--capture"}, part, (str) => { input_device = str; });
120 set_for_prefixes ({"-P", "--playback"}, part, (str) => { output_device = str; });
121 set_for_prefixes ({"-X", "--midi"}, part, (str) => { midi_driver = str; });
122 if (part == "-s" || part == "--softmode") soft_mode = true;
123 if (part == "-S" || part == "--shorts") 16bit = true;
124 if (part == "-H" || part == "--hwmon") hw_monitoring = true;
125 if (part == "-m" || part == "--monitor") monitor_ports = true;
130 protected string active_config {
131 get { return gconf.get_string (root () + "/active_config"); }
132 set { gconf.set_string (root () + "/active_config", value); }
135 public string server_command {
136 get { return gconf.get_string (config_root + "/server_command"); }
137 set { gconf.set_string (config_root + "/server_command", value); }
140 public bool realtime {
141 get { return gconf.get_bool (config_root + "/realtime"); }
142 set { gconf.set_bool (config_root + "/realtime", value); }
145 public int realtime_priority {
146 get { return gconf.get_int (config_root + "/realtime_priority"); }
147 set { gconf.set_int (config_root + "/realtime_priority", value); }
150 public string driver {
151 get { return gconf.get_string (config_root + "/driver"); }
152 set { gconf.set_string (config_root + "/driver", value); }
155 public string midi_driver {
156 get { return gconf.get_string (config_root + "/midi_driver"); }
157 set { gconf.set_string (config_root + "/midi_driver", value); }
160 public int sample_rate {
161 get { return gconf.get_int (config_root + "/sample_rate"); }
162 set { gconf.set_int (config_root + "/sample_rate", value); }
165 public int buffer_size {
166 get { return gconf.get_int (config_root + "/buffer_size"); }
167 set { gconf.set_int (config_root + "/buffer_size", value); }
170 public int periods {
171 get { return gconf.get_int (config_root + "/periods"); }
172 set { gconf.set_int (config_root + "/periods", value); }
175 public bool soft_mode {
176 get { return gconf.get_bool (config_root + "/soft_mode"); }
177 set { gconf.set_bool (config_root + "/soft_mode", value); }
180 public bool 16bit {
181 get { return gconf.get_bool (config_root + "/16bit"); }
182 set { gconf.set_bool (config_root + "/16bit", value); }
185 public bool hw_monitoring {
186 get { return gconf.get_bool (config_root + "/hw_monitoring"); }
187 set { gconf.set_bool (config_root + "/hw_monitoring", value); }
190 public bool monitor_ports {
191 get { return gconf.get_bool (config_root + "/monitor_ports"); }
192 set { gconf.set_bool (config_root + "/monitor_ports", value); }
195 public string input_device {
196 get { return gconf.get_string (config_root + "/input_device"); }
197 set { gconf.set_string (config_root + "/input_device", value); }
200 public string output_device {
201 get { return gconf.get_string (config_root + "/output_device"); }
202 set { gconf.set_string (config_root + "/output_device", value); }
205 public string jack_options {
206 get { return gconf.get_string (config_root + "/jack_options"); }
207 set { gconf.set_string (config_root + "/jack_options", value); }
210 public string driver_options {
211 get { return gconf.get_string (config_root + "/driver_options"); }
212 set { gconf.set_string (config_root + "/driver_options", value); }
217 } //namespace Jackpanel