aboutsummaryrefslogtreecommitdiff
path: root/shared/hammer/gui.c
blob: 388b14b34465d8bf6626855be852b11df9c910de (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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* Copyright (c) 2003-2004 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* FIXME use guiconnect */

#include <stdio.h>
#include <string.h>
#include "m_pd.h"
#include "g_canvas.h"
#include "hammer/gui.h"

#ifdef KRZYSZCZ
//#define HAMMERGUI_DEBUG
#endif

static t_class *hammergui_class = 0;
static t_hammergui *hammergui_sink = 0;
static t_symbol *ps_hashhammergui;
static t_symbol *ps__hammergui;
static t_symbol *ps__up;
static t_symbol *ps__focus;
static t_symbol *ps__vised;

static void hammergui_anything(t_hammergui *snk,
			       t_symbol *s, int ac, t_atom *av)
{
    /* Dummy method, filtering out messages from gui to the masters.  This is
       needed in order to keep Pd's message system happy in a ``gray period''
       -- after last master is unbound, and before gui bindings are cleared. */
#ifdef HAMMERGUI_DEBUG
    /* FIXME */
    startpost("%s", s->s_name);
    postatom(ac, av);
    post(" (sink %x)", (int)snk);
#endif
}

/* filtering out redundant "_up" messages */
static void hammergui__up(t_hammergui *snk, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "_up %g (sink %x)\n", f, (int)snk);
#endif
    if (!snk->g_psmouse)
    {
	bug("hammergui__up");
	return;
    }
    if ((int)f)
    {
	if (!snk->g_isup)
	{
	    snk->g_isup = 1;
	    if (snk->g_psmouse->s_thing)
	    {
		t_atom at;
		SETFLOAT(&at, 1);
		pd_typedmess(snk->g_psmouse->s_thing, ps__up, 1, &at);
	    }
	}
    }
    else
    {
	if (snk->g_isup)
	{
	    snk->g_isup = 0;
	    if (snk->g_psmouse->s_thing)
	    {
		t_atom at;
		SETFLOAT(&at, 0);
		pd_typedmess(snk->g_psmouse->s_thing, ps__up, 1, &at);
	    }
	}
    }
}

static void hammergui__focus(t_hammergui *snk, t_symbol *s, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "_focus %s %g (sink %x)\n",
	    (s ? s->s_name : "???"), f, (int)snk);
#endif
    if (!snk->g_psfocus)
    {
	bug("hammergui__focus");
	return;
    }
    if (snk->g_psfocus->s_thing)
    {
	t_atom at[2];
	SETSYMBOL(&at[0], s);
	SETFLOAT(&at[1], f);
	pd_typedmess(snk->g_psfocus->s_thing, ps__focus, 2, at);
    }
}

static void hammergui__vised(t_hammergui *snk, t_symbol *s, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "_vised %s %g (sink %x)\n",
	    (s ? s->s_name : "???"), f, (int)snk);
#endif
    if (!snk->g_psvised)
    {
	bug("hammergui__vised");
	return;
    }
    if (snk->g_psvised->s_thing)
    {
	t_atom at[2];
	SETSYMBOL(&at[0], s);
	SETFLOAT(&at[1], f);
	pd_typedmess(snk->g_psvised->s_thing, ps__vised, 2, at);
    }
#if 0
    /* How to be notified about changes of button state, prior to gui objects
       in a canvas?  LATER find a reliable way -- delete if failed */
    sys_vgui("bindtags %s {hammertag %s Canvas . all}\n",
	     s->s_name, s->s_name);
#endif
}


static void hammergui_dobindmouse(t_hammergui *snk)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "dobindmouse (sink %x)\n", (int)snk);
#endif
#if 0
    /* How to be notified about changes of button state, prior to gui objects
       in a canvas?  LATER find a reliable way -- delete if failed */
    sys_vgui("bind hammertag <<hammerdown>> {pd [concat %s _up 0 \\;]}\n",
	     snk->g_psgui->s_name);
    sys_vgui("bind hammertag <<hammerup>> {pd [concat %s _up 1 \\;]}\n",
	     snk->g_psgui->s_name);
