|
| 1 | +//go:build android |
| 2 | + |
| 3 | +package android |
| 4 | + |
| 5 | +import ( |
| 6 | + "unsafe" |
| 7 | + |
| 8 | + "github.com/xaionaro-go/ndk/activity" |
| 9 | + "github.com/xaionaro-go/ndk/input" |
| 10 | +) |
| 11 | + |
| 12 | +// Handlers defines callbacks for Android lifecycle events and input. |
| 13 | +// All callbacks receive the App instance for accessing platform APIs. |
| 14 | +type Handlers struct { |
| 15 | + OnCreate func(app *App) |
| 16 | + OnResume func(app *App) |
| 17 | + OnPause func(app *App) |
| 18 | + OnDestroy func(app *App) |
| 19 | + OnWindowCreated func(app *App) |
| 20 | + OnWindowDestroyed func(app *App) |
| 21 | + OnWindowFocusChanged func(app *App, hasFocus bool) |
| 22 | + OnInputEvent func(app *App, event *input.Event) bool |
| 23 | +} |
| 24 | + |
| 25 | +// Run sets up NativeActivity lifecycle callbacks and manages the App instance. |
| 26 | +// Call this from an init() function in your main package. |
| 27 | +func Run(handlers Handlers) { |
| 28 | + app := &App{} |
| 29 | + |
| 30 | + activity.SetLifecycleCallbacks(activity.LifecycleCallbacks{ |
| 31 | + OnCreate: func(act *activity.Activity) { |
| 32 | + app.setActivity(act) |
| 33 | + if handlers.OnCreate != nil { |
| 34 | + handlers.OnCreate(app) |
| 35 | + } |
| 36 | + }, |
| 37 | + OnNativeWindowCreated: func(act *activity.Activity, win unsafe.Pointer) { |
| 38 | + app.setActivity(act) |
| 39 | + app.setWindow(win) |
| 40 | + if handlers.OnWindowCreated != nil { |
| 41 | + handlers.OnWindowCreated(app) |
| 42 | + } |
| 43 | + }, |
| 44 | + OnResume: func(act *activity.Activity) { |
| 45 | + app.setActivity(act) |
| 46 | + if handlers.OnResume != nil { |
| 47 | + handlers.OnResume(app) |
| 48 | + } |
| 49 | + }, |
| 50 | + OnPause: func(act *activity.Activity) { |
| 51 | + if handlers.OnPause != nil { |
| 52 | + handlers.OnPause(app) |
| 53 | + } |
| 54 | + }, |
| 55 | + OnWindowFocusChanged: func(_ *activity.Activity, hasFocus int32) { |
| 56 | + if handlers.OnWindowFocusChanged != nil { |
| 57 | + handlers.OnWindowFocusChanged(app, hasFocus != 0) |
| 58 | + } |
| 59 | + }, |
| 60 | + OnNativeWindowDestroyed: func(_ *activity.Activity, _ unsafe.Pointer) { |
| 61 | + if handlers.OnWindowDestroyed != nil { |
| 62 | + handlers.OnWindowDestroyed(app) |
| 63 | + } |
| 64 | + app.setWindow(nil) |
| 65 | + }, |
| 66 | + OnInputQueueCreated: func(_ *activity.Activity, queuePtr unsafe.Pointer) { |
| 67 | + if handlers.OnInputEvent != nil { |
| 68 | + q := input.NewQueueFromPointer(queuePtr) |
| 69 | + go drainInput(app, q, handlers.OnInputEvent) |
| 70 | + } |
| 71 | + }, |
| 72 | + OnDestroy: func(_ *activity.Activity) { |
| 73 | + if handlers.OnDestroy != nil { |
| 74 | + handlers.OnDestroy(app) |
| 75 | + } |
| 76 | + app.setActivity(nil) |
| 77 | + app.setWindow(nil) |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func drainInput( |
| 83 | + app *App, |
| 84 | + q *input.Queue, |
| 85 | + handler func(app *App, event *input.Event) bool, |
| 86 | +) { |
| 87 | + for { |
| 88 | + ev := q.GetEvent() |
| 89 | + if ev == nil { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if q.PreDispatchEvent(ev) { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + handled := handler(app, ev) |
| 98 | + var result int32 |
| 99 | + if handled { |
| 100 | + result = 1 |
| 101 | + } |
| 102 | + q.FinishEvent(ev, result) |
| 103 | + } |
| 104 | +} |
0 commit comments