| 1 |
/* Touch and Drawing Tablets |
| 2 |
* |
| 3 |
* Demonstrates advanced handling of event information from exotic |
| 4 |
* input devices. |
| 5 |
* |
| 6 |
* On one hand, this snippet demonstrates management of drawing tablets, |
| 7 |
* those contain additional information for the pointer other than |
| 8 |
* X/Y coordinates. Tablet pads events are mapped to actions, which |
| 9 |
* are both defined and interpreted by the application. |
| 10 |
* |
| 11 |
* Input axes are dependent on hardware devices, on linux/unix you |
| 12 |
* can see the device axes through xinput list <device>. Each time |
| 13 |
* a different hardware device is used to move the pointer, the |
| 14 |
* master device will be updated to match the axes it provides, |
| 15 |
* these changes can be tracked through GdkDevice::changed, or |
| 16 |
* checking gdk_event_get_source_device(). |
| 17 |
* |
| 18 |
* On the other hand, this demo handles basic multitouch events, |
| 19 |
* each event coming from an specific touchpoint will contain a |
| 20 |
* GdkEventSequence that's unique for its lifetime, so multiple |
| 21 |
* touchpoints can be tracked. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <glib/gi18n.h> |
| 25 |
#include <gtk/gtk.h> |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
GdkDevice *last_source; |
| 29 |
GdkDeviceTool *last_tool; |
| 30 |
gdouble *axes; |
| 31 |
GdkRGBA color; |
| 32 |
gdouble x; |
| 33 |
gdouble y; |
| 34 |
} AxesInfo; |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
GHashTable *pointer_info; /* GdkDevice -> AxesInfo */ |
| 38 |
GHashTable *touch_info; /* GdkEventSequence -> AxesInfo */ |
| 39 |
} EventData; |
| 40 |
|
| 41 |
const gchar *colors[] = { |
| 42 |
"black", |
| 43 |
"orchid", |
| 44 |
"fuchsia", |
| 45 |
"indigo", |
| 46 |
"thistle", |
| 47 |
"sienna", |
| 48 |
"azure", |
| 49 |
"plum", |
| 50 |
"lime", |
| 51 |
"navy", |
| 52 |
"maroon", |
| 53 |
"burlywood" |
| 54 |
}; |
| 55 |
|
| 56 |
static GtkPadActionEntry pad_actions[] = { |
| 57 |
{ GTK_PAD_ACTION_BUTTON, 1, -1, N_("Nuclear strike"), "pad.nuke" }, |
| 58 |
{ GTK_PAD_ACTION_BUTTON, 2, -1, N_("Release siberian methane reserves"), "pad.heat" }, |
| 59 |
{ GTK_PAD_ACTION_BUTTON, 3, -1, N_("Release solar flare"), "pad.fry" }, |
| 60 |
{ GTK_PAD_ACTION_BUTTON, 4, -1, N_("De-stabilize Oort cloud"), "pad.fall" }, |
| 61 |
{ GTK_PAD_ACTION_BUTTON, 5, -1, N_("Ignite WR-104"), "pad.burst" }, |
| 62 |
{ GTK_PAD_ACTION_BUTTON, 6, -1, N_("Lart whoever asks about this button"), "pad.lart" }, |
| 63 |
{ GTK_PAD_ACTION_RING, -1, -1, N_("Earth axial tilt"), "pad.tilt" }, |
| 64 |
{ GTK_PAD_ACTION_STRIP, -1, -1, N_("Extent of weak nuclear force"), "pad.dissolve" }, |
| 65 |
}; |
| 66 |
|
| 67 |
static const gchar *pad_action_results[] = { |
| 68 |
"☢", |
| 69 |
"♨", |
| 70 |
"☼", |
| 71 |
"☄", |
| 72 |
"⚡", |
| 73 |
"💫", |
| 74 |
"◑", |
| 75 |
"⚛" |
| 76 |
}; |
| 77 |
|
| 78 |
static guint cur_color = 0; |
| 79 |
static guint pad_action_timeout_id = 0; |
| 80 |
|
| 81 |
static AxesInfo * |
| 82 |
axes_info_new (void) |
| 83 |
{ |
| 84 |
AxesInfo *info; |
| 85 |
|
| 86 |
info = g_new0 (AxesInfo, 1); |
| 87 |
gdk_rgba_parse (&info->color, colors[cur_color]); |
| 88 |
|
| 89 |
cur_color = (cur_color + 1) % G_N_ELEMENTS (colors); |
| 90 |
|
| 91 |
return info; |
| 92 |
} |
| 93 |
|
| 94 |
static EventData * |
| 95 |
event_data_new (void) |
| 96 |
{ |
| 97 |
EventData *data; |
| 98 |
|
| 99 |
data = g_new0 (EventData, 1); |
| 100 |
data->pointer_info = g_hash_table_new_full (NULL, NULL, NULL, |
| 101 |
(GDestroyNotify) g_free); |
| 102 |
data->touch_info = g_hash_table_new_full (NULL, NULL, NULL, |
| 103 |
(GDestroyNotify) g_free); |
| 104 |
|
| 105 |
return data; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
event_data_free (EventData *data) |
| 110 |
{ |
| 111 |
g_hash_table_destroy (data->pointer_info); |
| 112 |
g_hash_table_destroy (data->touch_info); |
| 113 |
g_free (data); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
update_axes_from_event (GdkEvent *event, |
| 118 |
EventData *data) |
| 119 |
{ |
| 120 |
GdkDevice *device, *source_device; |
| 121 |
GdkEventSequence *sequence; |
| 122 |
GdkDeviceTool *tool; |
| 123 |
gdouble x, y; |
| 124 |
AxesInfo *info; |
| 125 |
|
| 126 |
device = gdk_event_get_device (event); |
| 127 |
source_device = gdk_event_get_source_device (event); |
| 128 |
sequence = gdk_event_get_event_sequence (event); |
| 129 |
tool = gdk_event_get_device_tool (event); |
| 130 |
|
| 131 |
if (event->type == GDK_TOUCH_END || |
| 132 |
event->type == GDK_TOUCH_CANCEL) |
| 133 |
{ |
| 134 |
g_hash_table_remove (data->touch_info, sequence); |
| 135 |
return; |
| 136 |
} |
| 137 |
else if (event->type == GDK_LEAVE_NOTIFY) |
| 138 |
{ |
| 139 |
g_hash_table_remove (data->pointer_info, device); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if (!sequence) |
| 144 |
{ |
| 145 |
info = g_hash_table_lookup (data->pointer_info, device); |
| 146 |
|
| 147 |
if (!info) |
| 148 |
{ |
| 149 |
info = axes_info_new (); |
| 150 |
g_hash_table_insert (data->pointer_info, device, info); |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
info = g_hash_table_lookup (data->touch_info, sequence); |
| 156 |
|
| 157 |
if (!info) |
| 158 |
{ |
| 159 |
info = axes_info_new (); |
| 160 |
g_hash_table_insert (data->touch_info, sequence, info); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if (info->last_source != source_device) |
| 165 |
info->last_source = source_device; |
| 166 |
|
| 167 |
if (info->last_tool != tool) |
| 168 |
info->last_tool = tool; |
| 169 |
|
| 170 |
g_clear_pointer (&info->axes, g_free); |
| 171 |
|
| 172 |
if (event->type == GDK_TOUCH_BEGIN || |
| 173 |
event->type == GDK_TOUCH_UPDATE) |
| 174 |
{ |
| 175 |
if (sequence && event->touch.emulating_pointer) |
| 176 |
g_hash_table_remove (data->pointer_info, device); |
| 177 |
} |
| 178 |
if (event->type == GDK_MOTION_NOTIFY) |
| 179 |
{ |
| 180 |
info->axes = |
| 181 |
g_memdup (event->motion.axes, |
| 182 |
sizeof (gdouble) * gdk_device_get_n_axes (source_device)); |
| 183 |
} |
| 184 |
else if (event->type == GDK_BUTTON_PRESS || |
| 185 |
event->type == GDK_BUTTON_RELEASE) |
| 186 |
{ |
| 187 |
info->axes = |
| 188 |
g_memdup (event->button.axes, |
| 189 |
sizeof (gdouble) * gdk_device_get_n_axes (source_device)); |
| 190 |
} |
| 191 |
|
| 192 |
if (gdk_event_get_coords (event, &x, &y)) |
| 193 |
{ |
| 194 |
info->x = x; |
| 195 |
info->y = y; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
static gboolean |
| 200 |
event_cb (GtkWidget *widget, |
| 201 |
GdkEvent *event, |
| 202 |
gpointer user_data) |
| 203 |
{ |
| 204 |
update_axes_from_event (event, user_data); |
| 205 |
gtk_widget_queue_draw (widget); |
| 206 |
return FALSE; |
| 207 |
} |
| 208 |
|
| 209 |
static void |
| 210 |
render_arrow (cairo_t *cr, |
| 211 |
gdouble x_diff, |
| 212 |
gdouble y_diff, |
| 213 |
const gchar *label) |
| 214 |
{ |
| 215 |
cairo_save (cr); |
| 216 |
|
| 217 |
cairo_set_source_rgb (cr, 0, 0, 0); |
| 218 |
cairo_new_path (cr); |
| 219 |
cairo_move_to (cr, 0, 0); |
| 220 |
cairo_line_to (cr, x_diff, y_diff); |
| 221 |
cairo_stroke (cr); |
| 222 |
|
| 223 |
cairo_move_to (cr, x_diff, y_diff); |
| 224 |
cairo_show_text (cr, label); |
| 225 |
|
| 226 |
cairo_restore (cr); |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
draw_axes_info (cairo_t *cr, |
| 231 |
AxesInfo *info, |
| 232 |
GtkAllocation *allocation) |
| 233 |
{ |
| 234 |
gdouble pressure, tilt_x, tilt_y, distance, wheel, rotation, slider; |
| 235 |
GdkAxisFlags axes = gdk_device_get_axes (info->last_source); |
| 236 |
|
| 237 |
cairo_save (cr); |
| 238 |
|
| 239 |
cairo_set_line_width (cr, 1); |
| 240 |
gdk_cairo_set_source_rgba (cr, &info->color); |
| 241 |
|
| 242 |
cairo_move_to (cr, 0, info->y); |
| 243 |
cairo_line_to (cr, allocation->width, info->y); |
| 244 |
cairo_move_to (cr, info->x, 0); |
| 245 |
cairo_line_to (cr, info->x, allocation->height); |
| 246 |
cairo_stroke (cr); |
| 247 |
|
| 248 |
cairo_translate (cr, info->x, info->y); |
| 249 |
|
| 250 |
if (!info->axes) |
| 251 |
{ |
| 252 |
cairo_restore (cr); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
if (axes & GDK_AXIS_FLAG_PRESSURE) |
| 257 |
{ |
| 258 |
cairo_pattern_t *pattern; |
| 259 |
|
| 260 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_PRESSURE, |
| 261 |
&pressure); |
| 262 |
|
| 263 |
pattern = cairo_pattern_create_radial (0, 0, 0, 0, 0, 100); |
| 264 |
cairo_pattern_add_color_stop_rgba (pattern, pressure, 1, 0, 0, pressure); |
| 265 |
cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 1, 0); |
| 266 |
|
| 267 |
cairo_set_source (cr, pattern); |
| 268 |
|
| 269 |
cairo_arc (cr, 0, 0, 100, 0, 2 * G_PI); |
| 270 |
cairo_fill (cr); |
| 271 |
|
| 272 |
cairo_pattern_destroy (pattern); |
| 273 |
} |
| 274 |
|
| 275 |
if (axes & GDK_AXIS_FLAG_XTILT && |
| 276 |
axes & GDK_AXIS_FLAG_YTILT) |
| 277 |
{ |
| 278 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_XTILT, |
| 279 |
&tilt_x); |
| 280 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_YTILT, |
| 281 |
&tilt_y); |
| 282 |
|
| 283 |
render_arrow (cr, tilt_x * 100, tilt_y * 100, "Tilt"); |
| 284 |
} |
| 285 |
|
| 286 |
if (axes & GDK_AXIS_FLAG_DISTANCE) |
| 287 |
{ |
| 288 |
double dashes[] = { 5.0, 5.0 }; |
| 289 |
cairo_text_extents_t extents; |
| 290 |
|
| 291 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_DISTANCE, |
| 292 |
&distance); |
| 293 |
|
| 294 |
cairo_save (cr); |
| 295 |
|
| 296 |
cairo_move_to (cr, distance * 100, 0); |
| 297 |
|
| 298 |
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); |
| 299 |
cairo_set_dash (cr, dashes, 2, 0.0); |
| 300 |
cairo_arc (cr, 0, 0, distance * 100, 0, 2 * G_PI); |
| 301 |
cairo_stroke (cr); |
| 302 |
|
| 303 |
cairo_move_to (cr, 0, -distance * 100); |
| 304 |
cairo_text_extents (cr, "Distance", &extents); |
| 305 |
cairo_rel_move_to (cr, -extents.width / 2, 0); |
| 306 |
cairo_show_text (cr, "Distance"); |
| 307 |
|
| 308 |
cairo_move_to (cr, 0, 0); |
| 309 |
|
| 310 |
cairo_restore (cr); |
| 311 |
} |
| 312 |
|
| 313 |
if (axes & GDK_AXIS_FLAG_WHEEL) |
| 314 |
{ |
| 315 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_WHEEL, |
| 316 |
&wheel); |
| 317 |
|
| 318 |
cairo_save (cr); |
| 319 |
cairo_set_line_width (cr, 10); |
| 320 |
cairo_set_source_rgba (cr, 0, 0, 0, 0.5); |
| 321 |
|
| 322 |
cairo_new_sub_path (cr); |
| 323 |
cairo_arc (cr, 0, 0, 100, 0, wheel * 2 * G_PI); |
| 324 |
cairo_stroke (cr); |
| 325 |
cairo_restore (cr); |
| 326 |
} |
| 327 |
|
| 328 |
if (axes & GDK_AXIS_FLAG_ROTATION) |
| 329 |
{ |
| 330 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_ROTATION, |
| 331 |
&rotation); |
| 332 |
rotation *= 2 * G_PI; |
| 333 |
|
| 334 |
cairo_save (cr); |
| 335 |
cairo_rotate (cr, - G_PI / 2); |
| 336 |
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 337 |
cairo_set_line_width (cr, 5); |
| 338 |
|
| 339 |
cairo_new_sub_path (cr); |
| 340 |
cairo_arc (cr, 0, 0, 100, 0, rotation); |
| 341 |
cairo_stroke (cr); |
| 342 |
cairo_restore (cr); |
| 343 |
} |
| 344 |
|
| 345 |
if (axes & GDK_AXIS_FLAG_SLIDER) |
| 346 |
{ |
| 347 |
cairo_pattern_t *pattern, *mask; |
| 348 |
|
| 349 |
gdk_device_get_axis (info->last_source, info->axes, GDK_AXIS_SLIDER, |
| 350 |
&slider); |
| 351 |
|
| 352 |
cairo_save (cr); |
| 353 |
|
| 354 |
cairo_move_to (cr, 0, -10); |
| 355 |
cairo_rel_line_to (cr, 0, -50); |
| 356 |
cairo_rel_line_to (cr, 10, 0); |
| 357 |
cairo_rel_line_to (cr, -5, 50); |
| 358 |
cairo_close_path (cr); |
| 359 |
|
| 360 |
cairo_clip_preserve (cr); |
| 361 |
|
| 362 |
pattern = cairo_pattern_create_linear (0, -10, 0, -60); |
| 363 |
cairo_pattern_add_color_stop_rgb (pattern, 0, 0, 1, 0); |
| 364 |
cairo_pattern_add_color_stop_rgb (pattern, 1, 1, 0, 0); |
| 365 |
cairo_set_source (cr, pattern); |
| 366 |
cairo_pattern_destroy (pattern); |
| 367 |
|
| 368 |
mask = cairo_pattern_create_linear (0, -10, 0, -60); |
| 369 |
cairo_pattern_add_color_stop_rgba (mask, 0, 0, 0, 0, 1); |
| 370 |
cairo_pattern_add_color_stop_rgba (mask, slider, 0, 0, 0, 1); |
| 371 |
cairo_pattern_add_color_stop_rgba (mask, slider, 0, 0, 0, 0); |
| 372 |
cairo_pattern_add_color_stop_rgba (mask, 1, 0, 0, 0, 0); |
| 373 |
cairo_mask (cr, mask); |
| 374 |
cairo_pattern_destroy (mask); |
| 375 |
|
| 376 |
cairo_set_source_rgb (cr, 0, 0, 0); |
| 377 |
cairo_stroke (cr); |
| 378 |
|
| 379 |
cairo_restore (cr); |
| 380 |
} |
| 381 |
|
| 382 |
cairo_restore (cr); |
| 383 |
} |
| 384 |
|
| 385 |
static const gchar * |
| 386 |
tool_type_to_string (GdkDeviceToolType tool_type) |
| 387 |
{ |
| 388 |
switch (tool_type) |
| 389 |
{ |
| 390 |
case GDK_DEVICE_TOOL_TYPE_PEN: |
| 391 |
return "Pen"; |
| 392 |
case GDK_DEVICE_TOOL_TYPE_ERASER: |
| 393 |
return "Eraser"; |
| 394 |
case GDK_DEVICE_TOOL_TYPE_BRUSH: |
| 395 |
return "Brush"; |
| 396 |
case GDK_DEVICE_TOOL_TYPE_PENCIL: |
| 397 |
return "Pencil"; |
| 398 |
case GDK_DEVICE_TOOL_TYPE_AIRBRUSH: |
| 399 |
return "Airbrush"; |
| 400 |
case GDK_DEVICE_TOOL_TYPE_MOUSE: |
| 401 |
return "Mouse"; |
| 402 |
case GDK_DEVICE_TOOL_TYPE_LENS: |
| 403 |
return "Lens cursor"; |
| 404 |
case GDK_DEVICE_TOOL_TYPE_UNKNOWN: |
| 405 |
default: |
| 406 |
return "Unknown"; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
draw_device_info (GtkWidget *widget, |
| 412 |
cairo_t *cr, |
| 413 |
GdkEventSequence *sequence, |
| 414 |
gint *y, |
| 415 |
AxesInfo *info) |
| 416 |
{ |
| 417 |
PangoLayout *layout; |
| 418 |
GString *string; |
| 419 |
gint height; |
| 420 |
|
| 421 |
cairo_save (cr); |
| 422 |
|
| 423 |
string = g_string_new (NULL); |
| 424 |
g_string_append_printf (string, "Source: %s", |
| 425 |
gdk_device_get_name (info->last_source)); |
| 426 |
|
| 427 |
if (sequence) |
| 428 |
g_string_append_printf (string, "\nSequence: %d", |
| 429 |
GPOINTER_TO_UINT (sequence)); |
| 430 |
|
| 431 |
if (info->last_tool) |
| 432 |
{ |
| 433 |
const gchar *tool_type; |
| 434 |
guint64 serial; |
| 435 |
|
| 436 |
tool_type = tool_type_to_string (gdk_device_tool_get_tool_type (info->last_tool)); |
| 437 |
serial = gdk_device_tool_get_serial (info->last_tool); |
| 438 |
g_string_append_printf (string, "\nTool: %s", tool_type); |
| 439 |
|
| 440 |
if (serial != 0) |
| 441 |
g_string_append_printf (string, ", Serial: %lx", serial); |
| 442 |
} |
| 443 |
|
| 444 |
cairo_move_to (cr, 10, *y); |
| 445 |
layout = gtk_widget_create_pango_layout (widget, string->str); |
| 446 |
pango_cairo_show_layout (cr, layout); |
| 447 |
cairo_stroke (cr); |
| 448 |
|
| 449 |
pango_layout_get_pixel_size (layout, NULL, &height); |
| 450 |
|
| 451 |
gdk_cairo_set_source_rgba (cr, &info->color); |
| 452 |
cairo_set_line_width (cr, 10); |
| 453 |
cairo_move_to (cr, 0, *y); |
| 454 |
|
| 455 |
*y = *y + height; |
| 456 |
cairo_line_to (cr, 0, *y); |
| 457 |
cairo_stroke (cr); |
| 458 |
|
| 459 |
cairo_restore (cr); |
| 460 |
|
| 461 |
g_object_unref (layout); |
| 462 |
g_string_free (string, TRUE); |
| 463 |
} |
| 464 |
|
| 465 |
static gboolean |
| 466 |
draw_cb (GtkWidget *widget, |
| 467 |
cairo_t *cr, |
| 468 |
gpointer user_data) |
| 469 |
{ |
| 470 |
EventData *data = user_data; |
| 471 |
GtkAllocation allocation; |
| 472 |
AxesInfo *info; |
| 473 |
GHashTableIter iter; |
| 474 |
gpointer key, value; |
| 475 |
gint y = 0; |
| 476 |
|
| 477 |
gtk_widget_get_allocation (widget, &allocation); |
| 478 |
|
| 479 |
/* Draw Abs info */ |
| 480 |
g_hash_table_iter_init (&iter, data->pointer_info); |
| 481 |
|
| 482 |
while (g_hash_table_iter_next (&iter, NULL, &value)) |
| 483 |
{ |
| 484 |
info = value; |
| 485 |
draw_axes_info (cr, info, &allocation); |
| 486 |
} |
| 487 |
|
| 488 |
g_hash_table_iter_init (&iter, data->touch_info); |
| 489 |
|
| 490 |
while (g_hash_table_iter_next (&iter, NULL, &value)) |
| 491 |
{ |
| 492 |
info = value; |
| 493 |
draw_axes_info (cr, info, &allocation); |
| 494 |
} |
| 495 |
|
| 496 |
/* Draw name, color legend and misc data */ |
| 497 |
g_hash_table_iter_init (&iter, data->pointer_info); |
| 498 |
|
| 499 |
while (g_hash_table_iter_next (&iter, NULL, &value)) |
| 500 |
{ |
| 501 |
info = value; |
| 502 |
draw_device_info (widget, cr, NULL, &y, info); |
| 503 |
} |
| 504 |
|
| 505 |
g_hash_table_iter_init (&iter, data->touch_info); |
| 506 |
|
| 507 |
while (g_hash_table_iter_next (&iter, &key, &value)) |
| 508 |
{ |
| 509 |
info = value; |
| 510 |
draw_device_info (widget, cr, key, &y, info); |
| 511 |
} |
| 512 |
|
| 513 |
return FALSE; |
| 514 |
} |
| 515 |
|
| 516 |
static void |
| 517 |
update_label_text (GtkWidget *label, |
| 518 |
const gchar *text) |
| 519 |
{ |
| 520 |
gchar *markup = NULL; |
| 521 |
|
| 522 |
if (text) |
| 523 |
markup = g_strdup_printf ("<span font='48.0'>%s</span>", text); |
| 524 |
gtk_label_set_markup (GTK_LABEL (label), markup); |
| 525 |
g_free (markup); |
| 526 |
} |
| 527 |
|
| 528 |
static gboolean |
| 529 |
reset_label_text_timeout_cb (gpointer user_data) |
| 530 |
{ |
| 531 |
GtkWidget *label = user_data; |
| 532 |
|
| 533 |
update_label_text (label, NULL); |
| 534 |
pad_action_timeout_id = 0; |
| 535 |
return G_SOURCE_REMOVE; |
| 536 |
} |
| 537 |
|
| 538 |
static void |
| 539 |
update_label_and_timeout (GtkWidget *label, |
| 540 |
const gchar *text) |
| 541 |
{ |
| 542 |
if (pad_action_timeout_id) |
| 543 |
g_source_remove (pad_action_timeout_id); |
| 544 |
|
| 545 |
update_label_text (label, text); |
| 546 |
pad_action_timeout_id = g_timeout_add (200, reset_label_text_timeout_cb, label); |
| 547 |
} |
| 548 |
|
| 549 |
static void |
| 550 |
on_action_activate (GSimpleAction *action, |
| 551 |
GVariant *parameter, |
| 552 |
gpointer user_data) |
| 553 |
{ |
| 554 |
GtkWidget *label = user_data; |
| 555 |
const gchar *result; |
| 556 |
gchar *str; |
| 557 |
|
| 558 |
result = g_object_get_data (G_OBJECT (action), "action-result"); |
| 559 |
|
| 560 |
if (!parameter) |
| 561 |
update_label_and_timeout (label, result); |
| 562 |
else |
| 563 |
{ |
| 564 |
str = g_strdup_printf ("%s %.2f", result, g_variant_get_double (parameter)); |
| 565 |
update_label_and_timeout (label, str); |
| 566 |
g_free (str); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
static void |
| 571 |
init_pad_controller (GtkWidget *window, |
| 572 |
GtkWidget *label) |
| 573 |
{ |
| 574 |
GtkPadController *pad_controller; |
| 575 |
GSimpleActionGroup *action_group; |
| 576 |
GSimpleAction *action; |
| 577 |
gint i; |
| 578 |
|
| 579 |
action_group = g_simple_action_group_new (); |
| 580 |
pad_controller = gtk_pad_controller_new (GTK_WINDOW (window), |
| 581 |
G_ACTION_GROUP (action_group), |
| 582 |
NULL); |
| 583 |
|
| 584 |
for (i = 0; i < G_N_ELEMENTS (pad_actions); i++) |
| 585 |
{ |
| 586 |
if (pad_actions[i].type == GTK_PAD_ACTION_BUTTON) |
| 587 |
{ |
| 588 |
action = g_simple_action_new (pad_actions[i].action_name, NULL); |
| 589 |
} |
| 590 |
else |
| 591 |
{ |
| 592 |
action = g_simple_action_new_stateful (pad_actions[i].action_name, |
| 593 |
G_VARIANT_TYPE_DOUBLE, NULL); |
| 594 |
} |
| 595 |
|
| 596 |
g_signal_connect (action, "activate", |
| 597 |
G_CALLBACK (on_action_activate), label); |
| 598 |
g_object_set_data (G_OBJECT (action), "action-result", |
| 599 |
(gpointer) pad_action_results[i]); |
| 600 |
g_action_map_add_action (G_ACTION_MAP (action_group), G_ACTION (action)); |
| 601 |
g_object_unref (action); |
| 602 |
} |
| 603 |
|
| 604 |
gtk_pad_controller_set_action_entries (pad_controller, pad_actions, |
| 605 |
G_N_ELEMENTS (pad_actions)); |
| 606 |
g_object_set_data_full (G_OBJECT (window), "pad-controller", |
| 607 |
pad_controller, g_object_unref); |
| 608 |
|
| 609 |
g_object_unref (action_group); |
| 610 |
} |
| 611 |
|
| 612 |
GtkWidget * |
| 613 |
do_event_axes (GtkWidget *toplevel) |
| 614 |
{ |
| 615 |
static GtkWidget *window = NULL; |
| 616 |
EventData *event_data; |
| 617 |
GtkWidget *box, *label; |
| 618 |
|
| 619 |
if (!window) |
| 620 |
{ |
| 621 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 622 |
gtk_window_set_title (GTK_WINDOW (window), "Event Axes"); |
| 623 |
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400); |
| 624 |
|
| 625 |
g_signal_connect (window, "destroy", |
| 626 |
G_CALLBACK (gtk_widget_destroyed), &window); |
| 627 |
|
| 628 |
box = gtk_event_box_new (); |
| 629 |
gtk_container_add (GTK_CONTAINER (window), box); |
| 630 |
gtk_widget_set_support_multidevice (box, TRUE); |
| 631 |
gtk_widget_add_events (box, |
| 632 |
GDK_POINTER_MOTION_MASK | |
| 633 |
GDK_BUTTON_PRESS_MASK | |
| 634 |
GDK_BUTTON_RELEASE_MASK | |
| 635 |
GDK_SMOOTH_SCROLL_MASK | |
| 636 |
GDK_ENTER_NOTIFY_MASK | |
| 637 |
GDK_LEAVE_NOTIFY_MASK | |
| 638 |
GDK_TOUCH_MASK); |
| 639 |
|
| 640 |
event_data = event_data_new (); |
| 641 |
g_object_set_data_full (G_OBJECT (box), "gtk-demo-event-data", |
| 642 |
event_data, (GDestroyNotify) event_data_free); |
| 643 |
|
| 644 |
g_signal_connect (box, "event", |
| 645 |
G_CALLBACK (event_cb), event_data); |
| 646 |
g_signal_connect (box, "draw", |
| 647 |
G_CALLBACK (draw_cb), event_data); |
| 648 |
|
| 649 |
label = gtk_label_new (""); |
| 650 |
gtk_label_set_use_markup (GTK_LABEL (label), TRUE); |
| 651 |
gtk_container_add (GTK_CONTAINER (box), label); |
| 652 |
|
| 653 |
init_pad_controller (window, label); |
| 654 |
} |
| 655 |
|
| 656 |
if (!gtk_widget_get_visible (window)) |
| 657 |
gtk_widget_show_all (window); |
| 658 |
else |
| 659 |
gtk_widget_destroy (window); |
| 660 |
|
| 661 |
return window; |
| 662 |
} |