summaryrefslogtreecommitdiff
path: root/gui_app.c
blob: e58e4aeaf6451128443de9cc86157143cd8bfdb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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);
}