diff options
Diffstat (limited to 'src/multitouch.c')
| -rw-r--r-- | src/multitouch.c | 364 |
1 files changed, 161 insertions, 203 deletions
diff --git a/src/multitouch.c b/src/multitouch.c index 537ab69..2c9b98a 100644 --- a/src/multitouch.c +++ b/src/multitouch.c | |||
| @@ -1,11 +1,8 @@ | |||
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * | 2 | * |
| 3 | * addon-generic-kbd-backlight.c: | 3 | * Multitouch X driver |
| 4 | * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se> | 4 | * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se> |
| 5 | * | 5 | * |
| 6 | * Based on addon-generic-backlight.c: | ||
| 7 | * Copyright (C) 2008 Danny Kukawka <danny.kukawka@web.de> | ||
| 8 | * | ||
| 9 | * Licensed under the Academic Free License version 2.1 | 6 | * Licensed under the Academic Free License version 2.1 |
| 10 | * | 7 | * |
| 11 | * This program is free software; you can redistribute it and/or modify | 8 | * This program is free software; you can redistribute it and/or modify |
| @@ -24,218 +21,179 @@ | |||
| 24 | * | 21 | * |
| 25 | **************************************************************************/ | 22 | **************************************************************************/ |
| 26 | 23 | ||
| 27 | #include <string.h> | 24 | #include "xorg-server.h" |
| 28 | #include <sys/types.h> | 25 | #include <xf86.h> |
| 29 | #include <sys/stat.h> | 26 | #include <xf86_OSproc.h> |
| 30 | #include <fcntl.h> | 27 | #include <xf86Xinput.h> |
| 31 | #include <unistd.h> | 28 | #include <exevents.h> |
| 32 | #include <stdio.h> | 29 | #include <linux/input.h> |
| 33 | #include <stdlib.h> | 30 | |
| 34 | 31 | //////////////////////////////////////////////////////////////////////////// | |
| 35 | #include "glib-abi.h" | 32 | |
| 36 | #include "dbus-abi.h" | 33 | //#define BTN_MT_RAW_DATA 0x210 /* multitouch device */ |
| 37 | #include "hal-abi.h" | 34 | #define BTN_TOOL_PRESS 0x148 /* The trackpad is a physical button */ |
| 38 | 35 | #define BTN_MT_RAW_DATA 0x14b /* multitouch device */ | |
| 39 | #define HAL_PROPERTY "org.freedesktop.Hal.Device.KeyboardBacklight" | 36 | #define BTN_MT_RAW_FINGER 0x14c /* multitouch device */ |
| 40 | 37 | #define BTN_TOOL_QUADTAP 0x14f /* Four fingers on trackpad */ | |
| 41 | static const char prop_name[] = HAL_PROPERTY; | 38 | |
| 42 | static const char prop_invalid[] = HAL_PROPERTY ".Invalid"; | 39 | #define ABS_MT_TOUCH 0x30 |
| 43 | static const char prop_set[] = "SetBrightness"; | 40 | #define ABS_MT_TOUCH_MAJOR 0x30 |
| 44 | static const char prop_get[] = "GetBrightness"; | 41 | #define ABS_MT_TOUCH_MINOR 0x31 |
| 45 | 42 | #define ABS_MT_WIDTH 0x32 | |
| 46 | static struct GMainLoop *main_loop; | 43 | #define ABS_MT_WIDTH_MAJOR 0x32 |
| 47 | static struct LibHalContext *halctx; | 44 | #define ABS_MT_WIDTH_MINOR 0x33 |
| 48 | static struct DBusConnection *conn; | 45 | #define ABS_MT_ORIENTATION 0x34 |
| 49 | 46 | #define ABS_MT_POSITION_X 0x35 | |
| 50 | static char sysfs_path[512]; | 47 | #define ABS_MT_POSITION_Y 0x36 |
| 51 | static int levels; | 48 | |
| 52 | 49 | typedef int bool; | |
| 53 | /** | 50 | |
| 54 | * Get keyboard backlight level | 51 | #define ADDCAP(s, c, x) strcat(s, c->has_##x ? " " #x : "") |
| 55 | * | 52 | |
| 56 | * Tries to read from the specified sysfs device, and if successful, | 53 | //////////////////////////////////////////////////////////////////////////// |
| 57 | * updates the internal keyboard backlight state. Warns if the device | 54 | |
| 58 | * cannot be opened or be read from. Always returns the last succesful | 55 | struct Capabilities { |
| 59 | * read. | 56 | bool has_left, has_middle; |
| 60 | */ | 57 | bool has_right, has_mtdata; |
| 61 | static int get_kbd_backlight() | 58 | bool has_touch_major, has_touch_minor; |
| 59 | bool has_width_major, has_width_minor; | ||
| 60 | bool has_orientation, has_dummy; | ||
| 61 | bool has_position_x, has_position_y; | ||
| 62 | struct input_absinfo abs_touch_major; | ||
| 63 | struct input_absinfo abs_touch_minor; | ||
| 64 | struct input_absinfo abs_width_major; | ||
| 65 | struct input_absinfo abs_width_minor; | ||
| 66 | struct input_absinfo abs_orientation; | ||
| 67 | struct input_absinfo abs_position_x; | ||
| 68 | struct input_absinfo abs_position_y; | ||
| 69 | }; | ||
| 70 | |||
| 71 | //////////////////////////////////////////////////////////////////////////// | ||
| 72 | |||
| 73 | static int read_capabilities(struct Capabilities *cap, int fd) | ||
| 62 | { | 74 | { |
| 63 | static int level; | 75 | unsigned long evbits[NBITS(EV_MAX)]; |
| 64 | int fd; | 76 | unsigned long absbits[NBITS(ABS_MAX)]; |
| 65 | char buf[64]; | 77 | unsigned long keybits[NBITS(KEY_MAX)]; |
| 66 | 78 | int rc; | |
| 67 | fd = open(sysfs_path, O_RDONLY); | 79 | |
| 68 | if (fd < 0) { | 80 | memset(cap, 0, sizeof(struct Capabilities)); |
| 69 | HAL_WARNING(("Could not open '%s'", sysfs_path)); | 81 | |
| 70 | return level; | 82 | SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_SYN, sizeof(evbits)), evbits)); |
| 71 | } | 83 | if (rc < 0) |
| 72 | 84 | return rc; | |
| 73 | memset(buf, 0, sizeof(buf)); | 85 | SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits)); |
| 74 | if (read(fd, buf, sizeof(buf)) < 0) | 86 | if (rc < 0) |
| 75 | HAL_WARNING(("Could not read from '%s'", sysfs_path)); | 87 | return rc; |
| 76 | else | 88 | SYSCALL(rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits)); |
| 77 | level = atoi(buf); | 89 | if (rc < 0) |
| 78 | 90 | return rc; | |
| 79 | close(fd); | 91 | |
| 80 | return level; | 92 | cap->has_left = TEST_BIT(BTN_LEFT, keybits); |
| 93 | cap->has_middle = TEST_BIT(BTN_MIDDLE, keybits); | ||
| 94 | cap->has_right = TEST_BIT(BTN_RIGHT, keybits); | ||
| 95 | cap->has_mtdata = TEST_BIT(BTN_MT_RAW_DATA, keybits); | ||
| 96 | |||
| 97 | SYSCALL(rc = ioctl(fd, EVIOCGABS(ABS_MT_TOUCH_MAJOR), | ||
| 98 | &cap->abs_touch_major)); | ||
| 99 | cap->has_touch_major = rc >= 0; | ||
| 81 | } | 100 | } |
| 82 | 101 | ||
| 83 | /** | 102 | static int output_capabilities(const struct Capabilities *cap) |
| 84 | * Set keyboard backlight level | ||
| 85 | * | ||
| 86 | * Warns if the device cannot be opened or be written to. | ||
| 87 | * Returns bytes written on success, negative value on failure. | ||
| 88 | */ | ||
| 89 | static int set_kbd_backlight(int level) | ||
| 90 | { | 103 | { |
| 91 | int fd, ret; | 104 | char line[1024]; |
| 92 | char buf[64]; | 105 | ADDCAP(line, cap, left); |
| 93 | 106 | ADDCAP(line, cap, middle); | |
| 94 | sprintf(buf, "%d", level); | 107 | ADDCAP(line, cap, right); |
| 95 | 108 | ADDCAP(line, cap, mtdata); | |
| 96 | fd = open(sysfs_path, O_WRONLY); | 109 | ADDCAP(line, cap, touch_major); |
| 97 | if (fd < 0) { | 110 | ADDCAP(line, cap, touch_minor); |
| 98 | HAL_WARNING(("Could not open '%s'", sysfs_path)); | 111 | ADDCAP(line, cap, width_major); |
| 99 | return -1; | 112 | ADDCAP(line, cap, width_minor); |
| 100 | } | 113 | ADDCAP(line, cap, orientation); |
| 101 | 114 | ADDCAP(line, cap, position_x); | |
| 102 | ret = write(fd, buf, strlen(buf)); | 115 | ADDCAP(line, cap, position_y); |
| 103 | if (ret < 0) | 116 | xf86Msg(X_INFO, "multitouch: caps:%s\n", line); |
| 104 | HAL_WARNING(("Could not write '%s' to '%s'", buf, sysfs_path)); | ||
| 105 | |||
| 106 | close(fd); | ||
| 107 | return ret; | ||
| 108 | } | 117 | } |
| 109 | 118 | ||
| 110 | /* DBus set keyboard backlight level */ | 119 | static InputInfoPtr preinit(InputDriverPtr drv, IDevPtr dev, int flags) |
| 111 | static struct DBusMessage *dbus_get_kbd_backlight(struct DBusMessage *msg) | ||
| 112 | { | 120 | { |
| 113 | struct DBusError err; | 121 | InputInfoPtr local = xf86AllocateInput(drv, 0); |
| 114 | struct DBusMessage *reply; | 122 | if (!local) |
| 115 | int level; | 123 | goto error; |
| 116 | 124 | ||
| 117 | dbus_error_init(&err); | 125 | local->name = dev->identifier; |
| 118 | if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INVALID)) | 126 | local->type_name = XI_TOUCHPAD; |
| 119 | return dbus_message_new_error(msg, prop_invalid, | 127 | local->device_control = 0;//DeviceControl; |
| 120 | "Invalid message"); | 128 | local->read_input = 0;//ReadInput; |
| 121 | 129 | local->control_proc = 0;//ControlProc; | |
| 122 | level = get_kbd_backlight(); | 130 | local->close_proc = 0;//CloseProc; |
| 123 | 131 | local->switch_mode = 0;//SwitchMode; | |
| 124 | reply = dbus_message_new_method_return(msg); | 132 | local->conversion_proc = 0;//ConvertProc; |
| 125 | if (reply) | 133 | local->reverse_conversion_proc = NULL; |
| 126 | dbus_message_append_args(reply, DBUS_TYPE_INT32, &level, | 134 | local->dev = NULL; |
| 127 | DBUS_TYPE_INVALID); | 135 | local->private = 0;//priv; |
| 128 | 136 | local->private_flags = 0; | |
| 129 | return reply; | 137 | local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS; |
| 138 | local->conf_idev = dev; | ||
| 139 | //local->motion_history_proc = xf86GetMotionEvents; | ||
| 140 | //local->history_size = 0; | ||
| 141 | local->always_core_feedback = 0; | ||
| 142 | |||
| 143 | xf86CollectInputOptions(local, NULL, NULL); | ||
| 144 | xf86OptionListReport(local->options); | ||
| 145 | |||
| 146 | local->fd = xf86OpenSerial(local->options); | ||
| 147 | if (local->fd < 0) { | ||
| 148 | xf86Msg(X_ERROR, "multitouch: cannot open device\n"); | ||
| 149 | goto error; | ||
| 150 | } | ||
| 151 | struct Capabilities cap; | ||
| 152 | read_capabilities(&cap, local->fd); | ||
| 153 | output_capabilities(&cap); | ||
| 154 | xf86CloseSerial(local->fd); | ||
| 155 | local->fd = -1; | ||
| 156 | local->flags |= XI86_CONFIGURED; | ||
| 157 | error: | ||
| 158 | return local; | ||
| 130 | } | 159 | } |
| 131 | 160 | ||
| 132 | /* DBus set keyboard backlight level */ | 161 | static void uninit(InputDriverPtr drv, InputInfoPtr local, int flags) |
| 133 | static struct DBusMessage *dbus_set_kbd_backlight(struct DBusMessage *msg) | ||
| 134 | { | 162 | { |
| 135 | struct DBusError err; | 163 | xf86DeleteInput(local, 0); |
| 136 | int level; | ||
| 137 | |||
| 138 | dbus_error_init(&err); | ||
| 139 | if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &level, | ||
| 140 | DBUS_TYPE_INVALID)) | ||
| 141 | return dbus_message_new_error(msg, prop_invalid, | ||
| 142 | "Brightness level argument missing"); | ||
| 143 | |||
| 144 | if (level < 0 || level > levels - 1) | ||
| 145 | return dbus_message_new_error(msg, prop_invalid, | ||
| 146 | "Brightness level is invalid"); | ||
| 147 | |||
| 148 | set_kbd_backlight(level); | ||
| 149 | |||
| 150 | return dbus_message_new_method_return(msg); | ||
| 151 | } | 164 | } |
| 152 | 165 | ||
| 153 | /* DBus filter function */ | 166 | //////////////////////////////////////////////////////////////////////////// |
| 154 | static DBusHandlerResult filter_function(struct DBusConnection *connection, | 167 | |
| 155 | struct DBusMessage *message, | 168 | static InputDriverRec MULTITOUCH = { |
| 156 | void *userdata) | 169 | 1, |
| 170 | "multitouch", | ||
| 171 | NULL, | ||
| 172 | preinit, | ||
| 173 | uninit, | ||
| 174 | NULL, | ||
| 175 | 0 | ||
| 176 | }; | ||
| 177 | |||
| 178 | static XF86ModuleVersionInfo VERSION = { | ||
| 179 | "multitouch", | ||
| 180 | MODULEVENDORSTRING, | ||
| 181 | MODINFOSTRING1, | ||
| 182 | MODINFOSTRING2, | ||
| 183 | XORG_VERSION_CURRENT, | ||
| 184 | 0, 1, 0, | ||
| 185 | ABI_CLASS_XINPUT, | ||
| 186 | ABI_XINPUT_VERSION, | ||
| 187 | MOD_CLASS_XINPUT, | ||
| 188 | {0, 0, 0, 0} | ||
| 189 | }; | ||
| 190 | |||
| 191 | static pointer setup(pointer module, pointer options, int *errmaj, int *errmin) | ||
| 157 | { | 192 | { |
| 158 | struct DBusMessage *reply = NULL; | 193 | xf86AddInputDriver(&MULTITOUCH, module, 0); |
| 159 | 194 | return module; | |
| 160 | if (dbus_message_is_method_call(message, prop_name, prop_get)) | ||
| 161 | reply = dbus_get_kbd_backlight(message); | ||
| 162 | else if (dbus_message_is_method_call(message, prop_name, prop_set)) | ||
| 163 | reply = dbus_set_kbd_backlight(message); | ||
| 164 | |||
| 165 | if (reply) { | ||
| 166 | dbus_connection_send(connection, reply, NULL); | ||
| 167 | dbus_message_unref(reply); | ||
| 168 | } | ||
| 169 | |||
| 170 | return DBUS_HANDLER_RESULT_HANDLED; | ||
| 171 | } | 195 | } |
| 172 | 196 | ||
| 173 | int main(int argc, char *argv[]) | 197 | XF86ModuleData multitouchModuleData = {&VERSION, &setup, NULL }; |
| 174 | { | ||
| 175 | struct DBusError err; | ||
| 176 | struct stat fs; | ||
| 177 | |||
| 178 | char *udi = getenv("UDI"); | ||
| 179 | char *path = getenv("HAL_PROP_LINUX_SYSFS_PATH"); | ||
| 180 | char *level_str = getenv("HAL_PROP_KEYBOARD_BACKLIGHT_NUM_LEVELS"); | ||
| 181 | |||
| 182 | setup_logger(); | ||
| 183 | |||
| 184 | if (udi == NULL) { | ||
| 185 | HAL_ERROR(("No device specified")); | ||
| 186 | return -2; | ||
| 187 | } | ||
| 188 | if (path == NULL) { | ||
| 189 | HAL_ERROR(("No sysfs path specified")); | ||
| 190 | return -2; | ||
| 191 | } | ||
| 192 | if (level_str == NULL) { | ||
| 193 | HAL_ERROR(("No keyboard_backlight.num_levels defined")); | ||
| 194 | return -2; | ||
| 195 | } | ||
| 196 | |||
| 197 | levels = atoi(level_str); | ||
| 198 | snprintf(sysfs_path, sizeof(sysfs_path), "%s/brightness", path); | ||
| 199 | |||
| 200 | HAL_DEBUG(("udi='%s', path='%s', levels='%d'", udi, path, levels)); | ||
| 201 | |||
| 202 | if (stat(sysfs_path, &fs)) { | ||
| 203 | HAL_ERROR(("The sysfs property path does not exist")); | ||
| 204 | return -2; | ||
| 205 | } | ||
| 206 | |||
| 207 | dbus_error_init(&err); | ||
| 208 | halctx = libhal_ctx_init_direct(&err); | ||
| 209 | |||
| 210 | if (halctx == NULL) { | ||
| 211 | HAL_ERROR(("Cannot connect to hald")); | ||
| 212 | return -3; | ||
| 213 | } | ||
| 214 | |||
| 215 | conn = libhal_ctx_get_dbus_connection(halctx); | ||
| 216 | dbus_connection_setup_with_g_main(conn, NULL); | ||
| 217 | |||
| 218 | dbus_connection_add_filter(conn, filter_function, NULL, NULL); | ||
| 219 | |||
| 220 | if (!libhal_device_claim_interface(halctx, udi, prop_name, | ||
| 221 | " <method name=\"GetBrightness\">\n" | ||
| 222 | " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" | ||
| 223 | " </method>\n" | ||
| 224 | " <method name=\"SetBrightness\">\n" | ||
| 225 | " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" | ||
| 226 | " </method>\n", | ||
| 227 | &err)) { | ||
| 228 | HAL_ERROR(("Cannot claim interface '" HAL_PROPERTY "'")); | ||
| 229 | return -4; | ||
| 230 | } | ||
| 231 | |||
| 232 | dbus_error_init(&err); | ||
| 233 | if (!libhal_device_addon_is_ready(halctx, udi, &err)) | ||
| 234 | return -4; | ||
| 235 | |||
| 236 | main_loop = g_main_loop_new(NULL, FALSE); | ||
| 237 | g_main_loop_run(main_loop); | ||
| 238 | |||
| 239 | return 0; | ||
| 240 | } | ||
| 241 | 198 | ||
| 199 | //////////////////////////////////////////////////////////////////////////// | ||
