File: nm_linux_b.c

package info (click to toggle)
waba 1.5-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 1,996 kB
  • ctags: 3,231
  • sloc: ansic: 17,303; java: 4,436; sh: 2,345; makefile: 417
file content (822 lines) | stat: -rw-r--r-- 19,603 bytes parent folder | download
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
/*
Copyright (C) 1998, 1999, 2000 Wabasoft

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., 675 Mass Ave,
Cambridge, MA 02139, USA. 
*/

/*
Copyright (C) 2000, 2001 SMARTDATA, http://www.smartdata.ch/

More help for porting is include in the file nmport_b.c. Please read it before
porting or modify this port.

Linux port.
===========

*/

#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "../waba.h"

WObject globalMainWin = 0;

/* the size/properties of the MainWindow */
int32 hwr_bpp    =  32;
int32 hwr_width  = 160;
int32 hwr_height = 160;

static int32 globalTimerInterval = 0;
static int32 globalTimerStart = 0;
static int classDbCount = 0;
static ULong appCreatorId;
static Word globalSocketLibRefNum;
static int globalNetState = NET_NOT_READY_FOR_OPEN;

/*************************************/

#ifdef QUICKBIND
static int32 postEventMethodMapNum = -1;
static int32 onTimerTickMethodMapNum = -1;
#endif

int isApplication = FALSE;

HELPER void * ui_init() {

  void *forReturn;

  if( ! is_ui_inited ) {
    forReturn = hwr_init();
    input_init(NULL);
    is_ui_inited = 1;
    return forReturn;

  } else {

    return NULL;

  }
}

HELPER void ui_exit(int code)
{
  if (is_ui_inited) {
    input_release();
    hwr_release();
  }
  is_ui_inited = 0;
  exit(code);
}

#ifdef WITH_THREAD

//*** Isao's Multithread implementation START ***
// Thread variables - Isao F. Yamashita 07/25/2000
//*** This "_onThreadStart()" method is called from "start()" method.
Var ThreadStart(Var stack[]) {
}

//*** This "_onThreadStop()" method is called from "stop()" method.
Var ThreadStop(Var stack[]) {
}

//*** This "_onThreadSleep()" method is called from "sleep()" method.
Var ThreadSleep(Var stack[]) {
}



//*** This "_onThreadWait()" method is called from "wait()" method.
Var ThreadWaitForSignal(Var stack[]) {
}

Var ThreadSignalAll(Var stack[]) {
}

//*** Isao's Multithread implementation END ***

#endif

HELPER int32 getTimeStamp()
{ // SD
  struct timeval now;
  static uint32 maxStamp = 1 << 30;

  (void)gettimeofday(&now, (struct timezone *)0);
  return ( (now.tv_sec * 1000) + (now.tv_usec / 1000) ) % maxStamp;
}



void drawErrorWin()
{ // SD
  char buf[256];
  int len;
  char *msg;

  extern char *errorMessages[];

  printf("An error was found in the program being\n"
	 "run by the WabaVM.\n\n"
	 "Error: ");

  msg = errorMessages[vmStatus.errNum - 1];
  if (msg)
    printf("%s ", msg);

  msg = vmStatus.arg1;
  if (msg[0])
    printf("%s ", msg);

  msg = vmStatus.arg2;
  if (msg[0])
    printf("%s ", msg);

  if (vmStatus.className[0] || vmStatus.methodName[0])
    printf("in\n");

  msg = vmStatus.className;
  if (msg[0])
    printf("%s", msg);

  msg = vmStatus.methodName;
  if (msg[0])
    printf(".%s\n", msg);

  if (vmStatus.errNum != ERR_CantAccessCoreClasses) {
    printf("Please notify the program's author.\n");
    if (isApplication)
      printf("(Or maybe it is a MainWindow application ? "
	     "try without the -a option)\n");
    else
      printf("(Or maybe it is a console application ? "
	     "try with the -a option)\n");
  }
  else
    printf("The WabaVM is not fully installed.\n");

  //PostQuitMessage(0);
  printf("*** EXITTING ***\n");
  ui_exit(1);
}


void drawMainWin()
{ // SD
  WObject mainWinObj;
  WClass *vclass;
  WClassMethod *method;
  Var params[7];

  mainWinObj = globalMainWin;
  if (!mainWinObj)
    return;
  //WinEraseWindow();
  vclass = WOBJ_class(mainWinObj); // get runtime class
  method = getMethod(vclass, createUtfString("_doPaint"),
		     createUtfString("(IIII)V"), &vclass);
  if (method == NULL)
    return;
  params[0].obj = mainWinObj;
  params[1].intValue = 0; // x
  params[2].intValue = 0; // y
  params[3].intValue = hwr_width; // width
  params[4].intValue = hwr_height; // height

  executeMethod(vclass, method, params, 5);
}


