summaryrefslogtreecommitdiff
path: root/hald/linux/probing/probe-hiddev.c
blob: bffc040e3027f46371f770b9237c8fae64b0749e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
 * CVSID: $Id$
 *
 * probe-input.c : Probe input devices
 *
 * Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* asm/types.h required for __s32 in linux/hiddev.h */
#include <asm/types.h>
#include <fcntl.h>
#include <linux/hiddev.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "libhal/libhal.h"

int 
main (int argc, char *argv[])
{
	int fd;
	int ret;
	char *udi;
	char *device_file;
	LibHalContext *ctx = NULL;
	DBusError error;
	char name[256] = "Unknown HID device";
	unsigned int i;
	struct hiddev_devinfo device_info;

	fd = -1;

	/* assume failure */
	ret = 1;

	dbus_error_init (&error);

	udi = getenv ("UDI");
	if (udi == NULL)
		goto out;

	if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
		goto out;

	device_file = getenv ("HAL_PROP_HIDDEV_DEVICE");
	if (device_file == NULL)
		goto out;

	fd = open (device_file, O_RDONLY);
	if (fd < 0)
		goto out;

	if (ioctl (fd, HIDIOCGNAME(sizeof (name)), name) >= 0) {
		if (!libhal_device_set_property_string (ctx, udi, "hiddev.product", name, &error))
			goto out;
		if (!libhal_device_set_property_string (ctx, udi, "info.product", name, &error))
			goto out;
	}

	if (ioctl (fd, HIDIOCGDEVINFO, &device_info) < 0)
		goto out;

	for (i = 0; i < device_info.num_applications; i++) {
		int appl;
		const char *appl_name;
		char buf[256];

		if ((appl = ioctl(fd, HIDIOCAPPLICATION, i)) < 0)
			goto out;

		/* The magic values come from various usage table specs */
		switch (appl >> 16)
		{
		case 0x01 :
			appl_name = "Generic Desktop Page";
			break;
		case 0x0c :
			appl_name = "Consumer Product Page";
			break;
		case 0x80 :
			appl_name = "USB Monitor Page";
			break;
		case 0x81 :
			appl_name = "USB Enumerated Values Page";
			break;
		case 0x82 :
			appl_name = "VESA Virtual Controls Page";
			break;
		case 0x83 :
			appl_name = "Reserved Monitor Page";
			break;
		case 0x84 :
			appl_name = "Power Device Page";
			break;
		case 0x85 :
			appl_name = "Battery System Page";
			break;
		case 0x86 :
		case 0x87 :
			appl_name = "Reserved Power Device Page";
			break;
		default :
			snprintf (buf, sizeof (buf), "Unknown page 0x%02x", appl);
			appl_name = buf;
		}

		if (!libhal_device_property_strlist_append (ctx, udi, "hiddev.application_pages", appl_name, &error))
			goto out;
	}

#if 0
	DBusConnection *conn;

	if (fork () == 0) {
		sleep (10);

		LIBHAL_FREE_DBUS_ERROR (&error);
		if ((conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error)) == NULL)
			goto out;
		
		if ((ctx = libhal_ctx_new ()) == NULL)
			goto out;
		if (!libhal_ctx_set_dbus_connection (ctx, conn))
			goto out;
		if (!libhal_ctx_init (ctx, &error))
			goto out;

		main2 (ctx, "/org/freedesktop/Hal/devices/usb_device_51d_2_QB0435136106  _if0_hiddev", fd);
	}
	else
		sleep (2);
#endif

	/* success */
	ret = 0;

out:
	if (fd >= 0)
		close (fd);

	LIBHAL_FREE_DBUS_ERROR (&error);

	if (ctx != NULL) {
		libhal_ctx_shutdown (ctx, &error);
		LIBHAL_FREE_DBUS_ERROR (&error);
		libhal_ctx_free (ctx);
	}

	return ret;
}