summaryrefslogtreecommitdiff
path: root/src/util/wacomcfg.c
blob: f9eb7cdd409b43919c4283b2f73ed942f0dbd6ab (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*****************************************************************************
** wacomcfg.c
**
** Copyright (C) 2003-2004 - John E. Joganic
** Copyright (C) 2004-2009 - Ping Cheng
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
** REVISION HISTORY
**   2003-05-02 0.0.1 - JEJ - created
**   2004-05-28 0.0.2 - PC - updated WacomConfigListDevices
**   2005-06-10 0.0.3 - PC - updated for x86_64
**   2005-10-24 0.0.4 - PC - Added Pad
**   2005-11-17 0.0.5 - PC - update mode code
**   2006-07-17 0.0.6 - PC - Exchange info directly with wacom_drv.o
**   2007-01-10 0.0.7 - PC - don't display uninitialized tools
**   2008-03-24 0.0.8 - PC - Added touch for serial TabletPC (ISDv4)
**   2008-07-31 0.0.9 - PC - Added patches from Danny Kukawka
**   2009-10-29 0.1.0 - PC - Support hot-plugged names
**
****************************************************************************/

#include "../include/util-config.h"
#include "wacomcfg.h"
#include "../include/Xwacom.h" /* Hopefully it will be included in XFree86 someday, but
                     * in the meantime, we are expecting it in the local
                     * directory. */

#include <stdio.h> /* debugging only, we've got no business output text */
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <memory.h>
#include <assert.h>

WACOMDEVICETYPE mapStringToType (const char*);
#if WCM_XF86CONFIG
	#include "xf86Parser.h"

	WACOMDEVICETYPE checkIfWacomDevice (XF86ConfigPtr, const char*);
	XF86ConfigPtr readConfig (char *);
	void VErrorF(const char*, va_list);
	void ErrorF (const char*, ...);
#endif

/*****************************************************************************
** Internal structures
*****************************************************************************/


/*****************************************************************************
** Library operations
*****************************************************************************/

static int CfgError(WACOMCONFIG* pCfg, int err, const char* pszText)
{
	/* report error */
	if (pCfg->pfnError)
		(*pCfg->pfnError)(err,pszText);

	/* set and return */
	errno = err;
	return -1;
}

static int CfgGetDevs(WACOMCONFIG* pCfg)
{
	/* request device list */
	pCfg->pDevs = XListInputDevices(pCfg->pDisp,
			&pCfg->nDevCnt);

	if (!pCfg->pDevs)
		return CfgError(pCfg,EIO,"CfgGetDevs: failed to get devices");

	return 0;
}

/*****************************************************************************
** Library initialization, termination
*****************************************************************************/

WACOMCONFIG * WacomConfigInit(Display* pDisplay, WACOMERRORFUNC pfnErrorHandler)
{
	WACOMCONFIG* pCfg;
	int nMajor, nFEV, nFER;

	/* check for XInput extension */
	if (!XQueryExtension(pDisplay,INAME,&nMajor,&nFEV,&nFER))
	{
		if (pfnErrorHandler)
			(*pfnErrorHandler)(EINVAL,"XInput not supported.");
		return NULL;
	}

	/* allocate configuration structure */
	pCfg = (WACOMCONFIG*)malloc(sizeof(WACOMCONFIG));
	if (!pCfg)
	{
		if (pfnErrorHandler)
			(*pfnErrorHandler)(errno,strerror(errno));
		return NULL;
	}

	memset(pCfg,0,sizeof(*pCfg));
	pCfg->pDisp = pDisplay;
	pCfg->pfnError = pfnErrorHandler;
	return pCfg;
}

void WacomConfigTerm(WACOMCONFIG *hConfig)
{
	if (!hConfig) return;
	free(hConfig);
}

int WacomConfigListDevices(WACOMCONFIG *hConfig, WACOMDEVICEINFO** ppInfo,
	unsigned int* puCount)
{
	int i, j, nSize, nPos, nLen, nCount;
	unsigned char* pReq;
	WACOMDEVICEINFO* pInfo;
	XDeviceInfo* info;
#if WCM_XF86CONFIG
	XF86ConfigPtr conf;
#endif
	char devName[64];

	if (!hConfig || !ppInfo || !puCount)
		{ errno=EINVAL; return -1; }

	/* load devices, if not already in memory */
	if (!hConfig->pDevs && CfgGetDevs(hConfig))
		return -1;

#if WCM_XF86CONFIG
	/* read the config in for wacom devices which don't use the commnon identifier */
    #if WCM_XORG
	conf = readConfig ("/etc/X11/xorg.conf");
    #else
	conf = readConfig ("/etc/X11/XF86Config");
    #endif
#endif

	/* estimate size of memory needed to hold structures */
	nSize = nCount = 0;
	for (i=0; i<hConfig->nDevCnt; ++i)
	{
		info = hConfig->pDevs + i;
#ifndef WCM_ISXEXTENSIONPOINTER
		if (info->use != IsXExtensionDevice) continue;
#else
		if (info->use != IsXExtensionDevice && info->use != IsXExtensionPointer 
			&& info->use != IsXExtensionKeyboard) continue;
#endif

		if (!info->num_classes) continue;
		nSize += sizeof(WACOMDEVICEINFO);
		nSize += strlen(info->name) + 1;
		++nCount;
	}

	/* allocate memory and zero */
	pReq = (unsigned char*)malloc(nSize);
	if (!pReq) return CfgError(hConfig,errno,
		"WacomConfigListDevices: failed to allocate memory");
	memset(pReq,0,nSize);

	/* populate data */
	pInfo = (WACOMDEVICEINFO*)pReq;
	nPos = nCount * sizeof(WACOMDEVICEINFO);
	nCount = 0;
	for (i=0; i<hConfig->nDevCnt; ++i)
	{
		info = hConfig->pDevs + i;
		/* ignore non-extension devices */
#ifndef WCM_ISXEXTENSIONPOINTER
		if (info->use != IsXExtensionDevice) continue;
#else
		if (info->use != IsXExtensionDevice && info->use != IsXExtensionPointer
			&& info->use != IsXExtensionKeyboard) continue;
#endif
		/* ignore uninitialized tools  */
		if (!info->num_classes) continue;
		/* copy name */
		nLen = strlen(info->name);
		pInfo->pszName = (char*)(pReq + nPos);
		memcpy(pReq+nPos,info->name,nLen+1);
		nPos += nLen + 1;
		/* guess type for now - discard unknowns */
		for (j=0; j<strlen(pInfo->pszName); j++)
			devName[j] = tolower(pInfo->pszName[j]);
		devName[j] = '\0';
		pInfo->type = mapStringToType (devName);

#if WCM_XF86CONFIG
		if ( pInfo->type == WACOMDEVICETYPE_UNKNOWN ) 
			pInfo->type = checkIfWacomDevice (conf, pInfo->pszName);
#endif

		if ( pInfo->type != WACOMDEVICETYPE_UNKNOWN )
		{
			++pInfo;
			++nCount;
		}
	}

	/* double check our work */
	assert(nPos == nSize);
	
	*ppInfo = (WACOMDEVICEINFO*)pReq;
	*puCount = nCount;
	return 0;
}

#if WCM_XF86CONFIG
WACOMDEVICETYPE checkIfWacomDevice (XF86ConfigPtr conf, const char* pszDeviceName) {
	XF86ConfInputPtr ip;

	if (!conf || !pszDeviceName) { return WACOMDEVICETYPE_UNKNOWN; }

	ip = (XF86ConfInputPtr) conf->conf_input_lst;
	if (!ip) { return WACOMDEVICETYPE_UNKNOWN; }

	for (;ip;ip=ip->list.next) 
	{
		XF86OptionPtr op = (XF86OptionPtr) ip->inp_option_lst;
		char* type = 0;

		if (!op) { return WACOMDEVICETYPE_UNKNOWN; }

		if (strcasecmp(ip->inp_identifier, pszDeviceName) != 0) 
			continue;

		if (strcasecmp(ip->inp_driver,"wacom") != 0)
			continue;

		for (;op;op=op->list.next) 
		{
			if (strcasecmp(op->opt_name,"Type") == 0) 
			{
				type = op->opt_val;
				return mapStringToType(type);
			}
		}
	}
	
	return WACOMDEVICETYPE_UNKNOWN;
}
#endif

WACOMDEVICETYPE mapStringToType (const char* name) 
{
	if (!name)
		return WACOMDEVICETYPE_UNKNOWN;

	/* If there is a white space in the name, 
		the "wacom" string has to be in it too */
	if (strstr(name," ") != NULL && strstr(name,"wacom") == NULL)
		return WACOMDEVICETYPE_UNKNOWN;
	if (strstr(name,"cursor") != NULL)
		return WACOMDEVICETYPE_CURSOR;
	else if (strstr(name,"stylus") != NULL)
		return WACOMDEVICETYPE_STYLUS;
	else if (strstr(name,"eraser") != NULL)
		return WACOMDEVICETYPE_ERASER;
	else if (strstr(name,"touch") != NULL)
		return WACOMDEVICETYPE_TOUCH;
	else if (strstr(name,"pad") != NULL)
		return WACOMDEVICETYPE_PAD;
	else
		return WACOMDEVICETYPE_UNKNOWN;
	
}

WACOMDEVICE * WacomConfigOpenDevice(WACOMCONFIG * hConfig,
	const char* pszDeviceName)
{
	int i, j;
	WACOMDEVICE* pInt;
	XDevice* pDev;
	XDeviceInfo *pDevInfo = NULL, *info;
	char nameOut[60];

	/* sanity check input */
	if (!hConfig || !pszDeviceName) { errno=EINVAL; return NULL; }

	/* load devices, if not already in memory */
	if (!hConfig->pDevs && CfgGetDevs(hConfig))
		return NULL;

	/* find device in question */
	for (i=0; i<hConfig->nDevCnt; ++i)
	{
		info = hConfig->pDevs + i;

		/* convert the underscores back to spaces for name */
		for(j=0; j<strlen(pszDeviceName); j++)
		{
			if(pszDeviceName[j] == '_') 
				nameOut[j] = ' ';
			else
				nameOut[j] = pszDeviceName[j];
		}
		nameOut[strlen(pszDeviceName)] = '\0';

		if (!strcmp(info->name, nameOut) && info->num_classes)
			pDevInfo = info;
	}

	/* no device, bail. */
	if (!pDevInfo)
	{
		CfgError(hConfig,ENOENT,"WacomConfigOpenDevice: No such device");
		return NULL;
	}

	/* Open the device. */
	pDev = XOpenDevice(hConfig->pDisp,pDevInfo->id);
	if (!pDev)
	{
		CfgError(hConfig,EIO,"WacomConfigOpenDevice: "
			"Failed to open device");
		return NULL;
	}

	/* allocate device structure and return */
	pInt = (WACOMDEVICE*)malloc(sizeof(WACOMDEVICE));
	memset(pInt,0,sizeof(*pInt));
	pInt->pCfg = hConfig;
	pInt->pDev = pDev;

	return pInt;
}

int WacomConfigCloseDevice(WACOMDEVICE *hDevice)
{
	if (!hDevice) { errno=EINVAL; return -1; }

	if (hDevice->pDev)
		XFree(hDevice->pDev);
	free(hDevice);
	return 0;
}

int WacomConfigSetRawParam(WACOMDEVICE *hDevice, int nParam, int nValue, unsigned * keys)
{
	int nReturn, i;
	int nValues[2];
	XDeviceResolutionControl c;
	XDeviceControl *dc = (XDeviceControl *)(void *)&c;

	nValues[0] = nParam;
	nValues[1] = nValue;
	if (!hDevice || !nParam) { errno=EINVAL; return -1; }

	c.control = DEVICE_RESOLUTION;
	c.length = sizeof(c);
	c.first_valuator = 0;
	c.num_valuators = 2;
	c.resolutions = nValues;
	/* Dispatch request */
	nReturn = XChangeDeviceControl (hDevice->pCfg->pDisp, hDevice->pDev,
					DEVICE_RESOLUTION, dc);

	/* Convert error codes:
	 * hell knows what XChangeDeviceControl should return */
	if (nReturn == BadValue || nReturn == BadRequest)
		return CfgError(hDevice->pCfg,EINVAL,
				"WacomConfigSetRawParam: failed");

	if (nParam >= XWACOM_PARAM_BUTTON1 && nParam <= XWACOM_PARAM_STRIPRDN)
	{
		for (i=1; i<((nValue & AC_NUM_KEYS)>>20); i += 2)
		{
			nValues[1] = keys[i] | (keys[i+1]<<16);
			nReturn = XChangeDeviceControl (hDevice->pCfg->pDisp, hDevice->pDev,
					DEVICE_RESOLUTION, dc);

			if (nReturn == BadValue || nReturn == BadRequest)
				return CfgError(hDevice->pCfg,EINVAL,
					"WacomConfigSetRawParam: keystroke failed");
		}
	}

	if (nParam == XWACOM_PARAM_MODE)
	{
		/* tell Xinput the mode has been changed */
		XSetDeviceMode(hDevice->pCfg->pDisp, hDevice->pDev, nValue);
	}
	return 0;
}

int WacomConfigGetRawParam(WACOMDEVICE *hDevice, int nParam, int *nValue, int valu, unsigned * keys)
{
	int nReturn, i;
	XDeviceResolutionControl c;
	XDeviceResolutionState *ds;
	int nValues[1];

	if (!hDevice || !nParam) { errno=EINVAL; return -1; }

	nValues[0] = nParam;

	c.control = DEVICE_RESOLUTION;
	c.length = sizeof(c);
	c.first_valuator = 0;
	c.num_valuators = valu;
	c.resolutions = nValues;
	/* Dispatch request */
	nReturn = XChangeDeviceControl(hDevice->pCfg->pDisp, hDevice->pDev,
		DEVICE_RESOLUTION, (XDeviceControl *)(void *)&c);

	if (nReturn == BadValue || nReturn == BadRequest)
	{
error:		return CfgError(hDevice->pCfg, EINVAL,
			"WacomConfigGetRawParam: failed");
	}

	ds = (XDeviceResolutionState *)XGetDeviceControl (hDevice->pCfg->pDisp,
		hDevice->pDev, DEVICE_RESOLUTION);

	if (!ds)
		goto error;

	*nValue = ds->resolutions [ds->num_valuators-1];

	if (nParam >= XWACOM_PARAM_BUTTON1 && nParam <= XWACOM_PARAM_STRIPRDN)
	{
		int num = (*nValue & AC_NUM_KEYS)>>20;
		if (num) keys[0] = ((*nValue) & AC_CODE);

		for (i=1; i<num; i += 2)
		{
			nReturn = XChangeDeviceControl(hDevice->pCfg->pDisp, 
				hDevice->pDev, DEVICE_RESOLUTION, 
				(XDeviceControl *)(void *)&c);

			if (nReturn == BadValue || nReturn == BadRequest)
			{
errork:				return CfgError(hDevice->pCfg, EINVAL,
					"WacomConfigGetRawParam: keystroke failed");
			}

			ds = (XDeviceResolutionState *)XGetDeviceControl (hDevice->pCfg->pDisp,
			hDevice->pDev, DEVICE_RESOLUTION);
			if (!ds)
				goto errork;

			keys[i] = ds->resolutions [ds->num_valuators-1] & 0xffff;
			keys[i+1] = ((ds->resolutions [ds->num_valuators-1] & 0xffff0000)>>16);
		}
	}

	/* Restore resolution */
	nValues [0] = 0;
	XChangeDeviceControl(hDevice->pCfg->pDisp, hDevice->pDev,
		DEVICE_RESOLUTION, (XDeviceControl *)(void *)&c);

	XFreeDeviceControl ((XDeviceControl *)ds);

	return 0;
}

void WacomConfigFree(void* pvData)
{
	/* JEJ - if in the future, more context is needed, make an alloc
	 *       function that places the context before the data.
	 *       In the meantime, free is good enough. */

	free(pvData);
}

#if WCM_XF86CONFIG
XF86ConfigPtr readConfig (char *filename) 
{
	XF86ConfigPtr conf = 0;
	const char *file   = 0;
	char CONFPATH[]= "%A,%R,/etc/%R,%P/etc/X11/%R,%E,%F,/etc/X11/%F," \
			 "%P/etc/X11/%F,%D/%X,/etc/X11/%X,/etc/%X,%P/etc/X11/%X.%H," \
			 "%P/etc/X11/%X,%P/lib/X11/%X.%H,%P/lib/X11/%X";

	if (!(file = (char*)xf86openConfigFile (CONFPATH, filename, 0))) 
	{
		fprintf (stderr, "Unable to open config file\n");
		return 0;
	}

	if ((conf = (XF86ConfigPtr)xf86readConfigFile ()) == 0) 
	{
		fprintf (stderr, "Problem when parsing config file\n");
		xf86closeConfigFile ();
		return 0;
	}

	xf86closeConfigFile ();
	return conf;
}

void VErrorF(const char *f, va_list args) 
{
	vfprintf(stderr, f, args);
}

void ErrorF(const char *f, ...) 
{
	va_list args;
	va_start(args, f);
	vfprintf(stderr, f, args);
	va_end(args);
}
#endif