Thread: [Waba-commits] CVS: waba/src/port/c nmport_a.c,NONE,1.1 nmport_a.h,NONE,1.1 nmport_b.c,NONE,1.1 nmpo
Status: Abandoned
Brought to you by:
bornet
From: Manfred R. <mr...@us...> - 2004-07-10 15:30:51
|
Update of /cvsroot/waba/waba/src/port/c In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10313/src/port/c Added Files: nmport_a.c nmport_a.h nmport_b.c nmport_b.h nmport_c.c nmport_c.h Log Message: Moved sources to new location --- NEW FILE: nmport_a.c --- /* $Id: nmport_a.c,v 1.1 2004/07/10 15:30:41 mriem Exp $ 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. */ /* This file is modified by Monaka (mo...@mo...) */ /* This file is the starting point when porting the WabaVM to a new platform. To port the WabaVM, you need to fill in platform dependent code in 3 files: - nm<platform>_a.c - nm<platform>_b.c - nm<platform>_c.c The nm stands for 'native methods'. The 3 files contain: - code to deal with platform-specific type sizes and byte order - code to allocate a few blocks of memory - code to load a class from memory or disk - the main application loop - native functions This file contains the code to deal with platform-specific types and allocation of blocks of memory. To port the VM, read though the comments and look for the 'WHEN PORTING' comments and fill in what you need to. You'll probably want to take a look at waba.c as you go along to see where the code is used. A WabaVM is comprised of the 3 native function files and the VM code itself contained in waba.c */ // WHEN PORTING: define FREE_ON_EXIT if you want all object destructors // to be called when the program exits. Under OS's that release system // resources automatically, you don't need to do this. On OS's that don't // automatically release system resources (images, memory, etc.) that // native functions may allocate, you should set this to 1 so when // the waba program exits, all native method object destructors get called // to clean things up #define FREE_ON_EXIT 1 // WHEN PORTING: You need to define the basic types of uchar, int32, etc. // for the platform being ported to as follows: // // - uchar is an 8 bit unsigned value // - int32 is a signed 32 bit value // - uint32 is an unsigned 32 bit value // - float32 is a signed 32 bit floating point value // - int16 is a signed 16 bit value // - uint16 is an unsigned 16 bit value // // below is an example from PalmOS /* all the needed include */ #include "../waba.h" // // type converters // #ifdef WITH_64BITS // get one 64 float value from buf and return it float64 getFloat64bits(uchar *buf) { float64HiLo fhl; fhl.ul.lo = getInt32( buf ); fhl.ul.hi = getInt32( buf + 4 ); return fhl.value; } // get one 64 float value from buf and return it int64 getInt64bits(uchar *buf) { int64HiLo ihl; ihl.ul.lo = getInt32( buf ); ihl.ul.hi = getInt32( buf + 4 ); return ihl.value; } #endif /* WITH_64BITS */ float32 getFloat32(uchar *buf) { uint32 i; float32 f; // we need to make sure we're aligned before casting i = ((uint32)buf[0] << 24) | ((uint32)buf[1] << 16) | ((uint32)buf[2] << 8) | (uint32)buf[3]; f = *((float32 *)&i); return f; } --- NEW FILE: nmport_a.h --- /* $Id: nmport_a.h,v 1.1 2004/07/10 15:30:41 mriem Exp $ 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. */ /* This file is modified by Monaka (mo...@mo...) */ #undef FREE_ON_EXIT #undef SECURE_CLASS_HEAP #define LOCK_CLASS_HEAP #define UNLOCK_CLASS_HEAP #define uchar unsigned char #define int32 long #define uint32 unsigned long #define float32 float #define int16 short #define uint16 unsigned short #ifdef WITH_64BITS // - int64 is a signed 64 bit value #define int64 long long // - uint64 is an unsigned 64 bit value #define uint64 unsigned long long // - float64 is a signed 64 bit floating point value #define float64 double // --- define a struct for double and long support in two Var ... // ... the hi/lo decomposition typedef struct { uint32 lo; uint32 hi; } HiLoDoubleOrLong; //... for the double typedef union { float64 value; HiLoDoubleOrLong ul; } float64HiLo; //... for the long typedef union { int64 value; HiLoDoubleOrLong ul; } int64HiLo; #endif /* WITH_64BITS */ // WHEN PORTING: You need to define functions that convert a string // of 2 or 4 bytes in network byte order to a 16 or 32 bit value as follows: // // getUInt32() - converts 4 bytes to a unsigned 32 bit integer // getUInt16() - converts 2 bytes to a unsigned 16 bit integer // getInt32() - converts 4 bytes to a signed 32 bit integer // getInt16() - converts 2 bytes to a signed 16 bit integer // // Here is an example from PalmOS, you can make these functions instead of // a macro if you want the executable to be smaller #define getUInt32(b) (uint32)( (uint32)((b)[0])<<24 | (uint32)((b)[1])<<16 | (uint32)((b)[2])<<8 | (uint32)((b)[3]) ) #define getUInt16(b) (uint16)(((b)[0]<<8)|(b)[1]) #define getInt32(b) (int32)( (uint32)((b)[0])<<24 | (uint32)((b)[1])<<16 | (uint32)((b)[2])<<8 | (uint32)((b)[3]) ) #define getInt16(b) (int16)(((b)[0]<<8)|(b)[1]) #ifdef WITH_64BITS // WHEN PORTING: You need to define functions that convert a string // of 8 bytes in network byte order to a 64 bit value as follows: // get one 8byte float value from buf and return it float64 getFloat64bits(uchar *buf); // get one 8byte int value from buf and return it int64 getInt64bits(uchar *buf); #endif /* WITH_64BITS */ // WHEN PORTING: You need to define a function that converts 4 bytes in // network byte order to a 32 bit floating point value // // Here is an example from PalmOS: static float32 getFloat32(uchar *buf) { uint32 i; float32 f; // we need to make sure we're aligned before casting i = ((uint32)buf[0] << 24) | ((uint32)buf[1] << 16) | ((uint32)buf[2] << 8) | (uint32)buf[3]; f = *((float32 *)&i); return f; } // WHEN PORTING: You need to define the following functions: // // xstrncmp(s1, s2, n) - compares 2 strings n bytes up to a 0 terminator // xstrncpy(s1, s2, n) - copies 2 strings n bytes up to a 0 terminator // xstrlen(s) - returns the length of a string // xstrcat(dst, src) - concats one string to another // xmemmove(dst, src, size) - copies size bytes from src to dst // xmemzero(mem, len) - zeros out len bytes at location mem // // You could find the native functions that map to the above functions // or (easier) just use the ones below. In any case, its probably best // to use the ones below first and then you switch out after things // are running in case the machines functions don't do exactly what // is expected. static int xstrncmp(char *s1, char *s2, uint32 n) { uint32 i; for (i = 0; i < n; i++) if (s1[i] != s2[i]) return 1; return 0; } static void xstrncpy(char *dst, char *src, uint32 n) { uint32 i; for (i = 0; i < n; i++) dst[i] = src[i]; } static uint32 xstrlen(char *s) { uint32 len; len = 0; while (*s != 0) { len++; s++; } return len; } static void xstrcat(char *dst, char *src) { while (*dst != 0) dst++; while (*src != 0) *dst++ = *src++; *dst = 0; } static void xmemmove(uchar *dst, uchar *src, uint32 size) { uint32 i; for (i = 0; i < size; i++) dst[i] = src[i]; } static void xmemzero(uchar *mem, uint32 len) { uint32 i; for (i = 0; i < len; i++) mem[i] = 0; } // WHEN PORTING: You need to define a function that allocates a block // of memory and a function that frees a block of memory. The WabaVM // allocates 4 blocks of memory when it starts up and these methods // are only used to allocate and free those 4 blocks of memory. When // the VM is running, it does not call these functions. So, if you have // a processor without any heap management, it should be rather easy // to get the VM running. Just allocate 4 blocks of memory for it and // return pointers to them below. You don't need a heap based malloc() // implementation or anything to run with the WabaVM. The VM does its // own memory management within the 4 blocks. // // The two functions you need to implement are: // // xmalloc(size) - allocates size bytes of memory and returns a pointer // xfree(ptr) - frees the memory allocated previously // // On platforms that have malloc() and free, we can simply use them as // follows: #define xmalloc(size) malloc(size); #define xfree(ptr) free(ptr) // WHEN PORTING: That's it for the types and memory allocation, the next // step is to implement the class loader and native functions in the // nm<platform>_b.c and nm<platform>_c.c files --- NEW FILE: nmport_b.c --- /* $Id: nmport_b.c,v 1.1 2004/07/10 15:30:41 mriem Exp $ 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. */ /* If you're looking here, you've probably already looked at (or worked on) the nm<platform>_a.c file. This file is the second step in porting the VM. In it, you need to place the main application loop and class loader. If you get through this, you'll have a basic VM running and from there you can implement the native functions so you can actually see something on the screen. The WabaVM, like other applications, needs some type of main application loop where the OS sends it key clicks, mouse presses, etc. The VM also needs to load classes from memory or disk or wherever. Different platforms have different main application loops. For example, under Win32, a program has a WinMain() function. Under PalmOS, applications have a PilotMain() function. You'll want to implement a main application loop for the platform you are porting to. Then, when things start up (initialization), you need to: - parse the launch command and then call VmInit() - call VmStartApp passing the class name of the program's main window You'll also need to figure out how programs will launch the VM. The VM is normally passed parameters telling it what class heap, class name, etc. to use. It's usually another program that does this but you could do it by building a waba launcher that draws a tray of icons and when you click one, it runs the WabaVM with various parameters as an example of something different. Whatever you do for this is platform-specific. Just before the program exits, you'll should call VmStopApp(mainWinObj); That's it for initialization and exit, then you need to do some work to hook up key presses, mouse presses, etc. to the VM. When a key is pressed, you'll want to invoke the main window's onEvent() method. Here's an example of how to do that: mainWinObj = globalMainWin; if (!mainWinObj) return; vclass = WOBJ_class(mainWinObj); // get runtime class method = getMethod(vclass, createUtfString("_postEvent"), createUtfString("(IIIIII)V"), &vclass); 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); } This gets the runtime class of the main window object, gets a reference to the _postEvent method, fills in an array of parameters to pass and then calls executeMethod() to execute the method. There is also a quickbind version of this which is quicker than the above. You can look at nmpalm_b.c or nmwin32_b.c to get an idea how that works (look for the #ifdef QUICKBIND) but you should start without using the quickbind method. You'll want to hook up mouse or pen click, key presses, calls from the OS to repaint the window (which should call the main window's _doPaint routine). Overall, you need to make sure the following main window functions get called: _onTimerTick() _postEvent() for key presses and mouse/pen clicks and moves You'll need to map special keys (escape, etc.) if the device has them to suitable numbers for waba. To do this, you can see how the other native implementations do key mapping (a big switch statement). The _onTimerTick() one can be tricky for devices that don't have a built in timer (like PalmOS). When using one of those devices, copy the code from the nmpalm_b.c to deal with timers. Basically, this consists of getting the current time and looking for an event up until the time the next timer period expires. The VM only needs a single timer, the waba core classes have the code to support multiple timers even though the underlying system only supports a single one. The last code you need to implement is: nativeLoadClass() This is the function that load a class from memory, disk or whatever. It looks like this: static uchar *nativeLoadClass(UtfString className, uint32 *size) and it is passed a class name (not zero terminated, the UtfString contains the len in: className.len and it returns a pointer to the class in memory after it is loaded and fills in the *size variable with the size of the class in bytes. The classes loaded are classes that are compiled by a java compiler or other compiler that can generate code in the subset of java bytecode that waba supports. */ /* all the needed include */ #include "../waba.h" #ifdef WITH_THREAD /* PORT: If you wanna use thread, you need to define function follows * so that they are required by waba.c */ //*** This method is called from "start()" method. Var ThreadStart(Var stack[]) { } //*** This method is called from "stop()" method. Var ThreadStop(Var stack[]) { } //*** This method is called from "sleep()" method. Var ThreadSleep(Var stack[]) { } //*** This method is called from "wait()" method. Var ThreadWaitForSignal(Var stack[]) { } Var ThreadSignalAll(Var stack[]) { } //*** This "ThreadCurrentThread()" method is called from "curentThread()" method. Var ThreadCurrentThread(Var stack[]) { Var v; v.obj = 0; return v; } void tickThread(void) { } int registerAsSyncronized(WClassMethod* method) { } void exitFromSyncronized(WClassMethod* method) { } int enterMonitor(WObject obj) { } int exitMonitor(WObject obj) { } #endif --- NEW FILE: nmport_b.h --- /* $Id: nmport_b.h,v 1.1 2004/07/10 15:30:41 mriem Exp $ */ #ifdef WITH_THREAD /* PORT: the required functions by waba.c are follows. */ //*** Isao's Multithread implementation START *** //*** 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 *** //*** This "ThreadCurrentThread()" method is called from "curentThread()" method. extern Var ThreadCurrentThread(Var stack[]); void tickThread(void); int registerAsSyncronized(WClassMethod* method); void exitFromSyncronized(WClassMethod* method); int enterMonitor(WObject obj); int exitMonitor(WObject obj); #endif /* WITH_THREAD */ --- NEW FILE: nmport_c.c --- /* $Id: nmport_c.c,v 1.1 2004/07/10 15:30:41 mriem Exp $ 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. */ /* This file is modified by Monaka (mon...@mo...) */ #ifdef COMMENT [...992 lines suppressed...] v.obj = 0; return v; } static Var VmSetDeviceAutoOff(Var stack[]) { Var v; v.obj = 0; return v; } static Var VmGetUserName(Var stack[]) { Var v; v.obj = 0; return v; } --- NEW FILE: nmport_c.h --- /* $Id: nmport_c.h,v 1.1 2004/07/10 15:30:41 mriem Exp $ */ #ifndef __NMPORT_C_H__ #define __NMPORT_C_H__ /* 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. */ static void WindowDestroy(WObject obj); static void MainWinDestroy(WObject obj); static void GraphicsDestroy(WObject obj); static void ImageDestroy(WObject obj); #if WITH_CATALOG_CLASS static void CatalogDestroy(WObject obj); #endif static void FileDestroy(WObject obj); static void SocketDestroy(WObject obj); static void SerialPortDestroy(WObject obj); extern ClassHook classHooks[]; #define FUNC(f) extern Var f (Var stack[]) /* some generic functions */ FUNC(Return0Func); FUNC(ReturnNeg1Func); /* all the native functions */ #define VmPrint Return0Func #define VmPrintLn Return0Func FUNC(VmExec); FUNC(VmGetTimeStamp); FUNC(copyArray); FUNC(VmExit); FUNC(VmSleep); FUNC(VmSetDeviceAutoOff); FUNC(VmGetUserName); FUNC(VmGetPlatform); FUNC(VmIsColor); FUNC(FileGetLength); FUNC(FileCreateDir); FUNC(FileReadBytes); FUNC(FileRename); FUNC(FileCreate); FUNC(FileWriteBytes); FUNC(FileListDir); FUNC(FileSeekWaba); FUNC(FileIsDir); FUNC(FileCloseWaba); FUNC(FileIsOpen); FUNC(FileDeleteWaba); FUNC(FileExists); FUNC(ImageCreate); FUNC(ImageFree); FUNC(ImageSetPixels); FUNC(ImageUseImagePalette); FUNC(ImageLoad); FUNC(SoundBeep); FUNC(SoundTone); FUNC(TimeCreate); FUNC(SocketCreate); FUNC(SocketRead); FUNC(SocketWrite); FUNC(SocketSetReadTimeout); FUNC(SocketClose); FUNC(SocketIsOpen); FUNC(WindowCreate); #if WITH_CATALOG_CLASS FUNC(CatalogListCatalogs); FUNC(CatalogAddRecord); FUNC(CatalogSkipBytes); FUNC(CatalogCreate); FUNC(CatalogRead); FUNC(CatalogDeleteRecord); FUNC(CatalogSetRecordPos); FUNC(CatalogGetRecordSize); FUNC(CatalogResizeRecord); FUNC(CatalogWrite); FUNC(CatalogGetRecordCount); FUNC(CatalogClose); FUNC(CatalogIsOpen); FUNC(CatalogDelete); #endif //WITH_CATALOG_CLASS FUNC(GraphicsCopyRect); FUNC(GraphicsClearClip); FUNC(GraphicsSetFont); FUNC(GraphicsSetDrawOp); FUNC(GraphicsSetClip); FUNC(GraphicsSetBackColor); FUNC(GraphicsSetColor); FUNC(GraphicsGetClip); FUNC(GraphicsFillRect); FUNC(GraphicsDrawLine); FUNC(GraphicsTranslate); FUNC(GraphicsDrawDots); FUNC(GraphicsDrawChars); FUNC(GraphicsCreate); FUNC(GraphicsDrawCursor); FUNC(GraphicsSetClip); FUNC(GraphicsFree); FUNC(GraphicsSetForeColor); FUNC(GraphicsSetTextColor); FUNC(GraphicsFillPolygon); FUNC(GraphicsDrawString); FUNC(ConvertStringToInt); FUNC(ConvertFloatToIntBitwise); FUNC(ConvertCharToString); FUNC(ConvertIntToFloatBitwise); FUNC(ConvertFloatToString); #if WITH_64BITS FUNC(ConvertDoubleToString); #endif FUNC(ConvertIntToString); #if WITH_64BITS FUNC(ConvertLongToString); #endif FUNC(ConvertBooleanToString); FUNC(SoundClipPlay); FUNC(PalmOsPrefSetPalmOsPref); FUNC(PalmOsPrefGetPalmOsPref); FUNC(MainWinCreate); FUNC(MainWinExit); FUNC(MainWinSetTimerInterval); FUNC(SerialPortReadCheck); FUNC(SerialPortRead); FUNC(SerialPortWrite); FUNC(SerialPortSetReadTimeout); FUNC(SerialPortClose); FUNC(SerialPortSetFlowControl); FUNC(SerialPortIsOpen); FUNC(SerialPortCreate); FUNC(FontMetricsGetStringWidth); FUNC(FontMetricsGetCharWidth); FUNC(FontMetricsCreate); FUNC(FontMetricsGetCharArrayWidth); #endif /* __NMPORT_C_H__ */ |