enroll.c 3.52 KB
Newer Older
Daniel Drake's avatar
Daniel Drake committed
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
/*
 * 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"

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

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

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

36
	manager = dbus_g_proxy_new_for_name(connection,
Daniel Drake's avatar
Daniel Drake committed
37
		"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
38
		"net.reactivated.Fprint.Manager");
Daniel Drake's avatar
Daniel Drake committed
39
40
}

41
static DBusGProxy *open_device(const char *username)
Daniel Drake's avatar
Daniel Drake committed
42
43
44
45
46
{
	GError *error = NULL;
	gchar *path;
	DBusGProxy *dev;

Bastien Nocera's avatar
Bastien Nocera committed
47
	if (!net_reactivated_Fprint_Manager_get_default_device(manager, &path, &error))
Daniel Drake's avatar
Daniel Drake committed
48
49
		g_error("list_devices failed: %s", error->message);
	
Bastien Nocera's avatar
Bastien Nocera committed
50
	if (path == NULL) {
Daniel Drake's avatar
Daniel Drake committed
51
52
53
54
55
56
57
58
59
60
		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
61
	g_free (path);
Daniel Drake's avatar
Daniel Drake committed
62

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

68
static void enroll_result(GObject *object, const char *result, void *user_data)
Daniel Drake's avatar
Daniel Drake committed
69
70
{
	gboolean *enroll_completed = user_data;
71
72
73
74
	g_print("Enroll result: %s\n", result);
	if (g_str_equal(result, "enroll-completed") ||
	    g_str_equal(result, "enroll-failed") ||
	    g_str_equal(result, "enroll-unknown-error"))
Daniel Drake's avatar
Daniel Drake committed
75
76
77
78
79
80
81
82
83
84
85
86
87
		*enroll_completed = TRUE;
}

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

	dbus_g_proxy_add_signal(dev, "EnrollStatus", G_TYPE_INT, NULL);
	dbus_g_proxy_connect_signal(dev, "EnrollStatus", G_CALLBACK(enroll_result),
		&enroll_completed, NULL);

	g_print("Enrolling right index finger.\n");
88
	if (!net_reactivated_Fprint_Device_enroll_start(dev, "right-index-finger", &error))
Daniel Drake's avatar
Daniel Drake committed
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;
Daniel Drake's avatar
Daniel Drake committed
113
114
115
116
117

	g_type_init();
	loop = g_main_loop_new(NULL, FALSE);
	create_manager();

118
119
120
121
	username = NULL;
	if (argc == 2)
		username = argv[1];
	dev = open_device(username);
Daniel Drake's avatar
Daniel Drake committed
122
123
124
125
126
	do_enroll(dev);
	release_device(dev);
	return 0;
}