Bump libgtk2.0 dependency.
[gmidimonitor.git] / main.c
blob4418a02b81a3587cb828c9b8cde5ca7056bf96cb
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * gmidimonitor main code.
6 * This file is part of gmidimonitor
8 * Copyright (C) 2005,2006,2007,2008,2011 Nedko Arnaudov <nedko@arnaudov.name>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 *****************************************************************************/
25 #include "config.h"
27 #include <string.h>
28 #include <glib.h>
29 #include <glib/gprintf.h>
30 #include <gtk/gtk.h>
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <unistd.h>
35 #ifdef HAVE_LASH_1_0
36 #include <lash/lash.h>
37 #endif
39 #include "common.h"
40 #include "path.h"
41 #include "gm.h"
42 #include "jack.h"
43 #include "alsa.h"
45 GtkBuilder * g_builder;
46 GtkWidget * g_main_window_ptr;
48 gboolean g_midi_ignore = FALSE;
50 int g_row_count;
52 #ifdef HAVE_LASH_1_0
53 lash_client_t * g_lashc;
54 #endif
56 const char * g_note_names[12] =
58 "C",
59 "C#",
60 "D",
61 "D#",
62 "E",
63 "F",
64 "F#",
65 "G",
66 "G#",
67 "A",
68 "A#",
69 "B",
72 void
73 create_mainwindow()
75 GtkWidget * child_ptr;
76 GtkListStore * list_store_ptr;
77 GtkTreeViewColumn * column_ptr;
78 GtkCellRenderer * text_renderer_ptr;
79 GError * error;
80 gchar * ui_filename;
82 ui_filename = path_get_data_filename("gmidimonitor.ui");
83 if (ui_filename == NULL)
85 g_warning("Cannot find UI description file.");
86 exit(1);
89 /* load the interface builder */
90 error = NULL;
91 g_builder = gtk_builder_new();
92 if (!gtk_builder_add_from_file (g_builder, FILE, &error))
94 g_warning ("Couldn't load builder file: %s", error->message);
95 g_error_free (error);
97 g_free(ui_filename);
99 /* Retrieve the main window and connect the signals in the interface */
100 g_main_window_ptr = GTK_WIDGET (gtk_builder_get_object (g_builder, "main_window"));
101 gtk_builder_connect_signals(g_builder, NULL);
103 child_ptr = GTK_WIDGET (gtk_builder_get_object (g_builder, "list"));
105 text_renderer_ptr = gtk_cell_renderer_text_new();
107 g_object_set(
108 G_OBJECT(text_renderer_ptr),
109 "family",
110 "Monospace",
111 NULL);
113 /* column_ptr = gtk_tree_view_column_new_with_attributes( */
114 /* "Time", */
115 /* text_renderer_ptr, */
116 /* "text", COL_TIME, */
117 /* NULL); */
119 /* gtk_tree_view_append_column( */
120 /* GTK_TREE_VIEW(child_ptr), */
121 /* column_ptr); */
123 column_ptr = gtk_tree_view_column_new_with_attributes(
124 "Channel",
125 text_renderer_ptr,
126 "text", COL_CHANNEL,
127 NULL);
129 gtk_tree_view_append_column(
130 GTK_TREE_VIEW(child_ptr),
131 column_ptr);
133 column_ptr = gtk_tree_view_column_new_with_attributes(
134 "Message",
135 text_renderer_ptr,
136 "text", COL_MESSAGE,
137 NULL);
139 gtk_tree_view_append_column(
140 GTK_TREE_VIEW(child_ptr),
141 column_ptr);
143 gtk_widget_show_all(g_main_window_ptr);
145 list_store_ptr = gtk_list_store_new(
146 NUM_COLS,
147 G_TYPE_STRING,
148 G_TYPE_STRING,
149 G_TYPE_STRING);
151 gtk_tree_view_set_model(GTK_TREE_VIEW(child_ptr), GTK_TREE_MODEL(list_store_ptr));
154 /* stop button toggle */
155 void on_button_stop_toggled(
156 GtkAction * action_ptr,
157 GtkWidget * widget_ptr)
159 GtkWidget * child_ptr;
161 child_ptr = GTK_WIDGET (gtk_builder_get_object (g_builder, "button_stop"));
163 if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(child_ptr)))
165 g_midi_ignore = TRUE;
167 else
169 g_midi_ignore = FALSE;
173 void on_clear_clicked
174 (GtkButton *button, gpointer user_data)
176 gtk_list_store_clear(
177 GTK_LIST_STORE(
178 gtk_tree_view_get_model(
179 GTK_TREE_VIEW(
180 gtk_builder_get_object(
181 g_builder,
182 "list")))));
183 g_row_count = 0;
186 #ifdef HAVE_LASH_1_0
188 void
189 process_lash_event(lash_event_t * event_ptr)
191 enum LASH_Event_Type type;
192 const char * str;
194 type = lash_event_get_type(event_ptr);
195 str = lash_event_get_string(event_ptr);
197 switch (type)
199 case LASH_Quit:
200 g_warning("LASH_Quit received.\n");
201 g_lashc = NULL;
202 gtk_main_quit();
203 break;
204 case LASH_Save_File:
205 case LASH_Restore_File:
206 case LASH_Save_Data_Set:
207 default:
208 g_warning("LASH Event. Type = %u, string = \"%s\"\n",
209 (unsigned int)type,
210 (str == NULL)?"":str);
214 void
215 process_lash_config(lash_config_t * config_ptr)
217 const char * key;
218 const void * data;
219 size_t data_size;
221 key = lash_config_get_key(config_ptr);
222 data = lash_config_get_value(config_ptr);
223 data_size = lash_config_get_value_size(config_ptr);
225 g_warning("LASH Config. Key = \"%s\"\n", key);
228 /* process lash events callback */
229 gboolean
230 process_lash_events(gpointer data)
232 lash_event_t * event_ptr;
233 lash_config_t * config_ptr;
235 /* Process events */
236 while ((event_ptr = lash_get_event(g_lashc)) != NULL)
238 process_lash_event(event_ptr);
239 lash_event_destroy(event_ptr);
242 /* Process configs */
243 while ((config_ptr = lash_get_config(g_lashc)) != NULL)
245 process_lash_config(config_ptr);
246 lash_config_destroy(config_ptr);
249 return TRUE;
252 #endif /* #ifdef HAVE_LASH_1_0 */
254 static
255 void
256 sigint_handler(
257 int i)
259 gtk_main_quit();
263 main(int argc, char *argv[])
265 #ifdef HAVE_LASH_1_0
266 lash_event_t * lash_event_ptr;
267 #endif
268 GString * client_name_str_ptr;
270 int i;
271 gboolean want_any = TRUE;
272 #ifdef HAVE_JACK
273 gboolean want_jack = FALSE;
274 gboolean jack_enabled = FALSE;
275 #endif
276 #ifdef HAVE_ALSA
277 gboolean want_alsa = FALSE;
278 gboolean alsa_enabled = FALSE;
279 #endif
280 gboolean io_enabled = FALSE;
281 int ret = 0;
283 for (i = 1; i < argc; i++)
285 #ifdef HAVE_JACK
286 if (strcmp(argv[i], "--jack") == 0)
288 want_jack = TRUE;
289 want_any = FALSE;
290 continue;
292 #endif
293 #ifdef HAVE_ALSA
294 if (strcmp(argv[i], "--alsa") == 0)
296 want_alsa = TRUE;
297 want_any = FALSE;
298 continue;
300 #endif
301 if (strcmp(argv[i], "--help") != 0)
303 fprintf(stderr, "Unrecognized parameter \"%s\"\n", argv[i]);
304 ret = 1;
307 fprintf(
308 ret == 0 ? stdout : stderr,
309 "Usage: %s"
310 #ifdef HAVE_JACK
311 " [--jack]"
312 #endif
313 #ifdef HAVE_ALSA
314 " [--alsa]"
315 #endif
316 "\n",
317 argv[0]);
319 exit(ret);
322 /* init threads */
323 g_thread_init(NULL);
324 gdk_threads_init();
325 gdk_threads_enter();
327 gtk_init(&argc, &argv);
329 path_init(argv[0]);
331 #ifdef HAVE_LASH_1_0
332 if (getenv("LADISH_APPNAME") != NULL)
334 g_lashc = lash_init(
335 lash_extract_args(&argc, &argv),
336 "gmidimonitor",
338 LASH_PROTOCOL_VERSION);
339 if (g_lashc == NULL)
341 g_warning("Failed to connect to LASH. Session management will not occur.\n");
343 else
345 lash_event_ptr = lash_event_new_with_type(LASH_Client_Name);
346 lash_event_set_string(lash_event_ptr, "GMIDImonitor");
347 lash_send_event(g_lashc, lash_event_ptr);
348 g_timeout_add(250, process_lash_events, NULL);
351 #endif
353 /* interface creation */
354 create_mainwindow();
356 client_name_str_ptr = g_string_new("");
357 g_string_sprintf(client_name_str_ptr, "MIDI monitor");
359 g_row_count = 0;
361 #ifdef HAVE_JACK
362 if (want_jack || want_any)
364 jack_enabled = jack_init(client_name_str_ptr->str);
365 if (jack_enabled)
367 g_printf("JACK MIDI enabled\n");
368 io_enabled = TRUE;
370 else if (want_jack)
372 goto failed_jack;
375 #endif
377 #ifdef HAVE_ALSA
378 if (want_alsa || (want_any && !io_enabled))
380 alsa_enabled = alsa_init(client_name_str_ptr->str);
381 if (alsa_enabled)
383 g_printf("ALSA MIDI enabled\n");
384 io_enabled = TRUE;
386 else if (want_alsa)
388 goto failed_alsa;
391 #endif
393 if (io_enabled)
395 signal(SIGINT, &sigint_handler);
396 signal(SIGTERM, &sigint_handler);
398 /* main loop */
399 gtk_main();
402 #ifdef HAVE_ALSA
403 if (alsa_enabled)
405 alsa_uninit();
407 failed_alsa:
408 #endif
410 #ifdef HAVE_JACK
411 if (jack_enabled)
413 jack_uninit();
415 failed_jack:
416 #endif
418 gdk_threads_leave();
420 path_init(argv[0]);
422 return ret;