forked from jeffboody/loax
/
loax_server.c
546 lines (493 loc) · 15.4 KB
/
loax_server.c
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
/*
* Copyright (c) 2013 Jeff Boody
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <a3d/a3d_time.h>
#include "gl2.h"
#include "loax_event.h"
#include "loax_gl2.h"
#include "loax_cmd.h"
#include "loax_server.h"
#include "loax_server_gl2.h"
#include "loax_server_cmd.h"
#define LOG_TAG "loax"
#include "loax_log.h"
/***********************************************************
* globals *
***********************************************************/
loax_server_t* g_server = NULL;
/***********************************************************
* private - function map *
***********************************************************/
typedef void (*loax_function_cb)(void);
static loax_function_cb loax_gl_map[] =
{
loaxSwapBuffers,
loaxSize,
loaxActiveTexture,
loaxAttachShader,
loaxBindAttribLocation,
loaxBindBuffer,
loaxBindFramebuffer,
loaxBindRenderbuffer,
loaxBindTexture,
loaxBlendColor,
loaxBlendEquation,
loaxBlendEquationSeparate,
loaxBlendFunc,
loaxBlendFuncSeparate,
loaxBufferData,
loaxBufferSubData,
loaxCheckFramebufferStatus,
loaxClear,
loaxClearColor,
loaxClearDepthf,
loaxClearStencil,
loaxColorMask,
loaxCompileShader,
loaxCompressedTexImage2D,
loaxCompressedTexSubImage2D,
loaxCopyTexImage2D,
loaxCopyTexSubImage2D,
loaxCreateProgram,
loaxCreateShader,
loaxCullFace,
loaxDeleteBuffers,
loaxDeleteFramebuffers,
loaxDeleteProgram,
loaxDeleteRenderbuffers,
loaxDeleteShader,
loaxDeleteTextures,
loaxDepthFunc,
loaxDepthMask,
loaxDepthRangef,
loaxDetachShader,
loaxDisable,
loaxDisableVertexAttribArray,
loaxDrawArrays,
loaxDrawElements,
loaxEnable,
loaxEnableVertexAttribArray,
loaxFinish,
loaxFlush,
loaxFramebufferRenderbuffer,
loaxFramebufferTexture2D,
loaxFrontFace,
loaxGenBuffers,
loaxGenerateMipmap,
loaxGenFramebuffers,
loaxGenRenderbuffers,
loaxGenTextures,
loaxGetActiveAttrib,
loaxGetActiveUniform,
loaxGetAttachedShaders,
loaxGetAttribLocation,
loaxGetBooleanv,
loaxGetBufferParameteriv,
loaxGetError,
loaxGetFloatv,
loaxGetFramebufferAttachmentParameteriv,
loaxGetIntegerv,
loaxGetProgramiv,
loaxGetProgramInfoLog,
loaxGetRenderbufferParameteriv,
loaxGetShaderiv,
loaxGetShaderInfoLog,
loaxGetShaderPrecisionFormat,
loaxGetShaderSource,
loaxGetString,
loaxGetTexParameterfv,
loaxGetTexParameteriv,
loaxGetUniformfv,
loaxGetUniformiv,
loaxGetUniformLocation,
loaxGetVertexAttribfv,
loaxGetVertexAttribiv,
loaxGetVertexAttribPointerv,
loaxHint,
loaxIsBuffer,
loaxIsEnabled,
loaxIsFramebuffer,
loaxIsProgram,
loaxIsRenderbuffer,
loaxIsShader,
loaxIsTexture,
loaxLineWidth,
loaxLinkProgram,
loaxPixelStorei,
loaxPolygonOffset,
loaxReadPixels,
loaxReleaseShaderCompiler,
loaxRenderbufferStorage,
loaxSampleCoverage,
loaxScissor,
loaxShaderBinary,
loaxShaderSource,
loaxStencilFunc,
loaxStencilFuncSeparate,
loaxStencilMask,
loaxStencilMaskSeparate,
loaxStencilOp,
loaxStencilOpSeparate,
loaxTexImage2D,
loaxTexParameterf,
loaxTexParameterfv,
loaxTexParameteri,
loaxTexParameteriv,
loaxTexSubImage2D,
loaxUniform1f,
loaxUniform1fv,
loaxUniform1i,
loaxUniform1iv,
loaxUniform2f,
loaxUniform2fv,
loaxUniform2i,
loaxUniform2iv,
loaxUniform3f,
loaxUniform3fv,
loaxUniform3i,
loaxUniform3iv,
loaxUniform4f,
loaxUniform4fv,
loaxUniform4i,
loaxUniform4iv,
loaxUniformMatrix2fv,
loaxUniformMatrix3fv,
loaxUniformMatrix4fv,
loaxUseProgram,
loaxValidateProgram,
loaxVertexAttrib1f,
loaxVertexAttrib1fv,
loaxVertexAttrib2f,
loaxVertexAttrib2fv,
loaxVertexAttrib3f,
loaxVertexAttrib3fv,
loaxVertexAttrib4f,
loaxVertexAttrib4fv,
loaxVertexAttribPointer,
loaxViewport,
};
static loax_function_cb loax_cmd_map[] =
{
loaxCmdAccelerometerEnable,
loaxCmdAccelerometerDisable,
loaxCmdMagnetometerEnable,
loaxCmdMagnetometerDisable,
loaxCmdGpsEnable,
loaxCmdGpsDisable,
loaxCmdGyroscopeEnable,
loaxCmdGyroscopeDisable,
loaxCmdKeepScreenOnEnable,
loaxCmdKeepScreenOnDisable,
};
/***********************************************************
* private - Android conversions *
***********************************************************/
#define LOAX_ANDROID_MOTION_ACTION_DOWN 0
#define LOAX_ANDROID_MOTION_ACTION_UP 1
#define LOAX_ANDROID_MOTION_ACTION_MOVE 2
/***********************************************************
* public *
***********************************************************/
loax_server_t* loax_server_new(loax_server_cmd_fn cmd_fn)
{
assert(cmd_fn);
LOGD("debug");
loax_server_t* self = (loax_server_t*) malloc(sizeof(loax_server_t));
if(self == NULL)
{
LOGE("malloc failed");
return NULL;
}
self->w = 0;
self->h = 0;
// JNI callback(s)
self->cmd_fn = cmd_fn;
self->socket_render = net_socket_connect("localhost", "6120", NET_SOCKET_TCP_NODELAY);
if(self->socket_render == NULL)
{
goto fail_render;
}
int retry = 0;
while(1)
{
self->socket_event = net_socket_connect("localhost", "6121", NET_SOCKET_TCP_BUFFERED);
if(self->socket_event)
{
break;
}
else if(retry == 3)
{
LOGE("aborted after retry=%i", retry);
goto fail_event;
}
// event socket can fail if server connects too fast
++retry;
LOGW("retry=%i", retry);
usleep(50000*retry);
}
// success
g_server = self; // TODO
return self;
// failure
fail_event:
net_socket_close(&self->socket_render);
fail_render:
free(self);
return NULL;
}
void loax_server_delete(loax_server_t** _self)
{
assert(_self);
loax_server_t* self = *_self;
if(self)
{
LOGD("debug");
net_socket_close(&self->socket_event);
net_socket_close(&self->socket_render);
free(self);
*_self = NULL;
g_server = NULL;
}
}
void loax_server_resize(loax_server_t* self, int w, int h)
{
assert(self);
LOGI("%ix%i", w, h);
self->w = w;
self->h = h;
int type = LOAX_EVENT_RESIZE;
double utime = a3d_utime();
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &w, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &h, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_keydown(loax_server_t* self, int keycode, int meta,
double utime)
{
assert(self);
LOGD("debug keycode=0x%X, meta=0x%X, utime=%lf", keycode, meta, utime);
int type = LOAX_EVENT_KEYDOWN;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &keycode, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &meta, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_keyup(loax_server_t* self, int keycode, int meta,
double utime)
{
assert(self);
LOGD("debug keycode=0x%X, meta=0x%X, utime=%lf", keycode, meta, utime);
int type = LOAX_EVENT_KEYUP;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &keycode, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &meta, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_buttondown(loax_server_t* self, int id, int keycode,
double utime)
{
assert(self);
LOGD("debug id=%i, keycode=0x%X", id, keycode);
int type = LOAX_EVENT_BUTTONDOWN;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &keycode, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &id, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_buttonup(loax_server_t* self, int id, int keycode,
double utime)
{
assert(self);
LOGD("debug id=%i, keycode=0x%X, utime=%lf", id, keycode, utime);
int type = LOAX_EVENT_BUTTONUP;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &keycode, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &id, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_axismove(loax_server_t* self, int id, int axis,
float value, double utime)
{
assert(self);
LOGD("debug id=%i, axis=0x%X, value=%f, utime=%lf", id, axis, value, utime);
int type = LOAX_EVENT_AXISMOVE;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &id, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &axis, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &value, sizeof(float));
net_socket_flush(self->socket_event);
}
void loax_server_touch(loax_server_t* self, int action, int count,
float* coord, double utime)
{
assert(self);
assert(coord);
LOGD("debug action=%i, count=%i, x0=%0.1f, y0=%0.1f, x1=%0.1f, y1=%0.1f, utime=%lf",
action, count, coord[0], coord[1], coord[2], coord[3], utime);
int type;
if(action == LOAX_ANDROID_MOTION_ACTION_DOWN)
{
type = LOAX_EVENT_TOUCHDOWN;
}
else if(action == LOAX_ANDROID_MOTION_ACTION_UP)
{
type = LOAX_EVENT_TOUCHUP;
}
else if(action == LOAX_ANDROID_MOTION_ACTION_MOVE)
{
type = LOAX_EVENT_TOUCHMOVE;
}
else
{
// ignore depreciated actions
return;
}
if(count < 1)
{
LOGW("invalid count=%i", count);
return;
}
if(count > LOAX_EVENT_TOUCHMAX)
{
// silently clamp to the max touch events
count = LOAX_EVENT_TOUCHMAX;
}
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &count, sizeof(int));
int i;
for(i = 0; i < count; ++i)
{
net_socket_sendall(self->socket_event, (const void*) &coord[2*i], sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &coord[2*i + 1], sizeof(float));
}
net_socket_flush(self->socket_event);
}
void loax_server_accelerometer(loax_server_t* self,
float ax, float ay, float az,
int rotation, double utime)
{
assert(self);
LOGD("debug ax=%f, ay=%f, az=%f", ax, ay, az);
LOGD("debug rotation=%i, utime=%lf", rotation, utime);
int type = LOAX_EVENT_ACCELEROMETER;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &ax, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &ay, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &az, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &rotation, sizeof(int));
net_socket_flush(self->socket_event);
}
void loax_server_magnetometer(loax_server_t* self,
float mx, float my, float mz,
double utime)
{
assert(self);
LOGD("debug mx=%f, my=%f, mz=%f, utime=%lf", mx, my, mz, utime);
int type = LOAX_EVENT_MAGNETOMETER;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &mx, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &my, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &mz, sizeof(float));
net_socket_flush(self->socket_event);
}
void loax_server_gps(loax_server_t* self,
double lat, double lon,
float accuracy, float altitude,
float speed, float bearing,
double utime)
{
assert(self);
LOGD("debug lat=%lf, lon=%lf", lat, lon);
LOGD("debug accuracy=%f, altitude=%f, speed=%f, bearing=%f, utime=%lf",
accuracy, altitude, speed, bearing, utime);
int type = LOAX_EVENT_GPS;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &lat, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &lon, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &accuracy, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &altitude, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &speed, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &bearing, sizeof(float));
net_socket_flush(self->socket_event);
}
void loax_server_gyroscope(loax_server_t* self,
float ax, float ay, float az,
double utime)
{
assert(self);
LOGD("debug ax=%f, ay=%f, az=%f, utime=%lf", ax, ay, az, utime);
int type = LOAX_EVENT_GYROSCOPE;
net_socket_sendall(self->socket_event, (const void*) &type, sizeof(int));
net_socket_sendall(self->socket_event, (const void*) &utime, sizeof(double));
net_socket_sendall(self->socket_event, (const void*) &ax, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &ay, sizeof(float));
net_socket_sendall(self->socket_event, (const void*) &az, sizeof(float));
net_socket_flush(self->socket_event);
}
int loax_server_draw(loax_server_t* self)
{
int cmd;
int recvd;
while(net_socket_recvall(self->socket_render, (void*) &cmd, sizeof(int), &recvd) > 0)
{
if((cmd >= 0) && (cmd < LOAX_GL_MAX))
{
loax_gl_map[cmd]();
if(cmd == LOAX_SwapBuffers)
{
int one = 1;
net_socket_sendall(self->socket_render, (void*) &one, sizeof(int));
return 1;
}
else if(cmd == LOAX_Size)
{
net_socket_sendall(self->socket_render, (void*) &self->w, sizeof(int));
net_socket_sendall(self->socket_render, (void*) &self->h, sizeof(int));
}
}
else if((cmd >= 0x10000) && (cmd < LOAX_CMD_MAX))
{
cmd &= 0xFFFF;
loax_cmd_map[cmd]();
}
else
{
LOGE("invalid cmd=%i", cmd);
break;
}
}
return 0;
}