waf buildable compat liblash
[ladish.git] / lash_compat / liblash / event.c
blob6ba1c65b5808003db9f0fbc4af0fa80884e16490
1 /*
2 * LASH
4 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
5 * Copyright (C) 2002 Robert Ham <rah@bash.sh>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <dbus/dbus.h>
26 #include "../../common/safety.h"
27 #include "../../common/debug.h"
29 #include "../../dbus/method.h"
31 #include "lash/event.h"
33 #include "event.h"
34 #include "client.h"
36 #include "lash/client_interface.h"
38 #define set_string_property(property, value) \
39 do { \
40 if (property) \
41 free(property); \
42 if (value) \
43 property = lash_strdup(value); \
44 else \
45 property = NULL; \
46 } while (0)
48 lash_event_t *
49 lash_event_new(void)
51 return lash_calloc(1, sizeof(lash_event_t));
54 // LASH_Client_Name = 1, /* set the client's user-visible name */
55 // LASH_Jack_Client_Name, /* tell the server what name the client is connected to jack with */
56 // LASH_Save_File, /* tell clients to save to files */
57 // LASH_Restore_File, /* tell clients to restore from files */
58 // LASH_Save_Data_Set, /* tell clients to send the server a data set */
59 // LASH_Restore_Data_Set, /* tell clients a data set will be arriving */
60 // LASH_Save, /* save the project */
61 // LASH_Quit, /* tell the server to close the connection */
63 // LASH_Server_Lost, /* the server disconnected */
64 // LASH_Project_Add, /* new project has been created */
65 // LASH_Project_Remove, /* existing project has been lost */
66 // LASH_Project_Dir, /* change project dir */
67 // LASH_Project_Name, /* change project name */
68 // LASH_Client_Add, /* a new client has been added to a project */
69 // LASH_Client_Remove, /* a client has been lost from a project */
70 // LASH_Percentage /* display a percentage of an action to the user */
72 struct _handler_ctx
74 lash_client_t *client;
75 int ev_type;
78 static void
79 _lash_id_query_handler(DBusPendingCall *pending,
80 void *data)
82 DBusMessage *msg = dbus_pending_call_steal_reply(pending);
83 DBusError err;
84 const char *name;
85 dbus_bool_t retval;
86 lash_event_t *event;
87 lash_client_t *client;
88 struct _handler_ctx *ctx;
89 const char *err_str;
91 ctx = data;
92 client = ctx->client;
94 if (!msg) {
95 lash_error("Cannot get method return from pending call");
96 goto end;
99 if (!method_return_verify(msg, &err_str)) {
100 lash_error("Server failed to return client name: %s", err_str);
101 goto end_unref_msg;
104 dbus_error_init(&err);
106 retval = dbus_message_get_args(msg, &err,
107 DBUS_TYPE_STRING, &name,
108 DBUS_TYPE_INVALID);
110 if (!retval) {
111 lash_error("Cannot get message argument: %s", err.message);
112 dbus_error_free(&err);
113 goto end_unref_msg;
116 if (name && !name[0])
117 name = NULL;
119 /* Create an event and add it to the incoming queue */
121 if (!(event = lash_event_new_with_all(ctx->ev_type,
122 name))) {
123 lash_error("Failed to allocate event");
124 goto end_unref_msg;
127 lash_client_add_event(client, event);
129 end_unref_msg:
130 dbus_message_unref(msg);
132 end:
133 dbus_pending_call_unref(pending);
136 static void
137 _lash_client_name(lash_client_t *client,
138 lash_event_t *event)
140 if (event->string) {
141 lash_info("Not sending deprecated LASH_Client_Name event");
142 } else {
143 struct _handler_ctx ctx = { client, LASH_Client_Name };
145 method_call_new_void(client->dbus_service, &ctx,
146 _lash_id_query_handler, false,
147 "org.nongnu.LASH",
148 "/",
149 "org.nongnu.LASH.Server",
150 "GetName");
154 static void
155 _lash_jack_client_name(lash_client_t *client,
156 lash_event_t *event)
158 if (event->string) {
159 lash_jack_client_name(client, (const char *) event->string);
160 } else {
161 struct _handler_ctx ctx = { client, LASH_Jack_Client_Name };
163 method_call_new_void(client->dbus_service, &ctx,
164 _lash_id_query_handler, false,
165 "org.nongnu.LASH",
166 "/",
167 "org.nongnu.LASH.Server",
168 "GetJackName");
172 static void
173 _lash_task_done(lash_client_t *client,
174 lash_event_t *event)
176 if (client->pending_task) {
177 const uint8_t x = 255;
178 method_call_new_valist(client->dbus_service, NULL,
179 method_default_handler, false,
180 "org.nongnu.LASH",
181 "/",
182 "org.nongnu.LASH.Server",
183 "Progress",
184 DBUS_TYPE_UINT64, &client->pending_task,
185 DBUS_TYPE_BYTE, &x,
186 DBUS_TYPE_INVALID);
187 client->pending_task = 0;
188 } else {
189 lash_error("No pending task to send notification about");
193 static void
194 _lash_save_data_set(lash_client_t *client,
195 lash_event_t *event)
197 if (!client->pending_task) {
198 lash_error("Server has not requested a save, not sending configs");
199 return;
202 /* This is the same code as in lash_new_save_data_set_task(), but
203 I'm too lazy to make it into a function because it's for the
204 compat API. */
206 if (!dbus_message_iter_close_container(&client->iter, &client->array_iter)) {
207 lash_error("Failed to close array container");
208 dbus_message_unref(client->unsent_configs.message);
209 goto end;
212 if (!method_send(&client->unsent_configs, false)) {
213 lash_error("Failed to send CommitDataSet method call");
214 goto end;
217 lash_debug("Sent data set message");
219 end:
220 client->pending_task = 0;
224 * Tell the server to save the project the client is attached to
226 static void
227 _lash_save(lash_client_t *client,
228 lash_event_t *event)
230 method_call_new_void(client->dbus_service, NULL,
231 method_default_handler, false,
232 "org.nongnu.LASH",
233 "/",
234 "org.nongnu.LASH.Server",
235 "SaveProject");
239 * Tell the server to close the project the client is attached to
241 static void
242 _lash_quit(lash_client_t *client,
243 lash_event_t *event)
245 method_call_new_void(client->dbus_service, NULL,
246 method_default_handler, false,
247 "org.nongnu.LASH",
248 "/",
249 "org.nongnu.LASH.Server",
250 "CloseProject");
253 static const LASHEventConstructor g_lash_event_ctors[] = {
254 NULL,
255 _lash_client_name,
256 _lash_jack_client_name,
257 NULL,
258 _lash_task_done,
259 _lash_task_done,
260 _lash_save_data_set,
261 _lash_task_done,
262 _lash_save,
263 _lash_quit,
264 NULL,
265 NULL,
266 NULL,
267 NULL,
268 NULL,
269 NULL,
270 NULL,
271 NULL
274 lash_event_t *
275 lash_event_new_with_type(enum LASH_Event_Type type)
277 if (type < 1 || type > 17) {
278 lash_error("Invalid type");
279 return NULL;
282 lash_event_t *event;
284 event = lash_calloc(1, sizeof(lash_event_t));
285 event->type = type;
286 event->ctor = g_lash_event_ctors[type];
288 return event;
291 lash_event_t *
292 lash_event_new_with_all(enum LASH_Event_Type type,
293 const char *string)
295 if (type < 1 || type > 17) {
296 lash_error("Invalid type");
297 return NULL;
300 lash_event_t *event;
302 event = lash_calloc(1, sizeof(lash_event_t));
303 event->type = type;
304 event->ctor = g_lash_event_ctors[type];
305 lash_event_set_string(event, string);
307 return event;
310 void
311 lash_event_destroy(lash_event_t *event)
313 if (event) {
314 lash_free(&event->string);
315 lash_free(&event->project);
316 free(event);
320 enum LASH_Event_Type
321 lash_event_get_type(const lash_event_t *event)
323 if (event)
324 return event->type;
326 return LASH_Event_Unknown;
329 void
330 lash_event_set_type(lash_event_t *event,
331 enum LASH_Event_Type type)
333 if (type < 1 || type > 17) {
334 lash_error("Invalid type");
335 return;
338 event->type = type;
339 event->ctor = g_lash_event_ctors[type];
342 const char *
343 lash_event_get_string(const lash_event_t *event)
345 if (event)
346 return event->string;
348 return NULL;
351 void
352 lash_event_set_string(lash_event_t *event,
353 const char *string)
355 if (event)
356 set_string_property(event->string, string);
359 const char *
360 lash_event_get_project(const lash_event_t *event)
362 if (event)
363 return event->project;
365 return NULL;
368 void
369 lash_event_set_project(lash_event_t *event,
370 const char *project)
372 if (event)
373 set_string_property(event->project, project);
376 void
377 lash_event_get_client_id(const lash_event_t *event,
378 uuid_t id)
380 if (event)
381 uuid_copy(id, event->client_id);
384 void
385 lash_event_set_client_id(lash_event_t *event,
386 uuid_t id)
388 if (event)
389 uuid_copy(event->client_id, id);
392 void
393 lash_event_set_alsa_client_id(lash_event_t *event,
394 unsigned char alsa_id)
396 return;
399 unsigned char
400 lash_event_get_alsa_client_id(const lash_event_t *event)
402 return 0;
405 void
406 lash_str_set_alsa_client_id(char *str,
407 unsigned char alsa_id)
409 return;
412 unsigned char
413 lash_str_get_alsa_client_id(const char *str)
415 return 0;
418 /* EOF */