summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorNicolas Schodet2019-11-13 22:35:11 +0100
committerNicolas Schodet2019-11-14 00:41:59 +0100
commitea90facf4674046e0a58969a6e0eb23054c550de (patch)
tree2ad747ab3f376b0102bedb21d996555fa1416343 /main.c
parentaea9101e6b3da183adcd99f5377c799d194efd11 (diff)
Use GLib for command line too
Diffstat (limited to 'main.c')
-rw-r--r--main.c87
1 files changed, 8 insertions, 79 deletions
diff --git a/main.c b/main.c
index 9ffd6f2..2abeeda 100644
--- a/main.c
+++ b/main.c
@@ -20,88 +20,17 @@
* Web: http://ni.fr.eu.org/
* Email: <nico at ni.fr.eu.org>
*/
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <png.h>
+#include <gtk/gtk.h>
-#include "device.h"
-#include "gui.h"
-#include "options.h"
-#include "utils.h"
-
-void
-cli_read_images(libusb_context *usb, struct device *device, int count,
- const char *out)
-{
- device_start(device);
- for (int i = 0; i < count;) {
- const struct device_image *device_image = device_read(device);
- if (device_image) {
- char *name = NULL;
- if (asprintf(&name, out, i) < 0)
- utils_fatal("can not prepare file name");
- utils_info("write %s", name);
- png_image image;
- memset(&image, 0, sizeof(image));
- image.version = PNG_IMAGE_VERSION;
- image.width = device_image->width;
- image.height = device_image->height;
- image.format = PNG_FORMAT_BGRA;
- int r = png_image_write_to_file(&image, name, 0,
- device_image->pixels, device_image->stride, NULL);
- if (r == 0)
- utils_fatal("can not write image: %s", image.message);
- free(name);
- i++;
- } else {
- libusb_handle_events(usb);
- }
- }
- device_stop(device);
-}
-
-int
-cli_run(struct options *options)
-{
- libusb_context *usb;
- int r = libusb_init(&usb);
- if (r)
- utils_fatal("unable to initialize libusb: %s", libusb_strerror(r));
- struct device *device = device_open(usb);
- if (!device)
- utils_fatal("unable to find device");
- const struct device_info *info = device_get_info(device);
- if (options->exposure_ms > 0.0)
- device_set_exposure(device, options->exposure_ms);
- if (options->gain > 0.0)
- device_set_gain(device, options->gain);
- int width = -1, height = -1;
- for (int i = 0; width == -1 && i < info->resolutions; i++) {
- const struct device_info_resolution *ir = &info->resolution[i];
- if ((options->width == -1 || options->width == ir->width)
- && (options->height == -1 || options->height == ir->height)) {
- width = ir->width;
- height = ir->height;
- }
- }
- if (width == -1)
- utils_fatal("no matching resolution");
- device_set_resolution(device, width, height, 0);
- cli_read_images(usb, device, options->count, options->out);
- device_close(device);
- libusb_exit(usb);
- return EXIT_SUCCESS;
-}
+#include "gui_app.h"
int
main(int argc, char **argv)
{
- struct options options;
- options_parse(argc, argv, &options);
- if (!options.count)
- return gui_run(argc, argv);
- else
- return cli_run(&options);
+ int status;
+ GuiApp *app;
+ app = gui_app_new();
+ status = g_application_run(G_APPLICATION(app), argc, argv);
+ g_object_unref(app);
+ return status;
}