HELPER void timerCheck()
{ // SD
  WObject mainWinObj;
  int32 now, diff;
  WClass *vclass;
  WClassMethod *method;
  Var params[1];
  printf( "...\n" );
  mainWinObj = globalMainWin;
  if (!mainWinObj)
    return;
  now = getTimeStamp();
  diff = now - globalTimerStart;
  if (diff < 0)
    diff += (1L << 30); // max stamp is (1 << 30)
  if (diff < globalTimerInterval)
    return;
  vclass = WOBJ_class(mainWinObj); // get runtime class
#ifdef QUICKBIND
  method = getMethodByMapNum(vclass, &vclass,
			     (uint16)onTimerTickMethodMapNum);
#else
  method = getMethod(vclass, createUtfString("_onTimerTick"),
		     createUtfString("()V"), &vclass);
#endif
  if (method != NULL)
    {
      params[0].obj = mainWinObj;
      executeMethod(vclass, method, params, 1);
    }
  globalTimerStart = now;
}


int handleMainWinEvent(struct ui_Event *event)
{
  WObject mainWinObj;
  WClass *vclass;
  WClassMethod *method;
  Var params[7];
  int quit = 0;
  int32 type, x, y;

  mainWinObj = globalMainWin;
  if (!mainWinObj)
    return;

  switch (event->type) {
  case quitEvent:
    DPUTS("EVT: Quit");
    quit = 1;
    break;

  case penUpEvent:
  case penDownEvent:
  case penMoveEvent: {

    x = event->x;
    y = event->y;

    if (event->type == penDownEvent) {

      type = WABA_EVENT_PEN_DOWN;

    } else if (event->type == penUpEvent)
      type = WABA_EVENT_PEN_UP;
    else
      type = WABA_EVENT_PEN_MOVE;

    vclass = WOBJ_class(mainWinObj); // get runtime class
#ifdef QUICKBIND
    method = getMethodByMapNum(vclass, &vclass,
			       (uint16)postEventMethodMapNum);
#else
    method = getMethod(vclass, createUtfString("_postEvent"),
		       createUtfString("(IIIIII)V"), &vclass);
#endif
    if (method != NULL) {
      params[0].obj = mainWinObj;
      params[1].intValue = type; // type
      params[2].intValue = 0; // key
      params[3].intValue = x; // x
      params[4].intValue = y; // y
      params[5].intValue = 0; // modifiers
      params[6].intValue = 0; // timeStamp
      executeMethod(vclass, method, params, 7);
    }
    break;
  }
  case keyDownEvent:
  case keyUpEvent:
    if( event->type == keyUpEvent ) {

      DPUTS("EVT: KEY_RELEASE -> NOT USED");

    } else  {

    int32 type, key, x, y;
    Word chr;

    x = event->x;
    y = event->y;

    type = WABA_EVENT_KEY_PRESS;

    key = ui_TranslateKey( event->code );

    vclass = WOBJ_class(mainWinObj); // get runtime class
#ifdef QUICKBIND
    method = getMethodByMapNum(vclass, &vclass,
			       (uint16)postEventMethodMapNum);
#else
    method = getMethod(vclass, createUtfString("_postEvent"),
		       createUtfString("(IIIIII)V"), &vclass);
#endif
    if (method != NULL) {
      params[0].obj = mainWinObj;
      params[1].intValue = type; // type
      params[2].intValue = key; // key
      params[3].intValue = 0; // x
      params[4].intValue = 0; // y
      params[5].intValue = 0; // modifiers
      params[6].intValue = 0; // timeStamp
      executeMethod(vclass, method, params, 7);
    }
    break;
  }

  case timerEvent: {
    int32 type, key, x, y;
    Word chr;

    x = event->x;
    y = event->y;

    type = 111; // KeyEvent.KEY_PRESS  AAAAAAAAAA UPDATER AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    chr = event->code;
    key = 0;

    // NOTE: This routine will only get called for main windows since
    // only those call SetTimer()

    vclass = WOBJ_class(mainWinObj); // get runtime class
#ifdef QUICKBIND
    method = getMethodByMapNum( vclass, &vclass,
				(uint16)onTimerTickMethodMapNum );
#else
    method = getMethod(vclass, createUtfString("_onTimerTick"),
		       createUtfString("()V"), &vclass);
#endif
    if (method != NULL) {
      params[0].obj = mainWinObj;
      params[1].intValue = type; // type
      params[2].intValue = key; // key
      params[3].intValue = 0; // x
      params[4].intValue = 0; // y
      params[5].intValue = 0; // modifiers
      params[6].intValue = 0; // timeStamp
      executeMethod(vclass, method, params, 7);
    }
    break;
  }

  default:
    DPRINTF( "??? EVENT NOT TREATED (%d) !!!\n", event->type );
    break;
  }

  return quit;
}


