aboutsummaryrefslogtreecommitdiff
path: root/standalone/main.cpp
blob: 7a5b6ee7ec4000398aa1c2cfaa04a6c299fa1cb1 (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
#include <stdio.h>
#include <unistd.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <pthread.h>
#include <ui/EventHub.h>
#include <ui/FramebufferNativeWindow.h>
#include <ui/EGLUtils.h>

extern void AndroidInitArgs(int argc, char** argv);
extern int AndroidInit();
extern int AndroidMotionEvent(unsigned long long eventTime, int action,
        float x, float y, float pressure, float size, int deviceId);
extern int AndroidEvent(int type, int value);
extern int AndroidStep(int width, int height);

static int gDisplayWidth;
static int gDisplayHeight;
static EGLDisplay gDisplay;
static EGLSurface gSurface;
static EGLContext gContext;

void checkEGLError(const char* msg) {
    unsigned int error = eglGetError();
    if (error != EGL_SUCCESS) {
        fprintf(stderr, "%s: error %u\n", msg, error);
    }
}

void checkGLError(const char* msg) {
    unsigned int error = glGetError();
    if (error != GL_NO_ERROR) {
        fprintf(stderr, "%s: error 0x%04X\n", msg, error);
    }
}

static android::sp<android::EventHub> gHub;

class EventQueue {
private:
    class Lock {
    public:
        Lock(pthread_mutex_t& mutex) {
            m_pMutex = &mutex;
            pthread_mutex_lock(m_pMutex);
        }
        ~Lock() {
            pthread_mutex_unlock(m_pMutex);
        }
        void wait(pthread_cond_t& cond) {
            pthread_cond_wait(&cond, m_pMutex);
        }
        void signal(pthread_cond_t& cond) {
            pthread_cond_signal(&cond);
        }
    private:
        pthread_mutex_t* m_pMutex;
    };
    
public:
    
    static const int MOTION_ACTION_DOWN = 0;
    static const int MOTION_ACTION_UP = 1;
    static const int MOTION_ACTION_MOVE = 2;

// Platform-specific event types.

    static const int EV_DEVICE_ADDED = android::EventHub::DEVICE_ADDED;
    static const int EV_DEVICE_REMOVED = android::EventHub::DEVICE_REMOVED;
    
    struct Event {
        int32_t deviceId;
        int32_t type;
        int32_t scancode;
        int32_t keycode;
        uint32_t flags;
        int32_t value;
        nsecs_t when;
    };
    
    EventQueue() {
        m_Head = 0;
        m_Count = 0;
        pthread_mutex_init(&m_mutex, NULL);
        pthread_cond_init(&m_space_available, NULL);
        startEventThread();
    }
    
    // Returns NULL if no event available.
    // Call recycleEvent when you're done with the event
    Event* getEvent() {
        Event* result = NULL;
        Lock lock(m_mutex);
        if (m_Count > 0) {
            result = m_Events + m_Head;
        }
        return result;
    }
    
    void recycleEvent(Event* pEvent) {
        Lock lock(m_mutex);
        if (pEvent == m_Events + m_Head && m_Count > 0) {
            m_Head = incQueue(m_Head);
            m_Count--;
            lock.signal(m_space_available);
        }
    }
    
private:
    inline size_t incQueue(size_t index) {
        return modQueue(index + 1);
    }
    
    inline size_t modQueue(size_t index) {
        return index & EVENT_SIZE_MASK;
    }
    
    void startEventThread() {
        pthread_create( &m_eventThread, NULL, &staticEventThreadMain, this);
    }
    
    static void* staticEventThreadMain(void* arg) {
        return ((EventQueue*) arg)->eventThreadMain();
    }
    
    void* eventThreadMain() {
        gHub = new android::EventHub();
        while(true) {
            android::RawEvent rawEvent;
            bool result = gHub->getEvent(& rawEvent);
            if (result) {
                Event event;
                event.deviceId = rawEvent.deviceId;
                event.when = rawEvent.when;
                event.type = rawEvent.type;
                event.value = rawEvent.value;
                event.keycode = rawEvent.keyCode;
                event.scancode = rawEvent.scanCode;
                event.flags = rawEvent.flags;

                Lock lock(m_mutex);
                while( m_Count == MAX_EVENTS) {
                    lock.wait(m_space_available);
                }
                m_Events[modQueue(m_Head + m_Count)] = event;
                m_Count = incQueue(m_Count);
            }
        }
        return NULL;
    }

    static const size_t MAX_EVENTS = 16;
    static const size_t EVENT_SIZE_MASK = 0xf;
    
    pthread_t m_eventThread;
    pthread_mutex_t m_mutex;
    pthread_cond_t  m_space_available;
    unsigned int m_Head;
    unsigned int m_Count;
    Event m_Events[MAX_EVENTS];
};

bool gNoEvents;
EventQueue* gpEventQueue;

int init(int argc, char** argv) {
    
    for(int i = 0; i < argc; i++) {
        char* p = argv[i];
        if (strcmp(p, "-noevents") == 0) {
            printf("-noevents: will not look for events.\n");
            gNoEvents = true;
        }
    }
    
    if (! gNoEvents) {
        gpEventQueue = new EventQueue();
    }

    EGLNativeWindowType window = android_createDisplaySurface();

    gDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    
    EGLint majorVersion;
    EGLint minorVersion;
    
    eglInitialize(gDisplay, &majorVersion, &minorVersion);
    checkEGLError("eglInitialize");
        
    EGLint configRequest[] = {
            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT|EGL_WINDOW_BIT,
            EGL_DEPTH_SIZE, 16,
            EGL_NONE
    };
 
    EGLConfig config;
    android::EGLUtils::selectConfigForNativeWindow(gDisplay, configRequest, window, &config);
    gSurface = eglCreateWindowSurface(gDisplay, config, window, NULL);

    eglQuerySurface(gDisplay, gSurface, EGL_WIDTH, &gDisplayWidth);
    eglQuerySurface(gDisplay, gSurface, EGL_HEIGHT, &gDisplayHeight);
    fprintf(stderr, "display width = %d, height = %d\n", gDisplayWidth,
            gDisplayHeight);

    gContext = eglCreateContext(gDisplay, config, NULL, NULL);
    checkEGLError("eglCreateContext");
    eglMakeCurrent(gDisplay, gSurface, gSurface, gContext);
    checkEGLError("eglMakeCurrent");
    
    printf("vendor    : %s\n", glGetString(GL_VENDOR));
    printf("renderer  : %s\n", glGetString(GL_RENDERER));
    printf("version   : %s\n", glGetString(GL_VERSION));
    printf("extensions: %s\n", glGetString(GL_EXTENSIONS));
        
    return 0;
}

// Quick and dirty implementation of absolute pointer events...

bool lastAbsDown = false;
bool absDown = false;
bool absChanged = false;
unsigned long long absDownTime = 0;
int absX = 0;
int absY = 0;
int absPressure = 0;
int absSize = 0;
int lastAbsX = 0;
int lastAbsY = 0;
int lastAbsPressure = 0;
int lastAbsSize = 0;


void checkEvents() {
    
    if (gpEventQueue == NULL) {
        return;
    }
    while(true) {
        EventQueue::Event* pEvent = gpEventQueue->getEvent();
        if (pEvent == NULL) {
            return;
        }
#if 1
        printf("Event deviceId: %d, type: %d, scancode: %d,  keyCode: %d, flags: %d, value: %d, when: %llu\n",
                pEvent->deviceId, pEvent->type, pEvent->scancode,
                pEvent->keycode, pEvent->flags, pEvent->value, pEvent->when);
#endif
        switch (pEvent->type) {
        case EV_KEY: // Keyboard input
            if (pEvent->scancode == BTN_TOUCH) {
                absDown = pEvent->value != 0;
                absChanged = true;
            }
            else {
                AndroidEvent(pEvent->value, pEvent->keycode);
            }
            break;
            
        case EV_ABS:
            if (pEvent->scancode == ABS_X) {
                absX = pEvent->value;
                absChanged = true;
            } else if (pEvent->scancode == ABS_Y) {
                absY = pEvent->value;
                absChanged = true;
            } else if (pEvent->scancode == ABS_PRESSURE) {
                absPressure = pEvent->value;
                absChanged = true;
            } else if (pEvent->scancode == ABS_TOOL_WIDTH) {
                absSize = pEvent->value;
                absChanged = true;
            }

        case EV_SYN:
        {
            if (absChanged) {
                 absChanged = false;
                 int action;
                 if (absDown != lastAbsDown) {
                     lastAbsDown = absDown;
                     if (absDown) {
                         action = EventQueue::MOTION_ACTION_DOWN;
                         absDownTime = pEvent->when;
                     } else {
                         action = EventQueue::MOTION_ACTION_UP;
                         absX = lastAbsX;
                         absY = lastAbsY;
                         absPressure = lastAbsPressure;
                         absSize = lastAbsSize;
                     }
                 } else {
                     action = EventQueue::MOTION_ACTION_MOVE;
                 }
                 float scaledX = absX;
                 float scaledY = absY;
                 float scaledPressure = 1.0f;
                 float scaledSize = 0;
#if 0
                 if (di != null) {
                     if (di.absX != null) {
                         scaledX = ((scaledX-di.absX.minValue)
                                     / di.absX.range)
                                 * (mDisplay.getWidth()-1);
                     }
                     if (di.absY != null) {
                         scaledY = ((scaledY-di.absY.minValue)
                                     / di.absY.range)
                                 * (mDisplay.getHeight()-1);
                     }
                     if (di.absPressure != null) {
                         scaledPressure = 
                             ((absPressure-di.absPressure.minValue)
                                     / (float)di.absPressure.range);
                     }
                     if (di.absSize != null) {
                         scaledSize = 
                             ((absSize-di.absSize.minValue)
                                     / (float)di.absSize.range);
                     }
                 }
#endif

                 unsigned long long whenMS = pEvent->when / 1000000;
                 AndroidMotionEvent(whenMS, action,
                         scaledX, scaledY, scaledPressure, scaledSize,
                         pEvent->deviceId);
                 lastAbsX = absX;
                 lastAbsY = absY;
            }
        }
        break;
        
        default:
            break;
        }
        gpEventQueue->recycleEvent(pEvent);
    }
}

int main(int argc, char** argv) {
    fprintf(stderr, "Welcome to stand-alone Android quake.\n");
    AndroidInitArgs(argc, argv);
    
    int result = init(argc, argv);
    if (result) {
        return result;
    }
    
    if (!AndroidInit()) {
        return 1;
    }

    while(true) {
        AndroidStep(gDisplayWidth, gDisplayHeight);
        checkGLError("AndroidStep");
        eglSwapBuffers(gDisplay, gSurface);
        checkEGLError("eglSwapBuffers");
        checkEvents();
    }
    return 0;
}