1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
| | /* Event handling for GTK 3 and GTK 4 -*- coding: utf-8 -*-
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include "config.h"
#include "frame.h"
#include "pgtkterm.h"
#ifdef HAVE_GTK_EVENT_CONTROLLER
#include "pgtkevent.h"
#include "blockinput.h"
#include "dynlib.h"
#include <math.h>
typedef struct _EmacsGtkEventHandlerPrivate
{
struct frame *f;
} EmacsGtkEventHandlerPrivate;
#if !defined (HAVE_GTK4) || defined (FIX_GTK_LEGACY_HANDLER_BUG)
G_DEFINE_TYPE_WITH_PRIVATE (EmacsGtkEventHandler, emacs_gtk_event_handler,
GTK_TYPE_EVENT_CONTROLLER);
#else
G_DEFINE_TYPE_WITH_PRIVATE (EmacsGtkEventHandler, emacs_gtk_event_handler,
G_TYPE_OBJECT);
#endif
#if defined (HAVE_GTK4) && !defined (FIX_GTK_LEGACY_HANDLER_BUG)
static gboolean
emacs_gtk_event_handler_handle_event (EmacsGtkEventHandler *handler,
GdkEvent *_ev, gdouble x, gdouble y);
static gboolean
emacs_gtk_event_handler_on_event (GtkEventControllerLegacy *controller,
GdkEvent *event,
gpointer user_data)
{
return emacs_gtk_event_handler_handle_event (user_data, event, 0, 0);
}
#endif
#ifdef HAVE_GTK4
static gboolean
scroll_cb (GtkEventControllerScroll *controller,
gdouble dx,
gdouble dy,
gpointer user_data)
{
EmacsGtkEventHandlerPrivate *priv
= emacs_gtk_event_handler_get_instance_private (((EmacsGtkEventHandler *) user_data));
if (((EmacsGtkEventHandler *) user_data)->scroll_event)
return ((EmacsGtkEventHandler *) user_data)
->scroll_event (controller, dx, dy, priv->f);
return false;
}
static void
drag_end_cb (EmacsGtkEventHandler *h,
GdkEventSequence *sequence,
GtkGesture *gesture)
{
Vpgtk_last_event_from_touchscreen = Qt;
h->s_flag = false;
h->hq_flag = false;
}
static void
drag_update_cb (EmacsGtkEventHandler *h,
gdouble xo, gdouble yo,
GtkGesture *gesture)
{
Vpgtk_last_event_from_touchscreen = Qt;
gdouble dt = fabs (xo) - fabs (yo);
if (dt < 1.0 && !h->se_blocked)
{
scroll_cb (NULL, 0, -yo * 0.5, h);
scroll_cb (NULL, -xo * 0.5, 0, h);
}
else if (!h->se_blocked)
scroll_cb (NULL, -xo * 0.5, -yo * 0.5, h);
}
static void
drag_begin_cb (EmacsGtkEventHandler *h,
gdouble start_x, gdouble start_y,
GtkGesture *gesture)
{
Vpgtk_last_event_from_touchscreen = Qt;
EmacsGtkEventHandlerPrivate *priv =
emacs_gtk_event_handler_get_instance_private (h);
enum window_part w = 0;
window_from_coordinates (priv->f, (int) round (start_x),
(int) round (start_y), &w, 0, 0);
if (w != ON_TEXT)
{
h->se_blocked = true;
emacs_gtk_event_handler_toggle_scroll_evs (h, 0);
}
if (h->se_blocked)
return;
h->dsy = start_y;
h->dsx = start_x;
h->hq_flag = true;
}
static void
long_press_cb (GtkGestureLongPress *p, gdouble x, gdouble y,
EmacsGtkEventHandler *h)
{
Vpgtk_last_event_from_touchscreen = Qt;
if (h->long_press_event)
h->long_press_event (((EmacsGtkEventHandlerPrivate *)
emacs_gtk_event_handler_get_instance_private (h))->f, x, y);
}
#endif
#ifdef HAVE_GTK4
static void
motion_enter (GtkEventControllerMotion *controller,
gdouble x,
gdouble y,
GdkCrossingMode mode,
gpointer user_data)
{
EmacsGtkEventHandler *h = user_data;
EmacsGtkEventHandlerPrivate *priv
= emacs_gtk_event_handler_get_instance_private (h);
h->enter (h->widget, mode, priv->f);
}
static void
motion_leave (GtkEventControllerMotion *controller,
GdkCrossingMode mode,
gpointer user_data)
{
EmacsGtkEventHandler *h = user_data;
EmacsGtkEventHandlerPrivate *priv
= emacs_gtk_event_handler_get_instance_private (h);
#ifdef HAVE_GTK4
if (FRAME_X_OUTPUT (priv->f)->ttip_popover)
return;
#endif
h->leave (h->widget, mode, priv->f);
}
#endif
static void
emacs_gtk_event_handler_init (EmacsGtkEventHandler *handler)
{
#if defined (HAVE_GTK4) && !defined (FIX_GTK_LEGACY_HANDLER_BUG)
handler->legacy = GTK_EVENT_CONTROLLER_LEGACY (gtk_event_controller_legacy_new ());
g_signal_connect_after (G_OBJECT (handler->legacy),
"event",
G_CALLBACK (emacs_gtk_event_handler_on_event),
handler);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->legacy),
GTK_PHASE_BUBBLE);
#endif
#ifdef HAVE_GTK4
handler->s_flag = false;
handler->hq_flag = false;
handler->se_blocked = false;
handler->scroll = GTK_EVENT_CONTROLLER_SCROLL
(gtk_event_controller_scroll_new (GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES
| GTK_EVENT_CONTROLLER_SCROLL_KINETIC));
handler->drag = gtk_gesture_drag_new ();
handler->lpress = gtk_gesture_long_press_new ();
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (handler->drag), true);
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (handler->lpress), true);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->lpress),
GTK_PHASE_CAPTURE);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->scroll),
GTK_PHASE_CAPTURE);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->drag),
GTK_PHASE_CAPTURE);
g_signal_connect (G_OBJECT (handler->lpress),
"pressed", G_CALLBACK (long_press_cb),
handler);
g_signal_connect (G_OBJECT (handler->scroll),
"scroll", G_CALLBACK (scroll_cb), handler);
g_signal_connect_swapped (handler->drag, "drag-begin",
G_CALLBACK (drag_begin_cb),
handler);
g_signal_connect_swapped (handler->drag, "drag-update",
G_CALLBACK (drag_update_cb),
handler);
g_signal_connect_swapped (handler->drag, "end",
G_CALLBACK (drag_end_cb),
handler);
handler->focus = (GtkEventControllerMotion *) gtk_event_controller_motion_new ();
g_signal_connect (G_OBJECT (handler->focus), "enter",
G_CALLBACK (motion_enter), handler);
g_signal_connect (G_OBJECT (handler->focus), "leave",
G_CALLBACK (motion_leave), handler);
g_object_ref (handler->focus);
g_object_ref (handler->drag);
g_object_ref (handler->lpress);
g_object_ref (handler->scroll);
#endif
}
#ifdef HAVE_GTK4
static gboolean
gtk_simulate_touchscreen (void)
{
static dynlib_handle_ptr dyn = NULL;
if (!dyn)
if (!(dyn = dynlib_open ("libgtk-4")))
return false;
gboolean (* fn) (void) = dynlib_sym (dyn, "gtk_simulate_touchscreen");
if (!fn)
return gtk_get_debug_flags () & GTK_DEBUG_TOUCHSCREEN;
return fn ();
}
static void
emacs_gtk_event_handler_dispose (GObject *o)
{
EmacsGtkEventHandler *h = (gpointer) o;
gtk_widget_remove_controller (h->widget, (gpointer) h->focus);
gtk_widget_remove_controller (h->widget, (gpointer) h->scroll);
gtk_widget_remove_controller (h->widget, (gpointer) h->drag);
gtk_widget_remove_controller (h->widget, (gpointer) h->lpress);
g_clear_pointer (&h->focus, g_object_unref);
g_clear_pointer (&h->scroll, g_object_unref);
g_clear_pointer (&h->drag, g_object_unref);
g_clear_pointer (&h->lpress, g_object_unref);
G_OBJECT_CLASS (emacs_gtk_event_handler_parent_class)->dispose (o);
}
#endif
void
emacs_gtk_event_handler_set_widget (EmacsGtkEventHandler *handler, GtkWidget *w)
{
#ifdef HAVE_GTK4
if (handler ==
FRAME_GTK_EV_HANDLER
(((EmacsGtkEventHandlerPrivate *) emacs_gtk_event_handler_get_instance_private (handler))->f))
{
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler->scroll));
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler->drag));
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler->lpress));
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler->focus));
}
#endif
#if !defined (HAVE_GTK4)
GTK_EVENT_CONTROLLER_CLASS (G_OBJECT_GET_CLASS (G_OBJECT (handler)))
->set_widget (GTK_EVENT_CONTROLLER (handler), w);
#elif !defined (FIX_GTK_LEGACY_HANDLER_BUG)
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler->legacy));
#else
gtk_widget_add_controller (w, GTK_EVENT_CONTROLLER (handler));
#endif
handler->widget = w;
}
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#ifndef HAVE_GTK4
static gboolean
emacs_gtk_event_handler_handle_event (GtkEventController *_handler, GdkEvent *ev)
#else
static gboolean
emacs_gtk_event_handler_handle_event (
#ifdef FIX_GTK_LEGACY_HANDLER_BUG
GtkEventController
#else
EmacsGtkEventHandler
#endif
*_handler, GdkEvent *ev,
gdouble x, gdouble y)
#endif
{
block_input ();
EmacsGtkEventHandler *handler = (gpointer) _handler;
EmacsGtkEventHandlerPrivate *priv
= emacs_gtk_event_handler_get_instance_private (handler);
#ifdef HAVE_GTK4
if (handler->s_flag)
return false;
if (handler->se_blocked || gdk_device_get_source (gdk_event_get_device (ev)) ==
GDK_SOURCE_TOUCHSCREEN || gtk_simulate_touchscreen ())
Vpgtk_last_event_from_touchscreen = Qt;
else
Vpgtk_last_event_from_touchscreen = Qnil;
if (!GTK_IS_ROOT (handler->widget))
gtk_window_set_focus (GTK_WINDOW (gtk_widget_get_root (handler->widget)),
handler->widget);
#endif
switch (gdk_event_get_event_type (ev))
{
case GDK_BUTTON_PRESS:
if (handler->button_down
&& handler->button_down (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
case GDK_BUTTON_RELEASE:
if (handler->button_up
&& handler->button_up (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
case GDK_KEY_PRESS:
if (handler->key_down && handler->key_down (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
case GDK_KEY_RELEASE:
if (handler->key_up && handler->key_up (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#ifndef HAVE_GTK4
case GDK_MAP:
if (handler->map_event
&& handler->map_event (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
case GDK_WINDOW_STATE:
if (handler->window_state_event
&& handler->window_state_event (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
#endif
case GDK_DELETE:
if (handler->delete_event
&& handler->delete_event (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#ifndef HAVE_GTK4
case GDK_SCROLL:
if (handler->scroll_event
&& handler->scroll_event (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#endif
#ifndef HAVE_GTK4
case GDK_SELECTION_CLEAR:
if (handler->selection_lost
&& handler->selection_lost (handler->widget, (GdkEventSelection *) ev,
priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#endif
case GDK_CONFIGURE:
if (handler->configure
&& handler->configure (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#ifndef HAVE_GTK4
case GDK_EXPOSE:
case GDK_DAMAGE:
if (handler->expose && handler->expose (handler->widget, (GdkEventExpose *) ev, 0))
{
unblock_input ();
return true;
}
#endif
case GDK_FOCUS_CHANGE:
#ifdef HAVE_GTK4
{
if (gdk_focus_event_get_in (ev))
#else
if (ev->focus_change.in)
#endif
if (handler->focus_in && handler->focus_in (handler->widget, ev, priv->f))
{
unblock_input ();
return false;
}
else
goto def;
else
#ifdef HAVE_GTK4
{
if (handler->focus_out
&& handler->focus_out (handler->widget, ev, priv->f))
{
unblock_input ();
return false;
}
else
goto def;
}
#endif
#ifdef HAVE_GTK4
}
case GDK_ENTER_NOTIFY:
if (handler->enter_notify &&
handler->enter_notify (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
case GDK_LEAVE_NOTIFY:
if (handler->leave_notify &&
handler->leave_notify (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
case GDK_MOTION_NOTIFY:
if (handler->motion_notify &&
handler->motion_notify (handler->widget, ev, priv->f))
{
unblock_input ();
return true;
}
else
goto def;
#endif
default:
def:
unblock_input ();
return false;
}
}
#pragma GCC diagnostic push
static void
emacs_gtk_event_handler_class_init (EmacsGtkEventHandlerClass *clazz)
{
#if !defined (HAVE_GTK4) || defined (FIX_GTK_LEGACY_HANDLER_BUG)
GtkEventControllerClass *cc = GTK_EVENT_CONTROLLER_CLASS (clazz);
cc->handle_event = emacs_gtk_event_handler_handle_event;
#endif
#ifdef HAVE_GTK4
G_OBJECT_CLASS (clazz)->dispose = emacs_gtk_event_handler_dispose;
#endif
}
EmacsGtkEventHandler *
emacs_gtk_event_handler_new (struct frame *f)
{
EmacsGtkEventHandler *handler
= (EmacsGtkEventHandler *) g_type_create_instance (EMACS_TYPE_GTK_EVENT_HANDLER);
EmacsGtkEventHandlerPrivate *priv
= emacs_gtk_event_handler_get_instance_private (handler);
priv->f = f;
return handler;
}
#ifdef HAVE_GTK4
void
emacs_gtk_event_handler_toggle_scroll_evs (EmacsGtkEventHandler *handler,
bool enable)
{
handler->se_blocked = !enable;
if (enable)
{
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->scroll),
GTK_PHASE_CAPTURE);
}
else
{
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (handler->scroll),
GTK_PHASE_NONE);
}
}
#endif
G_END_DECLS
#endif /* HAVE_GTK_EVENT_CONTROLLER */
|