void usage(void)
{
  printf("usage: waba [options] [classfile [args]]\n\n");
  printf("Options are:\n");
  printf("  -h, --help                    Display help and exit\n");
  printf("  -a, --as-application          Run void main(String[] args)\n");
  printf("                                  "
	 "instead of void MainWindow.onStart()\n");
  printf("  -x, --width                   Set horizontal size of the Main Window\n");
  printf("                                  "
	 "default to %d\n", hwr_width);
  printf("  -y, --height                  Set vertical size of the Main Window\n");
  printf("                                  "
	 "default to %d\n", hwr_height);
  printf("  -l, --class-heap-size <num>   Set class heap size\n");
  printf("  -m, --object-heap-size <num>  Set object heap size\n");
  printf("  -s, --vm-stack-size <num>     Set vm stack size\n");
  printf("  -t, --nm-stack-size <num>     Set native methods stack size\n");
}

WObject startApp(int argc, char** argv)
{
  char *className;
  ULong vmStackSize, nmStackSize, classHeapSize, objectHeapSize;
  int i, j, n;
  int appArgc = 0;
  char** appArgv;
  char *cp;
  char *p;

  // defaults
  isApplication = false;
  vmStackSize = 1500;
  nmStackSize = 300;
  classHeapSize = 14000;
  objectHeapSize = 8000;

  // NOTE: We need VoidHand to fall on a byte boundry for our xmalloc()
  // routine to work correctly (see xmalloc())
  if (sizeof(VoidHand) % 4 != 0) {
    VmQuickError(ERR_SanityCheckFailed);
    return 0;
  }

  // parse options
  i = 1;
  while (i<argc && argv[i][0]=='-') {
    int hasArg = i+1<argc;
    int hasNArg = hasArg && (argv[i+1][0]>='0' && argv[i+1][0]<='9');
    ULong val = hasNArg ? atoi(argv[i+1]) : 0;
    int argl = xstrlen(argv[i]);

    if ((!xstrncmp(argv[i],"-l",argl) ||
	 !xstrncmp(argv[i],"--class-heap-size",argl))
	&& hasNArg) {
      classHeapSize = val;
      ++i;
    }
    else if ((!xstrncmp(argv[i],"-m",argl) ||
	      !xstrncmp(argv[i],"--object-heap-size",argl))
	     && hasNArg) {
      objectHeapSize = val;
      ++i;
    }
    else if ((!xstrncmp(argv[i],"-s",argl) ||
	      !xstrncmp(argv[i],"--vm-stack-size",argl))
	     && hasNArg) {
      vmStackSize = val;
      ++i;
    }
    else if ((!xstrncmp(argv[i],"-t",argl) ||
	      !xstrncmp(argv[i],"--nm-stack-size",argl))
	     && hasNArg) {
      nmStackSize = val;
      ++i;
    }
    else if (!xstrncmp(argv[i],"-a",argl) ||
	     !xstrncmp(argv[i],"--as-application",argl)) {
      isApplication = true;
    }
    else if ((!xstrncmp(argv[i],"-x",argl) ||
	      !xstrncmp(argv[i],"--width",argl))
	     && hasNArg) {
      hwr_width = val;
      ++i;
    }
    else if ((!xstrncmp(argv[i],"-y",argl) ||
	      !xstrncmp(argv[i],"--height",argl))
	     && hasNArg) {
      hwr_height = val;
      ++i;
    }
    else if (!xstrncmp(argv[i],"-h",argl) ||
	     !xstrncmp(argv[i],"--help",argl)) {
      usage();
      exit(0);
    }
    else {
      printf("waba: bad option '%s'\n\n", argv[i]); 
      usage();
      exit(1);
    }
    ++i;
  }
  
  if (i<argc) {
    className = argv[i];
    appArgc = argc-i-1;
    appArgv = &argv[i+1];
  }
  else {
#if 0
    if(isApplication) {
      printf("waba: isApplication option requires a classfile name\n\n"); 
      usage();
      exit(1);
    }
#endif
    className = "waba/ui/Welcome";
  }

  cp = (char*)getenv("CLASSPATH");
  if((cp==NULL)||(strlen(cp)==0)){
    cp = ".:/usr/share/waba/classes";
  }
  // This one also gets freed by the system:
  classpath = xmalloc((cp?strlen(cp):0) + strlen(className) + 100);
  if (classpath==0) {
    printf("*** Memory error ***\n");
    exit(1);
  }
  *classpath = '\0';

  // Put dir of invoked class in path
  strcat(classpath, className);
  p = (char*)strrchr(classpath, '/');
  if (p)
    *p = 0;
  strcat(classpath, ":");

  // add CLASSPATH
  if (cp && strlen(cp)) {
    strcat(classpath, cp);
    strcat(classpath, ":");
  }
  else {
    // CLASSPATH unset, try a relative location
    strcat(classpath, "classes;");
  }

  DPRINTF("class:     [%s]\n", className);
  DPRINTF("classpath: [%s]\n", classpath);
  DPRINTF("vmStackSize    = %d\n", vmStackSize);
  DPRINTF("nmStackSize    = %d\n", nmStackSize);
  DPRINTF("classHeapSize  = %d\n", classHeapSize);
  DPRINTF("objectHeapSize = %d\n", objectHeapSize);
  DPRINTF("isApplication  = %d\n", isApplication);
  DPRINTF("arguments      = %d\n", appArgc);

#ifdef DEBUG
    if (appArgc) {
      int j;
      for (j=0; j<appArgc; ++j) {
	DPRINTF("  v[%d] = \"%s\"\n", j, appArgv[j]);
      }
    }
#endif

  DPRINTF("\n");

  DPRINTF("startApp(): calling VMInit\n");
  VmInit(vmStackSize, nmStackSize, classHeapSize, objectHeapSize);

  if (isApplication) {
    DPRINTF("startApp(): calling VmStartApplication\n");
    VmStartApplication(className, appArgc, appArgv);
    return 0;
  }
  else {
    DPRINTF("startApp(): calling VmStartApp\n");
    return  VmStartApp(className);
  }
}