#endif
    sys_vgui("bind all <<hammerdown>> {pd [concat %s _up 0 \\;]}\n",
	     snk->g_psgui->s_name);
    sys_vgui("bind all <<hammerup>> {pd [concat %s _up 1 \\;]}\n",
	     snk->g_psgui->s_name);
}

static void hammergui__remouse(t_hammergui *snk)
{
    if (!snk->g_psmouse)
    {
	bug("hammergui__remouse");
	return;
    }
    if (snk->g_psmouse->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding mouse...");
#endif
	hammergui_dobindmouse(snk);
    }
}

static void hammergui_dobindfocus(t_hammergui *snk)
{
    sys_vgui("bind Canvas <<hammerfocusin>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _focus %%W 1 \\;]}}\n", snk->g_psgui->s_name);
    sys_vgui("bind Canvas <<hammerfocusout>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _focus %%W 0 \\;]}}\n", snk->g_psgui->s_name);
}

static void hammergui__refocus(t_hammergui *snk)
{
    if (!snk->g_psfocus)
    {
	bug("hammergui__refocus");
	return;
    }
    if (snk->g_psfocus->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding focus...");
#endif
	hammergui_dobindfocus(snk);
    }
}

static void hammergui_dobindvised(t_hammergui *snk)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "dobindvised (sink %x)\n", (int)snk);
#endif
    sys_vgui("bind Canvas <<hammervised>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _vised %%W 1 \\;]}}\n", snk->g_psgui->s_name);
    sys_vgui("bind Canvas <<hammerunvised>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _vised %%W 0 \\;]}}\n", snk->g_psgui->s_name);
}

static void hammergui__revised(t_hammergui *snk)
{
    if (!snk->g_psvised)
    {
	bug("hammergui__revised");
	return;
    }
    if (snk->g_psvised->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding vised events...");
#endif
	hammergui_dobindvised(snk);
    }
}

static int hammergui_setup(void)
{
    ps_hashhammergui = gensym("#hammergui");
    ps__hammergui = gensym("_hammergui");
    ps__up = gensym("_up");
    ps__focus = gensym("_focus");
    ps__vised = gensym("_vised");
    if (ps_hashhammergui->s_thing)
    {
	char *cname = class_getname(*ps_hashhammergui->s_thing);
#ifdef HAMMERGUI_DEBUG
	fprintf(stderr,
		"'%s' already registered as the global hammergui sink \n",
		(cname ? cname : "???"));
#endif
	if (strcmp(cname, ps__hammergui->s_name))
	{
	    /* FIXME protect against the danger of someone else
	       (e.g. receive) binding to #hammergui */
	    bug("hammergui_setup");
	    return (0);
	}
	else
	{
	    /* FIXME compatibility test */
	    hammergui_class = *ps_hashhammergui->s_thing;
	    return (1);
	}
    }
    hammergui_class = class_new(ps__hammergui, 0, 0,
				sizeof(t_hammergui),
				CLASS_PD | CLASS_NOINLET, 0);
    class_addanything(hammergui_class, hammergui_anything);
    class_addmethod(hammergui_class, (t_method)hammergui__remouse,
		    gensym("_remouse"), 0);
    class_addmethod(hammergui_class, (t_method)hammergui__refocus,
		    gensym("_refocus"), 0);
    class_addmethod(hammergui_class, (t_method)hammergui__revised,
		    gensym("_revised"), 0);
    class_addmethod(hammergui_class, (t_method)hammergui__up,
		    ps__up, A_FLOAT, 0);
    class_addmethod(hammergui_class, (t_method)hammergui__focus,
		    ps__focus, A_SYMBOL, A_FLOAT, 0);
    class_addmethod(hammergui_class, (t_method)hammergui__vised,
		    ps__vised, A_SYMBOL, A_FLOAT, 0);

    /* Protect against pdCmd being called (via "Canvas <Destroy>" binding)
       during Tcl_Finalize().  FIXME this should be a standard exit handler. */
    sys_gui("proc hammergui_exithook {cmd op} {proc pd {} {}}\n");
    sys_gui("if {[info tclversion] >= 8.4} {\n\
 trace add execution exit enter hammergui_exithook}\n");

    sys_gui("proc hammergui_ispatcher {cv} {\n");
    sys_gui(" if {[string range $cv 0 1] == \".x\"");
    sys_gui("  && [string range $cv end-1 end] == \".c\"} {\n");
    sys_gui("  return 1} else {return 0}\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_remouse {} {\n");
    sys_gui(" bind all <<hammerdown>> {}\n");
    sys_gui(" bind all <<hammerup>> {}\n");
    sys_gui(" pd [concat #hammergui _remouse \\;]\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_mousexy {target} {\n");
    sys_gui(" set x [winfo pointerx .]\n");
    sys_gui(" set y [winfo pointery .]\n");
    sys_gui(" pd [concat #hammermouse $target $x $y \\;]\n");
    sys_gui("}\n");

    /* visibility hack for msw, LATER rethink */
    sys_gui("global hammergui_ispolling\n");
    sys_gui("global hammergui_x\n");
    sys_gui("global hammergui_y\n");
    sys_gui("set hammergui_ispolling 0\n");
    sys_gui("set hammergui_x 0\n");
    sys_gui("set hammergui_y 0\n");

    sys_gui("proc hammergui_poll {} {\n");
    sys_gui(" global hammergui_ispolling\n");
    sys_gui(" global hammergui_x\n");
    sys_gui(" global hammergui_y\n");
    sys_gui(" if {$hammergui_ispolling == 1} {\n");
    sys_gui("  set x [winfo pointerx .]\n");
    sys_gui("  set y [winfo pointery .]\n");
    sys_gui("  if {$hammergui_x != $x || $hammergui_y != $y} {\n");
    sys_gui("   pd [concat #hammermouse _poll $x $y \\;]\n");
    sys_gui("   set hammergui_x $x\n");
    sys_gui("   set hammergui_y $y\n");
    sys_gui("  }\n");
    sys_gui("  after 50 hammergui_poll\n");
    sys_gui(" }\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_refocus {} {\n");
    sys_gui(" bind Canvas <<hammerfocusin>> {}\n");
    sys_gui(" bind Canvas <<hammerfocusout>> {}\n");
    sys_gui(" pd [concat #hammergui _refocus \\;]\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_revised {} {\n");
    sys_gui(" bind Canvas <<hammervised>> {}\n");
    sys_gui(" bind Canvas <<hammerunvised>> {}\n");
    sys_gui(" pd [concat #hammergui _revised \\;]\n");
    sys_gui("}\n");
    return (1);
}

