-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathSDL_Main.cpp
1072 lines (907 loc) · 24.6 KB
/
SDL_Main.cpp
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tracker/sdl/SDL_Main.cpp
*
* Copyright 2009 Peter Barth, Christopher O'Neill, Dale Whinham
*
* This file is part of Milkytracker.
*
* Milkytracker 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.
*
* Milkytracker 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 Milkytracker. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* SDL_Main.cpp
* MilkyTracker SDL front end
*
* Created by Peter Barth on 19.11.05.
*
* 12/5/14 - Dale Whinham
* - Port to SDL2
* - Removed SDLMain.m for Mac - no longer required
* - OSX: '-psn_xxx' commandline argument ignored if Finder passes it to the executable
* - Removed GP2X-specific stuff; I don't think SDL2 is available for this platform yet
* - Added X-Y mousewheel support - other MilkyTracker files have changed to support this
*
* TODO: - Further cleanups - can we remove QTopia too?
* - Do we need that EEEPC segfault fix still with SDL2?
* - Look at the OpenGL stuff
*
* 15/2/08 - Peter Barth
* This code needs major clean up, there are too many workarounds going on
* for different platforms/configurations (MIDI, GP2X etc.)
* Please do not further pollute this single source code when possible
*
* 14/8/06 - Christopher O'Neill
* Ok, there are so many changes in this file that I've lost track...
* Here are some I remember:
* - ALSA Midi Support
* - GP2X mouse emulator (awaiting a rewrite one day..)
* - Various command line options
* - Fix for french azerty keyboards (their number keys are shifted)
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <limits.h>
#include <errno.h>
#include <SDL.h>
#include "SDL_KeyTranslation.h"
// ---------------------------- Tracker includes ----------------------------
#include "PPUI.h"
#include "DisplayDevice_SDL.h"
#include "DisplayDeviceFB_SDL.h"
#include "Screen.h"
#include "Tracker.h"
#include "PPMutex.h"
#include "PPSystem_POSIX.h"
#include "PPPath_POSIX.h"
#ifdef HAVE_LIBRTMIDI
#include "../midi/posix/MidiReceiver_pthread.h"
#endif
// --------------------------------------------------------------------------
static SDL_TimerID timer;
// Tracker globals
static PPScreen* myTrackerScreen = NULL;
static Tracker* myTracker = NULL;
static PPDisplayDevice* myDisplayDevice = NULL;
#ifdef HAVE_LIBRTMIDI
static MidiReceiver* myMidiReceiver = NULL;
#endif
// Okay what else do we need?
PPMutex* globalMutex = NULL;
static bool ticking = false;
struct MouseState {
pp_uint32 myTime;
PPPoint lastClickPosition;
pp_uint16 clickCount;
bool mouseDown;
pp_uint32 buttonDownStartTime;
};
static MouseState mouseLeft = { 0, PPPoint(0,0), 0, false, 0 };
static MouseState mouseRight = { 0, PPPoint(0,0), 0, false, 0 };
static MouseState mouseMiddle = { 0, PPPoint(0,0), 0, false, 0 };
static pp_uint32 timerTicker = 0;
static PPPoint p;
// This needs to be visible from outside
pp_uint32 PPGetTickCount()
{
return SDL_GetTicks();
}
// Same as above
void QueryKeyModifiers()
{
pp_uint32 mod = SDL_GetModState();
if((mod & KMOD_LSHIFT) || (mod & KMOD_RSHIFT))
setKeyModifier(KeyModifierSHIFT);
else
clearKeyModifier(KeyModifierSHIFT);
#ifndef __APPLE__
if((mod & KMOD_LCTRL) || (mod & KMOD_RCTRL))
#else
if((mod & KMOD_LGUI) || (mod & KMOD_RGUI))
#endif
setKeyModifier(KeyModifierCTRL);
else
clearKeyModifier(KeyModifierCTRL);
if((mod & KMOD_LALT) || (mod & KMOD_RALT))
setKeyModifier(KeyModifierALT);
else
clearKeyModifier(KeyModifierALT);
}
static void RaiseEventSerialized(PPEvent* event)
{
if (myTrackerScreen && myTracker)
{
globalMutex->lock();
myTrackerScreen->raiseEvent(event);
globalMutex->unlock();
}
}
enum SDLUserEvents
{
SDLUserEventTimer,
SDLUserEventLMouseRepeat,
SDLUserEventRMouseRepeat,
SDLUserEventMMouseRepeat,
SDLUserEventMidiKeyDown,
SDLUserEventMidiKeyUp,
};
static Uint32 SDLCALL timerCallback(Uint32 interval, void* param)
{
if (!myTrackerScreen || !myTracker || !ticking)
{
return interval;
}
SDL_UserEvent ev;
ev.type = SDL_USEREVENT;
if (!(timerTicker % 1))
{
ev.code = SDLUserEventTimer;
SDL_PushEvent((SDL_Event*)&ev);
//PPEvent myEvent(eTimer);
//RaiseEventSerialized(&myEvent);
}
timerTicker++;
if (mouseLeft.mouseDown &&
(timerTicker - mouseLeft.buttonDownStartTime) > 25)
{
ev.code = SDLUserEventLMouseRepeat;
ev.data1 = reinterpret_cast<void*>(p.x);
ev.data2 = reinterpret_cast<void*>(p.y);
SDL_PushEvent((SDL_Event*)&ev);
//PPEvent myEvent(eLMouseRepeat, &p, sizeof(PPPoint));
//RaiseEventSerialized(&myEvent);
}
if (mouseRight.mouseDown &&
(timerTicker - mouseRight.buttonDownStartTime) > 25)
{
ev.code = SDLUserEventRMouseRepeat;
ev.data1 = reinterpret_cast<void*>(p.x);
ev.data2 = reinterpret_cast<void*>(p.y);
SDL_PushEvent((SDL_Event*)&ev);
//PPEvent myEvent(eRMouseRepeat, &p, sizeof(PPPoint));
//RaiseEventSerialized(&myEvent);
}
if (mouseMiddle.mouseDown &&
(timerTicker - mouseMiddle.buttonDownStartTime) > 25)
{
ev.code = SDLUserEventMMouseRepeat;
ev.data1 = reinterpret_cast<void*>(p.x);
ev.data2 = reinterpret_cast<void*>(p.y);
SDL_PushEvent((SDL_Event*)&ev);
//PPEvent myEvent(eRMouseRepeat, &p, sizeof(PPPoint));
//RaiseEventSerialized(&myEvent);
}
return interval;
}
#ifdef HAVE_LIBRTMIDI
class MidiEventHandler : public MidiReceiver::MidiEventHandler
{
public:
virtual void keyDown(int note, int volume)
{
SDL_UserEvent ev;
ev.type = SDL_USEREVENT;
ev.code = SDLUserEventMidiKeyDown;
ev.data1 = reinterpret_cast<void*>(note);
ev.data2 = reinterpret_cast<void*>(volume);
SDL_PushEvent((SDL_Event*)&ev);
//globalMutex->lock();
//myTracker->sendNoteDown(note, volume);
//globalMutex->unlock();
}
virtual void keyUp(int note)
{
SDL_UserEvent ev;
ev.type = SDL_USEREVENT;
ev.code = SDLUserEventMidiKeyUp;
ev.data1 = reinterpret_cast<void*>(note);
SDL_PushEvent((SDL_Event*)&ev);
//globalMutex->lock();
//myTracker->sendNoteUp(note);
//globalMutex->unlock();
}
} midiEventHandler;
void StopMidiRecording()
{
if (myMidiReceiver)
{
myMidiReceiver->stopRecording();
}
}
void StartMidiRecording(unsigned int devID)
{
if (devID == (unsigned)-1)
return;
StopMidiRecording();
myMidiReceiver = new MidiReceiver(midiEventHandler);
if (!myMidiReceiver->startRecording(devID))
{
// Deal with error
fprintf(stderr, "Failed to initialise ALSA MIDI support.\n");
}
}
void InitMidi()
{
unsigned int portId = 0;
if(const char* port = std::getenv("MIDI_IN")) portId = atoi(port);
StartMidiRecording(portId);
printf("MIDI: selecting MIDI-in port: %i\n",portId);
printf("MIDI: run `MIDI_IN=x ./milkytracker` to select different port)\n", portId);
}
#endif
void translateMouseDownEvent(pp_int32 mouseButton, pp_int32 localMouseX, pp_int32 localMouseY)
{
if (mouseButton > 3 || !mouseButton)
return;
myDisplayDevice->transform(localMouseX, localMouseY);
p.x = localMouseX;
p.y = localMouseY;
// -----------------------------
if (mouseButton == 1)
{
PPEvent myEvent(eLMouseDown, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseLeft.mouseDown = true;
mouseLeft.buttonDownStartTime = timerTicker;
if (!mouseLeft.clickCount)
{
mouseLeft.myTime = PPGetTickCount();
mouseLeft.lastClickPosition.x = localMouseX;
mouseLeft.lastClickPosition.y = localMouseY;
}
else if (mouseLeft.clickCount == 2)
{
pp_uint32 deltat = PPGetTickCount() - mouseLeft.myTime;
if (deltat > 500)
{
mouseLeft.clickCount = 0;
mouseLeft.myTime = PPGetTickCount();
mouseLeft.lastClickPosition.x = localMouseX;
mouseLeft.lastClickPosition.y = localMouseY;
}
}
mouseLeft.clickCount++;
}
else if (mouseButton == 2)
{
PPEvent myEvent(eMMouseDown, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseMiddle.mouseDown = true;
mouseMiddle.buttonDownStartTime = timerTicker;
if (!mouseMiddle.clickCount)
{
mouseMiddle.myTime = PPGetTickCount();
mouseMiddle.lastClickPosition.x = localMouseX;
mouseMiddle.lastClickPosition.y = localMouseY;
}
else if (mouseMiddle.clickCount == 2)
{
pp_uint32 deltat = PPGetTickCount() - mouseRight.myTime;
if (deltat > 500)
{
mouseMiddle.clickCount = 0;
mouseMiddle.myTime = PPGetTickCount();
mouseMiddle.lastClickPosition.x = localMouseX;
mouseMiddle.lastClickPosition.y = localMouseY;
}
}
mouseMiddle.clickCount++;
}
else if (mouseButton == 3)
{
PPEvent myEvent(eRMouseDown, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseRight.mouseDown = true;
mouseRight.buttonDownStartTime = timerTicker;
if (!mouseRight.clickCount)
{
mouseRight.myTime = PPGetTickCount();
mouseRight.lastClickPosition.x = localMouseX;
mouseRight.lastClickPosition.y = localMouseY;
}
else if (mouseRight.clickCount == 2)
{
pp_uint32 deltat = PPGetTickCount() - mouseRight.myTime;
if (deltat > 500)
{
mouseRight.clickCount = 0;
mouseRight.myTime = PPGetTickCount();
mouseRight.lastClickPosition.x = localMouseX;
mouseRight.lastClickPosition.y = localMouseY;
}
}
mouseRight.clickCount++;
}
}
void translateMouseUpEvent(pp_int32 mouseButton, pp_int32 localMouseX, pp_int32 localMouseY)
{
if (mouseButton > 3 || !mouseButton)
return;
myDisplayDevice->transform(localMouseX, localMouseY);
p.x = localMouseX;
p.y = localMouseY;
// -----------------------------
if (mouseButton == 1)
{
mouseLeft.clickCount++;
if (mouseLeft.clickCount >= 4)
{
pp_uint32 deltat = PPGetTickCount() - mouseLeft.myTime;
if (deltat < 500)
{
p.x = localMouseX; p.y = localMouseY;
if (abs(p.x - mouseLeft.lastClickPosition.x) < 4 &&
abs(p.y - mouseLeft.lastClickPosition.y) < 4)
{
PPEvent myEvent(eLMouseDoubleClick, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
}
mouseLeft.clickCount = 0;
}
p.x = localMouseX; p.y = localMouseY;
PPEvent myEvent(eLMouseUp, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseLeft.mouseDown = false;
}
else if (mouseButton == 2)
{
mouseMiddle.clickCount++;
if (mouseMiddle.clickCount >= 4)
{
pp_uint32 deltat = PPGetTickCount() - mouseMiddle.myTime;
if (deltat < 500)
{
p.x = localMouseX; p.y = localMouseY;
if (abs(p.x - mouseMiddle.lastClickPosition.x) < 4 &&
abs(p.y - mouseMiddle.lastClickPosition.y) < 4)
{
PPEvent myEvent(eMMouseDoubleClick, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
}
mouseMiddle.clickCount = 0;
}
p.x = localMouseX; p.y = localMouseY;
PPEvent myEvent(eMMouseUp, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseMiddle.mouseDown = false;
}
else if (mouseButton == 3)
{
mouseRight.clickCount++;
if (mouseRight.clickCount >= 4)
{
pp_uint32 deltat = PPGetTickCount() - mouseRight.myTime;
if (deltat < 500)
{
p.x = localMouseX; p.y = localMouseY;
if (abs(p.x - mouseRight.lastClickPosition.x) < 4 &&
abs(p.y - mouseRight.lastClickPosition.y) < 4)
{
PPEvent myEvent(eRMouseDoubleClick, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
}
mouseRight.clickCount = 0;
}
p.x = localMouseX; p.y = localMouseY;
PPEvent myEvent(eRMouseUp, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
mouseRight.mouseDown = false;
}
}
void translateMouseWheelEvent(pp_int32 wheelX, pp_int32 wheelY) {
TMouseWheelEventParams mouseWheelParams;
// Deltas from wheel event
mouseWheelParams.deltaX = wheelX;
mouseWheelParams.deltaY = wheelY * 3;
// Use last stored coordinates
mouseWheelParams.pos.x = p.x;
mouseWheelParams.pos.y = p.y;
PPEvent myEvent(eMouseWheelMoved, &mouseWheelParams, sizeof(mouseWheelParams));
RaiseEventSerialized(&myEvent);
}
void translateMouseMoveEvent(pp_uint32 mouseState, pp_int32 localMouseX, pp_int32 localMouseY)
{
myDisplayDevice->transform(localMouseX, localMouseY);
p.x = localMouseX;
p.y = localMouseY;
if (mouseState == 0)
{
PPEvent myEvent(eMouseMoved, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
else
{
if (mouseState & ~(SDL_BUTTON_LMASK | SDL_BUTTON_RMASK))
{
return;
}
if (mouseState == SDL_BUTTON_LMASK && mouseLeft.mouseDown)
{
PPEvent myEvent(eLMouseDrag, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
else if (mouseState == SDL_BUTTON_RMASK && mouseRight.mouseDown)
{
PPEvent myEvent(eRMouseDrag, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
}
}
}
void preTranslateKey(SDL_Keysym& keysym)
{
// Rotate cursor keys if necessary
switch (myDisplayDevice->getOrientation())
{
case PPDisplayDevice::ORIENTATION_ROTATE90CW:
switch (keysym.sym)
{
case SDLK_UP:
keysym.sym = SDLK_LEFT;
break;
case SDLK_DOWN:
keysym.sym = SDLK_RIGHT;
break;
case SDLK_LEFT:
keysym.sym = SDLK_DOWN;
break;
case SDLK_RIGHT:
keysym.sym = SDLK_UP;
break;
}
break;
case PPDisplayDevice::ORIENTATION_ROTATE90CCW:
switch (keysym.sym)
{
case SDLK_DOWN:
keysym.sym = SDLK_LEFT;
break;
case SDLK_UP:
keysym.sym = SDLK_RIGHT;
break;
case SDLK_RIGHT:
keysym.sym = SDLK_DOWN;
break;
case SDLK_LEFT:
keysym.sym = SDLK_UP;
break;
}
break;
// ROTATE180 and UNKNOWN not handled
default: break;
}
}
void translateTextInputEvent(const SDL_Event& event)
{
#ifdef DEBUG
printf ("DEBUG: Text input: %s\n", event.text.text);
#endif
char character = event.text.text[0];
// Only deal with ASCII characters
if (character >= 32 && character <= 127)
{
PPEvent myEvent(eKeyChar, &character, sizeof(character));
RaiseEventSerialized(&myEvent);
}
}
void translateKeyDownEvent(const SDL_Event& event)
{
SDL_Keysym keysym = event.key.keysym;
// ALT+RETURN = Fullscreen toggle
if (keysym.sym == SDLK_RETURN && (keysym.mod & KMOD_LALT))
{
PPEvent myEvent(eFullScreen);
RaiseEventSerialized(&myEvent);
return;
}
preTranslateKey(keysym);
#ifdef DEBUG
printf ("DEBUG: Key pressed: VK: %d, SC: %d, Scancode: %d\n", toVK(keysym), toSC(keysym), keysym.sym);
#endif
pp_uint16 chr[3] = {toVK(keysym), toSC(keysym), static_cast<pp_uint16> (keysym.sym)};
PPEvent myEvent(eKeyDown, &chr, sizeof(chr));
RaiseEventSerialized(&myEvent);
}
void translateKeyUpEvent(const SDL_Event& event)
{
SDL_Keysym keysym = event.key.keysym;
preTranslateKey(keysym);
pp_uint16 chr[3] = {toVK(keysym), toSC(keysym), static_cast<pp_uint16> (keysym.sym)};
PPEvent myEvent(eKeyUp, &chr, sizeof(chr));
RaiseEventSerialized(&myEvent);
}
void processSDLEvents(const SDL_Event& event)
{
pp_uint32 mouseButton = 0;
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
mouseButton = event.button.button;
translateMouseDownEvent(mouseButton, event.button.x, event.button.y);
break;
case SDL_MOUSEBUTTONUP:
mouseButton = event.button.button;
translateMouseUpEvent(mouseButton, event.button.x, event.button.y);
break;
case SDL_MOUSEMOTION:
translateMouseMoveEvent(event.motion.state, event.motion.x, event.motion.y);
break;
case SDL_MOUSEWHEEL:
translateMouseWheelEvent(event.wheel.x, event.wheel.y);
break;
case SDL_TEXTINPUT:
translateTextInputEvent(event);
break;
case SDL_KEYDOWN:
translateKeyDownEvent(event);
break;
case SDL_KEYUP:
translateKeyUpEvent(event);
break;
}
}
void processSDLUserEvents(const SDL_UserEvent& event)
{
union {
void *ptr;
pp_int32 i32;
} data1, data2;
data1.ptr = event.data1;
data2.ptr = event.data2;
switch (event.code)
{
case SDLUserEventTimer:
{
// Prevent new timer events being pushed while we are processing the current one
ticking = false;
PPEvent myEvent(eTimer);
RaiseEventSerialized(&myEvent);
ticking = true;
break;
}
case SDLUserEventLMouseRepeat:
{
PPPoint p;
p.x = data1.i32;
p.y = data2.i32;
PPEvent myEvent(eLMouseRepeat, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
break;
}
case SDLUserEventRMouseRepeat:
{
PPPoint p;
p.x = data1.i32;
p.y = data2.i32;
PPEvent myEvent(eRMouseRepeat, &p, sizeof(PPPoint));
RaiseEventSerialized(&myEvent);
break;
}
case SDLUserEventMidiKeyDown:
{
pp_int32 note = data1.i32;
pp_int32 volume = data2.i32;
globalMutex->lock();
myTracker->sendNoteDown(note, volume);
globalMutex->unlock();
break;
}
case SDLUserEventMidiKeyUp:
{
pp_int32 note = data1.i32;
globalMutex->lock();
myTracker->sendNoteUp(note);
globalMutex->unlock();
break;
}
}
}
#ifdef __unix__
void crashHandler(int signum)
{
// Save backup.xm
static char buffer[1024]; // Should be enough :p
strncpy(buffer, getenv("HOME"), 1010);
strcat(buffer, "/BACKUP00.XM");
struct stat statBuf;
int num = 1;
while(stat(buffer, &statBuf) == 0 && num <= 100)
snprintf(buffer, sizeof(buffer), "%s/BACKUP%02i.XM", getenv("HOME"), num++);
if (signum == 15)
{
fprintf(stderr, "\nTERM signal received.\n");
SDL_Quit();
return;
}
else
{
fprintf(stderr, "\nCrashed with signal %i\n"
"Please submit a bug report stating exactly what you were doing "
"at the time of the crash, as well as the above signal number. "
"Also note if it is possible to reproduce this crash.\n", signum);
}
if (num != 100)
{
if (myTracker->saveModule(buffer) == MP_DEVICE_ERROR)
{
fprintf(stderr, "\nUnable to save backup (read-only filesystem?)\n\n");
}
else
{
fprintf(stderr, "\nA backup has been saved to %s\n\n", buffer);
}
}
// Try and quit SDL
SDL_Quit();
}
#endif
void initTracker(pp_uint32 bpp, PPDisplayDevice::Orientations orientation,
bool swapRedBlue, bool noSplash)
{
// Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(EXIT_FAILURE);
}
// Enable drag and drop
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
#if (defined(unix) || defined(__unix__) || defined(_AIX) || defined(__OpenBSD__)) && \
(!defined(__CYGWIN32__) && !defined(ENABLE_NANOX) && \
!defined(__QNXNTO__) && !defined(__AROS__))
// Initialise crash handler
struct sigaction act;
struct sigaction oldAct;
memset(&act, 0, sizeof(act));
act.sa_handler = crashHandler;
act.sa_flags = SA_RESETHAND;
sigaction(SIGTERM | SIGILL | SIGABRT | SIGFPE | SIGSEGV, &act, &oldAct);
sigaction(SIGILL, &act, &oldAct);
sigaction(SIGABRT, &act, &oldAct);
sigaction(SIGFPE, &act, &oldAct);
sigaction(SIGSEGV, &act, &oldAct);
#endif
// ------------ Initialise tracker ---------------
myTracker = new Tracker();
PPSize windowSize = myTracker->getWindowSizeFromDatabase();
bool fullScreen = myTracker->getFullScreenFlagFromDatabase();
pp_int32 scaleFactor = myTracker->getScreenScaleFactorFromDatabase();
#ifdef __LOWRES__
windowSize.width = DISPLAYDEVICE_WIDTH;
windowSize.height = DISPLAYDEVICE_HEIGHT;
#endif
myDisplayDevice = new PPDisplayDeviceFB(windowSize.width, windowSize.height, scaleFactor,
bpp, fullScreen, orientation, swapRedBlue);
SDL_SetWindowTitle(myDisplayDevice->getWindow(), "Loading MilkyTracker...");
myDisplayDevice->init();
myTrackerScreen = new PPScreen(myDisplayDevice, myTracker);
myTracker->setScreen(myTrackerScreen);
// Startup procedure
myTracker->startUp(noSplash);
#ifdef HAVE_LIBRTMIDI
InitMidi();
#endif
// Try to create timer
timer = SDL_AddTimer(20, timerCallback, NULL);
// Start capturing text input events
SDL_StartTextInput();
// Kickstart SDL event loop last to prevent overflowing message-queue on lowmem systems
// splash screen will still be visible
SDL_PumpEvents();
ticking = true;
}
static bool done;
void exitSDLEventLoop(bool serializedEventInvoked/* = true*/)
{
PPEvent event(eAppQuit);
RaiseEventSerialized(&event);
// it's necessary to make this mutex lock because the SDL modal event loop
// used in the modal dialogs expects modal dialogs to be invoked by
// events within these mutex lock calls
if (!serializedEventInvoked)
globalMutex->lock();
bool res = myTracker->shutDown();
if (!serializedEventInvoked)
globalMutex->unlock();
if (res)
done = 1;
}
void SendFile(char *file)
{
PPSystemString finalFile(file);
PPSystemString* strPtr = &finalFile;
PPEvent event(eFileDragDropped, &strPtr, sizeof(PPSystemString*));
RaiseEventSerialized(&event);
}
#if defined(__PSP__)
extern "C" int SDL_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
SDL_Event event;
char *loadFile = 0;
char loadFileAbsPath[PATH_MAX];
pp_int32 defaultBPP = -1;
PPDisplayDevice::Orientations orientation = PPDisplayDevice::ORIENTATION_NORMAL;
bool swapRedBlue = false, noSplash = false;
bool recVelocity = false;
// Parse command line
while ( argc > 1 )
{
--argc;
#ifdef __APPLE__
// OSX: Swallow "-psn_xxx" argument passed by Finder on OSX <10.9
if ( strncmp(argv[argc], "-psn", 4) == 0 )
{
continue;
}
else
#endif
if ( strcmp(argv[argc-1], "-bpp") == 0 )
{
defaultBPP = atoi(argv[argc]);
--argc;
}
else if ( strcmp(argv[argc], "-nosplash") == 0 )
{
noSplash = true;
}
else if ( strcmp(argv[argc], "-swap") == 0 )
{
swapRedBlue = true;
}
else if ( strcmp(argv[argc-1], "-orientation") == 0 )
{
if (strcmp(argv[argc], "NORMAL") == 0)
{
orientation = PPDisplayDevice::ORIENTATION_NORMAL;
}
else if (strcmp(argv[argc], "ROTATE90CCW") == 0)
{
orientation = PPDisplayDevice::ORIENTATION_ROTATE90CCW;
}
else if (strcmp(argv[argc], "ROTATE90CW") == 0)
{
orientation = PPDisplayDevice::ORIENTATION_ROTATE90CW;
}
else
goto unrecognizedCommandLineSwitch;
--argc;
}
else if ( strcmp(argv[argc], "-recvelocity") == 0)
{
recVelocity = true;
}
else
{
unrecognizedCommandLineSwitch:
if (argv[argc][0] == '-')
{
fprintf(stderr,
"Usage: %s [-bpp N] [-swap] [-orientation NORMAL|ROTATE90CCW|ROTATE90CW] [-nosplash] [-recvelocity]\n", argv[0]);
exit(1);
}
else
{
loadFile = argv[argc];
}
}
}
globalMutex = new PPMutex();
// Store current working path (init routine is likely to change it)
PPPath_POSIX path;
PPSystemString oldCwd = path.getCurrent();
globalMutex->lock();
initTracker(defaultBPP, orientation, swapRedBlue, noSplash);
globalMutex->unlock();
#ifdef HAVE_LIBRTMIDI
if (myMidiReceiver && recVelocity)
{
myMidiReceiver->setRecordVelocity(true);
}
#endif
if (loadFile)
{
PPSystemString newCwd = path.getCurrent();
path.change(oldCwd);
struct stat statBuf;
if (stat(realpath(loadFile, loadFileAbsPath), &statBuf) != 0)
{
fprintf(stderr, "could not open %s: %s\n", loadFile, strerror(errno));
}
else
{
SendFile(realpath(loadFile, loadFileAbsPath));
path.change(newCwd);
pp_uint16 chr[3] = {VK_RETURN, 0, 0};
PPEvent event(eKeyDown, &chr, sizeof(chr));
RaiseEventSerialized(&event);
}
}
// enable system screensaver