void stopApp(WObject mainWinObj)
{
  uint32 i;

  VmStopApp(mainWinObj);
  VmFree();

  // free memory mapped files
  /****
  for (i = 0; i < numMemFiles; i++)
    freeMemMapFile(&memFiles[i]);
  ***/
}




uchar *loadFromMem(char *path, uint32 pathLen, uint32 *size)
{
  /***********************************
  uchar *baseP, *offP, *p;
  uint32 i, off, nextOff, top, bot, mid;
  uint32 nameLen, minLen, numRecs;
  int cmp;

  // look in memory mapped files
  for (i = 0; i < numMemFiles; i++) {
    baseP = memFiles[i].ptr;
    numRecs = getUInt32(baseP + 4);
    if (numRecs == 0)
      continue;
    // NOTE: We do a binary search to find the class. So, a search
    // for N classes occurs in O(nlogn) time.
    top = 0;
    bot = numRecs;
    while (1) {
      mid = (bot + top) / 2;
      offP = baseP + 8 + (mid * 4);
      off = getUInt32(offP);
      p = baseP + off;
      nameLen = getUInt16(p);
      p += 2;
      if (pathLen > nameLen)
	minLen = nameLen;
      else
	minLen = pathLen;
      cmp = xstrncmp(path, p, minLen);
      if (!cmp) {
	if (pathLen == nameLen) {
	  if (size != NULL) {
	    nextOff = getUInt32(offP + 4);
	    *size = nextOff - off - nameLen - 2;
	  }
	  return p + nameLen;
	}
	if (pathLen > nameLen)
	  cmp = 1;
	else
	  cmp = -1;
      }
      if (mid == top)
	break; // not found
      if (cmp < 0)
	bot = mid;
      else
	top = mid;
    }
  }
  ******************************/
  return NULL;
}

uchar *readFileIntoMemory(char *path, int nullTerminate, uint32 *size)
{
  int fileDesc;
  ssize_t lenRead, len;
  struct stat st;
  uchar *p;

  //DPUTS("readFileIntoMemory()");
  //DPRINTF("path: [%s]\n", path);

  if (nullTerminate != 0)
    nullTerminate = 1;

  if (stat(path, &st) != 0) {
    return NULL;
  }

  fileDesc = open(path, O_RDONLY);
  if (fileDesc == -1)
    return NULL;

  len = st.st_size;

  if (size)
    *size = len + nullTerminate;

  p = (uchar *)xmalloc(len + nullTerminate);

  if (p != NULL) {
    lenRead = read(fileDesc, p, len);
    if (len != lenRead) {
      xfree(p);
      p = NULL;
    }
  }
  else
    VmQuickError(ERR_CantAllocateMemory);

  if (p && nullTerminate)
    p[len] = 0;

  close(fileDesc);

  //DPRINTF("readFileIntoMemory(): returning %p\n", p);
  return p;
}


