enroll.c 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * fprintd example to enroll right index finger
 * 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 <stdlib.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"
24
#include "marshal.h"
25 26 27 28 29 30 31 32

static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;

static void create_manager(void)
{
	GError *error = NULL;

33
	connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
34 35 36
	if (connection == NULL)
		g_error("Failed to connect to session bus: %s", error->message);

37
	manager = dbus_g_proxy_new_for_name(connection,
38
		"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
39
		"net.reactivated.Fprint.Manager");
40 41
}

42
static DBusGProxy *open_device(const char *username)
43 44 45 46 47
{
	GError *error = NULL;
	gchar *path;
	DBusGProxy *dev;

Bastien Nocera's avatar
Bastien Nocera committed
48
	if (!net_reactivated_Fprint_Manager_get_default_device(manager, &path, &error))
49 50
		g_error("list_devices failed: %s", error->message);
	
Bastien Nocera's avatar
Bastien Nocera committed
51
	if (path == NULL) {
52 53 54 55 56 57 58 59 60 61
		g_print("No devices found\n");
		exit(1);
	}

	g_print("Using device %s\n", path);

	/* FIXME use for_name_owner?? */
	dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
		path, "net.reactivated.Fprint.Device");

Bastien Nocera's avatar
Bastien Nocera committed
62
	g_free (path);
63

64
	if (!net_reactivated_Fprint_Device_claim(dev, username, &error))
65 66 67 68
		g_error("failed to claim device: %s", error->message);
	return dev;
}

69
static void enroll_result(GObject *object, const char *result, gboolean done, void *user_data)
70 71
{
	gboolean *enroll_completed = user_data;
72
	g_print("Enroll result: %s\n", result);
73 74
	if (done != FALSE)
		*enroll_completed = TRUE;
75 76 77 78 79 80 81 82
		*enroll_completed = TRUE;
}

static void do_enroll(DBusGProxy *dev)
{
	GError *error;
	gboolean enroll_completed = FALSE;

83
	dbus_g_proxy_add_signal(dev, "EnrollStatus", G_TYPE_STRING, G_TYPE_BOOLEAN, NULL);
84
	dbus_g_proxy_connect_signal(dev, "EnrollStatus", G_CALLBACK(enroll_result),
85
				    &enroll_completed, NULL);
86 87

	g_print("Enrolling right index finger.\n");
88
	if (!net_reactivated_Fprint_Device_enroll_start(dev, "right-index-finger", &error))
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		g_error("EnrollStart failed: %s", error->message);

	while (!enroll_completed)
		g_main_context_iteration(NULL, TRUE);

	dbus_g_proxy_disconnect_signal(dev, "EnrollStatus",
		G_CALLBACK(enroll_result), &enroll_completed);

	if (!net_reactivated_Fprint_Device_enroll_stop(dev, &error))
		g_error("VerifyStop failed: %s", error->message);
}

static void release_device(DBusGProxy *dev)
{
	GError *error = NULL;
	if (!net_reactivated_Fprint_Device_release(dev, &error))
		g_error("ReleaseDevice failed: %s", error->message);
}

int main(int argc, char **argv)
{
	GMainLoop *loop;
	DBusGProxy *dev;
112
	char *username;
113 114

	g_type_init();
115 116 117 118

	dbus_g_object_register_marshaller (fprintd_marshal_VOID__STRING_BOOLEAN,
					   G_TYPE_NONE, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID);

119 120 121
	loop = g_main_loop_new(NULL, FALSE);
	create_manager();

122 123 124 125
	username = NULL;
	if (argc == 2)
		username = argv[1];
	dev = open_device(username);
126 127 128 129 130
	do_enroll(dev);
	release_device(dev);
	return 0;
}