gpointing-device-settings r126 - in trunk: src test



Author: hiikezoe
Date: Fri Mar  6 07:23:26 2009
New Revision: 126
URL: http://svn.gnome.org/viewvc/gpointing-device-settings?rev=126&view=rev

Log:
added gpds-xinput-utils.[ch] for utility functions for XInput.

Added:
   trunk/src/gpds-xinput-utils.c
   trunk/src/gpds-xinput-utils.h
   trunk/test/test-xinput-utils.c

Added: trunk/src/gpds-xinput-utils.c
==============================================================================
--- (empty file)
+++ trunk/src/gpds-xinput-utils.c	Fri Mar  6 07:23:26 2009
@@ -0,0 +1,130 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ *  Copyright (C) 2009 Hiroyuki Ikezoe  <poincare ikezoe net>
+ *
+ *  This library 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This library 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include "gpds-xinput-utils.h"
+
+#include <gtk/gtk.h>
+#include <gdk/gdkx.h>
+#include <glib/gi18n.h>
+#include <string.h>
+
+GQuark
+gpds_xinput_utils_error_quark (void)
+{
+    return g_quark_from_static_string("gpds-xinput-utils-error-quark");
+}
+
+XDeviceInfo *
+gpds_xinput_utils_get_device_info (const gchar *device_name)
+{
+    XDeviceInfo *device_infos;
+    gint i, n_device_infos;
+
+    device_infos = XListInputDevices(GDK_DISPLAY(), &n_device_infos);
+
+    for (i = 0; i < n_device_infos; i++) {
+        if (device_infos[i].use != IsXExtensionPointer)
+            continue;
+        if (!strcmp(device_infos[i].name, device_name))
+            return &device_infos[i];
+    }
+
+    XFreeDeviceList(device_infos);
+
+    return NULL;
+}
+
+XDevice *
+gpds_xinput_utils_open_device (const gchar *device_name, GError **error)
+{
+    XDeviceInfo *device_info;
+    XDevice *device;
+
+    device_info = gpds_xinput_utils_get_device_info(device_name);
+    if (!device_info) {
+        g_set_error(error,
+                    GPDS_XINPUT_UTILS_ERROR,
+                    GPDS_XINPUT_UTILS_ERROR_NO_DEVICE,
+                    _("No device found."));
+        return NULL;
+    }
+
+    gdk_error_trap_push();
+    device = XOpenDevice(GDK_DISPLAY(), device_info->id);
+    gdk_error_trap_pop();
+    if (!device) {
+        g_set_error(error,
+                    GPDS_XINPUT_UTILS_ERROR,
+                    GPDS_XINPUT_UTILS_ERROR_NO_DEVICE,
+                    _("Could not open %s device."), device_name);
+        return NULL;
+    }
+
+    return device;
+}
+
+Atom
+gpds_xinput_utils_get_float_atom (GError **error)
+{
+    Atom float_atom;
+
+    float_atom = XInternAtom(GDK_DISPLAY(), "FLOAT", False);
+    if (float_atom == 0) {
+        g_set_error(error,
+                    GPDS_XINPUT_UTILS_ERROR,
+                    GPDS_XINPUT_UTILS_ERROR_NO_FLOAT_ATOM,
+                    _("No float atom in XServer"));
+    }
+
+    return float_atom;
+}
+
+gboolean
+gpds_xinput_utils_exist_device (const gchar *device_name)
+{
+    return gpds_xinput_utils_get_device_info(device_name) ? TRUE : FALSE;
+}
+
+GList *
+gpds_xinput_utils_get_pointer_device_names (void)
+{
+    GList *device_names = NULL;
+    XDeviceInfo *device_infos;
+    gint i, n_device_infos;
+
+    device_infos = XListInputDevices(GDK_DISPLAY(), &n_device_infos);
+
+    for (i = 0; i < n_device_infos; i++) {
+        if (device_infos[i].use != IsXExtensionPointer)
+            continue;
+        device_names = g_list_prepend(device_names, device_infos[i].name);
+    }
+
+    XFreeDeviceList(device_infos);
+
+    return device_names;
+}
+
+/*
+vi:ts=4:nowrap:ai:expandtab:sw=4
+*/

Added: trunk/src/gpds-xinput-utils.h
==============================================================================
--- (empty file)
+++ trunk/src/gpds-xinput-utils.h	Fri Mar  6 07:23:26 2009
@@ -0,0 +1,52 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ *  Copyright (C) 2009 Hiroyuki Ikezoe  <poincare ikezoe net>
+ *
+ *  This library 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This library 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef __GPDS_XINPUT_UTILS_H__
+#define __GPDS_XINPUT_UTILS_H__
+
+#include <glib.h>
+#include <X11/extensions/XInput.h>
+#include <X11/Xatom.h>
+
+G_BEGIN_DECLS
+
+#define GPDS_XINPUT_UTILS_ERROR           (gpds_xinput_utils_error_quark())
+
+typedef enum
+{
+    GPDS_XINPUT_UTILS_ERROR_NO_DEVICE,
+    GPDS_XINPUT_UTILS_ERROR_UNABLE_TO_OPEN_DEVICE,
+    GPDS_XINPUT_UTILS_ERROR_NO_FLOAT_ATOM
+} GpdsXInputError;
+
+GQuark       gpds_xinput_utils_error_quark              (void);
+XDeviceInfo *gpds_xinput_utils_get_device_info          (const gchar *device_name);
+XDevice     *gpds_xinput_utils_open_device              (const gchar *device_name, GError **error);
+Atom         gpds_xinput_utils_get_float_atom           (GError **error);
+gboolean     gpds_xinput_utils_exist_device             (const gchar *device_name);
+GList       *gpds_xinput_utils_get_pointer_device_names (void);
+
+G_END_DECLS
+
+#endif /* __GPDS_XINPUT_UTILS_H__ */
+
+/*
+vi:ts=4:nowrap:ai:expandtab:sw=4
+*/
+

Added: trunk/test/test-xinput-utils.c
==============================================================================
--- (empty file)
+++ trunk/test/test-xinput-utils.c	Fri Mar  6 07:23:26 2009
@@ -0,0 +1,59 @@
+#include "gpds-xinput-utils.h"
+
+#include <gcutter.h>
+#include <gdk/gdkx.h>
+
+void test_exist_device (void);
+void test_get_float_atom (void);
+void test_get_device_info (void);
+void test_open_device (void);
+
+static GError *error;
+static XDevice *device;
+
+void
+setup (void)
+{
+    error = NULL;
+    device = NULL;
+}
+
+void
+teardown (void)
+{
+    if (device)
+        XCloseDevice(GDK_DISPLAY(), device);
+    g_clear_error(&error);
+}
+
+void
+test_exist_device (void)
+{
+    cut_assert_false(gpds_xinput_utils_exist_device("There is no device."));
+}
+
+void
+test_get_float_atom (void)
+{
+    gpds_xinput_utils_get_float_atom(&error);
+    gcut_assert_error(error);
+}
+
+void
+test_get_device_info (void)
+{
+    XDeviceInfo *device_info = NULL; 
+    device_info = gpds_xinput_utils_get_device_info("Macintosh mouse button emulation");
+    cut_assert(device_info);
+}
+
+void
+test_open_device (void)
+{
+    device = gpds_xinput_utils_open_device ("Macintosh mouse button emulation", &error);
+    cut_assert(device);
+}
+
+/*
+vi:ts=4:nowrap:ai:expandtab:sw=4
+*/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]