static int hammergui_validate(int dosetup)
{
    if (dosetup && !hammergui_sink
	&& (hammergui_class || hammergui_setup()))
    {
	if (ps_hashhammergui->s_thing)
	    hammergui_sink = (t_hammergui *)ps_hashhammergui->s_thing;
	else
	{
	    hammergui_sink = (t_hammergui *)pd_new(hammergui_class);
	    hammergui_sink->g_psgui = ps_hashhammergui;
	    pd_bind((t_pd *)hammergui_sink,
		    ps_hashhammergui);  /* never unbound */
	}
    }
    if (hammergui_class && hammergui_sink)
	return (1);
    else
    {
	bug("hammergui_validate");
	return (0);
    }
}

static int hammergui_mousevalidate(int dosetup)
{
    if (dosetup && !hammergui_sink->g_psmouse)
    {
	hammergui_sink->g_psmouse = gensym("#hammermouse");
	sys_gui("event add <<hammerdown>> <ButtonPress>\n");
	sys_gui("event add <<hammerup>> <ButtonRelease>\n");
    }
    if (hammergui_sink->g_psmouse)
	return (1);
    else
    {
	bug("hammergui_mousevalidate");
	return (0);
    }
}

static int hammergui_pollvalidate(int dosetup)
{
    if (dosetup && !hammergui_sink->g_pspoll)
    {
	hammergui_sink->g_pspoll = gensym("#hammerpoll");
	pd_bind((t_pd *)hammergui_sink,
		hammergui_sink->g_pspoll);  /* never unbound */
    }
    if (hammergui_sink->g_pspoll)
	return (1);
    else
    {
	bug("hammergui_pollvalidate");
	return (0);
    }
}

static int hammergui_focusvalidate(int dosetup)
{
    if (dosetup && !hammergui_sink->g_psfocus)
    {
	hammergui_sink->g_psfocus = gensym("#hammerfocus");
	sys_gui("event add <<hammerfocusin>> <FocusIn>\n");
	sys_gui("event add <<hammerfocusout>> <FocusOut>\n");
    }
    if (hammergui_sink->g_psfocus)
	return (1);
    else
    {
	bug("hammergui_focusvalidate");
	return (0);
    }
}

