Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
accel: Ignore accelerometers that are part of a joypad
Browse files Browse the repository at this point in the history
This will avoid having a PlayStation 3 or 4 joypad control your
laptop or tablet's display orientation.

This code requires a newer gudev.
  • Loading branch information
hadess committed Aug 31, 2017
1 parent 91123ec commit 401d59e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if test x$enable_gtk_tests = xyes; then
fi
AM_CONDITIONAL(HAVE_GTK_TESTS, test "x$enable_gtk_tests" = "xyes")

PKG_CHECK_MODULES(IIO_SENSOR_PROXY, gio-2.0 gudev-1.0)
PKG_CHECK_MODULES(IIO_SENSOR_PROXY, gio-2.0 gudev-1.0 >= 232)

AC_PATH_PROG([GDBUS_CODEGEN],[gdbus-codegen])

Expand Down
62 changes: 62 additions & 0 deletions src/drv-input-accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,68 @@ static DrvData *drv_data = NULL;

static void input_accel_set_polling (gboolean state);

/* From src/linux/up-device-supply.c in UPower */
static GUdevDevice *
get_sibling_with_subsystem (GUdevDevice *device,
const char *subsystem)
{
GUdevDevice *parent;
GUdevClient *client;
GUdevDevice *sibling;
const char * class[] = { NULL, NULL };
const char *parent_path;
GList *devices, *l;

g_return_val_if_fail (device != NULL, NULL);
g_return_val_if_fail (subsystem != NULL, NULL);

parent = g_udev_device_get_parent (device);
if (!parent)
return NULL;
parent_path = g_udev_device_get_sysfs_path (parent);

sibling = NULL;
class[0] = subsystem;
client = g_udev_client_new (class);
devices = g_udev_client_query_by_subsystem (client, subsystem);
for (l = devices; l != NULL && sibling == NULL; l = l->next) {
GUdevDevice *d = l->data;
GUdevDevice *p;
const char *p_path;

p = g_udev_device_get_parent (d);
if (!p)
continue;
p_path = g_udev_device_get_sysfs_path (p);
if (g_strcmp0 (p_path, parent_path) == 0)
sibling = g_object_ref (d);

g_object_unref (p);
}

g_list_free_full (devices, (GDestroyNotify) g_object_unref);
g_object_unref (client);
g_object_unref (parent);

return sibling;
}

static gboolean
is_part_of_joypad (GUdevDevice *device)
{
g_autoptr(GUdevDevice) sibling;

sibling = get_sibling_with_subsystem (device, "input");
if (!sibling)
return FALSE;
return g_udev_device_get_property_as_boolean (sibling, "ID_INPUT_JOYSTICK");
}

static gboolean
input_accel_discover (GUdevDevice *device)
{
const char *path;
g_autoptr(GUdevDevice) parent = NULL;

if (g_strcmp0 (g_udev_device_get_property (device, "IIO_SENSOR_PROXY_TYPE"), "input-accel") != 0)
return FALSE;
Expand All @@ -48,6 +106,10 @@ input_accel_discover (GUdevDevice *device)
if (strstr (path, "/event") == NULL)
return FALSE;

parent = g_udev_device_get_parent (device);
if (parent && is_part_of_joypad (parent))
return FALSE;

g_debug ("Found input accel at %s", g_udev_device_get_sysfs_path (device));
return TRUE;
}
Expand Down

0 comments on commit 401d59e

Please sign in to comment.