demo: Port to new API

This commit is contained in:
Benjamin Berg 2019-06-13 17:26:57 +02:00
parent dd40aeaa79
commit 7d6b0c1376
4 changed files with 66 additions and 311 deletions

View file

@ -22,8 +22,6 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <libfprint/fprint.h> #include <libfprint/fprint.h>
#include "loop.h"
typedef GtkApplication LibfprintDemo; typedef GtkApplication LibfprintDemo;
typedef GtkApplicationClass LibfprintDemoClass; typedef GtkApplicationClass LibfprintDemoClass;
@ -45,10 +43,10 @@ typedef struct {
GtkWidget *spinner; GtkWidget *spinner;
GtkWidget *instructions; GtkWidget *instructions;
struct fp_dscv_dev *ddev; gboolean opened;
struct fp_dev *dev; FpDevice *dev;
struct fp_img *img; FpImage *img;
ImageDisplayFlags img_flags; ImageDisplayFlags img_flags;
} LibfprintDemoWindow; } LibfprintDemoWindow;
@ -67,27 +65,18 @@ typedef enum {
static void libfprint_demo_set_mode (LibfprintDemoWindow *win, static void libfprint_demo_set_mode (LibfprintDemoWindow *win,
LibfprintDemoMode mode); LibfprintDemoMode mode);
static void
pixbuf_destroy (guchar *pixels, gpointer data)
{
if (pixels == NULL)
return;
g_free (pixels);
}
static unsigned char * static unsigned char *
img_to_rgbdata (struct fp_img *img, img_to_rgbdata (const guint8 *imgdata,
int width, int width,
int height) int height)
{ {
int size = width * height; int size = width * height;
unsigned char *imgdata = fp_img_get_data (img); guint8 *rgbdata = g_malloc (size * 3);
unsigned char *rgbdata = g_malloc (size * 3);
size_t i; size_t i;
size_t rgb_offset = 0; size_t rgb_offset = 0;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
unsigned char pixel = imgdata[i]; guint8 pixel = imgdata[i];
rgbdata[rgb_offset++] = pixel; rgbdata[rgb_offset++] = pixel;
rgbdata[rgb_offset++] = pixel; rgbdata[rgb_offset++] = pixel;
@ -101,8 +90,7 @@ static void
plot_minutiae (unsigned char *rgbdata, plot_minutiae (unsigned char *rgbdata,
int width, int width,
int height, int height,
struct fp_minutia **minlist, GPtrArray *minutiae)
int nr_minutiae)
{ {
int i; int i;
#define write_pixel(num) do { \ #define write_pixel(num) do { \
@ -111,8 +99,8 @@ plot_minutiae (unsigned char *rgbdata,
rgbdata[((num) * 3) + 2] = 0; \ rgbdata[((num) * 3) + 2] = 0; \
} while(0) } while(0)
for (i = 0; i < nr_minutiae; i++) { for (i = 0; i < minutiae->len; i++) {
struct fp_minutia *min = minlist[i]; struct fp_minutia *min = g_ptr_array_index (minutiae, i);
int x, y; int x, y;
size_t pixel_offset; size_t pixel_offset;
@ -136,36 +124,37 @@ plot_minutiae (unsigned char *rgbdata,
} }
static GdkPixbuf * static GdkPixbuf *
img_to_pixbuf (struct fp_img *img, img_to_pixbuf (FpImage *img,
ImageDisplayFlags flags) ImageDisplayFlags flags)
{ {
int width; int width;
int height; int height;
const guint8 *data;
unsigned char *rgbdata; unsigned char *rgbdata;
width = fp_img_get_width (img); width = fp_image_get_width (img);
height = fp_img_get_height (img); height = fp_image_get_height (img);
if (flags & IMAGE_DISPLAY_BINARY) { if (flags & IMAGE_DISPLAY_BINARY)
struct fp_img *binary; data = fp_image_get_binarized (img, NULL);
binary = fp_img_binarize (img); else
rgbdata = img_to_rgbdata (binary, width, height); data = fp_image_get_data (img, NULL);
fp_img_free (binary);
} else { if (!data)
rgbdata = img_to_rgbdata (img, width, height); return NULL;
}
rgbdata = img_to_rgbdata (data, width, height);
if (flags & IMAGE_DISPLAY_MINUTIAE) { if (flags & IMAGE_DISPLAY_MINUTIAE) {
struct fp_minutia **minlist; GPtrArray *minutiae;
int nr_minutiae;
minlist = fp_img_get_minutiae (img, &nr_minutiae); minutiae = fp_image_get_minutiae (img);
plot_minutiae (rgbdata, width, height, minlist, nr_minutiae); plot_minutiae (rgbdata, width, height, minutiae);
} }
return gdk_pixbuf_new_from_data (rgbdata, GDK_COLORSPACE_RGB, return gdk_pixbuf_new_from_data (rgbdata, GDK_COLORSPACE_RGB,
FALSE, 8, width, height, FALSE, 8, width, height,
width * 3, pixbuf_destroy, width * 3, (GdkPixbufDestroyNotify) g_free,
NULL); NULL);
} }
@ -201,12 +190,10 @@ libfprint_demo_set_spinner_label (LibfprintDemoWindow *win,
static void static void
libfprint_demo_set_capture_label (LibfprintDemoWindow *win) libfprint_demo_set_capture_label (LibfprintDemoWindow *win)
{ {
struct fp_driver *drv; FpScanType scan_type;
enum fp_scan_type scan_type;
const char *message; const char *message;
drv = fp_dscv_dev_get_driver (win->ddev); scan_type = fp_device_get_scan_type(win->dev);
scan_type = fp_driver_get_scan_type(drv);
switch (scan_type) { switch (scan_type) {
case FP_SCAN_TYPE_PRESS: case FP_SCAN_TYPE_PRESS:
@ -223,46 +210,49 @@ libfprint_demo_set_capture_label (LibfprintDemoWindow *win)
} }
static void static void
dev_capture_start_cb (struct fp_dev *dev, dev_capture_start_cb (FpDevice *dev,
int result, GAsyncResult *res,
struct fp_img *img,
void *user_data) void *user_data)
{ {
g_autoptr(GError) error = NULL;
LibfprintDemoWindow *win = user_data; LibfprintDemoWindow *win = user_data;
FpImage *image = NULL;
if (result < 0) { image = fp_device_capture_finish (dev, res, &error);
if (!image) {
g_warning ("Error capturing data: %s", error->message);
libfprint_demo_set_mode (win, ERROR_MODE); libfprint_demo_set_mode (win, ERROR_MODE);
return; return;
} }
fp_async_capture_stop (dev, NULL, NULL); g_clear_object (&win->img);
win->img = image;
win->img = img;
update_image (win); update_image (win);
libfprint_demo_set_mode (win, CAPTURE_MODE); libfprint_demo_set_mode (win, CAPTURE_MODE);
} }
static void static void
dev_open_cb (struct fp_dev *dev, int status, void *user_data) dev_start_capture (LibfprintDemoWindow *win)
{ {
LibfprintDemoWindow *win = user_data;
int r;
if (status < 0) {
libfprint_demo_set_mode (win, ERROR_MODE);
return;
}
libfprint_demo_set_capture_label (win); libfprint_demo_set_capture_label (win);
win->dev = dev; fp_device_capture (win->dev, TRUE, NULL, (GAsyncReadyCallback) dev_capture_start_cb, win);
r = fp_async_capture_start (win->dev, FALSE, dev_capture_start_cb, user_data); }
if (r < 0) {
g_warning ("fp_async_capture_start failed: %d", r); static void
dev_open_cb (FpDevice *dev, GAsyncResult *res, void *user_data)
{
LibfprintDemoWindow *win = user_data;
g_autoptr(GError) error = NULL;
if (!fp_device_open_finish (dev, res, &error)) {
g_warning ("Failed to open device: %s", error->message);
libfprint_demo_set_mode (win, ERROR_MODE); libfprint_demo_set_mode (win, ERROR_MODE);
return; return;
} }
dev_start_capture(win);
} }
static void static void
@ -271,24 +261,19 @@ activate_capture (GSimpleAction *action,
gpointer user_data) gpointer user_data)
{ {
LibfprintDemoWindow *win = user_data; LibfprintDemoWindow *win = user_data;
int r;
libfprint_demo_set_mode (win, SPINNER_MODE); libfprint_demo_set_mode (win, SPINNER_MODE);
g_clear_pointer (&win->img, fp_img_free); g_clear_pointer (&win->img, g_object_unref);
if (win->dev != NULL) { if (win->opened) {
dev_open_cb (win->dev, 0, user_data); dev_start_capture (win);
return; return;
} }
libfprint_demo_set_spinner_label (win, "Opening fingerprint reader"); libfprint_demo_set_spinner_label (win, "Opening fingerprint reader");
r = fp_async_dev_open (win->ddev, dev_open_cb, user_data); win->opened = TRUE;
if (r < 0) { fp_device_open (win->dev, NULL, (GAsyncReadyCallback) dev_open_cb, user_data);
g_warning ("fp_async_dev_open failed: %d", r);
libfprint_demo_set_mode (win, ERROR_MODE);
return;
}
} }
static void static void
@ -397,7 +382,6 @@ static void
libfprint_demo_set_mode (LibfprintDemoWindow *win, libfprint_demo_set_mode (LibfprintDemoWindow *win,
LibfprintDemoMode mode) LibfprintDemoMode mode)
{ {
struct fp_driver *drv;
char *title; char *title;
switch (mode) { switch (mode) {
@ -415,8 +399,7 @@ libfprint_demo_set_mode (LibfprintDemoWindow *win,
gtk_stack_set_visible_child_name (GTK_STACK (win->mode_stack), "capture-mode"); gtk_stack_set_visible_child_name (GTK_STACK (win->mode_stack), "capture-mode");
gtk_widget_set_sensitive (win->capture_button, TRUE); gtk_widget_set_sensitive (win->capture_button, TRUE);
drv = fp_dscv_dev_get_driver (win->ddev); title = g_strdup_printf ("%s Test", fp_device_get_name (win->dev));
title = g_strdup_printf ("%s Test", fp_driver_get_full_name (drv));
gtk_header_bar_set_title (GTK_HEADER_BAR (win->header_bar), title); gtk_header_bar_set_title (GTK_HEADER_BAR (win->header_bar), title);
g_free (title); g_free (title);
@ -456,7 +439,8 @@ libfprint_demo_class_init (LibfprintDemoClass *class)
static void static void
libfprint_demo_window_init (LibfprintDemoWindow *window) libfprint_demo_window_init (LibfprintDemoWindow *window)
{ {
struct fp_dscv_dev **discovered_devs; FpContext *ctx;
GPtrArray *devices;
gtk_widget_init_template (GTK_WIDGET (window)); gtk_widget_init_template (GTK_WIDGET (window));
gtk_window_set_default_size (GTK_WINDOW (window), 700, 500); gtk_window_set_default_size (GTK_WINDOW (window), 700, 500);
@ -465,32 +449,26 @@ libfprint_demo_window_init (LibfprintDemoWindow *window)
win_entries, G_N_ELEMENTS (win_entries), win_entries, G_N_ELEMENTS (win_entries),
window); window);
if (fp_init () < 0) { ctx = fp_context_new ();
libfprint_demo_set_mode (window, ERROR_MODE);
return;
}
setup_pollfds (); devices = fp_context_get_devices(ctx);
if (!devices) {
discovered_devs = fp_discover_devs();
if (!discovered_devs) {
libfprint_demo_set_mode (window, ERROR_MODE); libfprint_demo_set_mode (window, ERROR_MODE);
return; return;
} }
/* Empty list? */ /* Empty list? */
if (discovered_devs[0] == NULL) { if (devices->len == 0) {
fp_dscv_devs_free (discovered_devs);
libfprint_demo_set_mode (window, EMPTY_MODE); libfprint_demo_set_mode (window, EMPTY_MODE);
return; return;
} }
if (!fp_driver_supports_imaging(fp_dscv_dev_get_driver(discovered_devs[0]))) { if (!fp_device_supports_capture(g_ptr_array_index (devices, 0))) {
libfprint_demo_set_mode (window, NOIMAGING_MODE); libfprint_demo_set_mode (window, NOIMAGING_MODE);
return; return;
} }
window->ddev = discovered_devs[0]; window->dev = g_object_ref (g_ptr_array_index (devices, 0));
libfprint_demo_set_mode (window, CAPTURE_MODE); libfprint_demo_set_mode (window, CAPTURE_MODE);
} }

View file

@ -1,196 +0,0 @@
/*
* fprint D-Bus daemon
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <glib.h>
#include <libfprint/fprint.h>
#include <poll.h>
#include <stdlib.h>
#include "loop.h"
struct fdsource {
GSource source;
GSList *pollfds;
};
static gboolean source_prepare(GSource *source, gint *timeout)
{
int r;
struct timeval tv;
r = fp_get_next_timeout(&tv);
if (r == 0) {
*timeout = -1;
return FALSE;
}
if (!timerisset(&tv))
return TRUE;
*timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
return FALSE;
}
static gboolean source_check(GSource *source)
{
struct fdsource *_fdsource = (struct fdsource *) source;
GSList *l;
struct timeval tv;
int r;
if (!_fdsource->pollfds)
return FALSE;
for (l = _fdsource->pollfds; l != NULL; l = l->next) {
GPollFD *pollfd = l->data;
if (pollfd->revents)
return TRUE;
}
r = fp_get_next_timeout(&tv);
if (r == 1 && !timerisset(&tv))
return TRUE;
return FALSE;
}
static gboolean source_dispatch(GSource *source, GSourceFunc callback,
gpointer data)
{
struct timeval zerotimeout = {
.tv_sec = 0,
.tv_usec = 0,
};
/* FIXME error handling */
fp_handle_events_timeout(&zerotimeout);
/* FIXME whats the return value used for? */
return TRUE;
}
static void source_finalize(GSource *source)
{
struct fdsource *_fdsource = (struct fdsource *) source;
GSList *l;
if (!_fdsource->pollfds)
return;
for (l = _fdsource->pollfds; l != NULL; l = l->next) {
GPollFD *pollfd = l->data;
g_source_remove_poll((GSource *) _fdsource, pollfd);
g_slice_free(GPollFD, pollfd);
_fdsource->pollfds = g_slist_delete_link(_fdsource->pollfds, l);
}
g_slist_free(_fdsource->pollfds);
}
static GSourceFuncs sourcefuncs = {
.prepare = source_prepare,
.check = source_check,
.dispatch = source_dispatch,
.finalize = source_finalize,
};
static struct fdsource *fdsource = NULL;
static void pollfd_add(int fd, short events)
{
GPollFD *pollfd;
pollfd = g_slice_new(GPollFD);
pollfd->fd = fd;
pollfd->events = 0;
pollfd->revents = 0;
if (events & POLLIN)
pollfd->events |= G_IO_IN;
if (events & POLLOUT)
pollfd->events |= G_IO_OUT;
fdsource->pollfds = g_slist_prepend(fdsource->pollfds, pollfd);
g_source_add_poll((GSource *) fdsource, pollfd);
}
static void pollfd_added_cb(int fd, short events)
{
g_debug("now monitoring fd %d", fd);
pollfd_add(fd, events);
}
static void pollfd_removed_cb(int fd)
{
GSList *l;
g_debug("no longer monitoring fd %d", fd);
if (!fdsource->pollfds) {
g_debug("cannot remove from list as list is empty?");
return;
}
for (l = fdsource->pollfds; l != NULL; l = l->next) {
GPollFD *pollfd = l->data;
if (pollfd->fd != fd)
continue;
g_source_remove_poll((GSource *) fdsource, pollfd);
g_slice_free(GPollFD, pollfd);
fdsource->pollfds = g_slist_delete_link(fdsource->pollfds, l);
return;
}
g_error("couldn't find fd %d in list\n", fd);
}
int setup_pollfds(void)
{
ssize_t numfds;
size_t i;
struct fp_pollfd *fpfds;
GSource *gsource;
gsource = g_source_new(&sourcefuncs, sizeof(struct fdsource));
fdsource = (struct fdsource *) gsource;
fdsource->pollfds = NULL;
numfds = fp_get_pollfds(&fpfds);
if (numfds < 0) {
if (fpfds)
free(fpfds);
return (int) numfds;
} else if (numfds > 0) {
for (i = 0; i < numfds; i++) {
struct fp_pollfd *fpfd = &fpfds[i];
pollfd_add(fpfd->fd, fpfd->events);
}
}
free(fpfds);
fp_set_pollfd_notifiers(pollfd_added_cb, pollfd_removed_cb);
g_source_attach(gsource, NULL);
return 0;
}

View file

@ -1,27 +0,0 @@
/*
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef POLL_H
#define POLL_H
int setup_pollfds(void);
#endif

View file

@ -7,7 +7,7 @@ bindir = join_paths(prefix, get_option('bindir'))
datadir = join_paths(prefix, get_option('datadir')) datadir = join_paths(prefix, get_option('datadir'))
executable('gtk-libfprint-test', executable('gtk-libfprint-test',
[ 'gtk-libfprint-test.c', 'loop.c', 'loop.h', gtk_test_resources ], [ 'gtk-libfprint-test.c', gtk_test_resources ],
dependencies: [ libfprint_dep, gtk_dep ], dependencies: [ libfprint_dep, gtk_dep ],
include_directories: [ include_directories: [
root_inc, root_inc,