static int hammergui_visedvalidate(int dosetup)
{
    if (dosetup && !hammergui_sink->g_psvised)
    {
	hammergui_sink->g_psvised = gensym("#hammervised");
	/* subsequent map events have to be filtered out at the caller's side,
	   LATER investigate */
	sys_gui("event add <<hammervised>> <Map>\n");
	sys_gui("event add <<hammerunvised>> <Destroy>\n");
    }
    if (hammergui_sink->g_psvised)
	return (1);
    else
    {
	bug("hammergui_visedvalidate");
	return (0);
    }
}

void hammergui_bindmouse(t_pd *master)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "bindmouse, master %x\n", (int)master);
#endif
    hammergui_validate(1);
    hammergui_mousevalidate(1);
    if (!hammergui_sink->g_psmouse->s_thing)
	hammergui_dobindmouse(hammergui_sink);
    pd_bind(master, hammergui_sink->g_psmouse);
}

void hammergui_unbindmouse(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_mousevalidate(0)
	&& hammergui_sink->g_psmouse->s_thing)
    {
	pd_unbind(master, hammergui_sink->g_psmouse);
	if (!hammergui_sink->g_psmouse->s_thing)
	    sys_gui("hammergui_remouse\n");
    }
    else bug("hammergui_unbindmouse");
}

void hammergui_mousexy(t_symbol *s)
{
    if (hammergui_validate(0))
	sys_vgui("hammergui_mousexy %s\n", s->s_name);
}

void hammergui_willpoll(void)
{
    hammergui_validate(1);
    hammergui_pollvalidate(1);
}

void hammergui_startpolling(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_pollvalidate(0))
    {
	int doinit =
	    (hammergui_sink->g_pspoll->s_thing == (t_pd *)hammergui_sink);
	pd_bind(master, hammergui_sink->g_pspoll);
	if (doinit)
	{
	    /* visibility hack for msw, LATER rethink */
	    sys_gui("global hammergui_ispolling\n");
	    sys_gui("set hammergui_ispolling 1\n");
	    sys_gui("hammergui_poll\n");
	}
    }
}

void hammergui_stoppolling(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_pollvalidate(0))
    {
	pd_unbind(master, hammergui_sink->g_pspoll);
	if (hammergui_sink->g_pspoll->s_thing == (t_pd *)hammergui_sink)
	{
	    sys_gui("after cancel hammergui_poll\n");
	    /* visibility hack for msw, LATER rethink */
	    sys_gui("global hammergui_ispolling\n");
	    sys_gui("set hammergui_ispolling 0\n");
	}
    }
}

void hammergui_bindfocus(t_pd *master)
{
    hammergui_validate(1);
    hammergui_focusvalidate(1);
    if (!hammergui_sink->g_psfocus->s_thing)
	hammergui_dobindfocus(hammergui_sink);
    pd_bind(master, hammergui_sink->g_psfocus);
}

void hammergui_unbindfocus(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_focusvalidate(0)
	&& hammergui_sink->g_psfocus->s_thing)
    {
	pd_unbind(master, hammergui_sink->g_psfocus);
	if (!hammergui_sink->g_psfocus->s_thing)
	    sys_gui("hammergui_refocus\n");
    }
    else bug("hammergui_unbindfocus");
}

void hammergui_bindvised(t_pd *master)
{
#ifdef HAMMERGUI_DEBUG
    fprintf(stderr, "bindvised, master %x\n", (int)master);
#endif
    hammergui_validate(1);
    hammergui_visedvalidate(1);
    if (!hammergui_sink->g_psvised->s_thing)
	hammergui_dobindvised(hammergui_sink);
    pd_bind(master, hammergui_sink->g_psvised);
}

void hammergui_unbindvised(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_visedvalidate(0)
	&& hammergui_sink->g_psvised->s_thing)
    {
	pd_unbind(master, hammergui_sink->g_psvised);
	if (!hammergui_sink->g_psvised->s_thing)
	    sys_gui("hammergui_revised\n");
    }
    else bug("hammergui_unbindvised");
}