/[gluid]/callbacks.c
ViewVC logotype

Annotation of /callbacks.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (hide annotations)
Wed Sep 30 20:32:22 2009 UTC (10 years, 7 months ago) by ben
File MIME type: text/plain
File size: 10679 byte(s)
rewrote instanciation: safer, simpler, no limit
1 ben 1 /*
2     * callbacks.c Copyright © 2009 by Benoît Rouits <brouits@free.fr>
3     * Published under the terms of the GNU General Public License v2 (GPLv2).
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     * Boston, MA 02110-1301, USA.
19     *
20     * see the COPYING file included in the jackie package or
21     * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt for the full licence
22     *
23     */
24    
25 ben 38 #include <unistd.h>
26 ben 1 #include <gtk/gtk.h>
27     #include <fluidsynth.h>
28 ben 24 #include <libintl.h>
29     #include <locale.h>
30 ben 10 #include "utils.h"
31     #include "type.h"
32 ben 1 #include "menu.h"
33     #include "about.h"
34     #include "version.h"
35 ben 24 #define _(string) gettext (string)
36 ben 1
37     /* handler for the volume button drag */
38     void volume_on_event (GtkWidget* instance, gdouble value, gpointer app_data) {
39     AppData* d = (AppData*) app_data;
40     fluid_synth_set_gain(d->synth,value*10.0);
41     instance = NULL;
42     app_data = NULL;
43     }
44    
45     /* handler for left-button click */
46     void status_icon_on_left_click(GtkStatusIcon* instance, gpointer app_data) {
47     AppData* d = (AppData*) app_data;
48     GtkVolumeButton* volume;
49     GtkWidget* popup;
50     double curvol;
51    
52     if (!d->synth) /* fluidsynth not started */
53     return;
54     volume = GTK_VOLUME_BUTTON(gtk_volume_button_new());
55     curvol = (fluid_synth_get_gain(d->synth)/10.0);
56     gtk_scale_button_set_value(GTK_SCALE_BUTTON(volume), curvol/10.0);
57     popup = gtk_scale_button_get_popup(GTK_SCALE_BUTTON(volume));
58     gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_MOUSE);
59     g_signal_connect(G_OBJECT(volume), "value-changed", G_CALLBACK(volume_on_event), app_data);
60     g_signal_connect_swapped(popup, "leave-notify-event", G_CALLBACK(gtk_widget_destroy), popup);
61     gtk_widget_show_all(popup);
62     instance = app_data = NULL;
63     }
64    
65     /* handler for right-button click */
66     void status_icon_on_right_click(GtkStatusIcon* instance, guint button, guint activate_time, gpointer app_data) {
67     AppData* d = (AppData*) app_data;
68    
69     if (app_data)
70     menu_show (GTK_MENU(d->right_menu), button, activate_time);
71     instance = NULL;
72     }
73    
74     int midi_ev_cb (void *data, fluid_midi_event_t *event) {
75     /* printf("event type: %d\n", fluid_midi_event_get_type(event)); */
76 ben 8 /* FIXME print something on tooltip ? */
77 ben 1 data = NULL;
78     event = NULL;
79     return 0;
80     }
81    
82     /* handler for the "Play" menu item */
83     void menu_item_on_startstop(GtkMenuItem* instance, gpointer app_data) {
84     AppData* d = (AppData*) app_data;
85 ben 36 gchar* temp;
86 ben 38 guint id;
87 ben 36
88 ben 1 if (!d->synth) { /* is stopped : start it */
89 ben 38 id = getpid();
90 ben 1 d->settings = new_fluid_settings();
91 ben 34 fluid_settings_setstr(d->settings, "audio.driver", d->audio);
92 ben 38 temp = g_strdup_printf("gluid_audio_%d", id);
93 ben 36 fluid_settings_setstr(d->settings, "audio.jack.id", temp);
94     g_free(temp);
95 ben 34 fluid_settings_setstr(d->settings, "midi.driver", d->midi);
96 ben 38 temp = g_strdup_printf("gluid_midi_%d", id);
97 ben 36 fluid_settings_setstr(d->settings, "midi.alsa_seq.id", temp);
98     fluid_settings_setstr(d->settings, "midi.jack.id", temp);
99     g_free(temp);
100 ben 34 fluid_settings_setstr(d->settings, "synth.reverb.active", d->reverb);
101     fluid_settings_setstr(d->settings, "synth.chorus.active", d->chorus);
102    
103 ben 1 d->synth = new_fluid_synth(d->settings);
104 ben 34
105 ben 1 if (fluid_is_soundfont(d->sfpath)) {
106     fluid_synth_sfload(d->synth, d->sfpath, 1);
107     } else {
108 ben 27 gtk_status_icon_set_tooltip(d->status_icon, _("no sound font found"));
109 ben 1 goto stop;
110     }
111 ben 32
112 ben 1 d->adriver = new_fluid_audio_driver(d->settings, d->synth);
113     if (!d->adriver) /* backend already used */
114     goto stop;
115 ben 34
116 ben 1 d->router = new_fluid_midi_router(d->settings, fluid_synth_handle_midi_event, (void*)d->synth);
117     fluid_synth_set_midi_router(d->synth, d->router);
118     if (!d->router)
119     goto stop;
120 ben 34
121 ben 1 d->mdriver = new_fluid_midi_driver(d->settings, fluid_midi_router_handle_midi_event, (void*)d->router);
122     if (!d->mdriver) /* backend already used */
123     goto stop;
124 ben 38 temp = g_strdup_printf (_("fluidsynth %d running"), id);
125 ben 36 gtk_status_icon_set_tooltip(d->status_icon, temp);
126 ben 24 gtk_menu_item_set_label(instance, _("Stop"));
127 ben 1 } else { /* is started : stop it*/
128     stop:
129     if (d->mdriver)
130     delete_fluid_midi_driver(d->mdriver);
131     d->mdriver = NULL;
132     if (d->router)
133     delete_fluid_midi_router(d->router);
134     d->router = NULL;
135     if (d->adriver)
136     delete_fluid_audio_driver(d->adriver);
137     d->adriver = NULL;
138     if (d->synth)
139     delete_fluid_synth(d->synth);
140     d->synth = NULL;
141     if (d->settings)
142     delete_fluid_settings(d->settings);
143     d->settings = NULL;
144    
145 ben 27 gtk_status_icon_set_tooltip(d->status_icon, _("fluidsynth stopped"));
146 ben 24 gtk_menu_item_set_label(instance, _("Start"));
147 ben 1 }
148     }
149    
150     /* handler for the "About" menu item (see version.h) */
151     void menu_item_on_about(GtkMenuItem* instance, gpointer unused) {
152     GtkAboutDialog* about;
153     const gchar* authors [] = {
154     PROG_AUTHOR0,
155     NULL
156     };
157    
158     about = about_create (PROG_NAME, PROG_VERSION, PROG_COPYRIGHT,
159     PROG_COMMENT, PROG_LICENSE, PROG_WEBSITE,
160     authors);
161     about_show (about);
162     unused = NULL; /* avoid compiler warnings */
163     instance = NULL; /* _ */
164     return;
165     }
166    
167     /* callback on Preferences window closed */
168     void pref_window_on_close (gpointer app_data) {
169     AppData* d = (AppData*) app_data;
170    
171     g_free(d->sfpath);
172 ben 10 d->sfpath = g_strdup (gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(d->sfpath_chooser)));
173 ben 1 g_free(d->audio);
174 ben 9 d->audio = g_strdup (gtk_combo_box_get_active_text(d->audio_combo));
175 ben 1 g_free(d->midi);
176 ben 9 d->midi = g_strdup (gtk_combo_box_get_active_text(d->midi_combo));
177 ben 33 g_free(d->reverb);
178 ben 32 d->reverb = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(d->reverb_btn))? g_strdup ("yes"): g_strdup("no");
179 ben 33 g_free(d->chorus);
180 ben 32 d->chorus = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(d->chorus_btn))? g_strdup ("yes"): g_strdup("no");
181 ben 1
182     /* write it to $HOME/.gluid */
183     write_config (d);
184     gtk_widget_hide (GTK_WIDGET(d->pref_window));
185     gtk_widget_destroy (GTK_WIDGET(d->pref_window));
186 ben 20 gtk_widget_set_sensitive (GTK_WIDGET(d->pref_item), TRUE);
187 ben 1 d->pref_window = NULL;
188     }
189    
190     /* handler for the "Preference" menu item */
191     void menu_item_on_pref(GtkMenuItem* instance, gpointer app_data) {
192     AppData* d = (AppData*) app_data;
193     GtkVBox* vbox;
194     GtkHBox* hbox1;
195     GtkHBox* hbox2;
196     GtkHBox* hbox3;
197 ben 13 GtkHBox* hbox4;
198 ben 32 GtkHBox* hbox5;
199 ben 1 GtkLabel* label1;
200     GtkLabel* label2;
201     GtkLabel* label3;
202 ben 32 GtkLabel* label0;
203 ben 13 GtkButton* button1;
204 ben 1
205     d->pref_window = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
206 ben 27 gtk_window_set_title (GTK_WINDOW(d->pref_window), _("Preferences"));
207 ben 15 gtk_window_set_default_size (GTK_WINDOW(d->pref_window), 300, 200);
208 ben 1 gtk_window_set_resizable (GTK_WINDOW(d->pref_window), TRUE);
209    
210 ben 13 vbox = GTK_VBOX(gtk_vbox_new (TRUE, 0));
211 ben 1 gtk_container_add (GTK_CONTAINER(d->pref_window), GTK_WIDGET(vbox));
212    
213     hbox1 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
214 ben 18 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox1), TRUE, FALSE, 10);
215 ben 1
216     hbox2 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
217 ben 18 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox2), TRUE, FALSE, 10);
218 ben 1
219     hbox3 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
220 ben 18 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox3), TRUE, FALSE, 10);
221 ben 1
222 ben 13 hbox4 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
223 ben 18 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox4), TRUE, FALSE, 10);
224 ben 13
225 ben 32 hbox5 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
226     gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox5), TRUE, FALSE, 10);
227    
228 ben 27 label1= GTK_LABEL(gtk_label_new (_("Soundfont Path:")));
229 ben 18 gtk_box_pack_start (GTK_BOX(hbox1), GTK_WIDGET(label1), TRUE, FALSE, 10);
230 ben 27 d->sfpath_chooser = GTK_FILE_CHOOSER_BUTTON(gtk_file_chooser_button_new(_("Choose SF2 file"), GTK_FILE_CHOOSER_ACTION_OPEN));
231 ben 11 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(d->sfpath_chooser), d->sfpath);
232 ben 18 gtk_box_pack_start(GTK_BOX(hbox1), GTK_WIDGET(d->sfpath_chooser), TRUE, TRUE, 10);
233 ben 32 label2 = GTK_LABEL(gtk_label_new (_("Audio Driver:")));
234     gtk_box_pack_start (GTK_BOX(hbox2), GTK_WIDGET(label2), TRUE, FALSE, 10);
235 ben 9 d->audio_combo = GTK_COMBO_BOX(gtk_combo_box_new_text ());
236 ben 14 gtk_combo_box_append_text (d->audio_combo, d->audio);
237 ben 9 gtk_combo_box_append_text (d->audio_combo, "alsa");
238     gtk_combo_box_append_text (d->audio_combo, "jack");
239     gtk_combo_box_append_text (d->audio_combo, "oss");
240     gtk_combo_box_append_text (d->audio_combo, "file");
241 ben 14 gtk_combo_box_set_active(d->audio_combo, 0);
242 ben 18 gtk_box_pack_start(GTK_BOX(hbox2), GTK_WIDGET(d->audio_combo), TRUE, FALSE, 10);
243 ben 32
244     label3 = GTK_LABEL(gtk_label_new (_("Midi Driver:")));
245     gtk_box_pack_start (GTK_BOX(hbox3), GTK_WIDGET(label3), TRUE, FALSE, 10);
246 ben 9 d->midi_combo = GTK_COMBO_BOX(gtk_combo_box_new_text ());
247 ben 14 gtk_combo_box_append_text (d->midi_combo, d->midi);
248 ben 19 gtk_combo_box_append_text (d->midi_combo, "alsa_raw");
249 ben 9 gtk_combo_box_append_text (d->midi_combo, "alsa_seq");
250     gtk_combo_box_append_text (d->midi_combo, "jack");
251     gtk_combo_box_append_text (d->midi_combo, "oss");
252 ben 14 gtk_combo_box_set_active(d->midi_combo, 0);
253 ben 18 gtk_box_pack_start(GTK_BOX(hbox3), GTK_WIDGET(d->midi_combo), TRUE, FALSE, 10);
254 ben 13
255 ben 32 d->reverb_btn = gtk_check_button_new_with_label (_("Reverb"));
256     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(d->reverb_btn), (gboolean)!g_strcmp0(d->reverb,"yes"));
257     gtk_box_pack_start (GTK_BOX(hbox4), GTK_WIDGET(d->reverb_btn), TRUE, FALSE, 10);
258    
259     d->chorus_btn = gtk_check_button_new_with_label (_("Chorus"));
260     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(d->chorus_btn), (gboolean)!g_strcmp0(d->chorus,"yes"));
261     gtk_box_pack_start (GTK_BOX(hbox4), GTK_WIDGET(d->chorus_btn), TRUE, FALSE, 10);
262    
263     label0 = GTK_LABEL(gtk_label_new (""));
264     gtk_box_pack_start (GTK_BOX(hbox5), GTK_WIDGET(label0), TRUE, FALSE, 10);
265    
266 ben 13 button1 = GTK_BUTTON(gtk_button_new_from_stock (GTK_STOCK_CLOSE));
267 ben 32 gtk_box_pack_start (GTK_BOX(hbox5), GTK_WIDGET(button1), FALSE, FALSE, 10);
268 ben 13
269     g_signal_connect_swapped (G_OBJECT(button1), "clicked", G_CALLBACK(pref_window_on_close), (gpointer)d);
270 ben 1 g_signal_connect_swapped (G_OBJECT(d->pref_window), "delete-event", G_CALLBACK(pref_window_on_close), (gpointer)d);
271    
272     gtk_widget_show_all (GTK_WIDGET(d->pref_window));
273 ben 20 gtk_widget_set_sensitive (GTK_WIDGET(instance), FALSE);
274 ben 1 }
275    
276     /* handler for the "Quit" menu item */
277     void menu_item_on_quit(GtkMenuItem* instance, gpointer app_data) {
278     AppData* d = (AppData*) app_data;
279     quit (d);
280     instance = NULL; /* useless but does not warn at compile time */
281     }
282    

  ViewVC Help
Powered by ViewVC 1.1.26