| 1 |
#include <stdlib.h> |
| 2 |
#include <gmodule.h> |
| 3 |
#include <clutter/clutter.h> |
| 4 |
|
| 5 |
#ifdef CLUTTER_WINDOWING_X11 |
| 6 |
#include <clutter/x11/clutter-x11.h> |
| 7 |
#endif |
| 8 |
|
| 9 |
typedef struct { |
| 10 |
ClutterActor *stage; |
| 11 |
|
| 12 |
GHashTable *devices; |
| 13 |
} TestDevicesApp; |
| 14 |
|
| 15 |
static const gchar * |
| 16 |
device_type_name (ClutterInputDevice *device) |
| 17 |
{ |
| 18 |
ClutterInputDeviceType d_type; |
| 19 |
|
| 20 |
d_type = clutter_input_device_get_device_type (device); |
| 21 |
switch (d_type) |
| 22 |
{ |
| 23 |
case CLUTTER_POINTER_DEVICE: |
| 24 |
return "Pointer"; |
| 25 |
|
| 26 |
case CLUTTER_KEYBOARD_DEVICE: |
| 27 |
return "Keyboard"; |
| 28 |
|
| 29 |
case CLUTTER_EXTENSION_DEVICE: |
| 30 |
return "Extension"; |
| 31 |
|
| 32 |
case CLUTTER_PEN_DEVICE: |
| 33 |
return "Pen"; |
| 34 |
|
| 35 |
case CLUTTER_ERASER_DEVICE: |
| 36 |
return "Eraser"; |
| 37 |
|
| 38 |
case CLUTTER_CURSOR_DEVICE: |
| 39 |
return "Cursor"; |
| 40 |
|
| 41 |
default: |
| 42 |
return "Unknown"; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static const gchar * |
| 47 |
axis_type_name (ClutterInputAxis axis) |
| 48 |
{ |
| 49 |
switch (axis) |
| 50 |
{ |
| 51 |
case CLUTTER_INPUT_AXIS_X: |
| 52 |
return "Absolute X"; |
| 53 |
|
| 54 |
case CLUTTER_INPUT_AXIS_Y: |
| 55 |
return "Absolute Y"; |
| 56 |
|
| 57 |
case CLUTTER_INPUT_AXIS_PRESSURE: |
| 58 |
return "Pressure"; |
| 59 |
|
| 60 |
case CLUTTER_INPUT_AXIS_XTILT: |
| 61 |
return "X Tilt"; |
| 62 |
|
| 63 |
case CLUTTER_INPUT_AXIS_YTILT: |
| 64 |
return "Y Tilt"; |
| 65 |
|
| 66 |
case CLUTTER_INPUT_AXIS_WHEEL: |
| 67 |
return "Wheel"; |
| 68 |
|
| 69 |
default: |
| 70 |
return "Unknown"; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static gboolean |
| 75 |
stage_button_event_cb (ClutterActor *actor, |
| 76 |
ClutterEvent *event, |
| 77 |
TestDevicesApp *app) |
| 78 |
{ |
| 79 |
ClutterInputDevice *device; |
| 80 |
ClutterInputDevice *source_device; |
| 81 |
ClutterActor *hand = NULL; |
| 82 |
gdouble *axes; |
| 83 |
guint n_axes, i; |
| 84 |
|
| 85 |
device = clutter_event_get_device (event); |
| 86 |
source_device = clutter_event_get_source_device (event); |
| 87 |
|
| 88 |
hand = g_hash_table_lookup (app->devices, device); |
| 89 |
|
| 90 |
g_print ("Device: '%s' (id:%d, type: %s, source: '%s', axes: %d)\n", |
| 91 |
clutter_input_device_get_device_name (device), |
| 92 |
clutter_input_device_get_device_id (device), |
| 93 |
device_type_name (device), |
| 94 |
source_device != device |
| 95 |
? clutter_input_device_get_device_name (source_device) |
| 96 |
: "<same>", |
| 97 |
clutter_input_device_get_n_axes (device)); |
| 98 |
|
| 99 |
if (hand != NULL) |
| 100 |
{ |
| 101 |
gfloat event_x, event_y; |
| 102 |
|
| 103 |
clutter_event_get_coords (event, &event_x, &event_y); |
| 104 |
clutter_actor_set_position (hand, event_x, event_y); |
| 105 |
} |
| 106 |
|
| 107 |
axes = clutter_event_get_axes (event, &n_axes); |
| 108 |
for (i = 0; i < n_axes; i++) |
| 109 |
{ |
| 110 |
ClutterInputAxis axis; |
| 111 |
|
| 112 |
axis = clutter_input_device_get_axis (device, i); |
| 113 |
if (axis == CLUTTER_INPUT_AXIS_IGNORE) |
| 114 |
continue; |
| 115 |
|
| 116 |
g_print ("\tAxis[%2d][%s].value: %.2f\n", |
| 117 |
i, |
| 118 |
axis_type_name (axis), |
| 119 |
axes[i]); |
| 120 |
} |
| 121 |
|
| 122 |
return FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
static gboolean |
| 126 |
stage_motion_event_cb (ClutterActor *actor, |
| 127 |
ClutterEvent *event, |
| 128 |
TestDevicesApp *app) |
| 129 |
{ |
| 130 |
ClutterInputDevice *device; |
| 131 |
ClutterActor *hand = NULL; |
| 132 |
|
| 133 |
device = clutter_event_get_device (event); |
| 134 |
|
| 135 |
hand = g_hash_table_lookup (app->devices, device); |
| 136 |
if (hand != NULL) |
| 137 |
{ |
| 138 |
gfloat event_x, event_y; |
| 139 |
|
| 140 |
clutter_event_get_coords (event, &event_x, &event_y); |
| 141 |
clutter_actor_set_position (hand, event_x, event_y); |
| 142 |
|
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
return FALSE; |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
manager_device_added_cb (ClutterDeviceManager *manager, |
| 151 |
ClutterInputDevice *device, |
| 152 |
TestDevicesApp *app) |
| 153 |
{ |
| 154 |
ClutterInputDeviceType device_type; |
| 155 |
ClutterActor *hand = NULL; |
| 156 |
|
| 157 |
g_print ("got a %s device '%s' with id %d\n", |
| 158 |
device_type_name (device), |
| 159 |
clutter_input_device_get_device_name (device), |
| 160 |
clutter_input_device_get_device_id (device)); |
| 161 |
|
| 162 |
device_type = clutter_input_device_get_device_type (device); |
| 163 |
if (device_type == CLUTTER_POINTER_DEVICE || |
| 164 |
device_type == CLUTTER_PEN_DEVICE || |
| 165 |
device_type == CLUTTER_POINTER_DEVICE) |
| 166 |
{ |
| 167 |
g_print ("*** enabling device '%s' ***\n", |
| 168 |
clutter_input_device_get_device_name (device)); |
| 169 |
|
| 170 |
clutter_input_device_set_enabled (device, TRUE); |
| 171 |
|
| 172 |
hand = clutter_texture_new_from_file (TESTS_DATADIR |
| 173 |
G_DIR_SEPARATOR_S |
| 174 |
"redhand.png", |
| 175 |
NULL); |
| 176 |
g_hash_table_insert (app->devices, device, hand); |
| 177 |
|
| 178 |
clutter_container_add_actor (CLUTTER_CONTAINER (app->stage), hand); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
manager_device_removed_cb (ClutterDeviceManager *manager, |
| 184 |
ClutterInputDevice *device, |
| 185 |
TestDevicesApp *app) |
| 186 |
{ |
| 187 |
ClutterInputDeviceType device_type; |
| 188 |
ClutterActor *hand = NULL; |
| 189 |
|
| 190 |
g_print ("removed a %s device '%s' with id %d\n", |
| 191 |
device_type_name (device), |
| 192 |
clutter_input_device_get_device_name (device), |
| 193 |
clutter_input_device_get_device_id (device)); |
| 194 |
|
| 195 |
device_type = clutter_input_device_get_device_type (device); |
| 196 |
if (device_type == CLUTTER_POINTER_DEVICE || |
| 197 |
device_type == CLUTTER_PEN_DEVICE || |
| 198 |
device_type == CLUTTER_POINTER_DEVICE) |
| 199 |
{ |
| 200 |
hand = g_hash_table_lookup (app->devices, device); |
| 201 |
if (hand != NULL) |
| 202 |
clutter_container_add_actor (CLUTTER_CONTAINER (app->stage), hand); |
| 203 |
|
| 204 |
g_hash_table_remove (app->devices, device); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
G_MODULE_EXPORT int |
| 209 |
test_devices_main (int argc, char **argv) |
| 210 |
{ |
| 211 |
ClutterActor *stage; |
| 212 |
TestDevicesApp *app; |
| 213 |
ClutterDeviceManager *manager; |
| 214 |
const GSList *stage_devices, *l; |
| 215 |
|
| 216 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 217 |
return 1; |
| 218 |
|
| 219 |
app = g_new0 (TestDevicesApp, 1); |
| 220 |
app->devices = g_hash_table_new (g_direct_hash, g_direct_equal) ; |
| 221 |
|
| 222 |
stage = clutter_stage_new (); |
| 223 |
clutter_actor_set_background_color (stage, CLUTTER_COLOR_LightSkyBlue); |
| 224 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Devices"); |
| 225 |
clutter_stage_hide_cursor (CLUTTER_STAGE (stage)); |
| 226 |
g_signal_connect (stage, |
| 227 |
"destroy", G_CALLBACK (clutter_main_quit), |
| 228 |
NULL); |
| 229 |
g_signal_connect (stage, |
| 230 |
"motion-event", G_CALLBACK (stage_motion_event_cb), |
| 231 |
app); |
| 232 |
g_signal_connect (stage, |
| 233 |
"button-press-event", G_CALLBACK (stage_button_event_cb), |
| 234 |
app); |
| 235 |
app->stage = stage; |
| 236 |
|
| 237 |
clutter_actor_show_all (stage); |
| 238 |
|
| 239 |
manager = clutter_device_manager_get_default (); |
| 240 |
g_signal_connect (manager, |
| 241 |
"device-added", G_CALLBACK (manager_device_added_cb), |
| 242 |
app); |
| 243 |
g_signal_connect (manager, |
| 244 |
"device-removed", G_CALLBACK (manager_device_removed_cb), |
| 245 |
app); |
| 246 |
|
| 247 |
stage_devices = clutter_device_manager_peek_devices (manager); |
| 248 |
|
| 249 |
if (stage_devices == NULL) |
| 250 |
g_error ("No input devices found."); |
| 251 |
|
| 252 |
for (l = stage_devices; l != NULL; l = l->next) |
| 253 |
{ |
| 254 |
ClutterInputDevice *device = l->data; |
| 255 |
ClutterInputDeviceType device_type; |
| 256 |
ClutterActor *hand = NULL; |
| 257 |
|
| 258 |
g_print ("got a %s device '%s' with id %d\n", |
| 259 |
device_type_name (device), |
| 260 |
clutter_input_device_get_device_name (device), |
| 261 |
clutter_input_device_get_device_id (device)); |
| 262 |
|
| 263 |
device_type = clutter_input_device_get_device_type (device); |
| 264 |
if (device_type == CLUTTER_POINTER_DEVICE || |
| 265 |
device_type == CLUTTER_PEN_DEVICE || |
| 266 |
device_type == CLUTTER_POINTER_DEVICE) |
| 267 |
{ |
| 268 |
g_print ("*** enabling device '%s' ***\n", |
| 269 |
clutter_input_device_get_device_name (device)); |
| 270 |
|
| 271 |
clutter_input_device_set_enabled (device, TRUE); |
| 272 |
|
| 273 |
hand = clutter_texture_new_from_file (TESTS_DATADIR |
| 274 |
G_DIR_SEPARATOR_S |
| 275 |
"redhand.png", |
| 276 |
NULL); |
| 277 |
g_hash_table_insert (app->devices, device, hand); |
| 278 |
|
| 279 |
clutter_container_add_actor (CLUTTER_CONTAINER (stage), hand); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
clutter_main (); |
| 284 |
|
| 285 |
return EXIT_SUCCESS; |
| 286 |
} |