Transient Designer: image and text for manual, small change to the UI
[calf.git] / src / session_mgr.cpp
blob46d6d93f6f25ad58cabbf9de0d79a9b6f92ca5c1
1 /* Calf DSP Library Utility Application - calfjackhost
2 * Session manager API implementation for LASH 0.5.4 and 0.6.0
4 * Copyright (C) 2010 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
21 #include "config.h"
22 #include <calf/session_mgr.h>
24 #if USE_LASH
26 #include <calf/utils.h>
27 #include <gtk/gtk.h>
28 #include <lash/lash.h>
29 #include <glib.h>
30 #include <string.h>
32 using namespace std;
33 using namespace calf_plugins;
36 class lash_session_manager_base: public session_manager_iface
38 protected:
39 session_client_iface *client;
40 int lash_source_id;
41 lash_client_t *lash_client;
42 bool restoring_session;
43 static gboolean update_lash(void *self) { ((session_manager_iface *)self)->poll(); return TRUE; }
44 public:
45 lash_session_manager_base(session_client_iface *_client);
47 void connect(const std::string &name)
49 if (lash_client)
51 lash_source_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 250, update_lash, (session_manager_iface *)this, NULL); // 4 LASH reads per second... should be enough?
54 void disconnect();
57 lash_session_manager_base::lash_session_manager_base(session_client_iface *_client)
59 client = _client;
60 lash_source_id = 0;
61 lash_client = NULL;
62 restoring_session = false;
65 void lash_session_manager_base::disconnect()
67 if (lash_source_id)
69 g_source_remove(lash_source_id);
70 lash_source_id = 0;
74 # if !USE_LASH_0_6
76 class old_lash_session_manager: public lash_session_manager_base
78 lash_args_t *lash_args;
80 public:
81 old_lash_session_manager(session_client_iface *_client, int &argc, char **&argv);
82 void send_lash(LASH_Event_Type type, const std::string &data);
83 virtual void connect(const std::string &name);
84 virtual void disconnect();
85 virtual bool is_being_restored() { return restoring_session; }
86 virtual void set_jack_client_name(const std::string &name);
87 virtual void poll();
91 old_lash_session_manager::old_lash_session_manager(session_client_iface *_client, int &argc, char **&argv)
92 : lash_session_manager_base(_client)
94 lash_args = NULL;
96 for (int i = 1; i < argc; i++)
98 if (!strncmp(argv[i], "--lash-project=", 14)) {
99 restoring_session = true;
100 break;
103 lash_args = lash_extract_args(&argc, &argv);
104 lash_client = lash_init(lash_args, PACKAGE_NAME, LASH_Config_Data_Set, LASH_PROTOCOL(2, 0));
105 if (!lash_client) {
106 g_warning("Failed to create a LASH connection");
110 void old_lash_session_manager::send_lash(LASH_Event_Type type, const std::string &data)
112 lash_send_event(lash_client, lash_event_new_with_all(type, data.c_str()));
115 void old_lash_session_manager::connect(const std::string &name)
117 if (lash_client)
119 send_lash(LASH_Client_Name, name);
121 lash_session_manager_base::connect(name);
124 void old_lash_session_manager::disconnect()
126 lash_session_manager_base::disconnect();
127 if (lash_args)
128 lash_args_destroy(lash_args);
131 void old_lash_session_manager::set_jack_client_name(const std::string &name)
133 if (lash_client)
134 lash_jack_client_name(lash_client, name.c_str());
137 void old_lash_session_manager::poll()
139 do {
140 lash_event_t *event = lash_get_event(lash_client);
141 if (!event)
142 break;
144 // printf("type = %d\n", lash_event_get_type(event));
146 switch(lash_event_get_type(event)) {
147 case LASH_Save_Data_Set:
149 struct writer: public session_save_iface
151 lash_client_t *client;
153 void write_next_item(const std::string &key, const std::string &value)
155 lash_config_t *cfg = lash_config_new_with_key(key.c_str());
156 lash_config_set_value(cfg, value.c_str(), value.length());
157 lash_send_config(client, cfg);
161 writer w;
162 w.client = lash_client;
163 client->save(&w);
164 send_lash(LASH_Save_Data_Set, "");
165 break;
168 case LASH_Restore_Data_Set:
170 struct reader: public session_load_iface
172 lash_client_t *client;
174 virtual bool get_next_item(std::string &key, std::string &value) {
175 lash_config_t *cfg = lash_get_config(client);
177 if(cfg) {
178 key = lash_config_get_key(cfg);
179 // printf("key = %s\n", lash_config_get_key(cfg));
180 value = string((const char *)lash_config_get_value(cfg), lash_config_get_value_size(cfg));
181 lash_config_destroy(cfg);
182 return true;
184 return false;
188 reader r;
189 r.client = lash_client;
190 client->load(&r);
191 send_lash(LASH_Restore_Data_Set, "");
192 break;
195 case LASH_Quit:
196 gtk_main_quit();
197 break;
199 default:
200 g_warning("Unhandled LASH event %d (%s)", lash_event_get_type(event), lash_event_get_string(event));
201 break;
203 } while(1);
206 session_manager_iface *calf_plugins::create_lash_session_mgr(session_client_iface *client, int &argc, char **&argv)
208 return new old_lash_session_manager(client, argc, argv);
211 # else
213 class new_lash_session_manager: public lash_session_manager_base
215 lash_args_t *lash_args;
217 static bool save_data_set_cb(lash_config_handle_t *handle, void *user_data);
218 static bool load_data_set_cb(lash_config_handle_t *handle, void *user_data);
219 static bool quit_cb(void *user_data);
221 public:
222 new_lash_session_manager(session_client_iface *_client)
224 virtual void set_jack_client_name(const std::string &) {}
227 new_lash_session_manager::new_lash_session_manager(session_client_iface *_client)
228 : lash_session_manager_base(_client)
230 lash_client = lash_client_open(PACKAGE_NAME, LASH_Config_Data_Set, argc, argv);
231 if (!lash_client) {
232 g_warning("Failed to create a LASH connection");
233 return;
235 restoring_session = lash_client_is_being_restored(lash_client);
236 lash_set_save_data_set_callback(lash_client, save_data_set_cb, this);
237 lash_set_load_data_set_callback(lash_client, load_data_set_cb, this);
238 lash_set_quit_callback(lash_client, quit_cb, NULL);
241 void new_lash_session_manager::poll()
243 lash_dispatch(lash_client);
246 bool new_lash_session_manager::save_data_set_cb(lash_config_handle_t *handle, void *user_data)
248 struct writer: public session_save_iface
250 lash_config_handle_t handle;
252 virtual bool write_next_item(const std::string &key, const std::string &value) {
253 lash_config_write_raw(handle, key.c_str(), (const void *)value.data(), value.length(), LASH_TYPE_RAW);
254 return true;
258 writer w;
259 w.handle = handle;
260 client->save(&w);
261 return true;
264 bool new_lash_session_manager::load_data_set_cb(lash_config_handle_t *handle, void *user_data)
266 struct reader: public session_load_iface
268 lash_config_handle_t handle;
270 virtual bool get_next_item(std::string &key, std::string &value) {
271 const char *key_cstr;
272 int type;
273 void *data;
274 size = lash_config_read(handle, &key_cstr, &data, &type);
275 if (size == -1 || type != LASH_TYPE_RAW)
276 return false;
277 key = key_cstr;
278 value = string((const char *)data, size);
279 return true;
283 reader r;
284 r.handle = handle;
285 client->load(&r);
286 return true;
289 bool new_lash_session_manager::quit_cb(void *user_data)
291 gtk_main_quit();
292 return true;
295 session_manager_iface *calf_plugins::create_lash_session_mgr(session_client_iface *_client, int &, char **&)
297 return new new_lash_session_manager(client);
300 # endif
302 #endif