summaryrefslogtreecommitdiff
path: root/samples/quake/jni/sys_android.cpp
blob: 7afb55c3323ac1cd67b3f49c4eebe9380a155ac7 (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
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
/*
Copyright (C) 1996-1997 Id Software, Inc.

This program 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 2
of the License, or (at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// System functions for Android OS.
// Based on sys_linux.c

#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
// #include <sys/ipc.h>
// #include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <errno.h>
int errno_portable();
#include <dirent.h>

#include <android/log.h>

#include "quakedef.h"

#define LOG_TAG "Quake sys_android"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

qboolean			isDedicated;

int noconinput = 0;
int nostdout = 0;

// Look for data on either the sdcard or the internal data store.
// (We look at the sdcard first

static const char *basedir1 = "/sdcard/data/quake";
static const char *basedir2 = "/data/quake";

static const char *cachedir = "/tmp";

cvar_t  sys_linerefresh = CVAR2("sys_linerefresh","0");// set for entity display


//
// system dependent
//

struct stat_portable {
    unsigned long long st_dev;
    unsigned char __pad0[4];

    unsigned long __st_ino;
    unsigned int st_mode;
    unsigned int st_nlink;

    unsigned long st_uid;
    unsigned long st_gid;

    unsigned long long st_rdev;
    unsigned char __pad3[4];

    long long st_size;
    unsigned long st_blksize;
    unsigned long long st_blocks;

    unsigned long st_atime;
    unsigned long st_atime_nsec;

    unsigned long st_mtime;
    unsigned long st_mtime_nsec;

    unsigned long st_ctime;
    unsigned long st_ctime_nsec;

    unsigned long long st_ino;
};

#if defined(__mips__)
struct stat_mips {
    unsigned long st_dev;
    unsigned long __pad0[3];

    unsigned long long st_ino;

    unsigned int st_mode;
    unsigned int st_nlink;

    unsigned long st_uid;
    unsigned long st_gid;

    unsigned long st_rdev;
    unsigned long __pad1[3];

    long long st_size;

    unsigned long st_atime;
    unsigned long st_atime_nsec;

    unsigned long st_mtime;
    unsigned long st_mtime_nsec;

    unsigned long st_ctime;
    unsigned long st_ctime_nsec;

    unsigned long st_blksize;
    unsigned long __pad2;

    unsigned long long st_blocks;
};
#endif  //__mips__

int stat_portable(const char *path, struct stat_portable *st);
int fstat_portable(int fd, struct stat_portable *st);


#if !defined(__GDK__)

#if defined(__mips__)
static void __copy_mips_stat_to_portable(struct stat_portable *pst_portable, struct stat/*_mips*/ *pst_mips)
{
   pst_portable->st_dev = pst_mips->st_dev;  //ToDo: sizeof st_dev is different !
   pst_portable->__st_ino = 0;             //ToDo: missing in mips!
   pst_portable->st_mode = pst_mips->st_mode;
   pst_portable->st_nlink = pst_mips->st_nlink;
   pst_portable->st_uid = pst_mips->st_uid;
   pst_portable->st_gid = pst_mips->st_gid;
   pst_portable->st_rdev = pst_mips->st_rdev;  //ToDo: st_rdev is different !
   pst_portable->st_size = pst_mips->st_size;
   pst_portable->st_blksize = pst_mips->st_blksize;
   pst_portable->st_blocks = pst_mips->st_blocks;
   pst_portable->st_atime = pst_mips->st_atime;
   pst_portable->st_atime_nsec = pst_mips->st_atime_nsec;
   pst_portable->st_mtime = pst_mips->st_mtime;
   pst_portable->st_mtime_nsec = pst_mips->st_mtime_nsec;
   pst_portable->st_ctime = pst_mips->st_ctime;
   pst_portable->st_ctime_nsec = pst_mips->st_ctime_nsec;
   pst_portable->st_ino = pst_mips->st_ino;
}
#endif // __mips__

int stat_portable(const char *path, struct stat_portable *st)
{
#if !defined(__mips__)
   struct stat st_orig;
   //assert(sizeof(st_orig) == sizeof(*st));   //ToDo: and offset of each field !

   return stat(path, (struct stat*)st);
#else
   struct stat/*_mips*/ st_mips;
   //assert(sizeof(st_mips) == sizeof(*st));   //ToDo: and offset of each field !

   int ret = stat(path, &st_mips);
   __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips);

   return ret;
#endif
}

int fstat_portable(int fd, struct stat_portable *st)
{
#if !defined(__mips__)
   struct stat st_orig;
   //assert(sizeof(st_orig) == sizeof(*st));   //ToDo: and offset of each field !

   return fstat(fd, (struct stat*)st);
#else
   struct stat/*_mips*/ st_mips;
   //assert(sizeof(st_mips) == sizeof(*st));   //ToDo: and offset of each field !

   int ret = fstat(fd, &st_mips);
   __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips);

   return ret;
