Follow changes in pkg.m4, fixing Gnome bug #166537, that happened on 2005-10-17 and...
[gmidimonitor.git] / main.c
blobb74c57ca35903d947068a14116d50ea769a53920
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 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 <gtk/gtk.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #ifdef HAVE_LASH
33 #include <lash/lash.h>
34 #endif
36 #include "common.h"
37 #include "path.h"
38 #include "glade.h"
39 #include "gm.h"
40 #include "jack.h"
41 #include "alsa.h"
43 GtkWidget * g_main_window_ptr;
45 gboolean g_midi_ignore = FALSE;
47 int g_row_count;
49 #ifdef HAVE_LASH
50 lash_client_t * g_lashc;
51 #endif
53 const char * g_note_names[12] =
55 "C",
56 "C#",
57 "D",
58 "D#",
59 "E",
60 "F",
61 "F#",
62 "G",
63 "G#",
64 "A",
65 "A#",
66 "B",
69 void
70 create_mainwindow()
72 GtkWidget * child_ptr;
73 GtkListStore * list_store_ptr;
74 GtkTreeViewColumn * column_ptr;
75 GtkCellRenderer * text_renderer_ptr;
77 g_main_window_ptr = construct_glade_widget("main_window");
79 child_ptr = get_glade_widget_child(g_main_window_ptr, "list");
81 text_renderer_ptr = gtk_cell_renderer_text_new();
83 g_object_set(
84 G_OBJECT(text_renderer_ptr),
85 "family",
86 "Monospace",
87 NULL);
89 /* column_ptr = gtk_tree_view_column_new_with_attributes( */
90 /* "Time", */
91 /* text_renderer_ptr, */
92 /* "text", COL_TIME, */
93 /* NULL); */
95 /* gtk_tree_view_append_column( */
96 /* GTK_TREE_VIEW(child_ptr), */
97 /* column_ptr); */
99 column_ptr = gtk_tree_view_column_new_with_attributes(
100 "Channel",
101 text_renderer_ptr,
102 "text", COL_CHANNEL,
103 NULL);
105 gtk_tree_view_append_column(
106 GTK_TREE_VIEW(child_ptr),
107 column_ptr);
109 column_ptr = gtk_tree_view_column_new_with_attributes(
110 "Message",
111 text_renderer_ptr,
112 "text", COL_MESSAGE,
113 NULL);
115 gtk_tree_view_append_column(
116 GTK_TREE_VIEW(child_ptr),
117 column_ptr);
119 gtk_widget_show_all(g_main_window_ptr);
121 list_store_ptr = gtk_list_store_new(
122 NUM_COLS,
123 G_TYPE_STRING,
124 G_TYPE_STRING,
125 G_TYPE_STRING);
127 gtk_tree_view_set_model(GTK_TREE_VIEW(child_ptr), GTK_TREE_MODEL(list_store_ptr));
130 /* stop button toggle */
131 void on_button_stop_toggled(
132 GtkAction * action_ptr,
133 GtkWidget * widget_ptr)
135 GtkWidget * child_ptr;
137 child_ptr = get_glade_widget_child(widget_ptr, "button_stop");
139 if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(child_ptr)))
141 g_midi_ignore = TRUE;
143 else
145 g_midi_ignore = FALSE;
149 void on_clear_clicked
150 (GtkButton *button, gpointer user_data)
152 gtk_list_store_clear(
153 GTK_LIST_STORE(
154 gtk_tree_view_get_model(
155 GTK_TREE_VIEW(
156 get_glade_widget_child(
157 g_main_window_ptr,
158 "list")))));
159 g_row_count = 0;
162 #ifdef HAVE_LASH
164 void
165 process_lash_event(lash_event_t * event_ptr)
167 enum LASH_Event_Type type;
168 const char * str;
170 type = lash_event_get_type(event_ptr);
171 str = lash_event_get_string(event_ptr);
173 switch (type)
175 case LASH_Quit:
176 g_warning("LASH_Quit received.\n");
177 g_lashc = NULL;
178 gtk_main_quit();
179 break;
180 case LASH_Save_File:
181 case LASH_Restore_File:
182 case LASH_Save_Data_Set:
183 default:
184 g_warning("LASH Event. Type = %u, string = \"%s\"\n",
185 (unsigned int)type,
186 (str == NULL)?"":str);
190 void
191 process_lash_config(lash_config_t * config_ptr)
193 const char * key;
194 const void * data;
195 size_t data_size;
197 key = lash_config_get_key(config_ptr);
198 data = lash_config_get_value(config_ptr);
199 data_size = lash_config_get_value_size(config_ptr);
201 g_warning("LASH Config. Key = \"%s\"\n", key);
204 /* process lash events callback */
205 gboolean
206 process_lash_events(gpointer data)
208 lash_event_t * event_ptr;
209 lash_config_t * config_ptr;
211 /* Process events */
212 while ((event_ptr = lash_get_event(g_lashc)) != NULL)
214 process_lash_event(event_ptr);
215 lash_event_destroy(event_ptr);
218 /* Process configs */
219 while ((config_ptr = lash_get_config(g_lashc)) != NULL)
221 process_lash_config(config_ptr);
222 lash_config_destroy(config_ptr);
225 return TRUE;
228 #endif /* #ifdef HAVE_LASH */
231 main(int argc, char *argv[])
233 #ifdef HAVE_LASH
234 lash_event_t * lash_event_ptr;
235 #endif
236 GString * client_name_str_ptr;
238 /* init threads */
239 g_thread_init(NULL);
240 gdk_threads_init();
241 gdk_threads_enter();
243 gtk_init(&argc, &argv);
245 path_init(argv[0]);
247 #ifdef HAVE_LASH
248 g_lashc = lash_init(
249 lash_extract_args(&argc, &argv),
250 "gmidimonitor",
252 LASH_PROTOCOL_VERSION);
254 if (g_lashc == NULL)
256 g_warning("Failed to connect to LASH. Session management will not occur.\n");
258 else
260 lash_event_ptr = lash_event_new_with_type(LASH_Client_Name);
261 lash_event_set_string(lash_event_ptr, "GMIDImonitor");
262 lash_send_event(g_lashc, lash_event_ptr);
263 g_timeout_add(250, process_lash_events, NULL);
265 #endif
267 /* interface creation */
268 create_mainwindow();
270 client_name_str_ptr = g_string_new("");
271 g_string_sprintf(client_name_str_ptr, "MIDI monitor (%u)", (unsigned int)getpid());
273 g_row_count = 0;
275 #ifdef HAVE_JACK_MIDI
276 if (!jack_init(client_name_str_ptr->str))
278 goto fail;
280 #endif
282 #ifdef HAVE_ALSA_MIDI
283 if (!alsa_init(client_name_str_ptr->str))
285 goto fail_uninit_jack;
287 #endif
289 /* main loop */
290 gtk_main();
292 #ifdef HAVE_ALSA_MIDI
293 alsa_uninit();
295 fail_uninit_jack:
296 #endif
298 #ifdef HAVE_JACK_MIDI
299 jack_uninit();
301 fail:
302 #endif
304 gdk_threads_leave();
306 path_init(argv[0]);
308 return 0;