jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

573 lines
17KB

  1. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  2. /*
  3. Copyright (C) 2011 Nedko Arnaudov
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <dbus/dbus.h>
  23. #include "jackdbus.h"
  24. #include "controller_internal.h"
  25. #include "jack/session.h"
  26. #include "jack/control.h"
  27. #define JACK_DBUS_IFACE_NAME "org.jackaudio.SessionManager"
  28. static
  29. void
  30. jack_controller_control_send_signal_session_state_changed(
  31. jack_session_event_type_t type,
  32. const char * target)
  33. {
  34. dbus_uint32_t u32;
  35. u32 = type;
  36. if (target == NULL)
  37. {
  38. target = "";
  39. }
  40. jack_dbus_send_signal(
  41. JACK_CONTROLLER_OBJECT_PATH,
  42. JACK_DBUS_IFACE_NAME,
  43. "StateChanged",
  44. DBUS_TYPE_UINT32,
  45. &u32,
  46. DBUS_TYPE_STRING,
  47. &target,
  48. DBUS_TYPE_INVALID);
  49. }
  50. static bool start_detached_thread(void * (* start_routine)(void *), void * arg)
  51. {
  52. int ret;
  53. static pthread_attr_t attr;
  54. pthread_t tid;
  55. ret = pthread_attr_init(&attr);
  56. if (ret != 0)
  57. {
  58. jack_error("pthread_attr_init() failed with %d", ret);
  59. goto exit;
  60. }
  61. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  62. if (ret != 0)
  63. {
  64. jack_error("pthread_attr_setdetachstate() failed with %d", ret);
  65. goto destroy_attr;
  66. }
  67. ret = pthread_create(&tid, &attr, start_routine, arg);
  68. if (ret != 0)
  69. {
  70. jack_error("pthread_create() failed with %d", ret);
  71. goto destroy_attr;
  72. }
  73. jack_log("Detached thread %d created", (int)tid);
  74. destroy_attr:
  75. pthread_attr_destroy(&attr);
  76. exit:
  77. return ret == 0;
  78. }
  79. static void send_session_notify_reply(struct jack_session_pending_command * pending_cmd_ptr, jack_session_command_t * commands)
  80. {
  81. struct jack_dbus_method_call call;
  82. const jack_session_command_t * cmd_ptr;
  83. DBusMessageIter top_iter, array_iter, struct_iter;
  84. dbus_uint32_t u32;
  85. /* jack_dbus_error() wants call struct */
  86. call.message = pending_cmd_ptr->message;
  87. call.connection = pending_cmd_ptr->connection;
  88. if (commands == NULL)
  89. {
  90. jack_dbus_error(&call, JACK_DBUS_ERROR_GENERIC, "jack_session_notify() failed");
  91. goto send_reply;
  92. }
  93. jack_info("Session notify complete, commands follow:");
  94. call.reply = dbus_message_new_method_return(pending_cmd_ptr->message);
  95. if (call.reply == NULL)
  96. {
  97. goto oom;
  98. }
  99. dbus_message_iter_init_append(call.reply, &top_iter);
  100. if (!dbus_message_iter_open_container(&top_iter, DBUS_TYPE_ARRAY, "(sssu)", &array_iter))
  101. {
  102. goto unref;
  103. }
  104. for (cmd_ptr = commands; cmd_ptr->uuid != NULL; cmd_ptr++)
  105. {
  106. if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
  107. {
  108. goto close_array;
  109. }
  110. if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &cmd_ptr->uuid))
  111. {
  112. goto close_struct;
  113. }
  114. if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &cmd_ptr->client_name))
  115. {
  116. goto close_struct;
  117. }
  118. if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &cmd_ptr->command))
  119. {
  120. goto close_struct;
  121. }
  122. u32 = cmd_ptr->flags;
  123. if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32, &u32))
  124. {
  125. goto close_struct;
  126. }
  127. jack_info("uuid='%s', client='%s', command='%s', flags=0x%"PRIX32, cmd_ptr->uuid, cmd_ptr->client_name, cmd_ptr->command, u32);
  128. if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
  129. {
  130. goto close_array;
  131. }
  132. }
  133. jack_info("End of session commands.");
  134. if (!dbus_message_iter_close_container(&top_iter, &array_iter))
  135. {
  136. goto unref;
  137. }
  138. goto send_reply;
  139. close_struct:
  140. dbus_message_iter_close_container(&array_iter, &struct_iter);
  141. close_array:
  142. dbus_message_iter_close_container(&top_iter, &array_iter);
  143. unref:
  144. dbus_message_unref(call.reply);
  145. goto oom;
  146. send_reply:
  147. if (call.reply != NULL)
  148. {
  149. if (!dbus_connection_send(pending_cmd_ptr->connection, call.reply, NULL))
  150. {
  151. jack_error("Ran out of memory trying to queue method return");
  152. }
  153. dbus_connection_flush(pending_cmd_ptr->connection);
  154. dbus_message_unref(call.reply);
  155. }
  156. else
  157. {
  158. oom:
  159. jack_error("Ran out of memory trying to construct method return");
  160. }
  161. }
  162. #define controller_ptr ((struct jack_controller *)context)
  163. void * jack_controller_process_session_command_thread(void * context)
  164. {
  165. struct jack_session_pending_command * pending_cmd_ptr;
  166. jack_session_command_t * commands;
  167. jack_log("jack_controller_process_session_command_thread enter");
  168. pthread_mutex_lock(&controller_ptr->lock);
  169. loop:
  170. /* get next command */
  171. assert(!list_empty(&controller_ptr->session_pending_commands));
  172. pending_cmd_ptr = list_entry(controller_ptr->session_pending_commands.next, struct jack_session_pending_command, siblings);
  173. pthread_mutex_unlock(&controller_ptr->lock);
  174. jack_info("Session notify initiated. target='%s', type=%d, path='%s'", pending_cmd_ptr->target, (int)pending_cmd_ptr->type, pending_cmd_ptr->path);
  175. jack_controller_control_send_signal_session_state_changed(pending_cmd_ptr->type, pending_cmd_ptr->target);
  176. commands = jack_session_notify(controller_ptr->client, pending_cmd_ptr->target, pending_cmd_ptr->type, pending_cmd_ptr->path);
  177. send_session_notify_reply(pending_cmd_ptr, commands);
  178. if (commands != NULL)
  179. {
  180. jack_session_commands_free(commands);
  181. }
  182. pthread_mutex_lock(&controller_ptr->lock);
  183. /* keep state consistent by sending signal after to lock */
  184. /* otherwise the main thread may receive not-to-be-queued request and fail */
  185. jack_controller_control_send_signal_session_state_changed(0, NULL);
  186. /* remove the head of the list (queue) */
  187. assert(!list_empty(&controller_ptr->session_pending_commands));
  188. assert(pending_cmd_ptr == list_entry(controller_ptr->session_pending_commands.next, struct jack_session_pending_command, siblings));
  189. list_del(&pending_cmd_ptr->siblings);
  190. /* command cleanup */
  191. dbus_message_unref(pending_cmd_ptr->message);
  192. dbus_connection_ref(pending_cmd_ptr->connection);
  193. free(pending_cmd_ptr);
  194. /* If there are more commands, process them. Otherwise - exit the thread */
  195. if (!list_empty(&controller_ptr->session_pending_commands))
  196. {
  197. goto loop;
  198. }
  199. pthread_mutex_unlock(&controller_ptr->lock);
  200. jack_log("jack_controller_process_session_command_thread exit");
  201. return NULL;
  202. }
  203. #undef controller_ptr
  204. #define controller_ptr ((struct jack_controller *)call->context)
  205. static
  206. void
  207. jack_controller_dbus_session_notify(
  208. struct jack_dbus_method_call * call)
  209. {
  210. dbus_bool_t queue;
  211. const char * target;
  212. dbus_uint32_t u32;
  213. const char * path;
  214. jack_session_event_type_t type;
  215. struct jack_session_pending_command * cmd_ptr;
  216. if (!controller_ptr->started)
  217. {
  218. jack_dbus_only_error(call, JACK_DBUS_ERROR_SERVER_NOT_RUNNING, "Can't execute method '%s' with stopped JACK server", call->method_name);
  219. return;
  220. }
  221. if (!jack_dbus_get_method_args(
  222. call,
  223. DBUS_TYPE_BOOLEAN,
  224. &queue,
  225. DBUS_TYPE_STRING,
  226. &target,
  227. DBUS_TYPE_UINT32,
  228. &u32,
  229. DBUS_TYPE_STRING,
  230. &path,
  231. DBUS_TYPE_INVALID))
  232. {
  233. /* The method call had invalid arguments meaning that jack_dbus_get_method_args() has constructed an error for us. */
  234. return;
  235. }
  236. if (*target == 0)
  237. {
  238. target = NULL;
  239. }
  240. type = (jack_session_event_type_t)u32;
  241. if (type != JackSessionSave &&
  242. type != JackSessionSaveAndQuit &&
  243. type != JackSessionSaveTemplate)
  244. {
  245. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "Invalid session event type %" PRIu32, u32);
  246. return;
  247. }
  248. pthread_mutex_lock(&controller_ptr->lock);
  249. if (list_empty(&controller_ptr->session_pending_commands))
  250. {
  251. if (!start_detached_thread(jack_controller_process_session_command_thread, controller_ptr))
  252. {
  253. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "Cannot start thread to process the command");
  254. goto unlock;
  255. }
  256. jack_log("Session notify thread started");
  257. }
  258. else if (!queue)
  259. {
  260. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "Busy");
  261. goto unlock;
  262. }
  263. cmd_ptr = malloc(sizeof(struct jack_session_pending_command));
  264. if (cmd_ptr == NULL)
  265. {
  266. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "malloc() failed for jack_session_pending_command struct");
  267. goto unlock;
  268. }
  269. cmd_ptr->message = dbus_message_ref(call->message);
  270. call->message = NULL; /* mark that reply will be sent asynchronously */
  271. cmd_ptr->connection = dbus_connection_ref(call->connection);
  272. /* it is safe to use the retrieved pointers because we already made an additional message reference */
  273. cmd_ptr->type = type;
  274. cmd_ptr->target = target;
  275. cmd_ptr->path = path;
  276. list_add_tail(&cmd_ptr->siblings, &controller_ptr->session_pending_commands);
  277. jack_log("Session notify scheduled. target='%s', type=%"PRIu32", path='%s'", target, u32, path);
  278. unlock:
  279. pthread_mutex_unlock(&controller_ptr->lock);
  280. }
  281. static
  282. void
  283. jack_controller_dbus_get_uuid_for_client_name(
  284. struct jack_dbus_method_call * call)
  285. {
  286. const char * client_name;
  287. char * client_uuid;
  288. if (!jack_dbus_get_method_args(
  289. call,
  290. DBUS_TYPE_STRING,
  291. &client_name,
  292. DBUS_TYPE_INVALID))
  293. {
  294. /* The method call had invalid arguments meaning that jack_dbus_get_method_args() has constructed an error for us. */
  295. return;
  296. }
  297. client_uuid = jack_get_uuid_for_client_name(controller_ptr->client, client_name);
  298. if (client_uuid == NULL)
  299. {
  300. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "jack_get_uuid_for_client_name(\"%s\") failed", client_name);
  301. return;
  302. }
  303. jack_dbus_construct_method_return_single(call, DBUS_TYPE_STRING, (message_arg_t)(const char *)client_uuid);
  304. free(client_uuid);
  305. }
  306. static
  307. void
  308. jack_controller_dbus_get_client_name_by_uuid(
  309. struct jack_dbus_method_call * call)
  310. {
  311. const char * client_uuid;
  312. char * client_name;
  313. if (!jack_dbus_get_method_args(
  314. call,
  315. DBUS_TYPE_STRING,
  316. &client_uuid,
  317. DBUS_TYPE_INVALID))
  318. {
  319. /* The method call had invalid arguments meaning that jack_dbus_get_method_args() has constructed an error for us. */
  320. return;
  321. }
  322. client_name = jack_get_client_name_by_uuid(controller_ptr->client, client_uuid);
  323. if (client_name == NULL)
  324. {
  325. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "jack_get_client_name_by_uuid(\"%s\") failed", client_uuid);
  326. return;
  327. }
  328. jack_dbus_construct_method_return_single(call, DBUS_TYPE_STRING, (message_arg_t)(const char *)client_name);
  329. free(client_name);
  330. }
  331. static
  332. void
  333. jack_controller_dbus_reserve_client_name(
  334. struct jack_dbus_method_call * call)
  335. {
  336. int ret;
  337. const char * client_name;
  338. const char * client_uuid;
  339. if (!jack_dbus_get_method_args(
  340. call,
  341. DBUS_TYPE_STRING,
  342. &client_name,
  343. DBUS_TYPE_STRING,
  344. &client_uuid,
  345. DBUS_TYPE_INVALID))
  346. {
  347. /* The method call had invalid arguments meaning that jack_dbus_get_method_args() has constructed an error for us. */
  348. return;
  349. }
  350. ret = jack_reserve_client_name(controller_ptr->client, client_name, client_uuid);
  351. if (ret < 0)
  352. {
  353. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "jack_reserve_client_name(name=\"%s\", uuid=\"%s\") failed (%d)", client_name, client_uuid, ret);
  354. return;
  355. }
  356. jack_dbus_construct_method_return_empty(call);
  357. }
  358. static
  359. void
  360. jack_controller_dbus_has_session_callback(
  361. struct jack_dbus_method_call * call)
  362. {
  363. int ret;
  364. const char * client_name;
  365. message_arg_t retval;
  366. if (!jack_dbus_get_method_args(
  367. call,
  368. DBUS_TYPE_STRING,
  369. &client_name,
  370. DBUS_TYPE_INVALID))
  371. {
  372. /* The method call had invalid arguments meaning that jack_dbus_get_method_args() has constructed an error for us. */
  373. return;
  374. }
  375. ret = jack_client_has_session_callback(controller_ptr->client, client_name);
  376. if (ret < 0)
  377. {
  378. jack_dbus_error(call, JACK_DBUS_ERROR_GENERIC, "jack_client_has_session_callback(\"%s\") failed (%d)", client_name, ret);
  379. return;
  380. }
  381. retval.boolean = ret;
  382. jack_dbus_construct_method_return_single(call, DBUS_TYPE_BOOLEAN, retval);
  383. }
  384. static
  385. void
  386. jack_controller_dbus_get_session_state(
  387. struct jack_dbus_method_call * call)
  388. {
  389. DBusMessageIter iter;
  390. struct jack_session_pending_command * cmd_ptr;
  391. const char * target;
  392. dbus_uint32_t type;
  393. bool append_failed;
  394. call->reply = dbus_message_new_method_return(call->message);
  395. if (call->reply == NULL)
  396. {
  397. goto oom;
  398. }
  399. dbus_message_iter_init_append(call->reply, &iter);
  400. pthread_mutex_lock(&controller_ptr->lock);
  401. if (list_empty(&controller_ptr->session_pending_commands))
  402. {
  403. type = 0;
  404. target = "";
  405. }
  406. else
  407. {
  408. cmd_ptr = list_entry(controller_ptr->session_pending_commands.next, struct jack_session_pending_command, siblings);
  409. type = (dbus_uint32_t)cmd_ptr->type;
  410. target = cmd_ptr->target;
  411. }
  412. append_failed =
  413. !dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &type) ||
  414. !dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &target);
  415. pthread_mutex_unlock(&controller_ptr->lock);
  416. if (!append_failed)
  417. {
  418. return;
  419. }
  420. dbus_message_unref(call->reply);
  421. call->reply = NULL;
  422. oom:
  423. jack_error("Ran out of memory trying to construct method return");
  424. }
  425. #undef controller_ptr
  426. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(Notify)
  427. JACK_DBUS_METHOD_ARGUMENT("queue", DBUS_TYPE_BOOLEAN_AS_STRING, false)
  428. JACK_DBUS_METHOD_ARGUMENT("target", DBUS_TYPE_STRING_AS_STRING, false)
  429. JACK_DBUS_METHOD_ARGUMENT("type", DBUS_TYPE_UINT32_AS_STRING, false)
  430. JACK_DBUS_METHOD_ARGUMENT("path", DBUS_TYPE_STRING_AS_STRING, false)
  431. JACK_DBUS_METHOD_ARGUMENT("result", "a(sssu)", true)
  432. JACK_DBUS_METHOD_ARGUMENTS_END
  433. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetUuidForClientName)
  434. JACK_DBUS_METHOD_ARGUMENT("name", DBUS_TYPE_STRING_AS_STRING, false)
  435. JACK_DBUS_METHOD_ARGUMENT("uuid", DBUS_TYPE_STRING_AS_STRING, true)
  436. JACK_DBUS_METHOD_ARGUMENTS_END
  437. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientNameByUuid)
  438. JACK_DBUS_METHOD_ARGUMENT("uuid", DBUS_TYPE_STRING_AS_STRING, false)
  439. JACK_DBUS_METHOD_ARGUMENT("name", DBUS_TYPE_STRING_AS_STRING, true)
  440. JACK_DBUS_METHOD_ARGUMENTS_END
  441. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ReserveClientName)
  442. JACK_DBUS_METHOD_ARGUMENT("name", DBUS_TYPE_STRING_AS_STRING, false)
  443. JACK_DBUS_METHOD_ARGUMENT("uuid", DBUS_TYPE_STRING_AS_STRING, false)
  444. JACK_DBUS_METHOD_ARGUMENTS_END
  445. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(HasSessionCallback)
  446. JACK_DBUS_METHOD_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING, false)
  447. JACK_DBUS_METHOD_ARGUMENT("has_session_callback", DBUS_TYPE_BOOLEAN_AS_STRING, true)
  448. JACK_DBUS_METHOD_ARGUMENTS_END
  449. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetState)
  450. JACK_DBUS_METHOD_ARGUMENT("type", DBUS_TYPE_UINT32_AS_STRING, true)
  451. JACK_DBUS_METHOD_ARGUMENT("target", DBUS_TYPE_STRING_AS_STRING, true)
  452. JACK_DBUS_METHOD_ARGUMENTS_END
  453. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(StateChanged)
  454. JACK_DBUS_SIGNAL_ARGUMENT("type", DBUS_TYPE_UINT32_AS_STRING)
  455. JACK_DBUS_SIGNAL_ARGUMENT("target", DBUS_TYPE_STRING_AS_STRING)
  456. JACK_DBUS_SIGNAL_ARGUMENTS_END
  457. JACK_DBUS_METHODS_BEGIN
  458. JACK_DBUS_METHOD_DESCRIBE(Notify, jack_controller_dbus_session_notify)
  459. JACK_DBUS_METHOD_DESCRIBE(GetUuidForClientName, jack_controller_dbus_get_uuid_for_client_name)
  460. JACK_DBUS_METHOD_DESCRIBE(GetClientNameByUuid, jack_controller_dbus_get_client_name_by_uuid)
  461. JACK_DBUS_METHOD_DESCRIBE(ReserveClientName, jack_controller_dbus_reserve_client_name)
  462. JACK_DBUS_METHOD_DESCRIBE(HasSessionCallback, jack_controller_dbus_has_session_callback)
  463. JACK_DBUS_METHOD_DESCRIBE(GetState, jack_controller_dbus_get_session_state)
  464. JACK_DBUS_METHODS_END
  465. JACK_DBUS_SIGNALS_BEGIN
  466. JACK_DBUS_SIGNAL_DESCRIBE(StateChanged)
  467. JACK_DBUS_SIGNALS_END
  468. JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_session_manager, JACK_DBUS_IFACE_NAME)
  469. JACK_DBUS_IFACE_EXPOSE_METHODS
  470. JACK_DBUS_IFACE_EXPOSE_SIGNALS
  471. JACK_DBUS_IFACE_END