#endif
}

#endif // !__GDK__

// =======================================================================
// General routines
// =======================================================================

void Sys_DebugNumber(int y, int val)
{
}

/*
void Sys_Printf (char *fmt, ...)
{
  va_list		argptr;
  char		text[1024];

  va_start (argptr,fmt);
  vsprintf (text,fmt,argptr);
  va_end (argptr);
  fprintf(stderr, "%s", text);

  Con_Print (text);
}

void Sys_Printf (char *fmt, ...)
{

    va_list     argptr;
    char        text[1024], *t_p;
    int         l, r;

    if (nostdout)
        return;

    va_start (argptr,fmt);
    vsprintf (text,fmt,argptr);
    va_end (argptr);

    l = strlen(text);
    t_p = text;

// make sure everything goes through, even though we are non-blocking
    while (l)
    {
        r = write (1, text, l);
        if (r != l)
            sleep (0);
        if (r > 0)
        {
            t_p += r;
            l -= r;
        }
    }

}
*/

#define USE_PMPEVENT

void Sys_Printf (const char *fmt, ...)
{
  va_list		argptr;
  char		text[2048];
  unsigned char		*p;

  va_start (argptr,fmt);
  vsnprintf (text, sizeof(text), fmt,argptr);
  va_end (argptr);

  text[sizeof(text)-1] = 0;
  LOGI("%s", text);

#ifdef USE_PMPEVENT
    PMPEVENT(("%s", text));
#else
    if (nostdout)
        return;

  for (p = (unsigned char *)text; *p; p++)
    if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
      printf("[%02x]", *p);
    else
      putc(*p, stdout);
#endif
}

qboolean soft_quit;

void Sys_Quit (void)
{
  Host_Shutdown();
#ifdef USE_PMPEVENT
  PMPERROR(("Sys_Quit - exiting."));
#else
  printf("Sys_Quit - exiting.\n");
#endif
    // fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  if (soft_quit) {
    return;
  }
    exit(0);
}

void Sys_Init(void)
{

}

void Sys_Error (const char *error, ...)
{
    va_list     argptr;
    char        string[1024];

// change stdin to non blocking
    // fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);

    va_start (argptr,error);
    vsprintf (string,error,argptr);
    va_end (argptr);
#ifdef USE_PMPEVENT
  PMPERROR(("Error: %s\n", string));
#else
  fprintf(stderr, "Error: %s\n", string);
#endif
  Host_Shutdown ();
#ifdef USE_PMPEVENT
  PMPERROR(("Sys_Error - exiting."));
#else
  printf("Sys_Error - exiting.\n");
#endif
  exit (1);

}

void Sys_Warn (const char *warning, ...)
{
    va_list     argptr;
    char        string[1024];

    va_start (argptr,warning);
    vsprintf (string,warning,argptr);
    va_end (argptr);
#ifdef USE_PMPEVENT
  PMPWARNING(("Warning: %s", string));
#else
  fprintf(stderr, "Warning: %s\n", string);
#endif
}

/*
============
Sys_FileTime

returns -1 if not present
============
*/
int	Sys_FileTime (const char *path)
{
  struct stat_portable buf;

  if (stat_portable(path,&buf) == -1)
    return -1;

  return buf.st_mtime;
}


void Sys_mkdir (const char *path)
{
    mkdir (path, 0777);
}

int Sys_FileOpenRead (const char *path, int *handle)
{
  int	h;
  struct stat_portable fileinfo;

  h = open(path, O_RDONLY, 0666);
  *handle = h;
  if (h == -1)
    return -1;

  if (fstat_portable(h,&fileinfo) == -1)
    Sys_Error ("Error fstating %s", path);

  return fileinfo.st_size;
}

int Sys_FileOpenWrite (const char *path)
{
  int     handle;

  umask (0);

  handle = open(path,O_RDWR | O_CREAT | O_TRUNC
  , 0666);

  if (handle == -1)
    Sys_Error ("Error opening %s: %s", path,strerror(errno_portable()));

  return handle;
}

int Sys_FileWrite (int handle, const void *src, int count)
{
  return write (handle, src, count);
}

void Sys_FileClose (int handle)
{
  close (handle);
}

void Sys_FileSeek (int handle, int position)
{
  lseek (handle, position, SEEK_SET);
}

int Sys_FileRead (int handle, void *dest, int count)
{
    return read (handle, dest, count);
}

void Sys_DebugLog(const char *file, char *fmt, ...)
{
    va_list argptr;
    static char data[1024];
    int fd;

    va_start(argptr, fmt);
    vsprintf(data, fmt, argptr);
    va_end(argptr);
//    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
    fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
    write(fd, data, strlen(data));
    close(fd);
}

