home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / cube3d / cube3D.exe / cube3D / src / device.c < prev    next >
C/C++ Source or Header  |  2000-12-26  |  4KB  |  145 lines

  1. /*
  2.  * @(#)device.c
  3.  *
  4.  * Copyright 1999-2000, Aaron Ardiri (mailto:aaron@ardiri.com)
  5.  * All rights reserved.
  6.  *
  7.  * The  source code  outlines a number of basic Palm Computing Programming
  8.  * principles and you  should be able to take the core structure and write 
  9.  * a large complex program. It is distributed WITHOUT ANY WARRANTY; use it
  10.  * "AS IS" and at your own risk.
  11.  *
  12.  * The code presented is Copyright 1999-2000 by Aaron Ardiri. It should be
  13.  * used for  educational purposes only.  You  shall not modify the  Cube3D 
  14.  * source code in any way and  re-distribute it as your  own,  however you
  15.  * are free to use  the code as  a guide for  developing  programs  on the 
  16.  * Palm Computing Platform.
  17.  */
  18.  
  19. #include "palm.h"
  20.  
  21. // globals variable structure
  22. typedef struct
  23. {
  24.   UInt32    romVersion;                    // the rom version of the device
  25.   UInt32    depthState;                    // the screen depth state (old)
  26. } DeviceGlobals;
  27.  
  28. /**
  29.  * Initialize the device.
  30.  */
  31. void
  32. DeviceInitialize()
  33. {
  34.   DeviceGlobals *gbls;
  35.  
  36.   // create the globals objects, and register it
  37.   gbls = (DeviceGlobals *)MemPtrNew(sizeof(DeviceGlobals));
  38.   MemSet(gbls, sizeof(DeviceGlobals), 0);
  39.   FtrSet(appCreator, ftrDeviceGlobals, (UInt32)gbls);
  40.  
  41.   // get the rom version and ram size for this device
  42.   FtrGet(sysFtrCreator, sysFtrNumROMVersion, &gbls->romVersion);
  43.  
  44.   // only OS 3.0 and above have > 1bpp display via API's
  45.   if (DeviceSupportsVersion(romVersion3)) {
  46.  
  47.     // save the current display state
  48.     WinScreenMode(winScreenModeGet,NULL,NULL,&gbls->depthState,NULL);
  49.  
  50.     // change into the "highest" possible mode :P
  51.     {
  52.       UInt32 depthsToTry[] = { 8, 4, 2, 1 };
  53.       UInt32 *depthPtr = &depthsToTry[0];
  54.  
  55.       // loop until a valid mode is found
  56.       while (WinScreenMode(winScreenModeSet,NULL,NULL,depthPtr,NULL)) {
  57.  
  58.         // try the next depth
  59.         depthPtr++;
  60.       }
  61.     }
  62.   }
  63. }
  64.  
  65. /**
  66.  * Get the supported depths the device can handle. 
  67.  *
  68.  * @return the depths supported (1011b = 2^3 | 2^1 | 2^0 = 4,2,1 bpp).
  69.  */
  70. UInt32  
  71. DeviceGetSupportedDepths()
  72. {
  73.   UInt32 result = 0x00000001;
  74.  
  75.   // only OS 3.0 and above have > 1bpp display via API's
  76.   if (DeviceSupportsVersion(romVersion3)) {
  77.     WinScreenMode(winScreenModeGetSupportedDepths,NULL,NULL,&result,NULL);
  78.   }
  79.  
  80.   return result;
  81. }
  82.  
  83. /**
  84.  * Check if the device is compatable with a particular ROM version.
  85.  *
  86.  * @param version the ROM version to compare against.
  87.  * @return true if it is compatable, false otherwise.
  88.  */
  89. Boolean 
  90. DeviceSupportsVersion(UInt32 version)
  91. {
  92.   DeviceGlobals *gbls;
  93.  
  94.   // get a globals reference
  95.   FtrGet(appCreator, ftrDeviceGlobals, (UInt32 *)&gbls);
  96.  
  97.   return (gbls->romVersion >= version);
  98. }
  99.  
  100. /**
  101.  * Determine the pointer to the bitmap data chunk for a specific window.
  102.  *
  103.  * @param win the window.
  104.  * @return a pointer to the bitmap data chunk.
  105.  */
  106. void *
  107. DeviceWindowGetPointer(WinHandle win)
  108. {
  109.   void *result = NULL;
  110.  
  111.   // palmos 3.5        - use BmpGetBits()
  112.   if (DeviceSupportsVersion(romVersion3_5)) {
  113.     result = BmpGetBits(WinGetBitmap(win));
  114.   }
  115.  
  116.   // palmos pre 3.5    - use standard technique
  117.   else
  118.     result = (void *)win->displayAddrV20;
  119.     
  120.   return result;
  121. }
  122.  
  123. /**
  124.  * Reset the device to its original state.
  125.  */
  126. void
  127. DeviceTerminate()
  128. {
  129.   DeviceGlobals *gbls;
  130.  
  131.   // get a globals reference
  132.   FtrGet(appCreator, ftrDeviceGlobals, (UInt32 *)&gbls);
  133.  
  134.   // restore the current display state
  135.   if (DeviceSupportsVersion(romVersion3)) {
  136.     WinScreenMode(winScreenModeSet,NULL,NULL,&gbls->depthState,NULL);
  137.   }
  138.  
  139.   // clean up memory
  140.   MemPtrFree(gbls);
  141.  
  142.   // unregister global data
  143.   FtrUnregister(appCreator, ftrDeviceGlobals);
  144. }
  145.