update the website url
[gmidimonitor.git] / main.c
blobd59ad8b26fef57ee74ce2492378c401c84d29d75
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 "glade.h"
42 #include "gm.h"
43 #include "jack.h"
44 #include "alsa.h"
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;
80 g_main_window_ptr = construct_glade_widget("main_window");
82 child_ptr = get_glade_widget_child(g_main_window_ptr, "list");
84 text_renderer_ptr = gtk_cell_renderer_text_new();
86 g_object_set(
87 G_OBJECT(text_renderer_ptr),
88 "family",
89 "Monospace",
90 NULL);
92 /* column_ptr = gtk_tree_view_column_new_with_attributes( */
93 /* "Time", */
94 /* text_renderer_ptr, */
95 /* "text", COL_TIME, */
96 /* NULL); */
98 /* gtk_tree_view_append_column( */
99 /* GTK_TREE_VIEW(child_ptr), */
100 /* column_ptr); */
102 column_ptr = gtk_tree_view_column_new_with_attributes(
103 "Channel",
104 text_renderer_ptr,
105 "text", COL_CHANNEL,
106 NULL);
108 gtk_tree_view_append_column(
109 GTK_TREE_VIEW(child_ptr),
110 column_ptr);
112 column_ptr = gtk_tree_view_column_new_with_attributes(
113 "Message",
114 text_renderer_ptr,
115 "text", COL_MESSAGE,
116 NULL);
118 gtk_tree_view_append_column(
119 GTK_TREE_VIEW(child_ptr),
120 column_ptr);
122 gtk_widget_show_all(g_main_window_ptr);
124 list_store_ptr = gtk_list_store_new(
125 NUM_COLS,
126 G_TYPE_STRING,
127 G_TYPE_STRING,
128 G_TYPE_STRING);
130 gtk_tree_view_set_model(GTK_TREE_VIEW(child_ptr), GTK_TREE_MODEL(list_store_ptr));
133 /* stop button toggle */
134 void on_button_stop_toggled(
135 GtkAction * action_ptr,
136 GtkWidget * widget_ptr)
138 GtkWidget * child_ptr;
140 child_ptr = get_glade_widget_child(widget_ptr, "button_stop");
142 if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(child_ptr)))
144 g_midi_ignore = TRUE;
146 else
148 g_midi_ignore = FALSE;
152 void on_clear_clicked
153 (GtkButton *button, gpointer user_data)
155 gtk_list_store_clear(
156 GTK_LIST_STORE(
157 gtk_tree_view_get_model(
158 GTK_TREE_VIEW(
159 get_glade_widget_child(
160 g_main_window_ptr,
161 "list")))));
162 g_row_count = 0;
165 #ifdef HAVE_LASH_1_0
167 void
168 process_lash_event(lash_event_t * event_ptr)
170 enum LASH_Event_Type type;
171 const char * str;
173 type = lash_event_get_type(event_ptr);
174 str = lash_event_get_string(event_ptr);
176 switch (type)
178 case LASH_Quit:
179 g_warning("LASH_Quit received.\n");
180 g_lashc = NULL;
181 gtk_main_quit();
182 break;
183 case LASH_Save_File:
184 case LASH_Restore_File:
185 case LASH_Save_Data_Set:
186 default:
187 g_warning("LASH Event. Type = %u, string = \"%s\"\n",
188 (unsigned int)type,
189 (str == NULL)?"":str);
193 void
194 process_lash_config(lash_config_t * config_ptr)
196 const char * key;
197 const void * data;
198 size_t data_size;
200 key = lash_config_get_key(config_ptr);
201 data = lash_config_get_value(config_ptr);
202 data_size = lash_config_get_value_size(config_ptr);
204 g_warning("LASH Config. Key = \"%s\"\n", key);
207 /* process lash events callback */
208 gboolean
209 process_lash_events(gpointer data)
211 lash_event_t * event_ptr;
212 lash_config_t * config_ptr;
214 /* Process events */
215 while ((event_ptr = lash_get_event(g_lashc)) != NULL)
217 process_lash_event(event_ptr);
218 lash_event_destroy(event_ptr);
221 /* Process configs */
222 while ((config_ptr = lash_get_config(g_lashc)) != NULL)
224 process_lash_config(config_ptr);
225 lash_config_destroy(config_ptr);
228 return TRUE;
231 #endif /* #ifdef HAVE_LASH_1_0 */
233 static
234 void
235 sigint_handler(
236 int i)
238 gtk_main_quit();
242 main(int argc, char *argv[])
244 #ifdef HAVE_LASH_1_0
245 lash_event_t * lash_event_ptr;
246 #endif
247 GString * client_name_str_ptr;
249 int i;
250 gboolean want_any = TRUE;
251 #ifdef HAVE_JACK
252 gboolean want_jack = FALSE;
253 gboolean jack_enabled = FALSE;
254 #endif
255 #ifdef HAVE_ALSA
256 gboolean want_alsa = FALSE;
257 gboolean alsa_enabled = FALSE;
258 #endif
259 gboolean io_enabled = FALSE;
260 int ret = 0;
262 for (i = 1; i < argc; i++)
264 #ifdef HAVE_JACK
265 if (strcmp(argv[i], "--jack") == 0)
267 want_jack = TRUE;
268 want_any = FALSE;
269 continue;
271 #endif
272 #ifdef HAVE_ALSA
273 if (strcmp(argv[i], "--alsa") == 0)
275 want_alsa = TRUE;
276 want_any = FALSE;
277 continue;
279 #endif
280 if (strcmp(argv[i], "--help") != 0)
282 fprintf(stderr, "Unrecognized parameter \"%s\"\n", argv[i]);
283 ret = 1;
286 fprintf(
287 ret == 0 ? stdout : stderr,
288 "Usage: %s"
289 #ifdef HAVE_JACK
290 " [--jack]"
291 #endif
292 #ifdef HAVE_ALSA
293 " [--alsa]"
294 #endif
295 "\n",
296 argv[0]);
298 exit(ret);
301 /* init threads */
302 g_thread_init(NULL);
303 gdk_threads_init();
304 gdk_threads_enter();
306 gtk_init(&argc, &argv);
308 path_init(argv[0]);
310 #ifdef HAVE_LASH_1_0
311 if (getenv("LADISH_APPNAME") != NULL)
313 g_lashc = lash_init(
314 lash_extract_args(&argc, &argv),
315 "gmidimonitor",
317 LASH_PROTOCOL_VERSION);
318 if (g_lashc == NULL)
320 g_warning("Failed to connect to LASH. Session management will not occur.\n");
322 else
324 lash_event_ptr = lash_event_new_with_type(LASH_Client_Name);
325 lash_event_set_string(lash_event_ptr, "GMIDImonitor");
326 lash_send_event(g_lashc, lash_event_ptr);
327 g_timeout_add(250, process_lash_events, NULL);
330 #endif
332 /* interface creation */
333 create_mainwindow();
335 client_name_str_ptr = g_string_new("");
336 g_string_sprintf(client_name_str_ptr, "MIDI monitor");
338 g_row_count = 0;
340 #ifdef HAVE_JACK
341 if (want_jack || want_any)
343 jack_enabled = jack_init(client_name_str_ptr->str);
344 if (jack_enabled)
346 g_printf("JACK MIDI enabled\n");
347 io_enabled = TRUE;
349 else if (want_jack)
351 goto failed_jack;
354 #endif
356 #ifdef HAVE_ALSA
357 if (want_alsa || (want_any && !io_enabled))
359 alsa_enabled = alsa_init(client_name_str_ptr->str);
360 if (alsa_enabled)
362 g_printf("ALSA MIDI enabled\n");
363 io_enabled = TRUE;
365 else if (want_alsa)
367 goto failed_alsa;
370 #endif
372 if (io_enabled)
374 signal(SIGINT, &sigint_handler);
375 signal(SIGTERM, &sigint_handler);
377 /* main loop */
378 gtk_main();
381 #ifdef HAVE_ALSA
382 if (alsa_enabled)
384 alsa_uninit();
386 failed_alsa:
387 #endif
389 #ifdef HAVE_JACK
390 if (jack_enabled)
392 jack_uninit();
394 failed_jack:
395 #endif
397 gdk_threads_leave();
399 path_init(argv[0]);
401 return ret;