uchar *nativeLoadClass(UtfString className, uint32 *size)
{
  uchar *p;
  uint16 len, i;
  char path[128];

  // try loading from memory mapped files first
  // make full path by appending .class
  len = className.len + 6;
  if (len > 128)
    return NULL;
  xstrncpy(path, className.str, className.len);
  xstrncpy(&path[className.len], ".class", 6);

  p = loadFromMem(path, len, size);
  if (p != NULL)
    return p;

  // not found in memory mapped files, try loading it from the CLASSPATH
  if (numClassPaths == -1) {
    char *s, *sp;
    
    classPaths[0] = ".";
    numClassPaths = 1;
    s = classpath; //(char*)getenv("CLASSPATH");
    if (s != NULL) {
      // NOTE: we duplicate the CLASSPATH here since strtok() modifies
      // it but we never explicitly free it, we let the OS free it when
      // the program exits. Also note we don't need to deal with UNICODE
      // since this section does not applicable to WinCE
      i = xstrlen(s);
      sp = (char*)xmalloc(i + 1);
      xstrncpy(sp, s, i);
      sp[i] = 0;
      s = sp;
      
      // parse through the elements of CLASSPATH
      sp = (char*)strtok(s, ":");
      while (sp != NULL) {
	classPaths[numClassPaths++] = sp;
	if (numClassPaths == MAX_CLASSPATHS)
	  break;
	sp = (char*)strtok(NULL, ":");
      }
    }
  }

  // NOTE: we never free the memory pointers we allocate here. We let the
  // OS clean up memory when the process exits. This works well but if we
  // ever do free these pointers, we need to make sure we differentiate
  // them from the memory mapped file pointers (nativeLoadClassFromMem())
  for (i = 0; i < numClassPaths; i++) {
    strcpy(path, classPaths[i]);
    if (path[strlen(path)-1] != '/')
      strcat(path, "/");
    len = strlen(path); 
    strncpy(&path[len], className.str, (size_t)className.len);
    path[len+className.len] = 0; // because className.str not null-terminated
    len = strlen(path);

    if (len>6 && strcmp(&path[len-6], ".class")!=0 )
      strcat(path, ".class");

    p = readFileIntoMemory(path, 0, size);
    if (p != NULL)
      break;

  }
  if (p == NULL)
    return NULL;
  // validate
  if (getUInt32(p) != (uint32)0xCAFEBABE) {
    VmError(ERR_BadClass, NULL, &className, NULL);
    xfree(p);
    return NULL;
  }

  //DPRINTF("nativeLoadClass(): returning %p\n", p);
  return p;
}



PUBLIC int main(int argc, char** argv)
{
  WObject mainWinObj;
  unsigned short err;


  // NOTE: set vmStatus to 0 since we may not call VmInit() if there
  // is an device compatibility error or error accessing a class database
  xmemzero((uchar *)&vmStatus, sizeof(vmStatus));

  mainWinObj = startApp(argc, argv);

#ifdef QUICKBIND
  if (mainWinObj != 0) {
      WClass *vclass;

      // cache method map numbers for commonly called methods
      vclass = WOBJ_class(mainWinObj);
      postEventMethodMapNum =
	getMethodMapNum(vclass, createUtfString("_postEvent"),
			createUtfString("(IIIIII)V"), SEARCH_ALL);
      onTimerTickMethodMapNum =
	getMethodMapNum(vclass, createUtfString("_onTimerTick"),
			createUtfString("()V"), SEARCH_ALL);
      if (postEventMethodMapNum == -1 || onTimerTickMethodMapNum == -1)
	mainWinObj = 0;
    }
#endif

  if (!isApplication && mainWinObj == 0)
    VmQuickError(ERR_CantAccessAppClasses);

  // NOTE: we don't get any OS level window repaint events after the program has
  // started because PalmOS uses save-unders when it pops up windows on top of
  // this one (for alerts, launch win, etc.)
  if (vmStatus.errNum == 0)
    drawMainWin();
  else
    drawErrorWin();

  globalNetState = NET_READY_FOR_OPEN;
  if (!isApplication) {

    ui_MainLoop();

  }
  stopApp(mainWinObj);

  if (globalNetState == NET_IS_OPEN)
    ; // TODO: NetLibClose(globalSocketLibRefNum, false);

  ui_exit(0);
}

/*
   Local Variables:
   c-file-style: "smartdata"
   End:
*/