summaryrefslogtreecommitdiff
path: root/gui/input.c
blob: f75ea3de1d4e0f0c2ad02909b2932d99940bb3f1 (plain)
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
/*
 * gui/input.c - Input operations
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 */

#include <stddef.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>

#include <gtk/gtk.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "gui/input.h"


#define	DRAG_RADIUS	5


static struct input {
	const struct input_ops *ops;
	void *user;

	enum state {
		input_idle,
		input_clicking,
		input_ignoring,	/* click rejected by moving the cursor */
		input_hovering,
		input_dragging,
	} state;

	struct input *next;
} *sp = NULL;

static int curr_x, curr_y;		/* last mouse position */
static int clicked_x, clicked_y;	/* button down position */


/* ----- Debugging tools --------------------------------------------------- */


static const char *state(void)
{
	switch (sp->state) {
	case input_idle:
		return "IDLE";
	case input_clicking:
		return "CLICKING";
	case input_ignoring:
		return "IGNORING";
	case input_hovering:
		return "HOVERING";
	case input_dragging:
		return "DRAGGING";
	default:
		abort();
	}
}


/* ----- Mouse button ------------------------------------------------------ */


static gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event,
    gpointer data)
{
	const struct input *old_sp = sp;

	curr_x = event->x;
	curr_y = event->y;

	if (!sp)
		return TRUE;

	progress(3, "motion %s\n", state());

	switch (sp->state) {
	case input_idle:
		if (sp->ops->hover_begin &&
		    sp->ops->hover_begin(sp->user, event->x, event->y))
			sp->state = input_hovering;
		assert(sp == old_sp);
		break;
	case input_clicking:
		if (hypot(event->x - clicked_x, event->y - clicked_y) <
		    DRAG_RADIUS)
			break;
		if (sp->ops->drag_begin &&
		    sp->ops->drag_begin(sp->user, clicked_x, clicked_y))
			sp->state = input_dragging;
		else
			sp->state = input_ignoring;
		assert(sp == old_sp);
		break;
	case input_ignoring:
		break;
	case input_hovering:
		if (!sp->ops->hover_update)
			break;

		/* Caution: hover_update may switch input layers */
		if (!sp->ops->hover_update(sp->user, event->x, event->y) &&
		    sp == old_sp) {
			sp->state = input_idle;
			if (sp->ops->hover_end)
				sp->ops->hover_end(sp->user);
		}
		break;
	case input_dragging:
		if (sp->ops->drag_move)
		    sp->ops->drag_move(sp->user,
		        event->x - clicked_x, event->y - clicked_y);
		clicked_x = event->x;
		clicked_y = event->y;
		break;
	default:
		abort();
	}
	return TRUE;
}


static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
    gpointer data)
{
	const struct input *old_sp = sp;

	if (event->button != 1)
		return TRUE;

	progress(3, "press %s\n", state());

	switch (sp->state) {
	case input_idle:
		sp->state = input_clicking;
		clicked_x = event->x;
		clicked_y = event->y;
		break;
	case input_clicking:
	case input_ignoring:
	case input_dragging:
		/* ignore double-click */
		break;
	case input_hovering:
		if (sp->ops->hover_click &&
		    sp->ops->hover_click(sp->user, event->x, event->y) &&
		    sp == old_sp) {
			sp->state = input_ignoring;
			if (sp->ops->hover_end)
				sp->ops->hover_end(sp->user);
		}
		break;
	default:
		abort();
	}

	return TRUE;
}


static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
    gpointer data)
{
	if (event->button != 1)
		return TRUE;

	progress(3, "release %s\n", state());

	switch (sp->state) {
	case input_idle:
		/* hover_click changed the input configuration */
		break;
	case input_clicking:
		sp->state = input_idle;
		if (sp->ops->click)
			sp->ops->click(sp->user, clicked_x, clicked_y);
		break;
	case input_ignoring:
		sp->state = input_idle;
		break;
	case input_dragging:
		sp->state = input_idle;
		if (sp->ops->drag_end)
			sp->ops->drag_end(sp->user);
		break;
	case input_hovering:
		break;
	default:
		abort();
	}

	return TRUE;
}


/* ----- Scroll wheel ------------------------------------------------------ */


static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event,
    gpointer data)
{
	if (!sp || !sp->ops->scroll)
		return TRUE;
	switch (event->direction) {
	case GDK_SCROLL_UP:
		sp->ops->scroll(sp->user, event->x, event->y, -1);
		break;
	case GDK_SCROLL_DOWN:
		sp->ops->scroll(sp->user, event->x, event->y, 1);
		break;
	default:
		/* ignore */;
	}
	return TRUE;
}


/* ----- Keys -------------------------------------------------------------- */


static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
    gpointer data)
{
	if (sp && sp->ops->key)
		sp->ops->key(sp->user, curr_x, curr_y, event->keyval);
	return TRUE;
}


/* ----- Covenience function for hover_begin and drag_begin ---------------- */


bool input_accept(void *user, int x, int y)
{
	return 1;
}


/* ----- Adding/removing interaction layers -------------------------------- */


static void cleanup(void)
{
	if (!sp)
		return;

	switch (sp->state) {
	case input_hovering:
		if (sp->ops->hover_end)
			sp->ops->hover_end(sp->user);
		break;
	case input_dragging:
		if (sp->ops->drag_end)
			sp->ops->drag_end(sp->user);
		break;
	default:
		;
	}
}


void input_push(const struct input_ops *ops, void *user)
{
	struct input *new;

	cleanup();

	new = alloc_type(struct input);
	new->ops = ops;
	new->user = user;
	new->state = input_idle;
	new->next = sp;
	sp = new;
}


void input_pop(void)
{
	struct input *next = sp->next;

	cleanup();
	free(sp);
	sp = next;
}


/* ----- Initialization ---------------------------------------------------- */


void input_setup(GtkWidget *da)
{
	gtk_widget_set_can_focus(da, TRUE);

	gtk_widget_add_events(da,
	    GDK_KEY_PRESS_MASK |
	    GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
	    GDK_SCROLL_MASK |
	    GDK_POINTER_MOTION_MASK);

	g_signal_connect(G_OBJECT(da), "key_press_event",
	    G_CALLBACK(key_press_event), NULL);
	g_signal_connect(G_OBJECT(da), "motion_notify_event",
	    G_CALLBACK(motion_notify_event), NULL);
	g_signal_connect(G_OBJECT(da), "button_press_event",
	    G_CALLBACK(button_press_event), NULL);
	g_signal_connect(G_OBJECT(da), "button_release_event",
	    G_CALLBACK(button_release_event), NULL);
	g_signal_connect(G_OBJECT(da), "scroll_event",
	    G_CALLBACK(scroll_event), NULL);
}