| 1 |
/* |
| 2 |
* Copyright © 2012 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software |
| 5 |
udo y and its documentation for any purpose is hereby granted without |
| 6 |
* fee, provided that the above copyright notice appear in all copies |
| 7 |
* and that both that copyright notice and this permission notice |
| 8 |
* appear in supporting documentation, and that the name of Red Hat |
| 9 |
* not be used in advertising or publicity pertaining to distribution |
| 10 |
* of the software without specific, written prior permission. Red |
| 11 |
* Hat makes no representations about the suitability of this software |
| 12 |
* for any purpose. It is provided "as is" without express or implied |
| 13 |
* warranty. |
| 14 |
* |
| 15 |
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 17 |
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 19 |
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 |
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Peter Hutterer ([email protected]) |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
|
| 29 |
#define _GNU_SOURCE |
| 30 |
#include <dirent.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <glib.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <unistd.h> |
| 39 |
|
| 40 |
#include "libwacom.h" |
| 41 |
|
| 42 |
typedef int (*NumModesFn)(const WacomDevice *device); |
| 43 |
|
| 44 |
static gboolean |
| 45 |
buttons_have_direction(WacomDevice *device) |
| 46 |
{ |
| 47 |
char button; |
| 48 |
int num_buttons; |
| 49 |
|
| 50 |
num_buttons = libwacom_get_num_buttons(device); |
| 51 |
if (num_buttons == 0) |
| 52 |
return TRUE; |
| 53 |
|
| 54 |
for (button = 'A'; button < 'A' + num_buttons; button++) { |
| 55 |
WacomButtonFlags flags; |
| 56 |
flags = libwacom_get_button_flag(device, button); |
| 57 |
if (!(flags & WACOM_BUTTON_DIRECTION)) |
| 58 |
return FALSE; |
| 59 |
} |
| 60 |
|
| 61 |
return TRUE; |
| 62 |
} |
| 63 |
|
| 64 |
static gboolean |
| 65 |
match_mode_switch(WacomDevice *device, |
| 66 |
NumModesFn get_num_modes, |
| 67 |
WacomButtonFlags flag) |
| 68 |
{ |
| 69 |
char button; |
| 70 |
int num_buttons; |
| 71 |
int num_switches; |
| 72 |
int num_modes; |
| 73 |
|
| 74 |
num_buttons = libwacom_get_num_buttons(device); |
| 75 |
num_modes = get_num_modes(device); |
| 76 |
num_switches = 0; |
| 77 |
|
| 78 |
for (button = 'A'; button < 'A' + num_buttons; button++) { |
| 79 |
WacomButtonFlags flags; |
| 80 |
flags = libwacom_get_button_flag(device, button); |
| 81 |
|
| 82 |
if (flags & flag) |
| 83 |
num_switches++; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* If we have more than one mode-switch button, then the |
| 88 |
* number of modes must match the number of mode-switch buttons. |
| 89 |
*/ |
| 90 |
if (num_switches > 1 && num_modes != num_switches) |
| 91 |
return FALSE; |
| 92 |
|
| 93 |
/* |
| 94 |
* If we have more than one mode, then we should find at least |
| 95 |
* one mode-switch button. |
| 96 |
*/ |
| 97 |
if (num_modes > 1 && num_switches == 0) |
| 98 |
return FALSE; |
| 99 |
|
| 100 |
return TRUE; |
| 101 |
} |
| 102 |
|
| 103 |
static gboolean |
| 104 |
tablet_has_lr_buttons(WacomDevice *device) |
| 105 |
{ |
| 106 |
int nleft = 0; |
| 107 |
int nright = 0; |
| 108 |
int num_buttons; |
| 109 |
char button; |
| 110 |
|
| 111 |
num_buttons = libwacom_get_num_buttons(device); |
| 112 |
|
| 113 |
for (button = 'A'; button < 'A' + num_buttons; button++) { |
| 114 |
WacomButtonFlags f = libwacom_get_button_flag(device, button); |
| 115 |
if (f & WACOM_BUTTON_POSITION_LEFT) |
| 116 |
nleft++; |
| 117 |
if (f & WACOM_BUTTON_POSITION_RIGHT) |
| 118 |
nright++; |
| 119 |
} |
| 120 |
|
| 121 |
if (nleft > 0 || nright > 0) |
| 122 |
return TRUE; |
| 123 |
|
| 124 |
return FALSE; |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
test_class(gconstpointer data) |
| 129 |
{ |
| 130 |
WacomDevice *device = (WacomDevice *)data; |
| 131 |
WacomClass cls; |
| 132 |
|
| 133 |
#pragma GCC diagnostic push |
| 134 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 135 |
cls = libwacom_get_class(device); |
| 136 |
#pragma GCC diagnostic pop |
| 137 |
switch (cls) { |
| 138 |
case WCLASS_BAMBOO: |
| 139 |
case WCLASS_ISDV4: |
| 140 |
case WCLASS_PEN_DISPLAYS: |
| 141 |
case WCLASS_GRAPHIRE: |
| 142 |
case WCLASS_REMOTE: |
| 143 |
case WCLASS_INTUOS: |
| 144 |
case WCLASS_INTUOS2: |
| 145 |
case WCLASS_INTUOS3: |
| 146 |
case WCLASS_INTUOS4: |
| 147 |
case WCLASS_INTUOS5: |
| 148 |
case WCLASS_CINTIQ: |
| 149 |
break; |
| 150 |
default: |
| 151 |
g_test_fail(); |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
test_name(gconstpointer data) |
| 158 |
{ |
| 159 |
WacomDevice *device = (WacomDevice *)data; |
| 160 |
|
| 161 |
g_assert_nonnull(libwacom_get_name(device)); |
| 162 |
g_assert_cmpstr(libwacom_get_name(device), !=, ""); |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
assert_vidpid(WacomBusType bus, |
| 167 |
int vid, |
| 168 |
int pid) |
| 169 |
{ |
| 170 |
switch (bus) { |
| 171 |
case WBUSTYPE_SERIAL: |
| 172 |
g_assert_cmpint(vid, >=, 0); |
| 173 |
g_assert_cmpint(pid, >=, 0); |
| 174 |
break; |
| 175 |
case WBUSTYPE_USB: |
| 176 |
if (vid == 0x056A) |
| 177 |
g_assert_cmpint(pid, !=, 0x84); /* wireless dongle */ |
| 178 |
g_assert_cmpint(vid, >, 0); |
| 179 |
g_assert_cmpint(pid, >, 0); |
| 180 |
break; |
| 181 |
case WBUSTYPE_BLUETOOTH: |
| 182 |
case WBUSTYPE_I2C: |
| 183 |
g_assert_cmpint(vid, >, 0); |
| 184 |
g_assert_cmpint(pid, >, 0); |
| 185 |
break; |
| 186 |
case WBUSTYPE_UNKNOWN: |
| 187 |
default: |
| 188 |
g_test_fail(); |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
test_vidpid(gconstpointer data) |
| 195 |
{ |
| 196 |
WacomDevice *device = (WacomDevice *)data; |
| 197 |
WacomBusType bus = libwacom_get_bustype(device); |
| 198 |
|
| 199 |
assert_vidpid(bus, |
| 200 |
libwacom_get_vendor_id(device), |
| 201 |
libwacom_get_product_id(device)); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
test_matches(gconstpointer data) |
| 206 |
{ |
| 207 |
WacomDevice *device = (WacomDevice *)data; |
| 208 |
|
| 209 |
g_assert_nonnull(libwacom_get_match(device)); |
| 210 |
g_assert_nonnull(libwacom_get_matches(device)); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
test_matches_vidpid(gconstpointer data) |
| 215 |
{ |
| 216 |
WacomDevice *device = (WacomDevice *)data; |
| 217 |
const WacomMatch **match = libwacom_get_matches(device); |
| 218 |
|
| 219 |
while (*match) { |
| 220 |
WacomBusType bus = libwacom_match_get_bustype(*match); |
| 221 |
assert_vidpid(bus, |
| 222 |
libwacom_match_get_vendor_id(*match), |
| 223 |
libwacom_match_get_product_id(*match)); |
| 224 |
match++; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static void |
| 229 |
test_dimensions(gconstpointer data) |
| 230 |
{ |
| 231 |
WacomDevice *device = (WacomDevice *)data; |
| 232 |
unsigned int flags = libwacom_get_integration_flags(device); |
| 233 |
|
| 234 |
switch (flags) { |
| 235 |
case WACOM_DEVICE_INTEGRATED_REMOTE: |
| 236 |
g_assert_cmpint(libwacom_get_width(device), ==, 0); |
| 237 |
g_assert_cmpint(libwacom_get_height(device), ==, 0); |
| 238 |
break; |
| 239 |
case WACOM_DEVICE_INTEGRATED_SYSTEM | WACOM_DEVICE_INTEGRATED_DISPLAY: |
| 240 |
/* ISDV4 devices may not always have a width/height set */ |
| 241 |
break; |
| 242 |
default: |
| 243 |
g_assert_cmpint(libwacom_get_width(device), >, 0); |
| 244 |
g_assert_cmpint(libwacom_get_height(device), >, 0); |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
static void |
| 250 |
test_buttons(gconstpointer data) |
| 251 |
{ |
| 252 |
WacomDevice *device = (WacomDevice *)data; |
| 253 |
|
| 254 |
g_assert_cmpint(libwacom_get_num_buttons(device), >=, 0); |
| 255 |
g_assert_true(buttons_have_direction(device)); |
| 256 |
|
| 257 |
if (libwacom_is_reversible(device) && libwacom_get_num_buttons(device) > 0) |
| 258 |
g_assert_true(tablet_has_lr_buttons(device)); |
| 259 |
} |
| 260 |
|
| 261 |
static void |
| 262 |
test_styli(gconstpointer data) |
| 263 |
{ |
| 264 |
WacomDevice *device = (WacomDevice *)data; |
| 265 |
int nstylus_ids, nstyli; |
| 266 |
g_autofree const WacomStylus **styli = NULL; |
| 267 |
const int *stylus_ids; |
| 268 |
|
| 269 |
#pragma GCC diagnostic push |
| 270 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 271 |
stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids); |
| 272 |
#pragma GCC diagnostic pop |
| 273 |
styli = libwacom_get_styli(device, &nstyli); |
| 274 |
|
| 275 |
g_assert_cmpint(nstyli, >, 0); |
| 276 |
g_assert_cmpint(nstyli, ==, nstylus_ids); |
| 277 |
g_assert_nonnull(styli); |
| 278 |
g_assert_nonnull(stylus_ids); |
| 279 |
g_assert_null(styli[nstyli]); /* NULL-terminated list */ |
| 280 |
|
| 281 |
for (int i = 0; i < nstyli; i++) { |
| 282 |
const WacomStylus *stylus = styli[i]; |
| 283 |
gboolean found = FALSE; |
| 284 |
for (int j = 0; !found && j < nstylus_ids; j++) { |
| 285 |
found = stylus_ids[j] == libwacom_stylus_get_id(stylus); |
| 286 |
} |
| 287 |
g_assert_true(found); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static void |
| 292 |
test_no_styli(gconstpointer data) |
| 293 |
{ |
| 294 |
WacomDevice *device = (WacomDevice *)data; |
| 295 |
int nstylus_ids, nstyli; |
| 296 |
g_autofree const WacomStylus **styli = NULL; |
| 297 |
const int *stylus_ids; |
| 298 |
|
| 299 |
#pragma GCC diagnostic push |
| 300 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 301 |
stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids); |
| 302 |
#pragma GCC diagnostic pop |
| 303 |
styli = libwacom_get_styli(device, &nstyli); |
| 304 |
|
| 305 |
g_assert_cmpint(nstyli, ==, 0); |
| 306 |
g_assert_cmpint(nstylus_ids, ==, 0); |
| 307 |
g_assert_nonnull(styli); |
| 308 |
g_assert_null(stylus_ids); |
| 309 |
g_assert_null(styli[0]); /* NULL-terminated list */ |
| 310 |
} |
| 311 |
|
| 312 |
static void |
| 313 |
test_realstylus(gconstpointer data) |
| 314 |
{ |
| 315 |
WacomDevice *device = (WacomDevice *)data; |
| 316 |
int nstylus_ids, nstyli; |
| 317 |
g_autofree const WacomStylus **styli = NULL; |
| 318 |
const int *stylus_ids; |
| 319 |
|
| 320 |
#pragma GCC diagnostic push |
| 321 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 322 |
stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids); |
| 323 |
#pragma GCC diagnostic pop |
| 324 |
styli = libwacom_get_styli(device, &nstyli); |
| 325 |
|
| 326 |
for (int i = 0; i < nstylus_ids; i++) { |
| 327 |
g_assert_cmpint(stylus_ids[i], !=, WACOM_STYLUS_FALLBACK_ID); |
| 328 |
g_assert_cmpint(stylus_ids[i], !=, WACOM_ERASER_FALLBACK_ID); |
| 329 |
} |
| 330 |
|
| 331 |
for (int i = 0; i < nstyli; i++) { |
| 332 |
const WacomStylus *stylus = styli[i]; |
| 333 |
g_assert_cmpint(libwacom_stylus_get_id(stylus), |
| 334 |
!=, |
| 335 |
WACOM_STYLUS_FALLBACK_ID); |
| 336 |
g_assert_cmpint(libwacom_stylus_get_id(stylus), |
| 337 |
!=, |
| 338 |
WACOM_ERASER_FALLBACK_ID); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
test_rings(gconstpointer data) |
| 344 |
{ |
| 345 |
WacomDevice *device = (WacomDevice *)data; |
| 346 |
|
| 347 |
g_assert_cmpint(libwacom_get_ring_num_modes(device), >=, 0); |
| 348 |
g_assert_cmpint(libwacom_get_ring2_num_modes(device), >=, 0); |
| 349 |
|
| 350 |
if (libwacom_get_num_rings(device) >= 1) |
| 351 |
g_assert_true(match_mode_switch(device, |
| 352 |
libwacom_get_ring_num_modes, |
| 353 |
WACOM_BUTTON_RING_MODESWITCH)); |
| 354 |
if (libwacom_get_num_rings(device) >= 2) |
| 355 |
g_assert_true(match_mode_switch(device, |
| 356 |
libwacom_get_ring2_num_modes, |
| 357 |
WACOM_BUTTON_RING2_MODESWITCH)); |
| 358 |
} |
| 359 |
|
| 360 |
static void |
| 361 |
test_strips(gconstpointer data) |
| 362 |
{ |
| 363 |
WacomDevice *device = (WacomDevice *)data; |
| 364 |
|
| 365 |
g_assert_cmpint(libwacom_get_num_strips(device), >=, 0); |
| 366 |
g_assert_cmpint(libwacom_get_strips_num_modes(device), >=, 0); |
| 367 |
|
| 368 |
if (libwacom_get_num_strips(device) > 0) |
| 369 |
g_assert_true(match_mode_switch(device, |
| 370 |
libwacom_get_strips_num_modes, |
| 371 |
WACOM_BUTTON_TOUCHSTRIP_MODESWITCH)); |
| 372 |
} |
| 373 |
|
| 374 |
static void |
| 375 |
test_dials(gconstpointer data) |
| 376 |
{ |
| 377 |
WacomDevice *device = (WacomDevice *)data; |
| 378 |
|
| 379 |
g_assert_cmpint(libwacom_get_num_dials(device), >=, 0); |
| 380 |
g_assert_cmpint(libwacom_get_dial_num_modes(device), >=, 0); |
| 381 |
|
| 382 |
if (libwacom_get_num_dials(device) > 0) |
| 383 |
g_assert_true(match_mode_switch(device, |
| 384 |
libwacom_get_dial_num_modes, |
| 385 |
WACOM_BUTTON_DIAL_MODESWITCH)); |
| 386 |
|
| 387 |
if (libwacom_get_num_dials(device) > 1) |
| 388 |
g_assert_true(match_mode_switch(device, |
| 389 |
libwacom_get_dial2_num_modes, |
| 390 |
WACOM_BUTTON_DIAL2_MODESWITCH)); |
| 391 |
} |
| 392 |
|
| 393 |
/* Wrapper function to make adding tests simpler. g_test requires |
| 394 |
* a unique test case name so we assemble that from the test function and |
| 395 |
* the tablet data. |
| 396 |
*/ |
| 397 |
static inline void |
| 398 |
_add_test(WacomDevice *device, |
| 399 |
GTestDataFunc func, |
| 400 |
const char *funcname) |
| 401 |
{ |
| 402 |
char buf[128]; |
| 403 |
static int count; /* guarantee unique test case names */ |
| 404 |
const char *prefix; |
| 405 |
|
| 406 |
/* tests must be test_foobar */ |
| 407 |
g_assert(strncmp(funcname, "test_", 5) == 0); |
| 408 |
prefix = &funcname[5]; |
| 409 |
|
| 410 |
snprintf(buf, |
| 411 |
128, |
| 412 |
"/tablet/%s/%03d/%04x:%04x-%s", |
| 413 |
prefix, |
| 414 |
++count, |
| 415 |
libwacom_get_vendor_id(device), |
| 416 |
libwacom_get_product_id(device), |
| 417 |
libwacom_get_name(device)); |
| 418 |
g_test_add_data_func(buf, device, func); |
| 419 |
} |
| 420 |
#define add_test(device_, func_) \ |
| 421 |
_add_test(device_, func_, #func_) |
| 422 |
|
| 423 |
static void |
| 424 |
setup_tests(WacomDevice *device) |
| 425 |
{ |
| 426 |
const char *name; |
| 427 |
WacomClass cls; |
| 428 |
|
| 429 |
name = libwacom_get_name(device); |
| 430 |
if (g_str_equal(name, "Generic")) |
| 431 |
return; |
| 432 |
|
| 433 |
add_test(device, test_class); |
| 434 |
add_test(device, test_name); |
| 435 |
add_test(device, test_vidpid); |
| 436 |
add_test(device, test_matches); |
| 437 |
add_test(device, test_matches_vidpid); |
| 438 |
add_test(device, test_buttons); |
| 439 |
add_test(device, test_rings); |
| 440 |
add_test(device, test_strips); |
| 441 |
add_test(device, test_dials); |
| 442 |
|
| 443 |
#pragma GCC diagnostic push |
| 444 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 445 |
cls = libwacom_get_class(device); |
| 446 |
#pragma GCC diagnostic pop |
| 447 |
|
| 448 |
add_test(device, test_dimensions); |
| 449 |
|
| 450 |
/* FIXME: we force the generic pen for these, should add a test */ |
| 451 |
if (libwacom_has_stylus(device)) |
| 452 |
add_test(device, test_styli); |
| 453 |
else |
| 454 |
add_test(device, test_no_styli); |
| 455 |
|
| 456 |
switch (cls) { |
| 457 |
case WCLASS_INTUOS: |
| 458 |
case WCLASS_INTUOS2: |
| 459 |
case WCLASS_INTUOS3: |
| 460 |
case WCLASS_INTUOS4: |
| 461 |
case WCLASS_INTUOS5: |
| 462 |
case WCLASS_CINTIQ: |
| 463 |
add_test(device, test_realstylus); |
| 464 |
break; |
| 465 |
default: |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
static WacomDeviceDatabase * |
| 471 |
load_database(void) |
| 472 |
{ |
| 473 |
WacomDeviceDatabase *db; |
| 474 |
const char *datadir; |
| 475 |
|
| 476 |
datadir = getenv("LIBWACOM_DATA_DIR"); |
| 477 |
if (!datadir) |
| 478 |
datadir = TOPSRCDIR "/data"; |
| 479 |
|
| 480 |
db = libwacom_database_new_for_path(datadir); |
| 481 |
if (!db) |
| 482 |
printf("Failed to load data from %s", datadir); |
| 483 |
|
| 484 |
g_assert(db); |
| 485 |
return db; |
| 486 |
} |
| 487 |
|
| 488 |
int |
| 489 |
main(int argc, |
| 490 |
char **argv) |
| 491 |
{ |
| 492 |
WacomDeviceDatabase *db; |
| 493 |
WacomDevice **devices; |
| 494 |
int rc; |
| 495 |
|
| 496 |
g_test_init(&argc, &argv, NULL); |
| 497 |
g_test_set_nonfatal_assertions(); |
| 498 |
|
| 499 |
db = load_database(); |
| 500 |
|
| 501 |
devices = libwacom_list_devices_from_database(db, NULL); |
| 502 |
g_assert(devices); |
| 503 |
g_assert(*devices); |
| 504 |
|
| 505 |
for (WacomDevice **device = devices; *device; device++) |
| 506 |
setup_tests(*device); |
| 507 |
|
| 508 |
rc = g_test_run(); |
| 509 |
|
| 510 |
libwacom_database_destroy(db); |
| 511 |
free(devices); |
| 512 |
|
| 513 |
return rc; |
| 514 |
} |
| 515 |
|
| 516 |
/* vim: set noexpandtab tabstop=8 shiftwidth=8: */ |