tablet: add support for libinput_tool_has_button

libwacom can tell us how many buttons we have per stylus, so we map those into
BTN_STYLUS and BTN_STYLUS2.
BTN_TOUCH is set on all styli.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Stephen Chandler Paul <thatslyude@gmail.com>
This commit is contained in:
Peter Hutterer 2015-02-18 13:53:31 +10:00
parent 733fef5a67
commit 9be6b3e864
5 changed files with 100 additions and 0 deletions

View file

@ -382,6 +382,51 @@ copy_axis_cap(const struct tablet_dispatch *tablet,
set_bit(tool->axis_caps, axis);
}
static inline void
copy_button_cap(const struct tablet_dispatch *tablet,
struct libinput_tool *tool,
uint32_t button)
{
struct libevdev *evdev = tablet->device->evdev;
if (libevdev_has_event_code(evdev, EV_KEY, button))
set_bit(tool->buttons, button);
}
static void
tool_set_bits_from_libwacom(const struct tablet_dispatch *tablet,
struct libinput_tool *tool)
{
#if HAVE_LIBWACOM
WacomDeviceDatabase *db;
const WacomStylus *s = NULL;
int code;
db = libwacom_database_new();
if (!db)
goto out;
s = libwacom_stylus_get_for_id(db, tool->tool_id);
if (!s)
goto out;
if (libwacom_stylus_get_type(s) == WSTYLUS_PUCK) {
for (code = BTN_LEFT;
code < BTN_LEFT + libwacom_stylus_get_num_buttons(s);
code++)
copy_button_cap(tablet, tool, code);
} else {
if (libwacom_stylus_get_num_buttons(s) >= 2)
copy_button_cap(tablet, tool, BTN_STYLUS2);
if (libwacom_stylus_get_num_buttons(s) >= 1)
copy_button_cap(tablet, tool, BTN_STYLUS);
copy_button_cap(tablet, tool, BTN_TOUCH);
}
out:
if (db)
libwacom_database_destroy(db);
#endif
}
static void
tool_set_bits(const struct tablet_dispatch *tablet,
struct libinput_tool *tool)
@ -413,6 +458,34 @@ tool_set_bits(const struct tablet_dispatch *tablet,
default:
break;
}
#if HAVE_LIBWACOM
tool_set_bits_from_libwacom(tablet, tool);
#else
/* If we don't have libwacom, copy all pen-related ones from the
tablet vs all mouse-related ones */
switch (type) {
case LIBINPUT_TOOL_PEN:
case LIBINPUT_TOOL_BRUSH:
case LIBINPUT_TOOL_AIRBRUSH:
case LIBINPUT_TOOL_PENCIL:
case LIBINPUT_TOOL_ERASER:
copy_button_cap(tablet, tool, BTN_STYLUS);
copy_button_cap(tablet, tool, BTN_STYLUS2);
copy_button_cap(tablet, tool, BTN_TOUCH);
break;
case LIBINPUT_TOOL_MOUSE:
case LIBINPUT_TOOL_LENS:
copy_button_cap(tablet, tool, BTN_LEFT);
copy_button_cap(tablet, tool, BTN_MIDDLE);
copy_button_cap(tablet, tool, BTN_RIGHT);
copy_button_cap(tablet, tool, BTN_SIDE);
copy_button_cap(tablet, tool, BTN_EXTRA);
break;
default:
break;
}
#endif
}
static struct libinput_tool *

View file

@ -191,6 +191,7 @@ struct libinput_tool {
uint32_t tool_id;
enum libinput_tool_type type;
unsigned char axis_caps[NCHARS(LIBINPUT_TABLET_AXIS_MAX + 1)];
unsigned char buttons[NCHARS(KEY_MAX) + 1];
int refcount;
void *user_data;
};

View file

@ -681,6 +681,16 @@ libinput_tool_has_axis(struct libinput_tool *tool,
return bit_is_set(tool->axis_caps, axis);
}
LIBINPUT_EXPORT int
libinput_tool_has_button(struct libinput_tool *tool,
uint32_t code)
{
if (NCHARS(code) > sizeof(tool->buttons))
return 0;
return bit_is_set(tool->buttons, code);
}
LIBINPUT_EXPORT void
libinput_tool_set_user_data(struct libinput_tool *tool,
void *user_data)

View file

@ -1241,6 +1241,21 @@ int
libinput_tool_has_axis(struct libinput_tool *tool,
enum libinput_tablet_axis axis);
/**
* @ingroup event_tablet
*
* Check if a tablet tool has a button with the
* passed-in code (see linux/input.h).
*
* @param tool A tablet tool
* @param code button code to check for
*
* @return 1 if the tool supports this button code, 0 if it does not
*/
int
libinput_tool_has_button(struct libinput_tool *tool,
uint32_t code);
/**
* @ingroup event_tablet
*

View file

@ -159,6 +159,7 @@ LIBINPUT_TABLET_SUPPORT {
libinput_tool_get_tool_id;
libinput_tool_get_type;
libinput_tool_get_user_data;
libinput_tool_has_button;
libinput_tool_has_axis;
libinput_tool_ref;
libinput_tool_set_user_data;