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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
*
* controller_linux_input.c
* Copyright (C) 2004-2007 Sven Neumann <sven@gimp.org>
* Michael Natterer <mitch@gimp.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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <glib/gstdio.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpconfig/gimpconfig.h"
#include "libgimpmodule/gimpmodule.h"
#include "libgimpwidgets/gimpwidgets.h"
#define GIMP_ENABLE_CONTROLLER_UNDER_CONSTRUCTION
#include "libgimpwidgets/gimpcontroller.h"
#include "gimpinputdevicestore.h"
#include "libgimp/libgimp-intl.h"
typedef struct
{
guint16 code;
const gchar *name;
const gchar *blurb;
} LinuxInputEvent;
static const LinuxInputEvent key_events[] =
{
{ BTN_0, "button-0", N_("Button 0") },
{ BTN_1, "button-1", N_("Button 1") },
{ BTN_2, "button-2", N_("Button 2") },
{ BTN_3, "button-3", N_("Button 3") },
{ BTN_4, "button-4", N_("Button 4") },
{ BTN_5, "button-5", N_("Button 5") },
{ BTN_6, "button-6", N_("Button 6") },
{ BTN_7, "button-7", N_("Button 7") },
{ BTN_8, "button-8", N_("Button 8") },
{ BTN_9, "button-9", N_("Button 9") },
{ BTN_MOUSE, "button-mouse", N_("Button Mouse") },
{ BTN_LEFT, "button-left", N_("Button Left") },
{ BTN_RIGHT, "button-right", N_("Button Right") },
{ BTN_MIDDLE, "button-middle", N_("Button Middle") },
{ BTN_SIDE, "button-side", N_("Button Side") },
{ BTN_EXTRA, "button-extra", N_("Button Extra") },
{ BTN_FORWARD, "button-forward", N_("Button Forward") },
{ BTN_BACK, "button-back", N_("Button Back") },
{ BTN_TASK, "button-task", N_("Button Task") },
#ifdef BTN_WHEEL
{ BTN_WHEEL, "button-wheel", N_("Button Wheel") },
#endif
#ifdef BTN_GEAR_DOWN
{ BTN_GEAR_DOWN, "button-gear-down", N_("Button Gear Down") },
#endif
#ifdef BTN_GEAR_UP
{ BTN_GEAR_UP, "button-gear-up", N_("Button Gear Up") }
#endif
};
static const LinuxInputEvent rel_events[] =
{
{ REL_X, "x-move-left", N_("X Move Left") },
{ REL_X, "x-move-right", N_("X Move Right") },
{ REL_Y, "y-move-forward", N_("Y Move Forward") },
{ REL_Y, "y-move-back", N_("Y Move Back") },
{ REL_Z, "z-move-up", N_("Z Move Up") },
{ REL_Z, "z-move-down", N_("Z Move Down") },
#ifdef REL_RX
{ REL_RX, "x-axis-tilt-forward", N_("X Axis Tilt Forward") },
{ REL_RX, "x-axis-tilt-back", N_("X Axis Tilt Back") },
{ REL_RY, "y-axis-tilt-right", N_("Y Axis Tilt Right") },
{ REL_RY, "y-axis-tilt-left", N_("Y Axis Tilt Left") },
{ REL_RZ, "z-axis-turn-left", N_("Z Axis Turn Left") },
{ REL_RZ, "z-axis-turn-right", N_("Z Axis Turn Right") },
#endif /* REL_RX */
{ REL_HWHEEL, "horizontal-wheel-turn-back", N_("Horiz. Wheel Turn Back") },
{ REL_HWHEEL, "horizontal-wheel-turn-forward", N_("Horiz. Wheel Turn Forward") },
{ REL_DIAL, "dial-turn-left", N_("Dial Turn Left") },
{ REL_DIAL, "dial-turn-right", N_("Dial Turn Right") },
{ REL_WHEEL, "wheel-turn-left", N_("Wheel Turn Left") },
{ REL_WHEEL, "wheel-turn-right", N_("Wheel Turn Right") },
};
enum
{
PROP_0,
PROP_DEVICE,
PROP_DEVICE_STORE
};
#define CONTROLLER_TYPE_LINUX_INPUT (controller_linux_input_get_type ())
#define CONTROLLER_LINUX_INPUT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTROLLER_TYPE_LINUX_INPUT, ControllerLinuxInput))
#define CONTROLLER_LINUX_INPUT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTROLLER_TYPE_LINUX_INPUT, ControllerLinuxInputClass))
#define CONTROLLER_IS_LINUX_INPUT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTROLLER_TYPE_LINUX_INPUT))
#define CONTROLLER_IS_LINUX_INPUT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTROLLER_TYPE_LINUX_INPUT))
typedef struct _ControllerLinuxInput ControllerLinuxInput;
typedef struct _ControllerLinuxInputClass ControllerLinuxInputClass;
struct _ControllerLinuxInput
{
GimpController parent_instance;
GimpInputDeviceStore *store;
gchar *device;
GIOChannel *io;
guint io_id;
};
struct _ControllerLinuxInputClass
{
GimpControllerClass parent_class;
};
GType controller_linux_input_get_type (void);
static void linux_input_dispose (GObject *object);
static void linux_input_finalize (GObject *object);
static void linux_input_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void linux_input_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static gint linux_input_get_n_events (GimpController *controller);
static const gchar * linux_input_get_event_name (GimpController *controller,
gint event_id);
static const gchar * linux_input_get_event_blurb (GimpController *controller,
gint event_id);
static void linux_input_device_changed (ControllerLinuxInput *controller,
const gchar *udi);
static gboolean linux_input_set_device (ControllerLinuxInput *controller,
const gchar *device);
static gboolean linux_input_read_event (GIOChannel *io,
GIOCondition cond,
gpointer data);
static const GimpModuleInfo linux_input_info =
{
GIMP_MODULE_ABI_VERSION,
N_("Linux input event controller"),
"Sven Neumann <sven@gimp.org>, Michael Natterer <mitch@gimp.org>",
"v0.2",
"(c) 2004-2007, released under the GPL",
"2004-2007"
};
G_DEFINE_DYNAMIC_TYPE (ControllerLinuxInput, controller_linux_input,
GIMP_TYPE_CONTROLLER)
G_MODULE_EXPORT const GimpModuleInfo *
gimp_module_query (GTypeModule *module)
{
return &linux_input_info;
}
G_MODULE_EXPORT gboolean
gimp_module_register (GTypeModule *module)
{
gimp_input_device_store_register_types (module);
controller_linux_input_register_type (module);
return TRUE;
}
static void
controller_linux_input_class_init (ControllerLinuxInputClass *klass)
{
GimpControllerClass *controller_class = GIMP_CONTROLLER_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = linux_input_dispose;
object_class->finalize = linux_input_finalize;
object_class->get_property = linux_input_get_property;
object_class->set_property = linux_input_set_property;
g_object_class_install_property (object_class, PROP_DEVICE,
g_param_spec_string ("device",
_("Device:"),
_("The name of the device to read Linux Input events from."),
NULL,
GIMP_CONFIG_PARAM_FLAGS));
#ifdef HAVE_LIBGUDEV
g_object_class_install_property (object_class, PROP_DEVICE_STORE,
g_param_spec_object ("device-values",
NULL, NULL,
GIMP_TYPE_INPUT_DEVICE_STORE,
G_PARAM_READABLE));
#endif
controller_class->name = _("Linux Input");
controller_class->help_id = "gimp-controller-linux-input";
controller_class->icon_name = GIMP_ICON_CONTROLLER_LINUX_INPUT;
controller_class->get_n_events = linux_input_get_n_events;
controller_class->get_event_name = linux_input_get_event_name;
controller_class->get_event_blurb = linux_input_get_event_blurb;
}
static void
controller_linux_input_class_finalize (ControllerLinuxInputClass *klass)
{
}
static void
controller_linux_input_init (ControllerLinuxInput *controller)
{
controller->store = gimp_input_device_store_new ();
if (controller->store)
{
g_signal_connect_swapped (controller->store, "device-added",
G_CALLBACK (linux_input_device_changed),
controller);
g_signal_connect_swapped (controller->store, "device-removed",
G_CALLBACK (linux_input_device_changed),
controller);
}
}
static void
linux_input_dispose (GObject *object)
{
ControllerLinuxInput *controller = CONTROLLER_LINUX_INPUT (object);
linux_input_set_device (controller, NULL);
G_OBJECT_CLASS (controller_linux_input_parent_class)->dispose (object);
}
static void
linux_input_finalize (GObject *object)
{
ControllerLinuxInput *controller = CONTROLLER_LINUX_INPUT (object);
if (controller->store)
{
g_object_unref (controller->store);
controller->store = NULL;
}
G_OBJECT_CLASS (controller_linux_input_parent_class)->finalize (object);
}
static void
linux_input_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
ControllerLinuxInput *controller = CONTROLLER_LINUX_INPUT (object);
switch (property_id)
{
case PROP_DEVICE:
linux_input_set_device (controller, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
linux_input_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
ControllerLinuxInput *controller = CONTROLLER_LINUX_INPUT (object);
switch (property_id)
{
case PROP_DEVICE:
g_value_set_string (value, controller->device);
break;
case PROP_DEVICE_STORE:
g_value_set_object (value, controller->store);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static gint
linux_input_get_n_events (GimpController *controller)
{
return G_N_ELEMENTS (key_events) + G_N_ELEMENTS (rel_events);
}
static const gchar *
linux_input_get_event_name (GimpController *controller,
gint event_id)
{
if (event_id < 0)
{
return NULL;
}
else if (event_id < G_N_ELEMENTS (key_events))
{
return key_events[event_id].name;
}
else if (event_id < linux_input_get_n_events (controller))
{
return rel_events[event_id - G_N_ELEMENTS (key_events)].name;
}
else
{
return NULL;
}
}
static const gchar *
linux_input_get_event_blurb (GimpController *controller,
gint event_id)
{
if (event_id < 0)
{
return NULL;
}
else if (event_id < G_N_ELEMENTS (key_events))
{
return gettext (key_events[event_id].blurb);
}
else if (event_id < linux_input_get_n_events (controller))
{
return gettext (rel_events[event_id - G_N_ELEMENTS (key_events)].blurb);
}
else
{
return NULL;
}
}
#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define OFF(x) ((x)%BITS_PER_LONG)
#define BIT(x) (1UL<<OFF(x))
#define LONG(x) ((x)/BITS_PER_LONG)
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
static void
linux_input_get_device_info (ControllerLinuxInput *controller,
int fd)
{
unsigned long evbit[NBITS (EV_MAX)];
unsigned long keybit[NBITS (KEY_MAX)];
unsigned long relbit[NBITS (REL_MAX)];
unsigned long absbit[NBITS (ABS_MAX)];
gint num_keys = 0;
gint num_ext_keys = 0;
gint num_buttons = 0;
gint num_rels = 0;
gint num_abs = 0;
/* get event type bits */
ioctl (fd, EVIOCGBIT (0, EV_MAX), evbit);
if (test_bit (EV_KEY, evbit))
{
gint i;
/* get keyboard bits */
ioctl (fd, EVIOCGBIT (EV_KEY, KEY_MAX), keybit);
/** count typical keyboard keys only */
for (i = KEY_Q; i < KEY_M; i++)
if (test_bit (i, keybit))
{
num_keys++;
g_print ("%s: key 0x%02x present\n", G_STRFUNC, i);
}
g_print ("%s: #keys = %d\n", G_STRFUNC, num_keys);
for (i = KEY_OK; i < KEY_MAX; i++)
if (test_bit (i, keybit))
{
num_ext_keys++;
g_print ("%s: ext key 0x%02x present\n", G_STRFUNC, i);
}
g_print ("%s: #ext_keys = %d\n", G_STRFUNC, num_ext_keys);
for (i = BTN_MISC; i < KEY_OK; i++)
if (test_bit (i, keybit))
{
num_buttons++;
g_print ("%s: button 0x%02x present\n", G_STRFUNC, i);
}
g_print ("%s: #buttons = %d\n", G_STRFUNC, num_buttons);
}
if (test_bit (EV_REL, evbit))
{
gint i;
/* get bits for relative axes */
ioctl (fd, EVIOCGBIT (EV_REL, REL_MAX), relbit);
for (i = 0; i < REL_MAX; i++)
if (test_bit (i, relbit))
{
num_rels++;
g_print ("%s: rel 0x%02x present\n", G_STRFUNC, i);
}
g_print ("%s: #rels = %d\n", G_STRFUNC, num_rels);
}
if (test_bit (EV_ABS, evbit))
{
gint i;
/* get bits for absolute axes */
ioctl (fd, EVIOCGBIT (EV_ABS, ABS_MAX), absbit);
for (i = 0; i < ABS_MAX; i++)
if (test_bit (i, absbit))
{
struct input_absinfo absinfo;
num_abs++;
/* get info for the absolute axis */
ioctl (fd, EVIOCGABS (i), &absinfo);
g_print ("%s: abs 0x%02x present [%d..%d]\n", G_STRFUNC, i,
absinfo.minimum, absinfo.maximum);
}
g_print ("%s: #abs = %d\n", G_STRFUNC, num_abs);
}
}
static void
linux_input_device_changed (ControllerLinuxInput *controller,
const gchar *identifier)
{
if (controller->device && strcmp (identifier, controller->device) == 0)
{
linux_input_set_device (controller, identifier);
g_object_notify (G_OBJECT (controller), "device");
}
}
static gboolean
linux_input_set_device (ControllerLinuxInput *controller,
const gchar *device)
{
gchar *filename;
if (controller->io)
{
g_source_remove (controller->io_id);
controller->io_id = 0;
g_io_channel_unref (controller->io);
controller->io = NULL;
}
if (controller->device)
g_free (controller->device);
controller->device = g_strdup (device);
g_object_set (controller, "name", _("Linux Input Events"), NULL);
if (controller->device && strlen (controller->device))
{
if (controller->store)
filename = gimp_input_device_store_get_device_file (controller->store,
controller->device);
else
filename = g_strdup (controller->device);
}
else
{
g_object_set (controller, "state", _("No device configured"), NULL);
return FALSE;
}
if (filename)
{
gchar *state;
gint fd;
fd = g_open (filename, O_RDONLY, 0);
if (fd >= 0)
{
gchar name[256];
name[0] = '\0';
if (ioctl (fd, EVIOCGNAME (sizeof (name)), name) >= 0 &&
strlen (name) > 0 &&
g_utf8_validate (name, -1, NULL))
{
g_object_set (controller, "name", name, NULL);
}
linux_input_get_device_info (controller, fd);
state = g_strdup_printf (_("Reading from %s"), filename);
g_object_set (controller, "state", state, NULL);
g_free (state);
g_free (filename);
controller->io = g_io_channel_unix_new (fd);
g_io_channel_set_close_on_unref (controller->io, TRUE);
g_io_channel_set_encoding (controller->io, NULL, NULL);
controller->io_id = g_io_add_watch (controller->io,
G_IO_IN,
linux_input_read_event,
controller);
return TRUE;
}
else
{
state = g_strdup_printf (_("Device not available: %s"),
g_strerror (errno));
g_object_set (controller, "state", state, NULL);
g_free (state);
}
g_free (filename);
}
else if (controller->store)
{
GError *error = gimp_input_device_store_get_error (controller->store);
if (error)
{
g_object_set (controller, "state", error->message, NULL);
g_error_free (error);
}
else
{
g_object_set (controller, "state", _("Device not available"), NULL);
}
}
return FALSE;
}
static gboolean
linux_input_read_event (GIOChannel *io,
GIOCondition cond,
gpointer data)
{
ControllerLinuxInput *input = CONTROLLER_LINUX_INPUT (data);
GIOStatus status;
GError *error = NULL;
struct input_event ev;
gsize n_bytes;
status = g_io_channel_read_chars (io,
(gchar *) &ev,
sizeof (struct input_event), &n_bytes,
&error);
switch (status)
{
case G_IO_STATUS_ERROR:
case G_IO_STATUS_EOF:
g_source_remove (input->io_id);
input->io_id = 0;
g_io_channel_unref (input->io);
input->io = NULL;
if (error)
{
gchar *state = g_strdup_printf (_("Device not available: %s"),
error->message);
g_object_set (input, "state", state, NULL);
g_free (state);
g_clear_error (&error);
}
else
{
g_object_set (input, "state", _("End of file"), NULL);
}
return FALSE;
break;
case G_IO_STATUS_AGAIN:
return TRUE;
default:
break;
}
if (n_bytes == sizeof (struct input_event))
{
GimpController *controller = GIMP_CONTROLLER (data);
GimpControllerEvent cevent = { 0, };
gint i;
switch (ev.type)
{
case EV_KEY:
g_print ("%s: EV_KEY code = 0x%02x\n", G_STRFUNC, ev.code);
for (i = 0; i < G_N_ELEMENTS (key_events); i++)
if (ev.code == key_events[i].code)
{
cevent.any.type = GIMP_CONTROLLER_EVENT_TRIGGER;
cevent.any.source = controller;
cevent.any.event_id = i;
gimp_controller_event (controller, &cevent);
break;
}
break;
case EV_REL:
g_print ("%s: EV_REL code = 0x%02x (value = %d)\n", G_STRFUNC,
ev.code, ev.value);
for (i = 0; i < G_N_ELEMENTS (rel_events); i++)
if (ev.code == rel_events[i].code)
{
cevent.any.type = GIMP_CONTROLLER_EVENT_VALUE;
cevent.any.source = controller;
cevent.any.event_id = G_N_ELEMENTS (key_events) + i;
g_value_init (&cevent.value.value, G_TYPE_DOUBLE);
if (ev.value < 0)
{
g_value_set_double (&cevent.value.value, -ev.value);
}
else
{
cevent.any.event_id++;
g_value_set_double (&cevent.value.value, ev.value);
}
gimp_controller_event (controller, &cevent);
g_value_unset (&cevent.value.value);
break;
}
break;
case EV_ABS:
g_print ("%s: EV_ABS code = 0x%02x (value = %d)\n", G_STRFUNC,
ev.code, ev.value);
break;
default:
break;
}
}
return TRUE;
}
|