summaryrefslogtreecommitdiff
path: root/apps/plugins/cube.c
blob: 5c6f9934eba3862f81a079919aaf929058b9f14c (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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Copyright (C) 2002 Damien Teney
* modified to use int instead of float math by Andreas Zwirtes
* heavily extended by Jens Arnold
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "plugin.h"
#include "lib/grey.h"
#include "lib/playergfx.h"
#include "lib/xlcd.h"
#include "lib/fixedpoint.h"

PLUGIN_HEADER

/* Loops that the values are displayed */
#define DISP_TIME 30

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define CUBE_QUIT          BUTTON_OFF
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_F1
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_ON

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define CUBE_QUIT          BUTTON_OFF
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_F1
#define CUBE_PAUSE         BUTTON_SELECT
#define CUBE_HIGHSPEED     BUTTON_ON

#elif CONFIG_KEYPAD == PLAYER_PAD
#define CUBE_QUIT          BUTTON_STOP
#define CUBE_INC           BUTTON_RIGHT
#define CUBE_DEC           BUTTON_LEFT
#define CUBE_NEXT          (BUTTON_ON | BUTTON_RIGHT)
#define CUBE_PREV          (BUTTON_ON | BUTTON_LEFT)
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED_PRE BUTTON_ON
#define CUBE_HIGHSPEED     (BUTTON_ON | BUTTON_REL)

#elif CONFIG_KEYPAD == ONDIO_PAD
#define CUBE_QUIT          BUTTON_OFF
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE_PRE      BUTTON_MENU
#define CUBE_MODE          (BUTTON_MENU | BUTTON_REL)
#define CUBE_PAUSE         (BUTTON_MENU | BUTTON_LEFT)
#define CUBE_HIGHSPEED     (BUTTON_MENU | BUTTON_RIGHT)

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define CUBE_QUIT          BUTTON_OFF
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MODE
#define CUBE_PAUSE         BUTTON_ON
#define CUBE_HIGHSPEED     BUTTON_SELECT

#define CUBE_RC_QUIT       BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define CUBE_QUIT          (BUTTON_SELECT | BUTTON_MENU)
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_SCROLL_FWD
#define CUBE_DEC           BUTTON_SCROLL_BACK
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED_PRE BUTTON_SELECT
#define CUBE_HIGHSPEED     (BUTTON_SELECT | BUTTON_REL)

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
#define CUBE_QUIT          BUTTON_PLAY
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MODE
#define CUBE_PAUSE         BUTTON_SELECT
#define CUBE_HIGHSPEED     BUTTON_EQ

#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_REC
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_SELECT
#define CUBE_HIGHSPEED     BUTTON_A

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_SCROLL_FWD
#define CUBE_DEC           BUTTON_SCROLL_BACK
#define CUBE_MODE          BUTTON_DOWN
#define CUBE_PAUSE         BUTTON_UP
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
#define CUBE_QUIT          (BUTTON_HOME|BUTTON_REPEAT)
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_SCROLL_FWD
#define CUBE_DEC           BUTTON_SCROLL_BACK
#define CUBE_MODE          BUTTON_DOWN
#define CUBE_PAUSE         BUTTON_UP
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
(CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
(CONFIG_KEYPAD == SANSA_M200_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_VOL_UP
#define CUBE_DEC           BUTTON_VOL_DOWN
#define CUBE_MODE          BUTTON_DOWN
#define CUBE_PAUSE         BUTTON_UP
#define CUBE_HIGHSPEED     BUTTON_SELECT


#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_SCROLL_UP
#define CUBE_DEC           BUTTON_SCROLL_DOWN
#define CUBE_MODE          BUTTON_REW
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_FF

#elif CONFIG_KEYPAD == MROBE500_PAD
#define CUBE_QUIT          BUTTON_POWER

#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
#define CUBE_QUIT          BUTTON_BACK
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif (CONFIG_KEYPAD == MROBE100_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
#define CUBE_QUIT          BUTTON_RC_REC
#define CUBE_NEXT          BUTTON_RC_FF
#define CUBE_PREV          BUTTON_RC_REW
#define CUBE_INC           BUTTON_RC_VOL_UP
#define CUBE_DEC           BUTTON_RC_VOL_DOWN
#define CUBE_MODE          BUTTON_RC_MODE
#define CUBE_PAUSE         BUTTON_RC_PLAY
#define CUBE_HIGHSPEED     BUTTON_RC_MENU

#elif CONFIG_KEYPAD == COWON_D2_PAD
#define CUBE_QUIT          BUTTON_POWER

#elif (CONFIG_KEYPAD == IAUDIO67_PAD)
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_VOLUP
#define CUBE_DEC           BUTTON_VOLDOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_STOP

#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
#define CUBE_QUIT          BUTTON_BACK
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_SELECT

#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_SELECT
#define CUBE_HIGHSPEED     BUTTON_VIEW

#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
#define CUBE_QUIT          BUTTON_POWER
#define CUBE_NEXT          BUTTON_NEXT
#define CUBE_PREV          BUTTON_PREV
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_RIGHT

#elif CONFIG_KEYPAD == ONDAVX747_PAD
#define CUBE_QUIT          BUTTON_POWER
#elif CONFIG_KEYPAD == ONDAVX777_PAD
#define CUBE_QUIT          BUTTON_POWER

#elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
#define CUBE_QUIT          BUTTON_REC
#define CUBE_NEXT          BUTTON_RIGHT
#define CUBE_PREV          BUTTON_LEFT
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_REW
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_FFWD

#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
#define CUBE_QUIT          BUTTON_REC
#define CUBE_NEXT          BUTTON_NEXT
#define CUBE_PREV          BUTTON_PREV
#define CUBE_INC           BUTTON_UP
#define CUBE_DEC           BUTTON_DOWN
#define CUBE_MODE          BUTTON_MENU
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_OK

#elif CONFIG_KEYPAD == MPIO_HD200_PAD
#define CUBE_QUIT          (BUTTON_REC | BUTTON_PLAY)
#define CUBE_NEXT          BUTTON_NEXT
#define CUBE_PREV          BUTTON_PREV
#define CUBE_INC           BUTTON_VOL_UP
#define CUBE_DEC           BUTTON_VOL_DOWN
#define CUBE_MODE          BUTTON_REC
#define CUBE_PAUSE         BUTTON_PLAY
#define CUBE_HIGHSPEED     BUTTON_SELECT

#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef CUBE_QUIT
#define CUBE_QUIT          BUTTON_TOPLEFT
#endif
#ifndef CUBE_NEXT
#define CUBE_NEXT          BUTTON_MIDRIGHT
#endif
#ifndef CUBE_PREV
#define CUBE_PREV          BUTTON_MIDLEFT
#endif
#ifndef CUBE_INC
#define CUBE_INC           BUTTON_TOPMIDDLE
#endif
#ifndef CUBE_DEC
#define CUBE_DEC           BUTTON_BOTTOMMIDDLE
#endif
#ifndef CUBE_MODE
#define CUBE_MODE          BUTTON_TOPRIGHT
#endif
#ifndef CUBE_PAUSE
#define CUBE_PAUSE         BUTTON_CENTER
#endif
#ifndef CUBE_HIGHSPEED
#define CUBE_HIGHSPEED     BUTTON_BOTTOMRIGHT
#endif
#endif


#ifdef HAVE_LCD_BITMAP

#define DIST (10 * MIN(LCD_HEIGHT, LCD_WIDTH) / 16)
static int x_off = LCD_WIDTH/2;
static int y_off = LCD_HEIGHT/2;

#if LCD_DEPTH == 1
#define USEGSLIB
GREY_INFO_STRUCT
struct my_lcd {
    void (*update)(void);
    void (*clear_display)(void);
    void (*drawline)(int x1, int y1, int x2, int y2);
    void (*putsxy)(int x, int y, const unsigned char *string);
};

static struct my_lcd greyfuncs = {
    grey_update, grey_clear_display, grey_drawline, grey_putsxy
};
static struct my_lcd lcdfuncs; /* initialised at runtime */
static struct my_lcd *mylcd = &greyfuncs;

#define MYLCD(fn) mylcd->fn
#define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) grey_filltriangle(x1, y1, x2, y2, x3, y3)
#define MY_SET_FOREGROUND(fg) grey_set_foreground(fg)
#define MY_GET_FOREGROUND() grey_get_foreground()

#else
#define MYLCD(fn) rb->lcd_ ## fn
#define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) xlcd_filltriangle(x1, y1, x2, y2, x3, y3)
#define MY_SET_FOREGROUND(fg) rb->lcd_set_foreground(fg)
#define MY_GET_FOREGROUND() rb->lcd_get_foreground()
#endif

#if CONFIG_LCD == LCD_SSD1815
#define ASPECT 320 /* = 1.25 (fixed point 24.8) */
#else
#define ASPECT 256 /* = 1.00 */
#endif

#else /* !LCD_BITMAP */

#define MYLCD(fn) pgfx_ ## fn
#define DIST 9
static int x_off = 10;
static int y_off = 7;
#define ASPECT 300 /* = 1.175 */

#endif /* !LCD_BITMAP */

struct point_3D {
    long x, y, z;
};

struct point_2D {
    long x, y;
};

struct line {
    int start, end;
};

struct face {
    int corner[4];
    int line[4];
};

/* initial, unrotated cube corners */
static const struct point_3D sommet[8] =
{
    {-DIST, -DIST, -DIST},
    { DIST, -DIST, -DIST},
    { DIST,  DIST, -DIST},
    {-DIST,  DIST, -DIST},
    {-DIST, -DIST,  DIST},
    { DIST, -DIST,  DIST},
    { DIST,  DIST,  DIST},
    {-DIST,  DIST,  DIST}
};

/* The 12 lines forming the edges */
static const struct line lines[12] =
{
    {0, 1}, {1, 2}, {2, 3}, {3, 0},
    {4, 7}, {7, 6}, {6, 5}, {5, 4},
    {0, 4}, {1, 5}, {2, 6}, {3, 7}
};

static bool lines_drawn[12];

/* The 6 faces of the cube; points are in clockwise order when viewed
   from the outside */
static const struct face faces[6] =
{
    {{0, 1, 2, 3}, {0, 1, 2, 3}},
    {{4, 7, 6, 5}, {4, 5, 6, 7}},
    {{0, 4, 5, 1}, {8, 7, 9, 0}},
    {{2, 6, 7, 3}, {10, 5, 11, 2}},
    {{0, 3, 7, 4}, {3, 11, 4, 8}},
    {{1, 5, 6, 2}, {9, 6, 10, 1}}
};

#if LCD_DEPTH > 1 || defined(USEGSLIB)
static const unsigned face_colors[6] =
{
#ifdef HAVE_LCD_COLOR
    LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 255, 0),
    LCD_RGBPACK(0, 255, 0), LCD_RGBPACK(0, 0, 255), LCD_RGBPACK(0, 0, 255)
#elif defined(USEGSLIB)
#ifdef MROBE_100
    GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
    GREY_DARKGRAY,  GREY_WHITE,     GREY_WHITE
#else
    GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
    GREY_DARKGRAY,  GREY_BLACK,     GREY_BLACK
#endif
#else
    LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY,
    LCD_DARKGRAY,  LCD_BLACK,     LCD_BLACK
#endif
};
#endif

enum {
#if LCD_DEPTH > 1 || defined(USEGSLIB)
    SOLID,
#endif
    HIDDEN_LINES,
    WIREFRAME,
    NUM_MODES
};

static int mode = 0;

struct counter {
    const char *label;
    short speed;
    short angle;
};

static struct counter axes[] = {
    {"x-axis", 1, 0},
    {"y-axis", 3, 0},
    {"z-axis", 2, 0}
};

static struct point_3D point3D[8];
static struct point_2D point2D[8];
static long matrice[3][3];

static const int nb_points = 8;
static long z_off = 600;

static void cube_rotate(int xa, int ya, int za)
{
    int i;
    /* Just to prevent unnecessary lookups */
    long sxa, cxa, sya, cya, sza, cza;

    sxa = fp14_sin(xa);
    cxa = fp14_cos(xa);
    sya = fp14_sin(ya);
    cya = fp14_cos(ya);
    sza = fp14_sin(za);
    cza = fp14_cos(za);

    /* calculate overall translation matrix */
    matrice[0][0] = (cza * cya) >> 14;
    matrice[1][0] = (sza * cya) >> 14;
    matrice[2][0] = -sya;

    matrice[0][1] = (((cza * sya) >> 14) * sxa - sza * cxa) >> 14;
    matrice[1][1] = (((sza * sya) >> 14) * sxa + cxa * cza) >> 14;
    matrice[2][1] = (sxa * cya) >> 14;

    matrice[0][2] = (((cza * sya) >> 14) * cxa + sza * sxa) >> 14;
    matrice[1][2] = (((sza * sya) >> 14) * cxa - cza * sxa) >> 14;
    matrice[2][2] = (cxa * cya) >> 14;

    /* apply translation matrix to all points */
    for (i = 0; i < nb_points; i++)
    {
        point3D[i].x = matrice[0][0] * sommet[i].x + matrice[1][0] * sommet[i].y
                     + matrice[2][0] * sommet[i].z;

        point3D[i].y = matrice[0][1] * sommet[i].x + matrice[1][1] * sommet[i].y
                     + matrice[2][1] * sommet[i].z;

        point3D[i].z = matrice[0][2] * sommet[i].x + matrice[1][2] * sommet[i].y
                     + matrice[2][2] * sommet[i].z;
    }
}

static void cube_viewport(void)
{
    int i;

    /* Do viewport transformation for all points */
    for (i = 0; i < nb_points; i++)
    {
#if ASPECT != 256
        point2D[i].x = (point3D[i].x * ASPECT) / (point3D[i].z + (z_off << 14))
                     + x_off;
#else
        point2D[i].x = (point3D[i].x << 8) / (point3D[i].z + (z_off << 14))
                     + x_off;
#endif
        point2D[i].y = (point3D[i].y << 8) / (point3D[i].z + (z_off << 14))
                     + y_off;
    }
}

static void cube_draw(void)
{
    int i, j, line;
#if LCD_DEPTH > 1 || defined(USEGSLIB)
    unsigned old_foreground;
#endif

    switch (mode)
    {
#if LCD_DEPTH > 1 || defined(USEGSLIB)
      case SOLID:
      
        old_foreground = MY_GET_FOREGROUND();
        for (i = 0; i < 6; i++)
        {
            /* backface culling; if the shape winds counter-clockwise, we are
             * looking at the backface, and the (simplified) cross product
             * is < 0. Do not draw it. */
            if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
                   * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
                   - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
                   * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
                continue;

            MY_SET_FOREGROUND(face_colors[i]);
            MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
                            point2D[faces[i].corner[0]].y,
                            point2D[faces[i].corner[1]].x,
                            point2D[faces[i].corner[1]].y,
                            point2D[faces[i].corner[2]].x,
                            point2D[faces[i].corner[2]].y);
            MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
                            point2D[faces[i].corner[0]].y,
                            point2D[faces[i].corner[2]].x,
                            point2D[faces[i].corner[2]].y,
                            point2D[faces[i].corner[3]].x,
                            point2D[faces[i].corner[3]].y);

        }
        MY_SET_FOREGROUND(old_foreground);
        break;
#endif /* (LCD_DEPTH > 1) || GSLIB */

      case HIDDEN_LINES:

        rb->memset(lines_drawn, 0, sizeof(lines_drawn));
        for (i = 0; i < 6; i++)
        {
            /* backface culling; if the shape winds counter-clockwise, we are
             * looking at the backface, and the (simplified) cross product
             * is < 0. Do not draw it. */
            if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
                   * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
                   - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
                   * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
                continue;

            for (j = 0; j < 4; j++)
            {
                line = faces[i].line[j];
                if (!lines_drawn[line])
                {
                    lines_drawn[line] = true;
                    MYLCD(drawline)(point2D[lines[line].start].x,
                                    point2D[lines[line].start].y,
                                    point2D[lines[line].end].x,
                                    point2D[lines[line].end].y);
                }
            }
        }
        break;

      case WIREFRAME:

        for (i = 0; i < 12; i++)
            MYLCD(drawline)(point2D[lines[i].start].x,
                            point2D[lines[i].start].y,
                            point2D[lines[i].end].x,
                            point2D[lines[i].end].y);
        break;
    }
}

void cleanup(void *parameter)
{
    (void)parameter;

#ifdef USEGSLIB
    grey_release();
#elif defined HAVE_LCD_CHARCELLS
    pgfx_release();
#endif
}

enum plugin_status plugin_start(const void* parameter)
{
    char buffer[30];
    int t_disp = 0;
#ifdef USEGSLIB
    unsigned char *gbuf;
    size_t gbuf_size = 0;
    bool mode_switch = true;
#endif

    int button;
    int lastbutton = BUTTON_NONE;
    int curr = 0;
    bool highspeed = false;
    bool paused = false;
    bool redraw = true;
    bool exit = false;

    (void)(parameter);

#ifdef HAVE_LCD_BITMAP
#if defined(USEGSLIB)
    gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
    if (!grey_init(gbuf, gbuf_size, GREY_BUFFERED,
                   LCD_WIDTH, LCD_HEIGHT, NULL))
    {
        rb->splash(HZ, "Couldn't init greyscale display");
        return PLUGIN_ERROR;
    }
    /* init lcd_ function pointers */
    lcdfuncs.update =        rb->lcd_update;
    lcdfuncs.clear_display = rb->lcd_clear_display;
    lcdfuncs.drawline =      rb->lcd_drawline;
    lcdfuncs.putsxy =        rb->lcd_putsxy;

#ifdef MROBE_100
    grey_set_background(GREY_BLACK);
#endif

    grey_setfont(FONT_SYSFIXED);
#endif
    rb->lcd_setfont(FONT_SYSFIXED);
#else /* LCD_CHARCELLS */
    if (!pgfx_init(4, 2))
    {
        rb->splash(HZ*2, "Old LCD :(");
        return PLUGIN_OK;
    }
    pgfx_display(0, 0);
#endif

    while(!exit)
    {
        if (redraw)
        {
            MYLCD(clear_display)();
            cube_rotate(axes[0].angle, axes[1].angle, axes[2].angle);
            cube_viewport();
            cube_draw();
            redraw = false;
        }

#ifdef HAVE_LCD_BITMAP
        if (t_disp > 0)
        {
            t_disp--;
            rb->snprintf(buffer, sizeof(buffer), "%s: %d %s",
                         axes[curr].label,
                         paused ? axes[curr].angle : axes[curr].speed,
                         highspeed ? "(hs)" : "");
            MYLCD(putsxy)(0, LCD_HEIGHT-8, buffer);
            if (t_disp == 0)
                redraw = true;
        }
#else
        if (t_disp > 0)
        {
            if (t_disp == DISP_TIME)
            {
                rb->lcd_puts(5, 0, axes[curr].label);
                rb->snprintf(buffer, sizeof(buffer), "%d %c",
                             paused ? axes[curr].angle : axes[curr].speed,
                             highspeed ? 'H' : ' ');
                rb->lcd_puts(5, 1, buffer);
            }
            t_disp--;
            if (t_disp == 0)
            {
                rb->lcd_clear_display();
                pgfx_display(0, 0);
            }
        }
#endif
#ifdef USEGSLIB
        if (mode_switch)
        {
            grey_show(mode == SOLID);
            mode_switch = false;
        }
#endif
        MYLCD(update)();

        if (!paused)
        {
            int i;

            for (i = 0; i < 3; i++)
            {
                axes[i].angle += axes[i].speed;
                if (axes[i].angle > 359)
                    axes[i].angle -= 360;
                else if (axes[i].angle < 0)
                    axes[i].angle += 360;
            }
            redraw = true;

            if (highspeed)
                rb->yield();
            else
                rb->sleep(HZ/25);
            button = rb->button_get(false);
        }
        else
        {
            button = rb->button_get_w_tmo(HZ/25);
        }

        switch (button)
        {
            case CUBE_INC:
            case CUBE_INC|BUTTON_REPEAT:
                if (!paused)
                {
                    if (axes[curr].speed < 10)
                        axes[curr].speed++;
                }
                else
                {
                    if (++axes[curr].angle > 359)
                        axes[curr].angle -= 360;
                }
                t_disp = DISP_TIME;
                redraw = true;
                break;

            case CUBE_DEC:
            case CUBE_DEC|BUTTON_REPEAT:
                if (!paused)
                {
                    if (axes[curr].speed > -10)
                        axes[curr].speed--;
                }
                else
                {
                    if (--axes[curr].angle < 0)
                        axes[curr].angle += 360;
                }
                t_disp = DISP_TIME;
                redraw = true;
                break;
                
            case CUBE_NEXT:
                if (++curr > 2)
                    curr = 0;
                t_disp = DISP_TIME;
                break;

            case CUBE_PREV:
                if (--curr < 0)
                    curr = 2;
                t_disp = DISP_TIME;
                break;

            case CUBE_MODE:
#ifdef CUBE_MODE_PRE
                if (lastbutton != CUBE_MODE_PRE)
                    break;
#endif
                if (++mode >= NUM_MODES)
                    mode = 0;
#ifdef USEGSLIB
                mylcd = (mode == SOLID) ? &greyfuncs : &lcdfuncs;
                mode_switch = true;
#endif
                redraw = true;
                break;

            case CUBE_PAUSE:
#ifdef CUBE_PAUSE_PRE
                if (lastbutton != CUBE_PAUSE_PRE)
                    break;
#endif
                paused = !paused;
                break;

            case CUBE_HIGHSPEED:
#ifdef CUBE_HIGHSPEED_PRE
                if (lastbutton != CUBE_HIGHSPEED_PRE)
                    break;
#endif
                highspeed = !highspeed;
                t_disp = DISP_TIME;
                break;

#ifdef CUBE_RC_QUIT
            case CUBE_RC_QUIT:
#endif
            case CUBE_QUIT:
                exit = true;
                break;

            default:
                if (rb->default_event_handler_ex(button, cleanup, NULL)
                    == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
                break;
        }
        if (button != BUTTON_NONE)
            lastbutton = button;
    }

#ifdef USEGSLIB
    grey_release();
#elif defined(HAVE_LCD_CHARCELLS)
    pgfx_release();
#endif
    return PLUGIN_OK;
}


e_dsp(); } reenter = 0; } void glist_noselect(t_glist *x) { if (x->gl_editor) { while (x->gl_editor->e_selection) glist_deselect(x, x->gl_editor->e_selection->sel_what); if (x->gl_editor->e_selectedline) glist_deselectline(x); } } void glist_selectall(t_glist *x) { if (x->gl_editor) { glist_noselect(x); if (x->gl_list) { t_selection *sel = (t_selection *)getbytes(sizeof(*sel)); t_gobj *y = x->gl_list; x->gl_editor->e_selection = sel; sel->sel_what = y; gobj_select(y, x, 1); while((y = y->g_next)) { t_selection *sel2 = (t_selection *)getbytes(sizeof(*sel2)); sel->sel_next = sel2; sel = sel2; sel->sel_what = y; gobj_select(y, x, 1); } sel->sel_next = 0; } } } /* get the index of a gobj in a glist. If y is zero, return the total number of objects. */ int glist_getindex(t_glist *x, t_gobj *y) { t_gobj *y2; int indx; for (y2 = x->gl_list, indx = 0; y2 && y2 != y; y2 = y2->g_next) indx++; return (indx); } /* get the index of the object, among selected items, if "selected" is set; otherwise, among unselected ones. If y is zero, just counts the selected or unselected objects. */ int glist_selectionindex(t_glist *x, t_gobj *y, int selected) { t_gobj *y2; int indx; for (y2 = x->gl_list, indx = 0; y2 && y2 != y; y2 = y2->g_next) if (selected == glist_isselected(x, y2)) indx++; return (indx); } static t_gobj *glist_nth(t_glist *x, int n) { t_gobj *y; int indx; for (y = x->gl_list, indx = 0; y; y = y->g_next, indx++) if (indx == n) return (y); return (0); } /* ------------------- support for undo/redo -------------------------- */ static t_undofn canvas_undo_fn; /* current undo function if any */ static int canvas_undo_whatnext; /* whether we can now UNDO or REDO */ static void *canvas_undo_buf; /* data private to the undo function */ static t_canvas *canvas_undo_canvas; /* which canvas we can undo on */ static const char *canvas_undo_name; void canvas_setundo(t_canvas *x, t_undofn undofn, void *buf, const char *name) { #ifndef ROCKBOX int hadone = 0; #endif /* blow away the old undo information. In one special case the old undo info is re-used; if so we shouldn't free it here. */ if (canvas_undo_fn && canvas_undo_buf && (buf != canvas_undo_buf)) { (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_FREE); #ifndef ROCKBOX hadone = 1; #endif } canvas_undo_canvas = x; canvas_undo_fn = undofn; canvas_undo_buf = buf; canvas_undo_whatnext = UNDO_UNDO; canvas_undo_name = name; #ifndef ROCKBOX if (x && glist_isvisible(x) && glist_istoplevel(x)) /* enable undo in menu */ sys_vgui("pdtk_undomenu .x%x %s no\n", x, name); else if (hadone) sys_vgui("pdtk_undomenu nobody no no\n"); #endif } /* clear undo if it happens to be for the canvas x. (but if x is 0, clear it regardless of who owns it.) */ void canvas_noundo(t_canvas *x) { if (!x || (x == canvas_undo_canvas)) canvas_setundo(0, 0, 0, "foo"); } static void canvas_undo(t_canvas *x) { if (x != canvas_undo_canvas) bug("canvas_undo 1"); else if (canvas_undo_whatnext != UNDO_UNDO) bug("canvas_undo 2"); else { /* post("undo"); */ (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_UNDO); /* enable redo in menu */ #ifndef ROCKBOX if (glist_isvisible(x) && glist_istoplevel(x)) sys_vgui("pdtk_undomenu .x%x no %s\n", x, canvas_undo_name); #endif canvas_undo_whatnext = UNDO_REDO; } } static void canvas_redo(t_canvas *x) { if (x != canvas_undo_canvas) bug("canvas_undo 1"); else if (canvas_undo_whatnext != UNDO_REDO) bug("canvas_undo 2"); else { /* post("redo"); */ (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_REDO); /* enable undo in menu */ #ifndef ROCKBOX if (glist_isvisible(x) && glist_istoplevel(x)) sys_vgui("pdtk_undomenu .x%x %s no\n", x, canvas_undo_name); #endif canvas_undo_whatnext = UNDO_UNDO; } } /* ------- specific undo methods: 1. connect and disconnect -------- */ typedef struct _undo_connect { int u_index1; int u_outletno; int u_index2; int u_inletno; } t_undo_connect; static void *canvas_undo_set_disconnect(t_canvas *x, int index1, int outno, int index2, int inno) { #ifdef ROCKBOX (void) x; #endif t_undo_connect *buf = (t_undo_connect *)getbytes(sizeof(*buf)); buf->u_index1 = index1; buf->u_outletno = outno; buf->u_index2 = index2; buf->u_inletno = inno; return (buf); } void canvas_disconnect(t_canvas *x, float index1, float outno, float index2, float inno) { t_linetraverser t; t_outconnect *oc; linetraverser_start(&t, x); while((oc = linetraverser_next(&t))) { int srcno = canvas_getindex(x, &t.tr_ob->ob_g); int sinkno = canvas_getindex(x, &t.tr_ob2->ob_g); if (srcno == index1 && t.tr_outno == outno && sinkno == index2 && t.tr_inno == inno) { #ifndef ROCKBOX sys_vgui(".x%x.c delete l%x\n", x, oc); #endif obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno); break; } } } static void canvas_undo_disconnect(t_canvas *x, void *z, int action) { t_undo_connect *buf = z; if (action == UNDO_UNDO) { canvas_connect(x, buf->u_index1, buf->u_outletno, buf->u_index2, buf->u_inletno); } else if (action == UNDO_REDO) { canvas_disconnect(x, buf->u_index1, buf->u_outletno, buf->u_index2, buf->u_inletno); } else if (action == UNDO_FREE) t_freebytes(buf, sizeof(*buf)); } /* connect just calls disconnect actions backward... */ static void *canvas_undo_set_connect(t_canvas *x, int index1, int outno, int index2, int inno) { return (canvas_undo_set_disconnect(x, index1, outno, index2, inno)); } static void canvas_undo_connect(t_canvas *x, void *z, int action) { int myaction; if (action == UNDO_UNDO) myaction = UNDO_REDO; else if (action == UNDO_REDO) myaction = UNDO_UNDO; else myaction = action; canvas_undo_disconnect(x, z, myaction); } /* ---------- ... 2. cut, clear, and typing into objects: -------- */ #define UCUT_CUT 1 /* operation was a cut */ #define UCUT_CLEAR 2 /* .. a clear */ #define UCUT_TEXT 3 /* text typed into a box */ typedef struct _undo_cut { t_binbuf *u_objectbuf; /* the object cleared or typed into */ t_binbuf *u_reconnectbuf; /* connections into and out of object */ t_binbuf *u_redotextbuf; /* buffer to paste back for redo if TEXT */ int u_mode; /* from flags above */ } t_undo_cut; static void *canvas_undo_set_cut(t_canvas *x, int mode) { t_undo_cut *buf; #ifndef ROCKBOX t_gobj *y; #endif t_linetraverser t; t_outconnect *oc; int nnotsel= glist_selectionindex(x, 0, 0); buf = (t_undo_cut *)getbytes(sizeof(*buf)); buf->u_mode = mode; buf->u_redotextbuf = 0; /* store connections into/out of the selection */ buf->u_reconnectbuf = binbuf_new(); linetraverser_start(&t, x); while((oc = linetraverser_next(&t))) { int issel1 = glist_isselected(x, &t.tr_ob->ob_g); int issel2 = glist_isselected(x, &t.tr_ob2->ob_g); if (issel1 != issel2) { binbuf_addv(buf->u_reconnectbuf, "ssiiii;", gensym("#X"), gensym("connect"), (issel1 ? nnotsel : 0) + glist_selectionindex(x, &t.tr_ob->ob_g, issel1), t.tr_outno, (issel2 ? nnotsel : 0) + glist_selectionindex(x, &t.tr_ob2->ob_g, issel2), t.tr_inno); } } if (mode == UCUT_TEXT) { buf->u_objectbuf = canvas_docopy(x); } else if (mode == UCUT_CUT) { buf->u_objectbuf = 0; } else if (mode == UCUT_CLEAR) { buf->u_objectbuf = canvas_docopy(x); } return (buf); } static void canvas_undo_cut(t_canvas *x, void *z, int action) { t_undo_cut *buf = z; int mode = buf->u_mode; if (action == UNDO_UNDO) { if (mode == UCUT_CUT) canvas_dopaste(x, copy_binbuf); else if (mode == UCUT_CLEAR) canvas_dopaste(x, buf->u_objectbuf); else if (mode == UCUT_TEXT) { t_gobj *y1, *y2; glist_noselect(x); for(y1 = x->gl_list; (y2 = y1->g_next); y1 = y2) ; if (y1) { if (!buf->u_redotextbuf) { glist_noselect(x); glist_select(x, y1); buf->u_redotextbuf = canvas_docopy(x); glist_noselect(x); } glist_delete(x, y1); } canvas_dopaste(x, buf->u_objectbuf); } pd_bind(&x->gl_pd, gensym("#X")); binbuf_eval(buf->u_reconnectbuf, 0, 0, 0); pd_unbind(&x->gl_pd, gensym("#X")); } else if (action == UNDO_REDO) { if (mode == UCUT_CUT || mode == UCUT_CLEAR) canvas_doclear(x); else if (mode == UCUT_TEXT) { t_gobj *y1, *y2; for(y1 = x->gl_list; (y2 = y1->g_next); y1 = y2) ; if (y1) glist_delete(x, y1); canvas_dopaste(x, buf->u_redotextbuf); pd_bind(&x->gl_pd, gensym("#X")); binbuf_eval(buf->u_reconnectbuf, 0, 0, 0); pd_unbind(&x->gl_pd, gensym("#X")); } } else if (action == UNDO_FREE) { if (buf->u_objectbuf) binbuf_free(buf->u_objectbuf); if (buf->u_reconnectbuf) binbuf_free(buf->u_reconnectbuf); if (buf->u_redotextbuf) binbuf_free(buf->u_redotextbuf); t_freebytes(buf, sizeof(*buf)); } } /* --------- 3. motion, including "tidy up" and stretching ----------- */ typedef struct _undo_move_elem { int e_index; int e_xpix; int e_ypix; } t_undo_move_elem; typedef struct _undo_move { t_undo_move_elem *u_vec; int u_n; } t_undo_move; static int canvas_undo_already_set_move; static void *canvas_undo_set_move(t_canvas *x, int selected) { int x1, y1, x2, y2, i, indx; t_gobj *y; t_undo_move *buf = (t_undo_move *)getbytes(sizeof(*buf)); buf->u_n = selected ? glist_selectionindex(x, 0, 1) : glist_getindex(x, 0); buf->u_vec = (t_undo_move_elem *)getbytes(sizeof(*buf->u_vec) * (selected ? glist_selectionindex(x, 0, 1) : glist_getindex(x, 0))); if (selected) { for (y = x->gl_list, i = indx = 0; y; y = y->g_next, indx++) if (glist_isselected(x, y)) { gobj_getrect(y, x, &x1, &y1, &x2, &y2); buf->u_vec[i].e_index = indx; buf->u_vec[i].e_xpix = x1; buf->u_vec[i].e_ypix = y1; i++; } } else { for (y = x->gl_list, indx = 0; y; y = y->g_next, indx++) { gobj_getrect(y, x, &x1, &y1, &x2, &y2); buf->u_vec[indx].e_index = indx; buf->u_vec[indx].e_xpix = x1; buf->u_vec[indx].e_ypix = y1; } } canvas_undo_already_set_move = 1; return (buf); } static void canvas_undo_move(t_canvas *x, void *z, int action) { t_undo_move *buf = z; if (action == UNDO_UNDO || action == UNDO_REDO) { int i; for (i = 0; i < buf->u_n; i++) { int x1, y1, x2, y2, newx, newy; t_gobj *y; newx = buf->u_vec[i].e_xpix; newy = buf->u_vec[i].e_ypix; y = glist_nth(x, buf->u_vec[i].e_index); if (y) { gobj_getrect(y, x, &x1, &y1, &x2, &y2); gobj_displace(y, x, newx-x1, newy - y1); buf->u_vec[i].e_xpix = x1; buf->u_vec[i].e_ypix = y1; } } } else if (action == UNDO_FREE) { t_freebytes(buf->u_vec, buf->u_n * sizeof(*buf->u_vec)); t_freebytes(buf, sizeof(*buf)); } } /* --------- 4. paste (also duplicate) ----------- */ typedef struct _undo_paste { int u_index; /* index of first object pasted */ } t_undo_paste; static void *canvas_undo_set_paste(t_canvas *x) { t_undo_paste *buf = (t_undo_paste *)getbytes(sizeof(*buf)); buf->u_index = glist_getindex(x, 0); return (buf); } static void canvas_undo_paste(t_canvas *x, void *z, int action) { t_undo_paste *buf = z; if (action == UNDO_UNDO) { t_gobj *y; glist_noselect(x); for (y = glist_nth(x, buf->u_index); y; y = y->g_next) glist_select(x, y); canvas_doclear(x); } else if (action == UNDO_REDO) { t_selection *sel; canvas_dopaste(x, copy_binbuf); /* if it was "duplicate" have to re-enact the displacement. */ if (canvas_undo_name && canvas_undo_name[0] == 'd') for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next) gobj_displace(sel->sel_what, x, 10, 10); } else if (action == UNDO_FREE) t_freebytes(buf, sizeof(*buf)); } /* recursively check for abstractions to reload as result of a save. Don't reload the one we just saved ("except") though. */ /* LATER try to do the same trick for externs. */ static void glist_doreload(t_glist *gl, t_symbol *name, t_symbol *dir, t_gobj *except) { t_gobj *g; int i, nobj = glist_getindex(gl, 0); /* number of objects */ for (g = gl->gl_list, i = 0; g && i < nobj; i++) { if (g != except && pd_class(&g->g_pd) == canvas_class && canvas_isabstraction((t_canvas *)g) && ((t_canvas *)g)->gl_name == name && canvas_getdir((t_canvas *)g) == dir) { /* we're going to remake the object, so "g" will go stale. Get its index here, and afterward restore g. Also, the replacement will be at teh end of the list, so we don't do g = g->g_next in this case. */ int j = glist_getindex(gl, g); if (!gl->gl_havewindow) canvas_vis(glist_getcanvas(gl), 1); glist_noselect(gl); glist_select(gl, g); canvas_setundo(gl, canvas_undo_cut, canvas_undo_set_cut(gl, UCUT_CLEAR), "clear"); canvas_doclear(gl); canvas_undo(gl); glist_noselect(gl); g = glist_nth(gl, j); } else { if (g != except && pd_class(&g->g_pd) == canvas_class) glist_doreload((t_canvas *)g, name, dir, except); g = g->g_next; } } } /* call canvas_doreload on everyone */ void canvas_reload(t_symbol *name, t_symbol *dir, t_gobj *except) { t_canvas *x; /* find all root canvases */ for (x = canvas_list; x; x = x->gl_next) glist_doreload(x, name, dir, except); } /* ------------------------ event handling ------------------------ */ #define CURSOR_RUNMODE_NOTHING 0 #define CURSOR_RUNMODE_CLICKME 1 #define CURSOR_RUNMODE_THICKEN 2 #define CURSOR_RUNMODE_ADDPOINT 3 #define CURSOR_EDITMODE_NOTHING 4 #define CURSOR_EDITMODE_CONNECT 5 #define CURSOR_EDITMODE_DISCONNECT 6 static char *cursorlist[] = { #ifdef MSW "right_ptr", /* CURSOR_RUNMODE_NOTHING */ #else "left_ptr", /* CURSOR_RUNMODE_NOTHING */ #endif "arrow", /* CURSOR_RUNMODE_CLICKME */ "sb_v_double_arrow", /* CURSOR_RUNMODE_THICKEN */ "plus", /* CURSOR_RUNMODE_ADDPOINT */ "hand2", /* CURSOR_EDITMODE_NOTHING */ "circle", /* CURSOR_EDITMODE_CONNECT */ "X_cursor" /* CURSOR_EDITMODE_DISCONNECT */ }; void canvas_setcursor(t_canvas *x, unsigned int cursornum) { static t_canvas *xwas; static unsigned int cursorwas; if (cursornum >= sizeof(cursorlist)/sizeof *cursorlist) { bug("canvas_setcursor"); return; } if (xwas != x || cursorwas != cursornum) { #ifndef ROCKBOX sys_vgui(".x%x configure -cursor %s\n", x, cursorlist[cursornum]); #endif xwas = x; cursorwas = cursornum; } } /* check if a point lies in a gobj. */ int canvas_hitbox(t_canvas *x, t_gobj *y, int xpos, int ypos, int *x1p, int *y1p, int *x2p, int *y2p) { int x1, y1, x2, y2; t_text *ob; if ((ob = pd_checkobject(&y->g_pd)) && !text_shouldvis(ob, x)) return (0); gobj_getrect(y, x, &x1, &y1, &x2, &y2); if (xpos >= x1 && xpos <= x2 && ypos >= y1 && ypos <= y2) { *x1p = x1; *y1p = y1; *x2p = x2; *y2p = y2; return (1); } else return (0); } /* find the last gobj, if any, containing the point. */ static t_gobj *canvas_findhitbox(t_canvas *x, int xpos, int ypos, int *x1p, int *y1p, int *x2p, int *y2p) { t_gobj *y, *rval = 0; for (y = x->gl_list; y; y = y->g_next) { if (canvas_hitbox(x, y, xpos, ypos, x1p, y1p, x2p, y2p)) rval = y; } return (rval); } /* right-clicking on a canvas object pops up a menu. */ static void canvas_rightclick(t_canvas *x, int xpos, int ypos, t_gobj *y) { #ifdef ROCKBOX (void) x; (void) xpos; (void) ypos; (void) y; #else /* ROCKBOX */ int canprop, canopen; canprop = (!y || (y && class_getpropertiesfn(pd_class(&y->g_pd)))); canopen = (y && zgetfn(&y->g_pd, gensym("menu-open"))); sys_vgui("pdtk_canvas_popup .x%x %d %d %d %d\n", x, xpos, ypos, canprop, canopen); #endif /* ROCKBOX */ } /* tell GUI to create a properties dialog on the canvas. We tell the user the negative of the "pixel" y scale to make it appear to grow naturally upward, whereas pixels grow downward. */ static void canvas_properties(t_glist *x) { #ifdef ROCKBOX (void) x; #else /* ROCKBOX */ char graphbuf[200]; sprintf(graphbuf, "pdtk_canvas_dialog %%s %g %g %g %g \n", glist_dpixtodx(x, 1), -glist_dpixtody(x, 1), (float)glist_isgraph(x), (float)x->gl_stretch); gfxstub_new(&x->gl_pd, x, graphbuf); #endif /* ROCKBOX */ } void canvas_setgraph(t_glist *x, int flag) { if (!flag && glist_isgraph(x)) { if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner)) gobj_vis(&x->gl_gobj, x->gl_owner, 0); x->gl_isgraph = 0; if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner)) { gobj_vis(&x->gl_gobj, x->gl_owner, 1); canvas_fixlinesfor(x->gl_owner, &x->gl_obj); } } else if (flag && !glist_isgraph(x)) { if (x->gl_pixwidth <= 0) x->gl_pixwidth = GLIST_DEFGRAPHWIDTH; if (x->gl_pixheight <= 0) x->gl_pixheight = GLIST_DEFGRAPHHEIGHT; if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner)) gobj_vis(&x->gl_gobj, x->gl_owner, 0); x->gl_isgraph = 1; /* if (x->gl_owner && glist_isvisible(x->gl_owner)) canvas_vis(x, 1); */ if (x->gl_loading && x->gl_owner && glist_isvisible(x->gl_owner)) canvas_create_editor(x, 1); if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner)) { gobj_vis(&x->gl_gobj, x->gl_owner, 1); canvas_fixlinesfor(x->gl_owner, &x->gl_obj); } } } /* called from the gui when "OK" is selected on the canvas properties dialog. Again we negate "y" scale. */ static void canvas_donecanvasdialog(t_glist *x, t_floatarg xperpix, t_floatarg yperpix, t_floatarg fgraphme) { int graphme = (fgraphme != 0), redraw = 0; yperpix = -yperpix; if (xperpix == 0) xperpix = 1; if (yperpix == 0) yperpix = 1; canvas_setgraph(x, graphme); if (!x->gl_isgraph && (xperpix != glist_dpixtodx(x, 1))) { if (xperpix > 0) { x->gl_x1 = 0; x->gl_x2 = xperpix; } else { x->gl_x1 = -xperpix * (x->gl_screenx2 - x->gl_screenx1); x->gl_x2 = x->gl_x1 + xperpix; } redraw = 1; } if (!x->gl_isgraph && (yperpix != glist_dpixtody(x, 1))) { if (yperpix > 0) { x->gl_y1 = 0; x->gl_y2 = yperpix; } else { x->gl_y1 = -yperpix * (x->gl_screeny2 - x->gl_screeny1); x->gl_y2 = x->gl_y1 + yperpix; } redraw = 1; } if (redraw) canvas_redraw(x); } /* called from the gui when a popup menu comes back with "properties," "open," or "help." */ static void canvas_done_popup(t_canvas *x, float which, float xpos, float ypos) { #ifdef ROCKBOX char namebuf[MAXPDSTRING]; #else char pathbuf[MAXPDSTRING], namebuf[MAXPDSTRING]; #endif t_gobj *y; for (y = x->gl_list; y; y = y->g_next) { int x1, y1, x2, y2; if (canvas_hitbox(x, y, xpos, ypos, &x1, &y1, &x2, &y2)) { if (which == 0) /* properties */ { if (!class_getpropertiesfn(pd_class(&y->g_pd))) continue; (*class_getpropertiesfn(pd_class(&y->g_pd)))(y, x); return; } else if (which == 1) /* open */ { if (!zgetfn(&y->g_pd, gensym("menu-open"))) continue; vmess(&y->g_pd, gensym("menu-open"), ""); return; } else /* help */ { char *dir; if (pd_class(&y->g_pd) == canvas_class && canvas_isabstraction((t_canvas *)y)) { t_object *ob = (t_object *)y; int ac = binbuf_getnatom(ob->te_binbuf); t_atom *av = binbuf_getvec(ob->te_binbuf); if (ac < 1) return; atom_string(av, namebuf, MAXPDSTRING); dir = canvas_getdir((t_canvas *)y)->s_name; } else { strcpy(namebuf, class_gethelpname(pd_class(&y->g_pd))); dir = class_gethelpdir(pd_class(&y->g_pd)); } if (strcmp(namebuf + strlen(namebuf) - 3, ".pd")) strcat(namebuf, ".pd"); open_via_helppath(namebuf, dir); return; } } } if (which == 0) canvas_properties(x); else if (which == 2) { #ifndef ROCKBOX strcpy(pathbuf, sys_libdir->s_name); strcat(pathbuf, "/doc/5.reference/0.INTRO.txt"); sys_vgui("menu_opentext %s\n", pathbuf); #endif } } #define NOMOD 0 #define SHIFTMOD 1 #define CTRLMOD 2 #define ALTMOD 4 #define RIGHTCLICK 8 /* on one-button-mouse machines, you can use double click to mean right click (which gets the popup menu.) Do this for Mac. */ #ifdef MACOSX #define SIMULATERIGHTCLICK #endif #ifdef SIMULATERIGHTCLICK static double canvas_upclicktime; static int canvas_upx, canvas_upy; #define DCLICKINTERVAL 0.25 #endif /* mouse click */ void canvas_doclick(t_canvas *x, int xpos, int ypos, int which, int mod, int doit) { t_gobj *y; int shiftmod, runmode, altmod, rightclick; int x1, y1, x2, y2, clickreturned = 0; #ifdef ROCKBOX (void) which; #endif if (!x->gl_editor) { bug("editor"); return; } shiftmod = (mod & SHIFTMOD); runmode = ((mod & CTRLMOD) || (!x->gl_edit)); altmod = (mod & ALTMOD); rightclick = (mod & RIGHTCLICK); canvas_undo_already_set_move = 0; /* if keyboard was grabbed, notify grabber and cancel the grab */ if (doit && x->gl_editor->e_grab && x->gl_editor->e_keyfn) { (* x->gl_editor->e_keyfn) (x->gl_editor->e_grab, 0); glist_grab(x, 0, 0, 0, 0, 0); } #ifdef SIMULATERIGHTCLICK if (doit && !runmode && xpos == canvas_upx && ypos == canvas_upy && sys_getrealtime() - canvas_upclicktime < DCLICKINTERVAL) rightclick = 1; #endif x->gl_editor->e_lastmoved = 0; if (doit) { x->gl_editor->e_grab = 0; x->gl_editor->e_onmotion = MA_NONE; } /* post("click %d %d %d %d", xpos, ypos, which, mod); */ if (x->gl_editor->e_onmotion != MA_NONE) return; x->gl_editor->e_xwas = xpos; x->gl_editor->e_ywas = ypos; if (runmode && !rightclick) { for (y = x->gl_list; y; y = y->g_next) { /* check if the object wants to be clicked */ if (canvas_hitbox(x, y, xpos, ypos, &x1, &y1, &x2, &y2) && (clickreturned = gobj_click(y, x, xpos, ypos, shiftmod, altmod, 0, doit))) break; } if (!doit) { if (y) canvas_setcursor(x, clickreturned); else canvas_setcursor(x, CURSOR_RUNMODE_NOTHING); } return; } /* if not a runmode left click, fall here. */ if((y = canvas_findhitbox(x, xpos, ypos, &x1, &y1, &x2, &y2))) { t_object *ob = pd_checkobject(&y->g_pd); /* check you're in the rectangle */ ob = pd_checkobject(&y->g_pd); if (rightclick) canvas_rightclick(x, xpos, ypos, y); else if (shiftmod) { if (doit) { t_rtext *rt; if (ob && (rt = x->gl_editor->e_textedfor) && rt == glist_findrtext(x, ob)) { rtext_mouse(rt, xpos - x1, ypos - y1, RTEXT_SHIFT); x->gl_editor->e_onmotion = MA_DRAGTEXT; x->gl_editor->e_xwas = x1; x->gl_editor->e_ywas = y1; } else { if (glist_isselected(x, y)) glist_deselect(x, y); else glist_select(x, y); } } } else { /* look for an outlet */ int noutlet; if (ob && (noutlet = obj_noutlets(ob)) && ypos >= y2-4) { int width = x2 - x1; int nout1 = (noutlet > 1 ? noutlet - 1 : 1); int closest = ((xpos-x1) * (nout1) + width/2)/width; int hotspot = x1 + (width - IOWIDTH) * closest / (nout1); if (closest < noutlet && xpos >= (hotspot-1) && xpos <= hotspot + (IOWIDTH+1)) { if (doit) { #ifndef ROCKBOX int issignal = obj_issignaloutlet(ob, closest); #endif x->gl_editor->e_onmotion = MA_CONNECT; x->gl_editor->e_xwas = xpos; x->gl_editor->e_ywas = ypos; #ifndef ROCKBOX sys_vgui( ".x%x.c create line %d %d %d %d -width %d -tags x\n", x, xpos, ypos, xpos, ypos, (issignal ? 2 : 1)); #endif } else canvas_setcursor(x, CURSOR_EDITMODE_CONNECT); } else if (doit) goto nooutletafterall; } /* not in an outlet; select and move */ else if (doit) { t_rtext *rt; /* check if the box is being text edited */ nooutletafterall: if (ob && (rt = x->gl_editor->e_textedfor) && rt == glist_findrtext(x, ob)) { rtext_mouse(rt, xpos - x1, ypos - y1, RTEXT_DOWN); x->gl_editor->e_onmotion = MA_DRAGTEXT; x->gl_editor->e_xwas = x1; x->gl_editor->e_ywas = y1; } else { /* otherwise select and drag to displace */ if (!glist_isselected(x, y)) { glist_noselect(x); glist_select(x, y); } x->gl_editor->e_onmotion = MA_MOVE; } } else canvas_setcursor(x, CURSOR_EDITMODE_NOTHING); } return; } /* if right click doesn't hit any boxes, call rightclick routine anyway */ if (rightclick) canvas_rightclick(x, xpos, ypos, 0); /* if not an editing action, and if we didn't hit a box, set cursor and return */ if (runmode || rightclick) { canvas_setcursor(x, CURSOR_RUNMODE_NOTHING); return; } /* having failed to find a box, we try lines now. */ if (!runmode && !altmod && !shiftmod) { t_linetraverser t; t_outconnect *oc; float fx = xpos, fy = ypos; t_glist *glist2 = glist_getcanvas(x); linetraverser_start(&t, glist2); while((oc = linetraverser_next(&t))) { float lx1 = t.tr_lx1, ly1 = t.tr_ly1, lx2 = t.tr_lx2, ly2 = t.tr_ly2; float area = (lx2 - lx1) * (fy - ly1) - (ly2 - ly1) * (fx - lx1); float dsquare = (lx2-lx1) * (lx2-lx1) + (ly2-ly1) * (ly2-ly1); if (area * area >= 50 * dsquare) continue; if ((lx2-lx1) * (fx-lx1) + (ly2-ly1) * (fy-ly1) < 0) continue; if ((lx2-lx1) * (lx2-fx) + (ly2-ly1) * (ly2-fy) < 0) continue; if (doit) { glist_selectline(glist2, oc, canvas_getindex(glist2, &t.tr_ob->ob_g), t.tr_outno, canvas_getindex(glist2, &t.tr_ob2->ob_g), t.tr_inno); } canvas_setcursor(x, CURSOR_EDITMODE_DISCONNECT); return; } } canvas_setcursor(x, CURSOR_EDITMODE_NOTHING); if (doit) { if (!shiftmod) glist_noselect(x); #ifndef ROCKBOX sys_vgui(".x%x.c create rectangle %d %d %d %d -tags x\n", x, xpos, ypos, xpos, ypos); #endif x->gl_editor->e_xwas = xpos; x->gl_editor->e_ywas = ypos; x->gl_editor->e_onmotion = MA_REGION; } } void canvas_mousedown(t_canvas *x, t_floatarg xpos, t_floatarg ypos, t_floatarg which, t_floatarg mod) { canvas_doclick(x, xpos, ypos, which, mod, 1); } int canvas_isconnected (t_canvas *x, t_text *ob1, int n1, t_text *ob2, int n2) { t_linetraverser t; t_outconnect *oc; linetraverser_start(&t, x); while((oc = linetraverser_next(&t))) if (t.tr_ob == ob1 && t.tr_outno == n1 && t.tr_ob2 == ob2 && t.tr_inno == n2) return (1); return (0); } void canvas_doconnect(t_canvas *x, int xpos, int ypos, int which, int doit) { int x11, y11, x12, y12; t_gobj *y1; int x21, y21, x22, y22; t_gobj *y2; int xwas = x->gl_editor->e_xwas, ywas = x->gl_editor->e_ywas; #ifdef ROCKBOX (void) which; #endif /* ROCKBOX */ #ifndef ROCKBOX if (doit) sys_vgui(".x%x.c delete x\n", x); else sys_vgui(".x%x.c coords x %d %d %d %d\n", x, x->gl_editor->e_xwas, x->gl_editor->e_ywas, xpos, ypos); #endif /* ROCKBOX */ if ((y1 = canvas_findhitbox(x, xwas, ywas, &x11, &y11, &x12, &y12)) && (y2 = canvas_findhitbox(x, xpos, ypos, &x21, &y21, &x22, &y22))) { t_object *ob1 = pd_checkobject(&y1->g_pd); t_object *ob2 = pd_checkobject(&y2->g_pd); int noutlet1, ninlet2; if (ob1 && ob2 && ob1 != ob2 && (noutlet1 = obj_noutlets(ob1)) && (ninlet2 = obj_ninlets(ob2))) { #ifndef ROCKBOX int hotspot1; int hotspot2; int lx1, lx2, ly1, ly2; t_outconnect *oc; #else int width1 = x12 - x11, closest1; int width2 = x22 - x21, closest2; #endif if (noutlet1 > 1) { closest1 = ((xwas-x11) * (noutlet1-1) + width1/2)/width1; #ifndef ROCKBOX hotspot1 = x11 + (width1 - IOWIDTH) * closest1 / (noutlet1-1); #endif } else { closest1 = 0; #ifndef ROCKBOX hotspot1 = x11; #endif } if (ninlet2 > 1) { closest2 = ((xpos-x21) * (ninlet2-1) + width2/2)/width2; #ifndef ROCKBOX hotspot2 = x21 + (width2 - IOWIDTH) * closest2 / (ninlet2-1); #endif } else { closest2 = 0; #ifndef ROCKBOX hotspot2 = x21; #endif } if (closest1 >= noutlet1) closest1 = noutlet1 - 1; if (closest2 >= ninlet2) closest2 = ninlet2 - 1; if (canvas_isconnected (x, ob1, closest1, ob2, closest2)) { canvas_setcursor(x, CURSOR_EDITMODE_NOTHING); return; } if (obj_issignaloutlet(ob1, closest1) && !obj_issignalinlet(ob2, closest2)) { if (doit) error("can't connect signal outlet to control inlet"); canvas_setcursor(x, CURSOR_EDITMODE_NOTHING); return; } if (doit) { #ifndef ROCKBOX oc = obj_connect(ob1, closest1, ob2, closest2); lx1 = x11 + (noutlet1 > 1 ? ((x12-x11-IOWIDTH) * closest1)/(noutlet1-1) : 0) + IOMIDDLE; ly1 = y12; lx2 = x21 + (ninlet2 > 1 ? ((x22-x21-IOWIDTH) * closest2)/(ninlet2-1) : 0) + IOMIDDLE; ly2 = y21; sys_vgui(".x%x.c create line %d %d %d %d -width %d -tags l%x\n", glist_getcanvas(x), lx1, ly1, lx2, ly2, (obj_issignaloutlet(ob1, closest1) ? 2 : 1), oc); #endif /* ROCKBOX */ canvas_setundo(x, canvas_undo_connect, canvas_undo_set_connect(x, canvas_getindex(x, &ob1->ob_g), closest1, canvas_getindex(x, &ob2->ob_g), closest2), "connect"); } else canvas_setcursor(x, CURSOR_EDITMODE_CONNECT); return; } } canvas_setcursor(x, CURSOR_EDITMODE_NOTHING); } void canvas_selectinrect(t_canvas *x, int lox, int loy, int hix, int hiy) { t_gobj *y; for (y = x->gl_list; y; y = y->g_next) { int x1, y1, x2, y2; gobj_getrect(y, x, &x1, &y1, &x2, &y2); if (hix >= x1 && lox <= x2 && hiy >= y1 && loy <= y2 && !glist_isselected(x, y)) glist_select(x, y); } } static void canvas_doregion(t_canvas *x, int xpos, int ypos, int doit) { if (doit) { int lox, loy, hix, hiy; if (x->gl_editor->e_xwas < xpos) lox = x->gl_editor->e_xwas, hix = xpos; else hix = x->gl_editor->e_xwas, lox = xpos; if (x->gl_editor->e_ywas < ypos) loy = x->gl_editor->e_ywas, hiy = ypos; else hiy = x->gl_editor->e_ywas, loy = ypos; canvas_selectinrect(x, lox, loy, hix, hiy); #ifndef ROCKBOX sys_vgui(".x%x.c delete x\n", x); #endif x->gl_editor->e_onmotion = 0; } #ifndef ROCKBOX else sys_vgui(".x%x.c coords x %d %d %d %d\n", x, x->gl_editor->e_xwas, x->gl_editor->e_ywas, xpos, ypos); #endif } void canvas_mouseup(t_canvas *x, t_floatarg fxpos, t_floatarg fypos, t_floatarg fwhich) { t_gobj *y; int xpos = fxpos, ypos = fypos, which = fwhich; if (!x->gl_editor) { bug("editor"); return; } #ifdef SIMULATERIGHTCLICK canvas_upclicktime = sys_getrealtime(); canvas_upx = xpos; canvas_upy = ypos; #endif if (x->gl_editor->e_onmotion == MA_CONNECT) canvas_doconnect(x, xpos, ypos, which, 1); else if (x->gl_editor->e_onmotion == MA_REGION) canvas_doregion(x, xpos, ypos, 1); else if (x->gl_editor->e_onmotion == MA_MOVE) { /* after motion, if there's only one item selected, activate it */ if (x->gl_editor->e_selection && !(x->gl_editor->e_selection->sel_next)) gobj_activate(x->gl_editor->e_selection->sel_what, x, 1); } else if (x->gl_editor->e_onmotion == MA_PASSOUT) x->gl_editor->e_onmotion = 0; x->gl_editor->e_onmotion = MA_NONE; #if 1 /* GG misused the (unused) dbl parameter to tell if mouseup */ for (y = x->gl_list; y; y = y->g_next) { int x1, y1, x2, y2, clickreturned = 0; /* check if the object wants to be clicked */ if (canvas_hitbox(x, y, xpos, ypos, &x1, &y1, &x2, &y2) && (clickreturned = gobj_click(y, x, xpos, ypos, 0, 0, 1, 0))) break; } #endif } /* displace the selection by (dx, dy) pixels */ static void canvas_displaceselection(t_canvas *x, int dx, int dy) { t_selection *y; int resortin = 0, resortout = 0; if (!canvas_undo_already_set_move) { canvas_setundo(x, canvas_undo_move, canvas_undo_set_move(x, 1), "motion"); canvas_undo_already_set_move = 1; } for (y = x->gl_editor->e_selection; y; y = y->sel_next) { t_class *cl = pd_class(&y->sel_what->g_pd); gobj_displace(y->sel_what, x, dx, dy); if (cl == vinlet_class) resortin = 1; else if (cl == voutlet_class) resortout = 1; } if (resortin) canvas_resortinlets(x); if (resortout) canvas_resortoutlets(x); canvas_dirty(x, 1);