summaryrefslogtreecommitdiff
path: root/src/gui_app.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui_app.c')
-rw-r--r--src/gui_app.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/gui_app.c b/src/gui_app.c
new file mode 100644
index 0000000..e7cc862
--- /dev/null
+++ b/src/gui_app.c
@@ -0,0 +1,127 @@
+/* Camicro - Microscope camera viewer.
+ *
+ * Copyright (C) 2019 Nicolas Schodet
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contact :
+ * Web: http://ni.fr.eu.org/
+ * Email: <nico at ni.fr.eu.org>
+ */
+#include "gui_app.h"
+#include "gui_app_window.h"
+#include "options.h"
+#include "usb_source.h"
+#include "utils.h"
+
+struct _GuiApp {
+ GtkApplication parent;
+};
+
+typedef struct _GuiAppPrivate GuiAppPrivate;
+
+struct _GuiAppPrivate {
+ libusb_context *usb;
+ GSource *usb_source;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(GuiApp, gui_app, GTK_TYPE_APPLICATION)
+
+static gboolean
+gui_app_usb_source_cb(gpointer user_data)
+{
+ GuiApp *app = user_data;
+ GuiAppPrivate *priv = gui_app_get_instance_private(app);
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ int r = libusb_handle_events_timeout(priv->usb, &tv);
+ if (r)
+ g_error("unable to handle libusb events: %s", libusb_strerror(r));
+ g_signal_emit_by_name(app, "video-ready");
+ return G_SOURCE_CONTINUE;
+}
+
+static void
+gui_app_init(GuiApp *app)
+{
+ GuiAppPrivate *priv = gui_app_get_instance_private(app);
+ priv->usb = NULL;
+ priv->usb_source = NULL;
+ options_add(G_APPLICATION(app));
+}
+
+static void
+gui_app_startup(GApplication *app)
+{
+ G_APPLICATION_CLASS(gui_app_parent_class)->startup(app);
+ GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
+ int r = libusb_init(&priv->usb);
+ if (r)
+ g_error("unable to initialize libusb: %s", libusb_strerror(r));
+ priv->usb_source = usb_source_new(priv->usb);
+ g_source_set_callback(priv->usb_source, gui_app_usb_source_cb, app,
+ NULL);
+ g_source_attach(priv->usb_source, NULL);
+}
+
+static void
+gui_app_activate(GApplication *app)
+{
+ GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
+ GError *error = NULL;
+ struct device *device = device_open(priv->usb, &error);
+ if (!device) {
+ utils_dialog_error(NULL, "unable to find device: %s", error->message);
+ g_error_free(error);
+ } else {
+ GuiAppWindow *window;
+ window = gui_app_window_new(GUI_APP(app));
+ gui_app_window_open(window, device);
+ gtk_window_present(GTK_WINDOW(window));
+ }
+}
+
+static void
+gui_app_shutdown(GApplication *app)
+{
+ GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
+ if (priv->usb_source) {
+ g_source_destroy(priv->usb_source);
+ g_source_unref(priv->usb_source);
+ }
+ if (priv->usb)
+ libusb_exit(priv->usb);
+ G_APPLICATION_CLASS(gui_app_parent_class)->shutdown(app);
+}
+
+static void
+gui_app_class_init(GuiAppClass *class)
+{
+ G_APPLICATION_CLASS(class)->startup = gui_app_startup;
+ G_APPLICATION_CLASS(class)->activate = gui_app_activate;
+ G_APPLICATION_CLASS(class)->shutdown = gui_app_shutdown;
+ G_APPLICATION_CLASS(class)->handle_local_options = options_handle;
+ g_signal_new("video-ready", GUI_APP_TYPE, G_SIGNAL_RUN_FIRST, 0, NULL,
+ NULL, NULL, G_TYPE_NONE, 0);
+}
+
+GuiApp *
+gui_app_new(void)
+{
+ return g_object_new(GUI_APP_TYPE,
+ "application-id", "org.eu.fr.ni.Camicro",
+ NULL);
+}