From 1465331397ffdd4c6a47d53a82c4ed33a022e36e Mon Sep 17 00:00:00 2001 From: c vw Date: Mon, 18 Nov 2019 18:18:32 +0100 Subject: [PATCH] Added button/dialogs for "importing" a MIDI description file --- discovery.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/discovery.c b/discovery.c index 7fccad5..a05c82e 100644 --- a/discovery.c +++ b/discovery.c @@ -27,6 +27,9 @@ #include #include #include +#ifdef MIDI +#include +#endif #include "discovered.h" #include "old_discovery.h" @@ -97,6 +100,94 @@ static gboolean start_cb (GtkWidget *widget, GdkEventButton *event, gpointer dat return TRUE; } +#ifdef MIDI +// +// This is a file open dialog. If we choose a readable file here, it is just copied +// to file "midi.inp" in the local directory +// +static gboolean midi_cb(GtkWidget *widget, GdkEventButton *event, gpointer data) { + GtkWidget *opfile,*message; + GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; + gint res; + int fdin, fdout; + char c; + size_t len,bytes_read,bytes_written; + + opfile = gtk_file_chooser_dialog_new ("Import MIDI description", + GTK_WINDOW(top_window), + action, + "Cancel", + GTK_RESPONSE_CANCEL, + "Open", + GTK_RESPONSE_ACCEPT, + NULL); + + res = gtk_dialog_run (GTK_DIALOG (opfile)); + if (res == GTK_RESPONSE_ACCEPT) { + char *filename; + struct stat statbuf; + GtkFileChooser *chooser = GTK_FILE_CHOOSER (opfile); + char *contents; + filename = gtk_file_chooser_get_filename (chooser); + fdin =open(filename, O_RDONLY); + bytes_read = bytes_written = 0; + if (fdin >= 0) { + fstat(fdin, &statbuf); + len=statbuf.st_size; + // + // Now first read the whole contents of the file, and then write it out. + // This is for new-bees trying to import the midi.inp in the working dir + // + contents=g_new(char, len); + bytes_read = bytes_written = 0; + if (contents) { + bytes_read=read(fdin, contents, len); + } + close(fdin); + } + fdout=0; + if (contents && bytes_read > 0) { + fdout=open("midi.inp", O_WRONLY | O_CREAT, 0644); + if (fdout >= 0) { + bytes_written=write(fdout, contents, len); + close(fdout); + g_free(contents); + } + } + if (fdin < 0) { + message = gtk_message_dialog_new (GTK_WINDOW(top_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + "Cannot read file!\n"); + gtk_dialog_run (GTK_DIALOG (message)); + gtk_widget_destroy(message); + } + if (fdout < 0) { + message = gtk_message_dialog_new (GTK_WINDOW(top_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + "Cannot write midi.inp file!\n"); + gtk_dialog_run (GTK_DIALOG (message)); + gtk_widget_destroy(message); + } + if (fdin >= 0 && fdout >= 0) { + message = gtk_message_dialog_new (GTK_WINDOW(top_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + "File Length : %ld\nBytes read : %ld\nBytes written: %ld\n",len,bytes_read,bytes_written); + gtk_dialog_run (GTK_DIALOG (message)); + gtk_widget_destroy(message); + } + g_free(filename); + } + gtk_widget_destroy (opfile); + return TRUE; +} +#endif + #ifdef GPIO static gboolean gpio_cb (GtkWidget *widget, GdkEventButton *event, gpointer data) { configure_gpio(discovery_dialog); @@ -410,6 +501,12 @@ fprintf(stderr,"%p Protocol=%d name=%s\n",d,d->protocol,d->name); g_signal_connect (exit_b, "button-press-event", G_CALLBACK(exit_cb), NULL); gtk_grid_attach(GTK_GRID(grid),exit_b,2,i,1,1); +#ifdef MIDI + GtkWidget *midi_b=gtk_button_new_with_label("ImportMIDI"); + g_signal_connect (midi_b, "button-press-event", G_CALLBACK(midi_cb), NULL); + gtk_grid_attach(GTK_GRID(grid),midi_b,3,i,1,1); +#endif + i++; GtkWidget *tcp_b=gtk_button_new_with_label("Use new TCP Addr:"); g_signal_connect (tcp_b, "button-press-event", G_CALLBACK(tcp_cb), NULL); -- 2.45.2