* added gconf support
[jackpanel.git] / jackpanel / JackpanelBase.vala
blobaeaa5721b2e32d2727c9b60d88303e4d62360870
1 using Gtk;
2 using Gdk;
3 using Prolooks;
5 namespace Jackpanel {
7 public static uint32 no_xruns;
9 public static int jackpanel_xrun_callback (void *panel) {
10 no_xruns++;
11 return 0;
14 public abstract class JackpanelBase : Alignment {
15 // state
16 Configuration config;
17 protected string client_name;
18 protected jack_client_t* client;
19 protected jack_status_t status;
20 protected string time_in_frames;
21 protected string time_in_bbt;
22 protected string time_h_min_sec;
23 protected Pid jackd_pid;
25 construct {
26 config = new Configuration ();
29 protected bool restart_jack () {
30 stop_jack_client ();
31 stop_jack ();
32 return initialize_jack ();
35 protected bool start_jack_client () {
36 client = jack_client_open (client_name, jack_options_t.JackNoStartServer, ref status);
38 if (client == null) {
39 stderr.printf ("jack_client_open() failed, status = 0x%2.0x\n", status);
40 if ((status & jack_status_t.JackServerFailed) != 0) {
41 stderr.printf ("Unable to connect to JACK server\n");
43 return false;
44 } else {
45 return true;
49 protected void stop_jack_client () {
50 if (client != null) {
51 jack_deactivate (client);
52 jack_client_close (client);
53 client = null;
57 protected void stop_jack () {
58 if (jackd_pid != (GLib.Pid)0) {
59 Posix.kill ((Posix.pid_t)jackd_pid, Posix.SIGTERM);
60 jackd_pid = (GLib.Pid)0;
64 protected bool start_jack () {
65 string jackdrc_filename = Environment.get_home_dir () + Path.DIR_SEPARATOR_S + ".jackdrc";
66 string jackdrc;
67 if (FileUtils.test (jackdrc_filename, FileTest.EXISTS) &&
68 FileUtils.test (jackdrc_filename, FileTest.IS_REGULAR)) {
69 FileUtils.get_contents (jackdrc_filename, out jackdrc);
71 config.sweep ();
72 config.from_command_line (jackdrc);
74 string [] argv;
75 string cmdline = config.to_command_line();
76 stdout.printf ("Starting jack with command line: %s\n", cmdline);
77 Shell.parse_argv (cmdline, out argv);
79 for (int i = 0; i < argv.length; i++) {
80 stdout.printf ("argument: %d: %s\n", i, argv[i]);
83 bool jack_started = Process.spawn_async (null, argv, null, 0, null, out jackd_pid);
85 ChildWatch.add (jackd_pid, (pid, status) => {
86 stdout.printf ("Process %d with status %d\n", (int)pid, (int)status);
87 });
89 if (!jack_started) {
90 return false;
91 } else {
92 return true;
96 return false;
99 protected bool initialize_jack () {
101 if (!start_jack_client ()) {
102 if (start_jack ()) {
103 Thread.usleep (2000000);
105 if (!start_jack_client ()) {
106 return false;
108 } else {
109 return false;
113 if ((status & jack_status_t.JackServerStarted) != 0) {
114 stderr.printf ("JACK server started\n");
116 if ((status & jack_status_t.JackNameNotUnique) != 0) {
117 client_name = jack_get_client_name(client);
118 stderr.printf ("unique name `%s' assigned\n", client_name);
121 no_xruns = 0;
122 jack_set_xrun_callback (client, jackpanel_xrun_callback, (void *)this);
124 if (jack_activate (client) != 0) {
125 stderr.printf ("cannot activate client");
126 return false;
129 return true;
132 protected abstract void on_transport_stopped ();
133 protected abstract void on_transport_rolling ();
135 protected void show_time (PanelDisplayBase display) {
136 jack_position_t current = jack_position_t ();
137 jack_transport_state_t transport_state;
138 jack_nframes_t frame_time;
140 transport_state = jack_transport_query (client, ref current);
141 frame_time = jack_frame_time (client);
143 var time_seconds = (int)current.frame / (int)current.frame_rate;
144 time_h_min_sec = "%02d:%02d:%02d".printf (time_seconds / 3600, (time_seconds % 3600) / 60, time_seconds % 60);
145 time_in_frames = "%u".printf ((jack_nframes_t)current.frame);
147 switch (transport_state) {
148 case jack_transport_state_t.JackTransportStopped:
149 on_transport_stopped ();
150 break;
151 case jack_transport_state_t.JackTransportRolling:
152 on_transport_rolling ();
153 break;
154 case jack_transport_state_t.JackTransportStarting:
155 //stdout.printf ("state: Starting");
156 break;
157 default:
158 stderr.printf ("jack transport state: [unknown]");
159 break;
162 if ((current.valid & jack_position_bits_t.JackPositionBBT) != 0) {
163 time_in_bbt = "%3u|%u|%04u".printf (current.bar, current.beat, current.tick);
164 display.time_alt = time_in_bbt;
165 } else {
166 display.time_alt = time_in_frames;
169 display.time = time_h_min_sec;
173 } //namespace Jackpanel