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)
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
22 #include <calf/session_mgr.h>
26 #include <calf/utils.h>
28 #include <lash/lash.h>
33 using namespace calf_plugins
;
36 class lash_session_manager_base
: public session_manager_iface
39 session_client_iface
*client
;
41 lash_client_t
*lash_client
;
42 bool restoring_session
;
43 static gboolean
update_lash(void *self
) { ((session_manager_iface
*)self
)->poll(); return TRUE
; }
45 lash_session_manager_base(session_client_iface
*_client
);
47 void connect(const std::string
&name
)
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?
57 lash_session_manager_base::lash_session_manager_base(session_client_iface
*_client
)
62 restoring_session
= false;
65 void lash_session_manager_base::disconnect()
69 g_source_remove(lash_source_id
);
76 class old_lash_session_manager
: public lash_session_manager_base
78 lash_args_t
*lash_args
;
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
);
91 old_lash_session_manager::old_lash_session_manager(session_client_iface
*_client
, int &argc
, char **&argv
)
92 : lash_session_manager_base(_client
)
96 for (int i
= 1; i
< argc
; i
++)
98 if (!strncmp(argv
[i
], "--lash-project=", 14)) {
99 restoring_session
= true;
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));
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
)
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();
128 lash_args_destroy(lash_args
);
131 void old_lash_session_manager::set_jack_client_name(const std::string
&name
)
134 lash_jack_client_name(lash_client
, name
.c_str());
137 void old_lash_session_manager::poll()
140 lash_event_t
*event
= lash_get_event(lash_client
);
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
);
162 w
.client
= lash_client
;
164 send_lash(LASH_Save_Data_Set
, "");
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
);
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
);
189 r
.client
= lash_client
;
191 send_lash(LASH_Restore_Data_Set
, "");
200 g_warning("Unhandled LASH event %d (%s)", lash_event_get_type(event
), lash_event_get_string(event
));
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
);
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
);
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
);
232 g_warning("Failed to create a LASH connection");
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
);
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
;
274 size
= lash_config_read(handle
, &key_cstr
, &data
, &type
);
275 if (size
== -1 || type
!= LASH_TYPE_RAW
)
278 value
= string((const char *)data
, size
);
289 bool new_lash_session_manager::quit_cb(void *user_data
)
295 session_manager_iface
*calf_plugins::create_lash_session_mgr(session_client_iface
*_client
, int &, char **&)
297 return new new_lash_session_manager(client
);