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.
 
 
 
 
 
 

518 lines
18 KiB

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
  2. *
  3. * Copyright (C) 2009 Bastien Nocera
  4. * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
  5. *
  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 of the License, or
  9. * (at your option) any later version.
  10. *
  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.
  15. *
  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 St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include "config.h"
  22. #include <glib.h>
  23. #include <glib/gi18n.h>
  24. #include <glib-object.h>
  25. #include <gtk/gtk.h>
  26. #include <canberra.h>
  27. #include <libmatemixer/matemixer.h>
  28. #include "gvc-speaker-test.h"
  29. #include "gvc-utils.h"
  30. #define GVC_SPEAKER_TEST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_SPEAKER_TEST, GvcSpeakerTestPrivate))
  31. struct _GvcSpeakerTestPrivate
  32. {
  33. GArray *controls;
  34. ca_context *canberra;
  35. MateMixerStream *stream;
  36. };
  37. enum {
  38. PROP_0,
  39. PROP_STREAM,
  40. N_PROPERTIES
  41. };
  42. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  43. static void gvc_speaker_test_class_init (GvcSpeakerTestClass *klass);
  44. static void gvc_speaker_test_init (GvcSpeakerTest *test);
  45. static void gvc_speaker_test_dispose (GObject *object);
  46. static void gvc_speaker_test_finalize (GObject *object);
  47. #if GTK_CHECK_VERSION (3, 4, 0)
  48. G_DEFINE_TYPE (GvcSpeakerTest, gvc_speaker_test, GTK_TYPE_GRID)
  49. #else
  50. G_DEFINE_TYPE (GvcSpeakerTest, gvc_speaker_test, GTK_TYPE_TABLE)
  51. #endif
  52. typedef struct {
  53. MateMixerChannelPosition position;
  54. guint left;
  55. guint top;
  56. } TablePosition;
  57. static const TablePosition positions[] = {
  58. /* Position, X, Y */
  59. { MATE_MIXER_CHANNEL_FRONT_LEFT, 0, 0, },
  60. { MATE_MIXER_CHANNEL_FRONT_LEFT_CENTER, 1, 0, },
  61. { MATE_MIXER_CHANNEL_FRONT_CENTER, 2, 0, },
  62. { MATE_MIXER_CHANNEL_MONO, 2, 0, },
  63. { MATE_MIXER_CHANNEL_FRONT_RIGHT_CENTER, 3, 0, },
  64. { MATE_MIXER_CHANNEL_FRONT_RIGHT, 4, 0, },
  65. { MATE_MIXER_CHANNEL_SIDE_LEFT, 0, 1, },
  66. { MATE_MIXER_CHANNEL_SIDE_RIGHT, 4, 1, },
  67. { MATE_MIXER_CHANNEL_BACK_LEFT, 0, 2, },
  68. { MATE_MIXER_CHANNEL_BACK_CENTER, 2, 2, },
  69. { MATE_MIXER_CHANNEL_BACK_RIGHT, 4, 2, },
  70. { MATE_MIXER_CHANNEL_LFE, 3, 2 }
  71. };
  72. MateMixerStream *
  73. gvc_speaker_test_get_stream (GvcSpeakerTest *test)
  74. {
  75. g_return_val_if_fail (GVC_IS_SPEAKER_TEST (test), NULL);
  76. return test->priv->stream;
  77. }
  78. static void
  79. gvc_speaker_test_set_stream (GvcSpeakerTest *test, MateMixerStream *stream)
  80. {
  81. MateMixerStreamControl *control;
  82. const gchar *name;
  83. guint i;
  84. name = mate_mixer_stream_get_name (stream);
  85. control = mate_mixer_stream_get_default_control (stream);
  86. ca_context_change_device (test->priv->canberra, name);
  87. for (i = 0; i < G_N_ELEMENTS (positions); i++) {
  88. gboolean has_position =
  89. mate_mixer_stream_control_has_channel_position (control, positions[i].position);
  90. gtk_widget_set_visible (g_array_index (test->priv->controls, GtkWidget *, i),
  91. has_position);
  92. }
  93. test->priv->stream = g_object_ref (stream);
  94. }
  95. static void
  96. gvc_speaker_test_set_property (GObject *object,
  97. guint prop_id,
  98. const GValue *value,
  99. GParamSpec *pspec)
  100. {
  101. GvcSpeakerTest *self = GVC_SPEAKER_TEST (object);
  102. switch (prop_id) {
  103. case PROP_STREAM:
  104. gvc_speaker_test_set_stream (self, g_value_get_object (value));
  105. break;
  106. default:
  107. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  108. break;
  109. }
  110. }
  111. static void
  112. gvc_speaker_test_get_property (GObject *object,
  113. guint prop_id,
  114. GValue *value,
  115. GParamSpec *pspec)
  116. {
  117. GvcSpeakerTest *self = GVC_SPEAKER_TEST (object);
  118. switch (prop_id) {
  119. case PROP_STREAM:
  120. g_value_set_object (value, self->priv->stream);
  121. break;
  122. default:
  123. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  124. break;
  125. }
  126. }
  127. static void
  128. gvc_speaker_test_class_init (GvcSpeakerTestClass *klass)
  129. {
  130. GObjectClass *object_class = G_OBJECT_CLASS (klass);
  131. object_class->dispose = gvc_speaker_test_dispose;
  132. object_class->finalize = gvc_speaker_test_finalize;
  133. object_class->set_property = gvc_speaker_test_set_property;
  134. object_class->get_property = gvc_speaker_test_get_property;
  135. properties[PROP_STREAM] =
  136. g_param_spec_object ("stream",
  137. "Stream",
  138. "MateMixer stream",
  139. MATE_MIXER_TYPE_STREAM,
  140. G_PARAM_READWRITE |
  141. G_PARAM_CONSTRUCT_ONLY |
  142. G_PARAM_STATIC_STRINGS);
  143. g_object_class_install_properties (object_class, N_PROPERTIES, properties);
  144. g_type_class_add_private (klass, sizeof (GvcSpeakerTestPrivate));
  145. }
  146. static const gchar *
  147. sound_name (MateMixerChannelPosition position)
  148. {
  149. switch (position) {
  150. case MATE_MIXER_CHANNEL_FRONT_LEFT:
  151. return "audio-channel-front-left";
  152. case MATE_MIXER_CHANNEL_FRONT_RIGHT:
  153. return "audio-channel-front-right";
  154. case MATE_MIXER_CHANNEL_FRONT_CENTER:
  155. return "audio-channel-front-center";
  156. case MATE_MIXER_CHANNEL_BACK_LEFT:
  157. return "audio-channel-rear-left";
  158. case MATE_MIXER_CHANNEL_BACK_RIGHT:
  159. return "audio-channel-rear-right";
  160. case MATE_MIXER_CHANNEL_BACK_CENTER:
  161. return "audio-channel-rear-center";
  162. case MATE_MIXER_CHANNEL_LFE:
  163. return "audio-channel-lfe";
  164. case MATE_MIXER_CHANNEL_SIDE_LEFT:
  165. return "audio-channel-side-left";
  166. case MATE_MIXER_CHANNEL_SIDE_RIGHT:
  167. return "audio-channel-side-right";
  168. default:
  169. return NULL;
  170. }
  171. }
  172. static const gchar *
  173. icon_name (MateMixerChannelPosition position, gboolean playing)
  174. {
  175. switch (position) {
  176. case MATE_MIXER_CHANNEL_FRONT_LEFT:
  177. return playing
  178. ? "audio-speaker-left-testing"
  179. : "audio-speaker-left";
  180. case MATE_MIXER_CHANNEL_FRONT_RIGHT:
  181. return playing
  182. ? "audio-speaker-right-testing"
  183. : "audio-speaker-right";
  184. case MATE_MIXER_CHANNEL_FRONT_CENTER:
  185. return playing
  186. ? "audio-speaker-center-testing"
  187. : "audio-speaker-center";
  188. case MATE_MIXER_CHANNEL_BACK_LEFT:
  189. return playing
  190. ? "audio-speaker-left-back-testing"
  191. : "audio-speaker-left-back";
  192. case MATE_MIXER_CHANNEL_BACK_RIGHT:
  193. return playing
  194. ? "audio-speaker-right-back-testing"
  195. : "audio-speaker-right-back";
  196. case MATE_MIXER_CHANNEL_BACK_CENTER:
  197. return playing
  198. ? "audio-speaker-center-back-testing"
  199. : "audio-speaker-center-back";
  200. case MATE_MIXER_CHANNEL_LFE:
  201. return playing
  202. ? "audio-subwoofer-testing"
  203. : "audio-subwoofer";
  204. case MATE_MIXER_CHANNEL_SIDE_LEFT:
  205. return playing
  206. ? "audio-speaker-left-side-testing"
  207. : "audio-speaker-left-side";
  208. case MATE_MIXER_CHANNEL_SIDE_RIGHT:
  209. return playing
  210. ? "audio-speaker-right-side-testing"
  211. : "audio-speaker-right-side";
  212. default:
  213. return NULL;
  214. }
  215. }
  216. static void
  217. update_button (GtkWidget *control)
  218. {
  219. GtkWidget *button;
  220. GtkWidget *image;
  221. gboolean playing;
  222. MateMixerChannelPosition position;
  223. button = g_object_get_data (G_OBJECT (control), "button");
  224. image = g_object_get_data (G_OBJECT (control), "image");
  225. position = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "position"));
  226. playing = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "playing"));
  227. gtk_button_set_label (GTK_BUTTON (button), playing ? _("Stop") : _("Test"));
  228. gtk_image_set_from_icon_name (GTK_IMAGE (image),
  229. icon_name (position, playing),
  230. GTK_ICON_SIZE_DIALOG);
  231. }
  232. static gboolean
  233. idle_cb (GtkWidget *control)
  234. {
  235. if (control != NULL) {
  236. /* This is called in the background thread, hence forward to main thread
  237. * via idle callback */
  238. g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
  239. update_button (control);
  240. }
  241. return FALSE;
  242. }
  243. static void
  244. finish_cb (ca_context *c, uint32_t id, int error_code, void *userdata)
  245. {
  246. GtkWidget *control = (GtkWidget *) userdata;
  247. if (error_code == CA_ERROR_DESTROYED || control == NULL)
  248. return;
  249. g_idle_add ((GSourceFunc) idle_cb, control);
  250. }
  251. static void
  252. on_test_button_clicked (GtkButton *button, GtkWidget *control)
  253. {
  254. gboolean playing;
  255. ca_context *canberra;
  256. canberra = g_object_get_data (G_OBJECT (control), "canberra");
  257. ca_context_cancel (canberra, 1);
  258. playing = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "playing"));
  259. if (playing) {
  260. g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
  261. } else {
  262. MateMixerChannelPosition position;
  263. const gchar *name;
  264. ca_proplist *proplist;
  265. position = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "position"));
  266. ca_proplist_create (&proplist);
  267. ca_proplist_sets (proplist,
  268. CA_PROP_MEDIA_ROLE, "test");
  269. ca_proplist_sets (proplist,
  270. CA_PROP_MEDIA_NAME,
  271. gvc_channel_position_to_pretty_string (position));
  272. ca_proplist_sets (proplist,
  273. CA_PROP_CANBERRA_FORCE_CHANNEL,
  274. gvc_channel_position_to_pulse_string (position));
  275. ca_proplist_sets (proplist, CA_PROP_CANBERRA_ENABLE, "1");
  276. name = sound_name (position);
  277. if (name != NULL) {
  278. ca_proplist_sets (proplist, CA_PROP_EVENT_ID, name);
  279. playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
  280. }
  281. if (!playing) {
  282. ca_proplist_sets (proplist, CA_PROP_EVENT_ID, "audio-test-signal");
  283. playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
  284. }
  285. if (!playing) {
  286. ca_proplist_sets(proplist, CA_PROP_EVENT_ID, "bell-window-system");
  287. playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
  288. }
  289. g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (playing));
  290. }
  291. update_button (control);
  292. }
  293. static GtkWidget *
  294. create_control (ca_context *canberra, MateMixerChannelPosition position)
  295. {
  296. GtkWidget *control;
  297. GtkWidget *box;
  298. GtkWidget *label;
  299. GtkWidget *image;
  300. GtkWidget *test_button;
  301. const gchar *name;
  302. #if GTK_CHECK_VERSION (3, 0, 0)
  303. control = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  304. box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  305. #else
  306. control = gtk_vbox_new (FALSE, 6);
  307. box = gtk_hbox_new (FALSE, 0);
  308. #endif
  309. g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
  310. g_object_set_data (G_OBJECT (control), "position", GINT_TO_POINTER (position));
  311. g_object_set_data (G_OBJECT (control), "canberra", canberra);
  312. name = icon_name (position, FALSE);
  313. if (name == NULL)
  314. name = "audio-volume-medium";
  315. image = gtk_image_new_from_icon_name (name, GTK_ICON_SIZE_DIALOG);
  316. g_object_set_data (G_OBJECT (control), "image", image);
  317. gtk_box_pack_start (GTK_BOX (control), image, FALSE, FALSE, 0);
  318. label = gtk_label_new (gvc_channel_position_to_pretty_string (position));
  319. gtk_box_pack_start (GTK_BOX (control), label, FALSE, FALSE, 0);
  320. test_button = gtk_button_new_with_label (_("Test"));
  321. g_signal_connect (G_OBJECT (test_button),
  322. "clicked",
  323. G_CALLBACK (on_test_button_clicked),
  324. control);
  325. g_object_set_data (G_OBJECT (control), "button", test_button);
  326. gtk_box_pack_start (GTK_BOX (box), test_button, TRUE, FALSE, 0);
  327. gtk_box_pack_start (GTK_BOX (control), box, FALSE, FALSE, 0);
  328. gtk_widget_show_all (control);
  329. return control;
  330. }
  331. static void
  332. create_controls (GvcSpeakerTest *test)
  333. {
  334. guint i;
  335. for (i = 0; i < G_N_ELEMENTS (positions); i++) {
  336. GtkWidget *control = create_control (test->priv->canberra, positions[i].position);
  337. #if GTK_CHECK_VERSION (3, 4, 0)
  338. gtk_grid_attach (GTK_GRID (test),
  339. control,
  340. positions[i].left,
  341. positions[i].top,
  342. 1, 1);
  343. #else
  344. gtk_table_attach (GTK_TABLE (test),
  345. control,
  346. positions[i].left,
  347. positions[i].left + 1,
  348. positions[i].top,
  349. positions[i].top + 1,
  350. GTK_EXPAND, GTK_EXPAND, 0, 0);
  351. #endif
  352. g_array_insert_val (test->priv->controls, i, control);
  353. }
  354. }
  355. static void
  356. gvc_speaker_test_init (GvcSpeakerTest *test)
  357. {
  358. GtkWidget *face;
  359. test->priv = GVC_SPEAKER_TEST_GET_PRIVATE (test);
  360. gtk_container_set_border_width (GTK_CONTAINER (test), 12);
  361. face = gtk_image_new_from_icon_name ("face-smile", GTK_ICON_SIZE_DIALOG);
  362. #if GTK_CHECK_VERSION (3, 4, 0)
  363. gtk_grid_attach (GTK_GRID (test),
  364. face,
  365. 1, 1,
  366. 3, 1);
  367. gtk_grid_set_baseline_row (GTK_GRID (test), 1);
  368. #else
  369. gtk_table_attach (GTK_TABLE (test),
  370. face,
  371. 2, 3, 1, 2,
  372. GTK_EXPAND,
  373. GTK_EXPAND,
  374. 0, 0);
  375. #endif
  376. gtk_widget_show (face);
  377. ca_context_create (&test->priv->canberra);
  378. /* The test sounds are played for a single channel, set up using the
  379. * FORCE_CHANNEL property of libcanberra; this property is only supported
  380. * in the PulseAudio backend, so avoid other backends completely */
  381. ca_context_set_driver (test->priv->canberra, "pulse");
  382. ca_context_change_props (test->priv->canberra,
  383. CA_PROP_APPLICATION_ID, "org.mate.VolumeControl",
  384. CA_PROP_APPLICATION_NAME, _("Volume Control"),
  385. CA_PROP_APPLICATION_VERSION, VERSION,
  386. CA_PROP_APPLICATION_ICON_NAME, "multimedia-volume-control",
  387. NULL);
  388. test->priv->controls = g_array_new (FALSE, FALSE, sizeof (GtkWidget *));
  389. create_controls (test);
  390. }
  391. static void
  392. gvc_speaker_test_dispose (GObject *object)
  393. {
  394. GvcSpeakerTest *test;
  395. test = GVC_SPEAKER_TEST (object);
  396. g_clear_object (&test->priv->stream);
  397. G_OBJECT_CLASS (gvc_speaker_test_parent_class)->dispose (object);
  398. }
  399. static void
  400. gvc_speaker_test_finalize (GObject *object)
  401. {
  402. GvcSpeakerTest *test;
  403. test = GVC_SPEAKER_TEST (object);
  404. ca_context_destroy (test->priv->canberra);
  405. G_OBJECT_CLASS (gvc_speaker_test_parent_class)->finalize (object);
  406. }
  407. GtkWidget *
  408. gvc_speaker_test_new (MateMixerStream *stream)
  409. {
  410. GObject *test;
  411. g_return_val_if_fail (MATE_MIXER_IS_STREAM (stream), NULL);
  412. test = g_object_new (GVC_TYPE_SPEAKER_TEST,
  413. "row-spacing", 6,
  414. "column-spacing", 6,
  415. #if GTK_CHECK_VERSION (3, 4, 0)
  416. "row-homogeneous", TRUE,
  417. "column-homogeneous", TRUE,
  418. #else
  419. "homogeneous", TRUE,
  420. "n-rows", 3,
  421. "n-columns", 5,
  422. #endif
  423. "stream", stream,
  424. NULL);
  425. return GTK_WIDGET (test);
  426. }