void Sys_EditFile(const char *filename)
{

  char cmd[256];
  char *term;
  const char *editor;

  term = getenv("TERM");
  if (term && !strcmp(term, "xterm"))
  {
    editor = getenv("VISUAL");
    if (!editor)
      editor = getenv("EDITOR");
    if (!editor)
      editor = getenv("EDIT");
    if (!editor)
      editor = "vi";
    sprintf(cmd, "xterm -e %s %s", editor, filename);
    system(cmd);
  }

}

double Sys_FloatTime (void)
{
    struct timeval tp;
    struct timezone tzp;
    static int      secbase;

    gettimeofday(&tp, &tzp);

    if (!secbase)
    {
        secbase = tp.tv_sec;
        return tp.tv_usec/1000000.0;
    }

    return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
}

// =======================================================================
// Sleeps for microseconds
// =======================================================================

static volatile int oktogo;

void alarm_handler(int x)
{
  oktogo=1;
}

void Sys_LineRefresh(void)
{
}

void floating_point_exception_handler(int whatever)
{
//	Sys_Warn("floating point exception\n");
  signal(SIGFPE, floating_point_exception_handler);
}

char *Sys_ConsoleInput(void)
{
#if 0
    static char text[256];
    int     len;

  if (cls.state == ca_dedicated) {
    len = read (0, text, sizeof(text));
    if (len < 1)
      return NULL;
    text[len-1] = 0;    // rip off the /n and terminate

    return text;
  }
#endif
  return NULL;
}

#if !id386
void Sys_HighFPPrecision (void)
{
}

void Sys_LowFPPrecision (void)
{
}
#endif

int		skipframes;

// The following APIs are called from the Java activity

double g_oldtime;

extern int scr_width;
extern int scr_height;

qboolean direxists(const char* path)
{
  struct stat_portable sb;
   
  if(stat_portable(path, &sb))
  {
LOGI("direxists 1: path=%s\n", path);
    return 0;	// error
  }
  if(sb.st_mode & S_IFDIR)
  {
LOGI("direxists 2: path=%s\n", path);
     return 1;
  }
LOGI("direxists 3: path=%s\n", path);
  return 0;
}

// Remove all files in path. Recurses into subdirectories

void rmDir(const char* path) {
  DIR* dir = opendir(path);
  if(!dir) {
    return;
  }
  struct dirent * dp;
  while((dp = readdir(dir)) != NULL) {
    const char* name = dp->d_name;
    if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
      continue;
    }
    char filePath[1024];
    if ((int) (sizeof(filePath)-1) < snprintf(filePath, sizeof(filePath), "%s/%s", path, name)) {
      continue; // buffer overflow
    }
    if(direxists(filePath)) {
      rmDir(filePath);
    }
    else {
      unlink(filePath);
    }
  }
  closedir(dir);
  rmdir(path);
}

// Increment this number whenever the data format of any of the files stored in glquake changes:

typedef unsigned long GLCacheVersion;

static const GLCacheVersion kCurrentCacheVersion = 0x3a914000; // The numbers mean nothing special

// #define FORCE_INVALIDATE_CACHE // Useful for testing

#define GLQUAKE_RELPATH "/id1/glquake"
void CheckGLCacheVersion(const char* baseDir)
{
  char cachePath[1024];
  if ((int) (sizeof(cachePath)-1) < snprintf(cachePath, sizeof(cachePath), "%s" GLQUAKE_RELPATH "/cacheversion", baseDir)) {
    return; // buffer overflow
  }
  bool validCache = false;
  {
    GLCacheVersion vernum = 0;
    FILE* f = fopen(cachePath, "rb");
    if (f) {
      if (1 == fread(&vernum, sizeof(vernum), 1, f)) {
        if (vernum == kCurrentCacheVersion) {
          validCache = true;
        }
      }
      fclose(f);
    }
  }

#ifdef FORCE_INVALIDATE_CACHE
  validCache = false;
#endif

  if(!validCache) {
    PMPLOG(("Invalidating glquake cache."));
    char cacheDirPath[1024];
    if ( (int)(sizeof(cacheDirPath)-1) < snprintf(cacheDirPath, sizeof(cacheDirPath), "%s" GLQUAKE_RELPATH, baseDir)) {
      return; // Ran out ot memory
    }
    rmDir(cacheDirPath);
    Sys_mkdir(cacheDirPath);
    FILE* f = fopen(cachePath, "wb");
    if (f) {
      GLCacheVersion vernum = kCurrentCacheVersion;
      fwrite(&vernum, sizeof(vernum), 1, f);
      fclose(f);
    } else {
        PMPLOG(("Could not write %s %d.\n", cachePath, errno_portable()));
    }
  }
}

static int gArgc;
static char** gArgv;

void AndroidInitArgs(int argc, char** argv) {
    gArgc = argc;
    gArgv = argv;
}

