29156e93dea15402aaecb31b69571a8e0763d4fb
[sdk] / ecere / src / gui / drivers / AndroidInterface.ec
1 namespace gui::drivers;
2
3 import "Window"
4 import "Interface"
5 import "Condition"
6
7 #define uint _uint
8 #define set _set
9 #include <errno.h>
10 #include <locale.h>
11 #include <pthread.h>
12 #include <unistd.h>
13
14 #include <android/configuration.h>
15 #include <android/looper.h>
16 #include <android/native_activity.h>
17 #include <android/sensor.h>
18 #include <android/log.h>
19 #include <android/window.h>
20
21 #include <jni.h>
22 #undef set
23 #undef uint
24
25 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "ecere-app", __VA_ARGS__))
26 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "ecere-app", __VA_ARGS__))
27 #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "ecere-app", __VA_ARGS__))
28 #ifdef _DEBUG
29 #define LOGV(...)  ((void)0)
30 #else
31 #define LOGV(...)  ((void)__android_log_print(ANDROID_LOG_VERBOSE, "ecere-app", __VA_ARGS__))
32 #endif
33
34 // *** NATIVE APP GLUE ********
35 enum LooperID { main = 1, input = 2, user = 3 };
36 enum AppCommand : byte
37 {
38    error = 0, inputChanged, initWindow, termWindow, windowResized, windowRedrawNeeded,
39    contentRectChanged, gainedFocus, lostFocus,
40    configChanged, lowMemory, start, resume, saveState, pause, stop, destroy
41 };
42
43 class AndroidPollSource
44 {
45 public:
46    void * userData;
47    LooperID id;
48    virtual void any_object::process();
49 };
50
51 class AndroidAppGlue : Thread
52 {
53    void* userData;
54    virtual void onAppCmd(AppCommand cmd);
55    virtual int onInputEvent(AInputEvent* event);
56    virtual void main();
57
58    ANativeActivity* activity;
59    AConfiguration* config;
60    void* savedState;
61    uint savedStateSize;
62
63    ALooper* looper;
64    AInputQueue* inputQueue;
65    ANativeWindow* window;
66    ARect contentRect;
67    AppCommand activityState;
68    bool destroyRequested;
69    char * moduleName;
70
71 private:
72    Mutex mutex { };
73    Condition cond { };
74
75    int msgread, msgwrite;
76
77    unsigned int Main()
78    {
79       config = AConfiguration_new();
80       AConfiguration_fromAssetManager(config, activity->assetManager);
81
82       print_cur_config();
83
84       looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
85       ALooper_addFd(looper, msgread, LooperID::main, ALOOPER_EVENT_INPUT, null, cmdPollSource);
86
87       mutex.Wait();
88       running = true;
89       cond.Signal();
90       mutex.Release();
91
92       main();
93
94       destroy();
95       return 0;
96    }
97
98    void destroy()
99    {
100       free_saved_state();
101       mutex.Wait();
102       if(inputQueue)
103          AInputQueue_detachLooper(inputQueue);
104       AConfiguration_delete(config);
105       destroyed = true;
106       cond.Signal();
107       mutex.Release();
108    }
109
110    AndroidPollSource cmdPollSource
111    {
112       this, main;
113
114       void process()
115       {
116          AppCommand cmd = read_cmd();
117          pre_exec_cmd(cmd);
118          onAppCmd(cmd);
119          post_exec_cmd(cmd);
120       }
121    };
122    AndroidPollSource inputPollSource
123    {
124       this, input;
125
126       void process()
127       {
128          AInputEvent* event = null;
129          if(AInputQueue_getEvent(inputQueue, &event) >= 0)
130          {
131             int handled = 0;
132             LOGV("New input event: type=%d\n", AInputEvent_getType(event));
133             if(AInputQueue_preDispatchEvent(inputQueue, event))
134                return;
135             handled = onInputEvent(event);
136             //AInputQueue_finishEvent(inputQueue, event, handled);
137          }
138          else
139             LOGE("Failure reading next input event: %s\n", strerror(errno));
140       }
141    };
142
143    bool running;
144    bool stateSaved;
145    bool destroyed;
146    AInputQueue* pendingInputQueue;
147    ANativeWindow* pendingWindow;
148    ARect pendingContentRect;
149
150    void free_saved_state()
151    {
152       mutex.Wait();
153       if(savedState)
154          free(savedState);
155       savedState = 0;
156       savedStateSize = 0;
157       mutex.Release();
158    }
159
160    AppCommand read_cmd()
161    {
162       AppCommand cmd;
163       if(read(msgread, &cmd, sizeof(cmd)) == sizeof(cmd))
164       {
165          if(cmd == saveState)
166             free_saved_state();
167          return cmd;
168       }
169       else
170          LOGE("No data on command pipe!");
171       return error;
172    }
173
174    void print_cur_config()
175    {
176       char lang[2], country[2];
177       AConfiguration_getLanguage(config, lang);
178       AConfiguration_getCountry(config, country);
179
180       LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
181               "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
182               "modetype=%d modenight=%d",
183               AConfiguration_getMcc(config),
184               AConfiguration_getMnc(config),
185               lang[0], lang[1], country[0], country[1],
186               AConfiguration_getOrientation(config),
187               AConfiguration_getTouchscreen(config),
188               AConfiguration_getDensity(config),
189               AConfiguration_getKeyboard(config),
190               AConfiguration_getNavigation(config),
191               AConfiguration_getKeysHidden(config),
192               AConfiguration_getNavHidden(config),
193               AConfiguration_getSdkVersion(config),
194               AConfiguration_getScreenSize(config),
195               AConfiguration_getScreenLong(config),
196               AConfiguration_getUiModeType(config),
197               AConfiguration_getUiModeNight(config));
198    }
199
200    void pre_exec_cmd(AppCommand cmd)
201    {
202       PrintLn("pre_exec_cmd: ", (int)cmd);
203       switch(cmd)
204       {
205          case inputChanged:
206             mutex.Wait();
207             if(inputQueue)
208                AInputQueue_detachLooper(inputQueue);
209             inputQueue = pendingInputQueue;
210             if(inputQueue)
211                AInputQueue_attachLooper(inputQueue, looper, LooperID::input, null, inputPollSource);
212             cond.Signal();
213             mutex.Release();
214             break;
215          case initWindow:
216             mutex.Wait();
217             window = pendingWindow;
218             cond.Signal();
219             mutex.Release();
220             break;
221          case termWindow:
222             cond.Signal();
223             break;
224          case resume:
225          case start:
226          case pause:
227          case stop:
228             mutex.Wait();
229             activityState = cmd;
230             cond.Signal();
231             mutex.Release();
232             break;
233          case configChanged:
234             AConfiguration_fromAssetManager(config, activity->assetManager);
235             print_cur_config();
236             break;
237          case destroy:
238             destroyRequested = true;
239             break;
240       }
241    }
242
243    void post_exec_cmd(AppCommand cmd)
244    {
245       PrintLn("post_exec_cmd: ", (int)cmd);
246       switch(cmd)
247       {
248          case termWindow:
249             mutex.Wait();
250             window = null;
251             cond.Signal();
252             mutex.Release();
253             break;
254          case saveState:
255             mutex.Wait();
256             stateSaved = true;
257             cond.Signal();
258             mutex.Release();
259             break;
260          case resume:
261             free_saved_state();
262             break;
263       }
264    }
265
266    void write_cmd(AppCommand cmd)
267    {
268       if(write(msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd))
269          LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
270    }
271
272    void set_input(AInputQueue* inputQueue)
273    {
274       mutex.Wait();
275       pendingInputQueue = inputQueue;
276       write_cmd(inputChanged);
277       while(inputQueue != pendingInputQueue)
278          cond.Wait(mutex);
279       mutex.Release();
280    }
281
282    void set_window(ANativeWindow* window)
283    {
284       mutex.Wait();
285       if(pendingWindow)
286          write_cmd(termWindow);
287       pendingWindow = window;
288       if(window)
289          write_cmd(initWindow);
290       while(window != pendingWindow)
291          cond.Wait(mutex);
292       mutex.Release();
293    }
294
295    void set_activity_state(AppCommand cmd)
296    {
297       mutex.Wait();
298       write_cmd(cmd);
299       while(activityState != cmd)
300          cond.Wait(mutex);
301       mutex.Release();
302    }
303
304    void cleanup()
305    {
306       mutex.Wait();
307       write_cmd(destroy);
308       while(!destroyed)
309          cond.Wait(mutex);
310       mutex.Release();
311       close(msgread);
312       close(msgwrite);
313    }
314
315    void setSavedState(void * state, uint size)
316    {
317       if(savedState)
318          free(savedState);
319       savedState = null;
320       if(state)
321       {
322          savedState = malloc(size);
323          savedStateSize = size;
324          memcpy(savedState, state, size);
325       }
326       else
327          savedStateSize = 0;
328    }
329
330    public void Create()
331    {
332       int msgpipe[2];
333       if(pipe(msgpipe))
334          LOGE("could not create pipe: %s", strerror(errno));
335       msgread = msgpipe[0];
336       msgwrite = msgpipe[1];
337
338       Thread::Create();
339
340       // Wait for thread to start.
341       mutex.Wait();
342       while(!running) cond.Wait(mutex);
343       mutex.Release();
344    }
345 }
346
347 // Callbacks
348 static void onDestroy(ANativeActivity* activity)
349 {
350    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
351    LOGV("Destroy: %p\n", activity);
352    app.cleanup();
353    app.Wait();
354    delete androidActivity;
355    delete __androidCurrentModule;
356    LOGV("THE END.");
357 }
358
359 static void onStart(ANativeActivity* activity)
360 {
361    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
362    LOGV("Start: %p\n", activity);
363    app.set_activity_state(start);
364 }
365
366 static void onResume(ANativeActivity* activity)
367 {
368    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
369    LOGV("Resume: %p\n", activity);
370    app.set_activity_state(resume);
371 }
372
373 static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen)
374 {
375    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
376    void* savedState = null;
377    LOGV("SaveInstanceState: %p\n", activity);
378    app.mutex.Wait();
379    app.stateSaved = false;
380    app.write_cmd(saveState);
381    while(!app.stateSaved)
382       app.cond.Wait(app.mutex);
383    if(app.savedState)
384    {
385       savedState = app.savedState;
386       *outLen = app.savedStateSize;
387       app.savedState = null;
388       app.savedStateSize = 0;
389    }
390    app.mutex.Release();
391    return savedState;
392 }
393
394 static void onPause(ANativeActivity* activity)
395 {
396    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
397    LOGV("Pause: %p\n", activity);
398    app.set_activity_state(pause);
399 }
400
401 static void onStop(ANativeActivity* activity)
402 {
403    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
404    LOGV("Stop: %p\n", activity);
405    app.set_activity_state(stop);
406 }
407
408 static void onConfigurationChanged(ANativeActivity* activity)
409 {
410    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
411    LOGV("ConfigurationChanged: %p\n", activity);
412    app.write_cmd(configChanged);
413 }
414
415 static void onLowMemory(ANativeActivity* activity)
416 {
417    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
418    LOGV("LowMemory: %p\n", activity);
419    app.write_cmd(lowMemory);
420 }
421
422 static void onWindowFocusChanged(ANativeActivity* activity, int focused)
423 {
424    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
425    LOGV("WindowFocusChanged: %p -- %d\n", activity, focused);
426    app.write_cmd(focused ? gainedFocus : lostFocus);
427 }
428
429 static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window)
430 {
431    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
432    LOGV("NativeWindowCreated: %p -- %p\n", activity, window);
433    app.set_window(window);
434 }
435
436 static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window)
437 {
438    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
439    LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window);
440    app.set_window(null);
441 }
442
443 static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
444 {
445    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
446    LOGV("InputQueueCreated: %p -- %p\n", activity, queue);
447    app.set_input(queue);
448 }
449
450 static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue)
451 {
452    AndroidAppGlue app = (AndroidAppGlue)activity->instance;
453    LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue);
454    app.inputQueue = null;
455    app.set_input(null);
456 }
457
458 default dllexport void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize)
459 {
460    AndroidAppGlue app;
461    char * moduleName;
462
463    // Determine our package name
464    JNIEnv* env=activity->env;
465    jclass clazz;
466    const char* str;
467    jboolean isCopy;
468    jmethodID methodID;
469    jobject result;
470
471    // *** Reinitialize static global variables ***
472    gotInit = false;
473    guiApplicationInitialized = false;
474    guiApp = null;
475    desktopW = 0; desktopH = 0;
476    clipBoardData = null;
477    __thisModule = null;
478    __androidCurrentModule = null;
479
480    LOGV("Creating: %p\n", activity);
481
482    //(*activity->vm)->AttachCurrentThread(activity->vm, &env, 0);
483    clazz = (*env)->GetObjectClass(env, activity->clazz);
484    methodID = (*env)->GetMethodID(env, clazz, "getPackageName", "()Ljava/lang/String;");
485    result = (*env)->CallObjectMethod(env, activity->clazz, methodID);
486    str = (*env)->GetStringUTFChars(env, (jstring)result, &isCopy);
487    // (*activity->vm)->DetachCurrentThread(activity->vm);
488    moduleName = strstr(str, "com.ecere.");
489    if(moduleName) moduleName += 10;
490    androidArgv[0] = moduleName;
491
492    // Create a base Application class
493    __androidCurrentModule = __ecere_COM_Initialize(true, 1, androidArgv);
494    // Load up Ecere
495    eModule_Load(__androidCurrentModule, "ecere", publicAccess);
496
497
498    if(activity->internalDataPath) PrintLn("internalDataPath is ", activity->internalDataPath);
499    if(activity->externalDataPath) PrintLn("externalDataPath is ", activity->externalDataPath);
500    {
501       char tmp[256];
502       PrintLn("cwd is ", GetWorkingDir(tmp, sizeof(tmp)));
503    }
504
505    ANativeActivity_setWindowFlags(activity, AWINDOW_FLAG_FULLSCREEN|AWINDOW_FLAG_KEEP_SCREEN_ON, 0 );
506    app = AndroidActivity { activity = activity, moduleName = moduleName };
507    incref app;
508    app.setSavedState(savedState, savedStateSize);
509    activity->callbacks->onDestroy = onDestroy;
510    activity->callbacks->onStart = onStart;
511    activity->callbacks->onResume = onResume;
512    activity->callbacks->onSaveInstanceState = onSaveInstanceState;
513    activity->callbacks->onPause = onPause;
514    activity->callbacks->onStop = onStop;
515    activity->callbacks->onConfigurationChanged = onConfigurationChanged;
516    activity->callbacks->onLowMemory = onLowMemory;
517    activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
518    activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
519    activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
520    activity->callbacks->onInputQueueCreated = onInputQueueCreated;
521    activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
522    activity->instance = app;
523    app.Create();
524 }
525
526 // *** END OF NATIVE APP GLUE ******
527
528 default:
529 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
530 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
531 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
532 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
533 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
534 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
535 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
536 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
537 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
538 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
539 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
540 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
541 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
542 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
543 private:
544
545 static Module __androidCurrentModule;
546 static char * androidArgv[1];
547
548 static int desktopW, desktopH;
549 static char * clipBoardData;
550 static int mouseX, mouseY;
551
552 class AndroidInterface : Interface
553 {
554    class_property(name) = "Android";
555
556    // --- User Interface System ---
557    bool Initialize()
558    {
559       setlocale(LC_ALL, "en_US.UTF-8");
560       return true;
561    }
562
563    void Terminate()
564    {
565
566    }
567
568    #define DBLCLICK_DELAY  300   // 0.3 second
569    #define DBLCLICK_DELTA  1
570
571    bool ProcessInput(bool processAll)
572    {
573       bool eventAvailable = false;
574
575       if(androidActivity.ident < 0)
576          androidActivity.ident = (LooperID)ALooper_pollAll(0, null, &androidActivity.events, (void**)&androidActivity.source);
577
578       if(gotInit && androidActivity.window)
579       {
580          int w = ANativeWindow_getWidth(androidActivity.window);
581          int h = ANativeWindow_getHeight(androidActivity.window);
582          if(desktopW != w || desktopH != h)
583          {
584             guiApp.SetDesktopPosition(0, 0, w, h, true);
585             desktopW = w;
586             desktopH = h;
587             guiApp.desktop.Update(null);
588          }
589       }
590
591       while(androidActivity.ident >= 0)
592       {
593          AndroidPollSource source = androidActivity.source;
594
595          androidActivity.source = null;
596          if(source)
597             source.process(source.userData);
598
599          // If a sensor has data, process it now.
600          /*
601          if(androidActivity.ident == user)
602          {
603             if(androidActivity.accelerometerSensor)
604             {
605                ASensorEvent event;
606                while (ASensorEventQueue_getEvents(androidActivity.sensorEventQueue, &event, 1) > 0)
607                   LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);
608             }
609          }
610          */
611
612          eventAvailable = true;
613          if(androidActivity.destroyRequested)
614          {
615             guiApp.desktop.Destroy(0);
616             eventAvailable = true;
617             androidActivity.ident = (LooperID)-1;
618          }
619          else if(processAll)
620             androidActivity.ident = (LooperID)ALooper_pollAll(0, null, &androidActivity.events, (void**)&androidActivity.source);
621          else
622             androidActivity.ident = (LooperID)-1;
623       }
624       return eventAvailable;
625    }
626
627    void Wait()
628    {
629       androidActivity.ident = (LooperID)ALooper_pollAll((int)(1000/18.2f), null, &androidActivity.events, (void**)&androidActivity.source);
630       // guiApp.WaitEvent();
631    }
632
633    void Lock(Window window)
634    {
635
636    }
637
638    void Unlock(Window window)
639    {
640
641    }
642
643    char ** GraphicsDrivers(int * numDrivers)
644    {
645       static char *graphicsDrivers[] = { "OpenGL" };
646       *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
647       return (char **)graphicsDrivers;
648    }
649
650    void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
651    {
652       *fullScreen = true;
653    }
654
655    void EnsureFullScreen(bool *fullScreen)
656    {
657       *fullScreen = true;
658    }
659
660    bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
661    {
662       bool result = true;
663
664       return result;
665    }
666
667    // --- Window Creation ---
668    void * CreateRootWindow(Window window)
669    {
670       return androidActivity.window;
671    }
672
673    void DestroyRootWindow(Window window)
674    {
675
676    }
677
678    // -- Window manipulation ---
679
680    void SetRootWindowCaption(Window window, char * name)
681    {
682
683    }
684
685    void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
686    {
687
688    }
689
690    void OrderRootWindow(Window window, bool topMost)
691    {
692
693    }
694
695    void SetRootWindowColor(Window window)
696    {
697
698    }
699
700    void OffsetWindow(Window window, int * x, int * y)
701    {
702
703    }
704
705    void UpdateRootWindow(Window window)
706    {
707       if(!window.parent || !window.parent.display)
708       {
709          if(window.visible)
710          {
711             Box box = window.box;
712             box.left -= window.clientStart.x;
713             box.top -= window.clientStart.y;
714             box.right -= window.clientStart.x;
715             box.bottom -= window.clientStart.y;
716             // Logf("Update root window %s\n", window.name);
717             window.Update(null);
718             box.left   += window.clientStart.x;
719             box.top    += window.clientStart.y;
720             box.right  += window.clientStart.x;
721             box.bottom += window.clientStart.y;
722             window.UpdateDirty(box);
723          }
724       }
725    }
726
727
728    void SetRootWindowState(Window window, WindowState state, bool visible)
729    {
730    }
731
732    void FlashRootWindow(Window window)
733    {
734
735    }
736
737    void ActivateRootWindow(Window window)
738    {
739
740    }
741
742    // --- Mouse-based window movement ---
743
744    void StartMoving(Window window, int x, int y, bool fromKeyBoard)
745    {
746
747    }
748
749    void StopMoving(Window window)
750    {
751
752    }
753
754    // -- Mouse manipulation ---
755
756    void GetMousePosition(int *x, int *y)
757    {
758       int rootWindow, childWindow;
759       int mx, my;
760       unsigned int state;
761
762       *x = mouseX;
763       *y = mouseY;
764    }
765
766    void SetMousePosition(int x, int y)
767    {
768       mouseX = x;
769       mouseY = y;
770    }
771
772    void SetMouseRange(Window window, Box box)
773    {
774    }
775
776    void SetMouseCapture(Window window)
777    {
778    }
779
780    // -- Mouse cursor ---
781
782    void SetMouseCursor(Window window, int cursor)
783    {
784       if(cursor == -1)
785       {
786
787       }
788    }
789
790    // --- Caret ---
791
792    void SetCaret(int x, int y, int size)
793    {
794       Window caretOwner = guiApp.caretOwner;
795       Window window = caretOwner ? caretOwner.rootWindow : null;
796       if(window && window.windowData)
797       {
798       }
799    }
800
801    void ClearClipboard()
802    {
803       if(clipBoardData)
804       {
805          delete clipBoardData;
806       }
807    }
808
809    bool AllocateClipboard(ClipBoard clipBoard, uint size)
810    {
811       bool result = false;
812       if((clipBoard.text = new0 byte[size]))
813          result = true;
814       return result;
815    }
816
817    bool SaveClipboard(ClipBoard clipBoard)
818    {
819       bool result = false;
820       if(clipBoard.text)
821       {
822          if(clipBoardData)
823             delete clipBoardData;
824
825          clipBoardData = clipBoard.text;
826          clipBoard.text = null;
827          result = true;
828       }
829       return result;
830    }
831
832    bool LoadClipboard(ClipBoard clipBoard)
833    {
834       bool result = false;
835
836       // The data is inside this client...
837       if(clipBoardData)
838       {
839          clipBoard.text = new char[strlen(clipBoardData)+1];
840          strcpy(clipBoard.text, clipBoardData);
841          result = true;
842       }
843       // The data is with another client...
844       else
845       {
846       }
847       return result;
848    }
849
850    void UnloadClipboard(ClipBoard clipBoard)
851    {
852       delete clipBoard.text;
853    }
854
855    // --- State based input ---
856
857    bool AcquireInput(Window window, bool state)
858    {
859       return false;
860    }
861
862    bool GetMouseState(MouseButtons * buttons, int * x, int * y)
863    {
864       bool result = false;
865       if(x) *x = 0;
866       if(y) *y = 0;
867       return result;
868    }
869
870    bool GetJoystickState(int device, Joystick joystick)
871    {
872       bool result = false;
873       return result;
874    }
875
876    bool GetKeyState(Key key)
877    {
878       int keyState = 0;
879       return keyState;
880    }
881
882    void SetTimerResolution(uint hertz)
883    {
884       // timerDelay = hertz ? (1000000 / hertz) : MAXINT;
885    }
886
887    bool SetIcon(Window window, BitmapResource resource)
888    {
889       if(resource)
890       {
891          /*Bitmap bitmap { };
892          if(bitmap.Load(resource.fileName, null, null))
893          {
894          }
895          delete bitmap;*/
896       }
897       return true;
898    }
899 }
900
901 struct SavedState
902 {
903     float angle;
904     int x;
905     int y;
906 };
907
908 static AndroidActivity androidActivity;
909
910 default const char * AndroidInterface_GetLibLocation()
911 {
912    if(androidActivity)
913    {
914       static char loc[MAX_LOCATION];
915       sprintf(loc, "/data/data/com.ecere.%s/lib/lib", androidActivity.moduleName);
916       return loc;
917    }
918    return null;
919 }
920
921 static bool gotInit;
922
923 default float AMotionEvent_getAxisValue(const AInputEvent* motion_event,
924         int32_t axis, size_t pointer_index);
925
926
927 static define AMETA_META_ON       = 0x00010000;
928 static define AMETA_META_LEFT_ON  = 0x00020000;
929 static define AMETA_META_RIGHT_ON = 0x00040000;
930
931 static Key keyCodeTable[] =
932 {
933     0, //AKEYCODE_UNKNOWN         = 0,
934     0, //AKEYCODE_SOFT_LEFT       = 1,
935     0, //AKEYCODE_SOFT_RIGHT      = 2,
936     0, //AKEYCODE_HOME            = 3,
937     0, //AKEYCODE_BACK            = 4,
938     0, //AKEYCODE_CALL            = 5,
939     0, //AKEYCODE_ENDCALL         = 6,
940     k0, //AKEYCODE_0               = 7,
941     k1, //AKEYCODE_1               = 8,
942     k2, //AKEYCODE_2               = 9,
943     k3, //AKEYCODE_3               = 10,
944     k4, //AKEYCODE_4               = 11,
945     k5, //AKEYCODE_5               = 12,
946     k6, //AKEYCODE_6               = 13,
947     k7, //AKEYCODE_7               = 14,
948     k8, //AKEYCODE_8               = 15,
949     k9, //AKEYCODE_9               = 16,
950     keyPadStar, //AKEYCODE_STAR            = 17,
951     Key { k3, shift = true }, //AKEYCODE_POUND           = 18,
952     up, //AKEYCODE_DPAD_UP         = 19,
953     down, //AKEYCODE_DPAD_DOWN       = 20,
954     left, //AKEYCODE_DPAD_LEFT       = 21,
955     right, //AKEYCODE_DPAD_RIGHT      = 22,
956     keyPad5, //AKEYCODE_DPAD_CENTER     = 23,
957     0, //AKEYCODE_VOLUME_UP       = 24,
958     0, //AKEYCODE_VOLUME_DOWN     = 25,
959     0, //AKEYCODE_POWER           = 26,
960     0, //AKEYCODE_CAMERA          = 27,
961     0, //AKEYCODE_CLEAR           = 28,
962     a, //AKEYCODE_A               = 29,
963     b, //AKEYCODE_B               = 30,
964     c, //AKEYCODE_C               = 31,
965     d, //AKEYCODE_D               = 32,
966     e, //AKEYCODE_E               = 33,
967     f, //AKEYCODE_F               = 34,
968     g, //AKEYCODE_G               = 35,
969     h, //AKEYCODE_H               = 36,
970     i, //AKEYCODE_I               = 37,
971     j, //AKEYCODE_J               = 38,
972     k, //AKEYCODE_K               = 39,
973     l, //AKEYCODE_L               = 40,
974     m, //AKEYCODE_M               = 41,
975     n, //AKEYCODE_N               = 42,
976     o, //AKEYCODE_O               = 43,
977     p, //AKEYCODE_P               = 44,
978     q, //AKEYCODE_Q               = 45,
979     r, //AKEYCODE_R               = 46,
980     s, //AKEYCODE_S               = 47,
981     t, //AKEYCODE_T               = 48,
982     u, //AKEYCODE_U               = 49,
983     v, //AKEYCODE_V               = 50,
984     w, //AKEYCODE_W               = 51,
985     x, //AKEYCODE_X               = 52,
986     y, //AKEYCODE_Y               = 53,
987     z, //AKEYCODE_Z               = 54,
988     comma, //AKEYCODE_COMMA           = 55,
989     period, //AKEYCODE_PERIOD          = 56,
990     leftAlt, //AKEYCODE_ALT_LEFT        = 57,
991     rightAlt, //AKEYCODE_ALT_RIGHT       = 58,
992     leftShift, //AKEYCODE_SHIFT_LEFT      = 59,
993     rightShift, //AKEYCODE_SHIFT_RIGHT     = 60,
994     tab, //AKEYCODE_TAB             = 61,
995     space, //AKEYCODE_SPACE           = 62,
996     0, //AKEYCODE_SYM             = 63,
997     0, //AKEYCODE_EXPLORER        = 64,
998     0, //AKEYCODE_ENVELOPE        = 65,
999     enter, //AKEYCODE_ENTER           = 66,
1000     backSpace, //AKEYCODE_DEL             = 67,
1001     backQuote, //AKEYCODE_GRAVE           = 68,
1002     minus, //AKEYCODE_MINUS           = 69,
1003     plus, //AKEYCODE_EQUALS          = 70,
1004     leftBracket, //AKEYCODE_LEFT_BRACKET    = 71,
1005     rightBracket, //AKEYCODE_RIGHT_BRACKET   = 72,
1006     backSlash, //AKEYCODE_BACKSLASH       = 73,
1007     semicolon, //AKEYCODE_SEMICOLON       = 74,
1008     quote, //AKEYCODE_APOSTROPHE      = 75,
1009     slash, //AKEYCODE_SLASH           = 76,
1010     Key { k2, shift = true }, //AKEYCODE_AT              = 77,
1011     0, //AKEYCODE_NUM             = 78,      // Interpreted as an Alt
1012     0, //AKEYCODE_HEADSETHOOK     = 79,
1013     0, //AKEYCODE_FOCUS           = 80,   // *Camera* focus
1014     keyPadPlus, //AKEYCODE_PLUS            = 81,
1015     0, //AKEYCODE_MENU            = 82,
1016     0, //AKEYCODE_NOTIFICATION    = 83,
1017     0, //AKEYCODE_SEARCH          = 84,
1018     0, //AKEYCODE_MEDIA_PLAY_PAUSE= 85,
1019     0, //AKEYCODE_MEDIA_STOP      = 86,
1020     0, //AKEYCODE_MEDIA_NEXT      = 87,
1021     0, //AKEYCODE_MEDIA_PREVIOUS  = 88,
1022     0, //AKEYCODE_MEDIA_REWIND    = 89,
1023     0, //AKEYCODE_MEDIA_FAST_FORWARD = 90,
1024     0, //AKEYCODE_MUTE            = 91,
1025     0, //AKEYCODE_PAGE_UP         = 92,
1026     0, //AKEYCODE_PAGE_DOWN       = 93,
1027     0, //AKEYCODE_PICTSYMBOLS     = 94,
1028     0, //AKEYCODE_SWITCH_CHARSET  = 95,
1029     0, //AKEYCODE_BUTTON_A        = 96,
1030     0, //AKEYCODE_BUTTON_B        = 97,
1031     0, //AKEYCODE_BUTTON_C        = 98,
1032     0, //AKEYCODE_BUTTON_X        = 99,
1033     0, //AKEYCODE_BUTTON_Y        = 100,
1034     0, //AKEYCODE_BUTTON_Z        = 101,
1035     0, //AKEYCODE_BUTTON_L1       = 102,
1036     0, //AKEYCODE_BUTTON_R1       = 103,
1037     0, //AKEYCODE_BUTTON_L2       = 104,
1038     0, //AKEYCODE_BUTTON_R2       = 105,
1039     0, //AKEYCODE_BUTTON_THUMBL   = 106,
1040     0, //AKEYCODE_BUTTON_THUMBR   = 107,
1041     0, //AKEYCODE_BUTTON_START    = 108,
1042     0, //AKEYCODE_BUTTON_SELECT   = 109,
1043     0, //AKEYCODE_BUTTON_MODE     = 110,
1044     escape, //AKEYCODE_BUTTON_ESCAPE     = 111,
1045     del //AKEYCODE_BUTTON_ESCAPE     = 112,
1046     leftControl, // = 113
1047     rightControl, // = 114
1048     capsLock, // = 115
1049     scrollLock, // = 116
1050     0, // = 117      KEYCODE_META_LEFT
1051     0, // = 118      KEYCODE_META_RIGHT
1052     0, // = 119      KEYCODE_FUNCTION
1053     printScreen, // = 120      KEYCODE_SYSRQ
1054     pauseBreak, // = 121
1055     home, // = 122
1056     end, // = 123
1057     insert // = 124
1058 };
1059
1060 // Why don't we have this in the NDK :(
1061 // default int32_t AKeyEvent_getUnichar(const AInputEvent* key_event);
1062
1063 class AndroidActivity : AndroidAppGlue
1064 {
1065    AndroidPollSource source;
1066    int events;
1067    LooperID ident;
1068    /*
1069    ASensorManager* sensorManager;
1070    const ASensor* accelerometerSensor;
1071    ASensorEventQueue* sensorEventQueue;
1072    */
1073    SavedState state;
1074
1075    int onInputEvent(AInputEvent* event)
1076    {
1077       Window window = guiApp.desktop;
1078       uint type = AInputEvent_getType(event);
1079       if(type == AINPUT_EVENT_TYPE_MOTION)
1080       {
1081          uint actionAndIndex = AMotionEvent_getAction(event);
1082          uint source = AInputEvent_getSource(event);
1083          uint action = actionAndIndex & AMOTION_EVENT_ACTION_MASK;
1084          uint index  = (actionAndIndex & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
1085          uint flags = AMotionEvent_getFlags(event);
1086          uint meta = AMotionEvent_getMetaState(event);
1087          uint edge = AMotionEvent_getEdgeFlags(event);
1088          int64 downTime = AMotionEvent_getDownTime(event);     // nanotime
1089          int64 eventTime = AMotionEvent_getDownTime(event);
1090          //float axis;
1091          Modifiers keyFlags = 0;
1092          int x = (int)AMotionEvent_getX(event, 0);
1093          int y = (int)AMotionEvent_getY(event, 0);
1094          bool shift = (meta & AMETA_SHIFT_ON) ? true : false;
1095          bool alt = (meta & AMETA_ALT_ON) ? true : false;
1096          bool sym = (meta & AMETA_SYM_ON) ? true : false;
1097
1098          keyFlags.shift = shift;
1099          keyFlags.alt = alt;
1100
1101          //PrintLn("Got a motion input event: ", action);
1102          /*
1103          if(action == 8) //AMOTION_EVENT_ACTION_SCROLL)
1104             axis = AMotionEvent_getAxisValue(event, 9, index); //AMOTION_EVENT_AXIS_VSCROLL);
1105          */
1106
1107          AInputQueue_finishEvent(inputQueue, event, 1);
1108          switch(action)
1109          {
1110             /*
1111             case 8: //AMOTION_EVENT_ACTION_SCROLL:
1112                window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (axis < 0) ? wheelUp : wheelDown, 0);
1113                break;
1114                */
1115             case AMOTION_EVENT_ACTION_DOWN:
1116                mouseX = x, mouseY = y;
1117                window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown, x, y, &keyFlags, false, true);
1118                break;
1119             case AMOTION_EVENT_ACTION_UP:
1120                mouseX = x, mouseY = y;
1121                window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp, x, y, &keyFlags, false, true);
1122                break;
1123             case AMOTION_EVENT_ACTION_MOVE:
1124                mouseX = x, mouseY = y;
1125                window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove, x, y, &keyFlags, false, true);
1126                break;
1127             case AMOTION_EVENT_ACTION_CANCEL: break;
1128             case AMOTION_EVENT_ACTION_OUTSIDE: break;
1129             case AMOTION_EVENT_ACTION_POINTER_DOWN: break;
1130             case AMOTION_EVENT_ACTION_POINTER_UP: break;
1131          }
1132          return 1;
1133       }
1134       else if(type == AINPUT_EVENT_TYPE_KEY)
1135       {
1136          uint action = AKeyEvent_getAction(event);
1137          uint flags = AKeyEvent_getFlags(event);
1138          uint keyCode = AKeyEvent_getKeyCode(event);
1139          uint meta = AKeyEvent_getMetaState(event);
1140          Key key = keyCodeTable[keyCode];
1141          bool shift = (meta & AMETA_SHIFT_ON) ? true : false;
1142          bool alt = (meta & AMETA_ALT_ON || meta & AMETA_ALT_LEFT_ON || meta & AMETA_ALT_RIGHT_ON) ? true : false;
1143          bool metaMeta = (meta & AMETA_META_ON || meta & AMETA_META_LEFT_ON || meta & AMETA_META_RIGHT_ON) ? true : false;
1144          bool sym = (meta & AMETA_SYM_ON) ? true : false;
1145          //unichar ch = AKeyEvent_getUnichar(event);
1146          unichar ch = 0;
1147
1148          key.shift = shift;
1149          key.alt = alt;
1150
1151          AInputQueue_finishEvent(inputQueue, event, 1);
1152
1153          // PrintLn("Got a key: action = ", action, ", flags = ", flags, ", keyCode = ", keyCode, ", meta = ", meta, ": key = ", (int)key);
1154
1155          if(key)
1156          {
1157             if(action == AKEY_EVENT_ACTION_DOWN || action == AKEY_EVENT_ACTION_MULTIPLE)
1158             {
1159                /*if(key == wheelDown || key == wheelUp)
1160                   window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, key, ch);
1161                else*/
1162                {
1163                   char c = Interface::TranslateKey(key.code, shift);
1164                   if(c > 0) ch = c;
1165                   window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, key, ch);
1166                }
1167             }
1168             else if(action == AKEY_EVENT_ACTION_UP)
1169                window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, key, ch);
1170          }
1171          return 1;
1172       }
1173       else
1174          AInputQueue_finishEvent(inputQueue, event, 0);
1175       return 0;
1176    }
1177
1178    void onAppCmd(AppCommand cmd)
1179    {
1180       switch(cmd)
1181       {
1182          case saveState:
1183             setSavedState(&state, sizeof(state));
1184             break;
1185          case initWindow:
1186             if(window)
1187             {
1188                int w, h;
1189                gotInit = true;
1190                ANativeWindow_setBuffersGeometry(window, 0, 0, 0); //format);
1191                w = ANativeWindow_getWidth(window);
1192                h = ANativeWindow_getHeight(window);
1193                guiApp.Initialize(false);
1194                guiApp.desktop.windowHandle = window;
1195                guiApp.interfaceDriver = null;
1196                guiApp.SwitchMode(true, null, 0, 0, 0, null, false);
1197
1198                if(desktopW != w || desktopH != h)
1199                {
1200                   guiApp.SetDesktopPosition(0, 0, w, h, true);
1201                   desktopW = w;
1202                   desktopH = h;
1203                }
1204                guiApp.desktop.Update(null);
1205             }
1206             break;
1207          case termWindow:
1208             guiApp.desktop.UnloadGraphics(false);
1209             break;
1210          case gainedFocus:
1211             guiApp.desktop.Update(null);
1212             guiApp.SetAppFocus(true);
1213             /*
1214             if(accelerometerSensor)
1215             {
1216                ASensorEventQueue_enableSensor(sensorEventQueue, accelerometerSensor);
1217                ASensorEventQueue_setEventRate(sensorEventQueue, accelerometerSensor, (1000L/60)*1000);
1218             }
1219             */
1220             break;
1221          case lostFocus:
1222             /*
1223             if(accelerometerSensor)
1224                ASensorEventQueue_disableSensor(sensorEventQueue, accelerometerSensor);
1225             */
1226             guiApp.SetAppFocus(false);
1227             guiApp.desktop.Update(null);
1228             break;
1229          case configChanged:
1230             if(window)
1231                guiApp.desktop.UpdateDisplay();
1232             break;
1233       }
1234    }
1235
1236    void main()
1237    {
1238       androidActivity = this;
1239       /* Let's have fun with sensors when we have an actual device to play with
1240       sensorManager = ASensorManager_getInstance();
1241       accelerometerSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
1242       sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LooperID::user, null, null);
1243       */
1244
1245       if(savedState)
1246          state = *(SavedState*)savedState;
1247
1248       {
1249          Module app;
1250
1251          // Evolve the Application class into a GuiApplication
1252          eInstance_Evolve((Instance *)&__androidCurrentModule, class(GuiApplication));
1253
1254          // Wait for the initWindow command:
1255          guiApp.interfaceDriver = class(AndroidInterface);
1256          while(!gotInit)
1257          {
1258             // Can't call the GuiApplication here, because GuiApplication::Initialize() has not been called yet
1259             guiApp.interfaceDriver.Wait();
1260             guiApp.interfaceDriver.ProcessInput(true);
1261          }
1262
1263          // Invoke __ecereDll_Load() in lib[our package name].so
1264          app = eModule_Load(__androidCurrentModule, moduleName, publicAccess);
1265          if(app)
1266          {
1267             Class c;
1268             // Find out if any GuiApplication class was defined in our module
1269             for(c = app.classes.first; c && !eClass_IsDerived(c, class(GuiApplication)); c = c.next);
1270             if(!c) c = class(GuiApplication);
1271
1272             // Evolve the Application into it
1273             eInstance_Evolve((Instance *)&__androidCurrentModule, c);
1274             guiApp = (GuiApplication)__androidCurrentModule;
1275
1276             {
1277                String skin = guiApp.skin;
1278                *&guiApp.currentSkin = null;
1279                guiApp.SelectSkin(skin);
1280             }
1281
1282             // Call Main()
1283             ((void (*)(void *))(void *)__androidCurrentModule._vTbl[12])(__androidCurrentModule);
1284          }
1285
1286          if(!destroyRequested)
1287             ANativeActivity_finish(activity);
1288          while(!destroyRequested)
1289          {
1290             guiApp.interfaceDriver.Wait();
1291             guiApp.interfaceDriver.ProcessInput(true);
1292          }
1293       }
1294    }
1295 }