]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
Added button/dialogs for "importing" a MIDI description file
authorc vw <dl1ycf@darc.de>
Mon, 18 Nov 2019 17:18:32 +0000 (18:18 +0100)
committerc vw <dl1ycf@darc.de>
Mon, 18 Nov 2019 17:18:32 +0000 (18:18 +0100)
discovery.c

index 7fccad552e29c32d1e1af5307be1408af75f3a43..a05c82ebac1a076445c17746f3f820ceec4890ef 100644 (file)
@@ -27,6 +27,9 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#ifdef MIDI
+#include <sys/stat.h>
+#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);