static qboolean gDoubleInitializeGuard;
static qboolean gInitialized;
void GL_ReInit();

#if !defined(__GDK__)
bool AndroidInit()
#else
extern "C" bool AndroidInit_LLVM()
#endif
{
  PMPLOG(("AndroidInit"));

  PMPLOG(("This function was compiled on " __DATE__ " at " __TIME__));

  if (gDoubleInitializeGuard && gInitialized)
  {
    GL_ReInit();
  }

  gDoubleInitializeGuard = true;
  return true;
}


// Note: Needs a valid OpenGL context

void AndroidInit2(int width, int height)
{
  PMPLOG(("AndroidInit2 %d,%d", width, height));

  gInitialized = true;
  PMPBEGIN(("AndroidInit2"));
  quakeparms_t parms;
  int j;
  int c = 0;
  const char* v[] = {"quake", (char*) 0};

  scr_width = width;
  scr_height = height;

//	static char cwd[1024];

//	signal(SIGFPE, floating_point_exception_handler);
//  signal(SIGFPE, SIG_IGN);

  memset(&parms, 0, sizeof(parms));

  if (gArgc) {
      COM_InitArgv(gArgc, (const char**) gArgv);
  }
  else {
      COM_InitArgv(c, (const char**) v);
  }

  parms.argc = com_argc;
  parms.argv = com_argv;

  parms.memsize = 16*1024*1024;

  j = COM_CheckParm("-mem");
  if (j)
    parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  parms.membase = malloc (parms.memsize);

  const char* basedir = basedir2;
  if(direxists(basedir1))
  {
    basedir = basedir1;
  }
  else if(direxists(basedir2))
  {
    basedir = basedir2;
  }
  else
  {
    Sys_Error("Could not find data directories %s or %s", basedir1, basedir2);
  }
  parms.basedir = basedir;

  CheckGLCacheVersion(basedir);

// caching is disabled by default, use -cachedir to enable
//	parms.cachedir = cachedir;

#if 0 // FNDELAY not implemented
  noconinput = COM_CheckParm("-noconinput");
  if (!noconinput)
    fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
#endif

  if (COM_CheckParm("-nostdout"))
    nostdout = 1;

  Sys_Init();

    Host_Init(&parms);

    g_oldtime = Sys_FloatTime ();
  PMPEND(("AndroidInit2"));
}

static int currentFrame;
frameTime fastestFrame;
frameTime slowestFrame;

void InitFrameTimes()
{
    currentFrame = 0;
  fastestFrame.time = 1e6;
  fastestFrame.frame = 0;
  slowestFrame.time = -1;
  slowestFrame.frame = 0;
}

static void UpdateFrameTimes(float time)
{
    if (currentFrame > 0) {

    if (fastestFrame.time > time) {
      fastestFrame.time = time;
      fastestFrame.frame = currentFrame;
    }
    if (slowestFrame.time < time) {
      slowestFrame.time = time;
      slowestFrame.frame = currentFrame;
    }
  }
  currentFrame++;
}

int AndroidStepImp(int width, int height)
{
  // PMPBEGIN(("AndroidStep"));
  double time, newtime;
  static int TotalCount = 0;
  static double TotalFPS = 0.0;
  if(!gInitialized)
    AndroidInit2(width, height);

  scr_width = width;
  scr_height = height;

  // find time spent rendering last frame
  newtime = Sys_FloatTime ();
  time = newtime - g_oldtime;

  UpdateFrameTimes(time);
 #if 0
   // Disable the following because given a series of Ti representing time spent per frame
   // 1/(sum(Ti)/n) isn't quite the same as sum(1/Ti)/n, especially when Ti has large variance.
   // See LOGI in host.cpp::Host_Frame for better implementation
  double fps = 1.0/time;
  if (fps > 0.0 && fps < 200.0) { // Sometimes it
    TotalCount += 1;
    TotalFPS += fps;
    LOGI("Quake fps: %3.2lf, Average: %3.2lf", fps, TotalFPS/TotalCount);
  }
 #endif
  Host_Frame(time);
  g_oldtime = newtime;
  // PMPEND(("AndroidStep"));
  return key_dest == key_game;
}

#if !defined(__GDK__)
int AndroidStep(int width, int height)
#else
extern "C" int AndroidStep_LLVM(int width, int height)
#endif
{
  for(;;) {
    host_framethrottled = false;
    int result = AndroidStepImp(width, height);
    if (!host_framethrottled) {
        return result;
    }
    usleep(1000);
    //LOGI("%s", "host_framethrottled");
  }
}

extern void Host_Quit();


#if !defined(__GDK__)
void AndroidQuit() {
#else
extern "C" void AndroidQuit_LLVM() {
#endif
  soft_quit = true;
  Host_Quit();
  soft_quit = false; // In case we live on after returning.
}