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.
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"
36 #include "lash/client_interface.h"
38 #define set_string_property(property, value) \
43 property = lash_strdup(value); \
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 */
74 lash_client_t
*client
;
79 _lash_id_query_handler(DBusPendingCall
*pending
,
82 DBusMessage
*msg
= dbus_pending_call_steal_reply(pending
);
87 lash_client_t
*client
;
88 struct _handler_ctx
*ctx
;
95 lash_error("Cannot get method return from pending call");
99 if (!method_return_verify(msg
, &err_str
)) {
100 lash_error("Server failed to return client name: %s", err_str
);
104 dbus_error_init(&err
);
106 retval
= dbus_message_get_args(msg
, &err
,
107 DBUS_TYPE_STRING
, &name
,
111 lash_error("Cannot get message argument: %s", err
.message
);
112 dbus_error_free(&err
);
116 if (name
&& !name
[0])
119 /* Create an event and add it to the incoming queue */
121 if (!(event
= lash_event_new_with_all(ctx
->ev_type
,
123 lash_error("Failed to allocate event");
127 lash_client_add_event(client
, event
);
130 dbus_message_unref(msg
);
133 dbus_pending_call_unref(pending
);
137 _lash_client_name(lash_client_t
*client
,
141 lash_info("Not sending deprecated LASH_Client_Name event");
143 struct _handler_ctx ctx
= { client
, LASH_Client_Name
};
145 method_call_new_void(client
->dbus_service
, &ctx
,
146 _lash_id_query_handler
, false,
149 "org.nongnu.LASH.Server",
155 _lash_jack_client_name(lash_client_t
*client
,
159 lash_jack_client_name(client
, (const char *) event
->string
);
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,
167 "org.nongnu.LASH.Server",
173 _lash_task_done(lash_client_t
*client
,
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,
182 "org.nongnu.LASH.Server",
184 DBUS_TYPE_UINT64
, &client
->pending_task
,
187 client
->pending_task
= 0;
189 lash_error("No pending task to send notification about");
194 _lash_save_data_set(lash_client_t
*client
,
197 if (!client
->pending_task
) {
198 lash_error("Server has not requested a save, not sending configs");
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
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
);
212 if (!method_send(&client
->unsent_configs
, false)) {
213 lash_error("Failed to send CommitDataSet method call");
217 lash_debug("Sent data set message");
220 client
->pending_task
= 0;
224 * Tell the server to save the project the client is attached to
227 _lash_save(lash_client_t
*client
,
230 method_call_new_void(client
->dbus_service
, NULL
,
231 method_default_handler
, false,
234 "org.nongnu.LASH.Server",
239 * Tell the server to close the project the client is attached to
242 _lash_quit(lash_client_t
*client
,
245 method_call_new_void(client
->dbus_service
, NULL
,
246 method_default_handler
, false,
249 "org.nongnu.LASH.Server",
253 static const LASHEventConstructor g_lash_event_ctors
[] = {
256 _lash_jack_client_name
,
275 lash_event_new_with_type(enum LASH_Event_Type type
)
277 if (type
< 1 || type
> 17) {
278 lash_error("Invalid type");
284 event
= lash_calloc(1, sizeof(lash_event_t
));
286 event
->ctor
= g_lash_event_ctors
[type
];
292 lash_event_new_with_all(enum LASH_Event_Type type
,
295 if (type
< 1 || type
> 17) {
296 lash_error("Invalid type");
302 event
= lash_calloc(1, sizeof(lash_event_t
));
304 event
->ctor
= g_lash_event_ctors
[type
];
305 lash_event_set_string(event
, string
);
311 lash_event_destroy(lash_event_t
*event
)
314 lash_free(&event
->string
);
315 lash_free(&event
->project
);
321 lash_event_get_type(const lash_event_t
*event
)
326 return LASH_Event_Unknown
;
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");
339 event
->ctor
= g_lash_event_ctors
[type
];
343 lash_event_get_string(const lash_event_t
*event
)
346 return event
->string
;
352 lash_event_set_string(lash_event_t
*event
,
356 set_string_property(event
->string
, string
);
360 lash_event_get_project(const lash_event_t
*event
)
363 return event
->project
;
369 lash_event_set_project(lash_event_t
*event
,
373 set_string_property(event
->project
, project
);
377 lash_event_get_client_id(const lash_event_t
*event
,
381 uuid_copy(id
, event
->client_id
);
385 lash_event_set_client_id(lash_event_t
*event
,
389 uuid_copy(event
->client_id
, id
);
393 lash_event_set_alsa_client_id(lash_event_t
*event
,
394 unsigned char alsa_id
)
400 lash_event_get_alsa_client_id(const lash_event_t
*event
)
406 lash_str_set_alsa_client_id(char *str
,
407 unsigned char alsa_id
)
413 lash_str_get_alsa_client_id(const char *str
)