home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / VideoToolbox / VideoToolboxSources / Identify.c < prev    next >
Encoding:
Text File  |  1995-10-27  |  29.9 KB  |  899 lines  |  [TEXT/CWIE]

  1. //#define NO_NAME_REGISTRY_LIB 1    /* Uncomment this if you're missing NameRegistryLib */
  2.  
  3. /* Identify.c
  4.  
  5. NOTE: several routines that use Apple's NameRegistryLib appear at the end of this file.
  6. If you don't have that file, then un-comment the first line above.
  7.  
  8. Each routine returns an informative C string. Here are samples. (I suggest you
  9. use BreakLines() to printf the longer ones.)
  10.  
  11. IdentifyCompiler:
  12. "Compiled by Metrowerks CodeWarrior C 7.00 for PowerPC, 4B int and 8B double."
  13.  
  14. IdentifyOwner:
  15. "Denis Pelli"
  16.  
  17. IdentifyModel:
  18. "PowerBook 170"
  19.  
  20. IdentifyMachine:
  21. "Denis Pelli's PowerBook 170, 68030, 68882, System 7.10."
  22. "Mal's PowerMac 8500/120, ppc604, L2 cache, System 7.52."
  23.  
  24. IdentifyVideo(device):
  25. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM) in slot 0"
  26. "PowerMac 8500/120 \"Built-in video\" (.Display_Video_Apple_Control)"
  27.  
  28. The computer model appears only if the slot==0, which indicates built-in video. 
  29. Quotes embrace the card name, and parentheses embrace the driver name.
  30. The driver's version number appears only if it's non-zero.
  31.  
  32. HISTORY:
  33. 1/29/92    dgp wrote IdentifyCompiler.
  34. 2/25/92    dgp    wrote IdentifyMachine.
  35. 8/26/92    dgp get owner and model name from the System file
  36. 2/20/93    dgp    added ROM version
  37. 2/27/93    dgp merged IdentifyCompiler.c and IdentifyMachine.c into the new Identify.c,
  38.             and added IdentifyVideo.
  39. 4/25/93    dgp    IdentifyMachine reports 24/32-bit addressing.
  40. 12/14/93 dgp If THINK C compiler version is "5" I report "5 or 6.0" since Symantec
  41.             forgot to increment the version number when they released 6.0. They
  42.             fixed this in version 6.01.
  43. 3/2/94    dgp    Extracted IdentifyOwner from within IdentifyMachine
  44. 7/28/94 dgp added support for Metrowerks CodeWarrior C compiler and PowerPC.
  45.             Eliminated use of "#s" printf format, since it's not supported by
  46.             Metrowerks CodeWarrior C.
  47. 9/5/94 dgp     removed assumption in printf's that int==short.
  48. 9/6/94    dgp    Enhanced IdentifyCompiler to specify the size of int.
  49. 9/6/94    dgp    Disable cache checks on PowerPC, because those traps are undefined.
  50. 10/5/94 dgp Removed all floating point code from IdentifyCompiler().
  51. 11/5/94 dgp Suppressed ROM info in Identify.
  52. 11/9/94 dgp added IdentifyApplication().
  53. 1/15/95 dgp added test for version 5 of CodeWarrior
  54. 5/24/95 dgp added test for version 6 of CodeWarrior
  55. 6/14/95 dgp added GetCPUSpeed() to IdentifyMachine() and abbreviated the report, 
  56.         omitting a lot of boring stuff.
  57. 6/27/95 dgp adjusted UNIVERSAL_HEADERS conditional for GMT struct, to use old
  58.         structure for version 1 universal headers.
  59. 7/20/95    dgp once again, made compatible with pre-universal headers
  60. 8/10/95 dgp updated IdentifyCompiler() for Symantec C.
  61. 9/26/95 dgp updated IdentifyCompiler() for CW7.
  62. 10/5/95 dgp minor editing of IdentifyVideo().
  63. 10/14/95 dgp added support for PCI Macs, using the NameRegistry. Since System 7.5 fails to
  64.         adequately name most Macs, I've reverted to using a built-in table of names in 
  65.         IdentifyModel. IdentifyMachine now also prints the Gestalt machine number to 
  66.         resolve any residual ambiguities.
  67.         IdentifyModel, IdentifyVideo, and IdentifyMachine now give complete information on all
  68.         Macs, provided that when running on PCI Macs they are compiled as PPC code. This 
  69.         limitation is due to the (easy) way that I implemented the calls to Apple's 
  70.         NameRegistryLib. The NameRegistry-related routines appear in a long self-contained
  71.         section at the end of this file.
  72. 10/24/95 dgp added pclk and bclk Gestalt calls, based on gestalt-selectors-31.
  73. */
  74. #include "VideoToolbox.h"
  75. #ifndef __TRAPS__
  76.     #include <Traps.h>        // _HWPriv
  77. #endif
  78. #include <Power.h>
  79. #if UNIVERSAL_HEADERS
  80.     #include <LowMem.h>
  81. #else
  82.     #define LMGetCurApName() ((StringPtr) 0x0910)
  83.     #define gestaltNativeCPUtype 'cput'
  84.     enum{
  85.         gestaltCPU601                = 0x101,
  86.         gestaltCPU603                = 0x103,
  87.         gestaltCPU604                = 0x104
  88.     };
  89.     enum {
  90.         gestaltSysArchitecture        = 'sysa',    /* Native System Architecture */
  91.         gestalt68k                    = 1,        /* Motorola MC68k architecture */
  92.         gestaltPowerPC                = 2            /* IBM PowerPC architecture */
  93.     };
  94.     #pragma parameter __D0 MaximumProcessorSpeed
  95.     extern pascal short MaximumProcessorSpeed(void)={0x7010, 0xA09E};
  96. #endif
  97. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  98. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *modelName);
  99. char *GetDeviceSlotName(GDHandle device);
  100. #define gestaltBusClockSpeed 'bclk'    /* gestalt-selectors-31.etx */
  101. #define gestaltCPUClockSpeed 'pclk'    /* gestalt-selectors-31.etx */
  102.  
  103. /*
  104. This table was complete in summer '92, but does not include the newer models.
  105. However, the table is used only as a backup, when the System lacks a machine name
  106. string, which is a feature of System 7. Since all new machines nominally require
  107. System 7, it is unlikely that it will ever be necessary to look them up in
  108. this table.
  109. */
  110. char *machineName[]={
  111.     "Unknown Mac"
  112.     ,"Mac 128K"
  113.     ,"Mac XL"
  114.     ,"Mac 512KE"
  115.     ,"Mac Plus"
  116.     ,"Mac SE"
  117.     ,"Mac II"
  118.     ,"Mac IIx"
  119.     ,"Mac IIcx"
  120.     ,"Mac SE-30"
  121.     ,"Mac Portable"
  122.     ,"Mac IIci"
  123.     ,"Unknown Mac"    /* 12 */
  124.     ,"Mac IIfx"
  125.     ,"Unknown Mac"
  126.     ,"Unknown Mac"
  127.     ,"Unknown Mac"
  128.     ,"Mac Classic","Mac IIsi","Mac LC","Mac Quadra 900","PowerBook 170"
  129.     ,"Mac Quadra 700","Mac Classic II","PowerBook 100","PowerBook 140","Quadra 950"
  130. // Despite the comment above, it appears that System 7.5 took a step backwards and lacks
  131. // specific names for many machines, so I used Gestalt Selectors List 2.8 (March '95)
  132. // to extend the list to include all currently available Macs.
  133.     ,"Mac LC III"
  134.     ,"Unknown Mac"
  135.     ,"Mac PowerBook 210"
  136.     ,"Mac Centris 650"
  137.     ,"Unknown Mac"
  138.     ,"Mac PowerBook 230"
  139.     ,"Mac PowerBook 180"
  140.     ,"Mac PowerBook 160"
  141.     ,"Mac Quadra 800"    /* 35 */
  142.     ,"Mac Quadra 650"
  143.     ,"Mac LC II"
  144.     ,"Mac PowerBook Duo 250"
  145.     ,"PowerMac 9150"
  146.     ,"PowerMac 8100/110"    /* 40 */
  147.     ,"Unknown Mac"
  148.     ,"Unknown Mac"
  149.     ,"Unknown Mac"
  150.     ,"Mac IIvi"
  151.     ,"Mac Performa 600"
  152.     ,"Unknown Mac"
  153.     ,"PowerMac 7100/80"    /* 47 */
  154.     ,"Mac IIvx"
  155.     ,"Mac Color Classic"
  156.     ,"Mac PowerBook 165c"
  157.     ,"Unknown Mac"
  158.     ,"Mac Centris 610"
  159.     ,"Mac Quadra 610"
  160.     ,"Mac PowerBook 145,145b"
  161.     ,"PowerMac 8100/100"    /* 55 */
  162.     ,"Mac LC 520"
  163.     ,"Unknown Mac"
  164.     ,"Unknown Mac"
  165.     ,"Unknown Mac"
  166.     ,"Mac Centris 660AV"
  167.     ,"Unknown Mac"
  168.     ,"Mac Performa 46x"
  169.     ,"Unknown Mac"
  170.     ,"Unknown Mac"
  171.     ,"PowerMac 8100/80"    /* 65 */
  172.     ,"Unknown Mac"
  173.     ,"PowerMac 9500"    /* 67 */
  174.     ,"PowerMac 7500"    /* 68 */
  175.     ,"PowerMac 8500"    /* 69 */
  176.     ,"Unknown Mac"
  177.     ,"Mac PowerBook 180c"
  178.     ,"Mac PowerBook 520,520c,540,540c"
  179.     ,"Unknown Mac"
  180.     ,"Unknown Mac"
  181.     ,"PowerMac 6100/60"    /* 75 */
  182.     ,"Unknown Mac"
  183.     ,"Mac PowerBook Duo 270c"
  184.     ,"Mac Quadra 840AV"
  185.     ,"Unknown Mac"
  186.     ,"Mac LC/Performa 550"
  187.     ,"Unknown Mac"
  188.     ,"Unknown Mac"
  189.     ,"Unknown Mac"
  190.     ,"Mac PowerBook 165"
  191.     ,"Unknown Mac"
  192.     ,"Unknown Mac"
  193.     ,"Unknown Mac"
  194.     ,"Mac TV"
  195.     ,"Mac LC/Performa 475"
  196.     ,"Unknown Mac"
  197.     ,"Unknown Mac"
  198.     ,"Mac LC 575"
  199.     ,"Unknown Mac"
  200.     ,"Mac Quadra 605"
  201.     ,"Unknown Mac"
  202.     ,"Unknown Mac"
  203.     ,"Unknown Mac"
  204.     ,"Mac LC/Quadra 630"
  205.     ,"Unknown Mac"
  206.     ,"PowerMac 6100/66"    /* 100 */
  207.     ,"PowerMac 6100/80??"    /* discovered by clock chippers */
  208.     ,"Mac PowerBook Duo 280"
  209.     ,"Mac PowerBook Duo 280c"
  210.     ,"PowerMac LC/Performa 475"
  211.     ,"PowerMac LC/Performa 575"
  212.     ,"PowerMac LC/Quadra 630"
  213.     ,"Unknown Mac"
  214.     ,"PowerMac 7200"
  215.     ,"Unknown Mac"
  216.     ,"Unknown Mac"
  217.     ,"Unknown Mac"
  218.     ,"PowerMac 7100/66"    /* 112 */
  219.     ,"PowerMac 7100/80??"    /* discovered by clock chippers */
  220.     ,"Unknown Mac"
  221.     ,"Mac PowerBook 150"
  222.     ,"PowerMac Quadra700"
  223.     ,"PowerMac Quadra900"
  224.     ,"PowerMac Quadra950"
  225.     ,"PowerMac Centris610"
  226.     ,"PowerMac Centris650"
  227.     ,"PowerMac Quadra610"
  228.     ,"PowerMac Quadra650"
  229.     ,"PowerMac Quadra800"
  230.     ,"Unknown Mac"
  231.     ,"Unknown Mac"
  232.     ,"Unknown Mac"
  233.     ,"Unknown Mac"
  234.     ,"PowerBook 5300"
  235. };
  236.  
  237. char fpuName[][32]={
  238.     "no floating point unit","68881","68882","built-in floating point"
  239.     ,"unknown floating point unit","Software FPU"
  240. };
  241.  
  242. char *IdentifyCompiler(void)
  243. {
  244.     static char string[200];
  245.     char *compiler,*longs,*floating,version[4],*universal;
  246.     double v=0;
  247.     Boolean fractionalVersion=0;
  248.     
  249.     string[0]=0;
  250.     compiler="";
  251.     #if THINK_C && !SYMANTEC_C
  252.         compiler="Symantec THINK C";
  253.         if(THINK_C==1)v=4;
  254.         else v=THINK_C;
  255.     #endif
  256.     #if THINK_CPLUS || SYMANTEC_C
  257.         #if __cplusplus || THINK_CPLUS
  258.             compiler="Symantec C++";
  259.         #else
  260.             compiler="Symantec C";
  261.         #endif
  262.         #if THINK_CPLUS
  263.             v=THINK_CPLUS;
  264.         #else
  265.             fractionalVersion=1;
  266.             v=SYMANTEC_C/0x100 + 0.1*(SYMANTEC_C/0x10%0x10) + 0.01*(SYMANTEC_C%0x10);
  267.         #endif
  268.     #endif
  269.     #if applec
  270.         compiler="MPW C";
  271.     #endif
  272.     #if __MWERKS__
  273.         compiler="Metrowerks CodeWarrior C";
  274.         #if __MWERKS__>1
  275.             fractionalVersion=1;
  276.             v=__MWERKS__/0x100 + 0.1*(__MWERKS__/0x10%0x10) + 0.01*(__MWERKS__%0x10);
  277.         #else
  278.             #if defined(__MC68K__)||defined(__POWERPC__)||defined(__INTEL__)
  279.                 v=6;
  280.             #else
  281.                 if(!iscntrl(0))v=4;    // bug in version 4.5 and earlier
  282.                 else v=5;
  283.             #endif
  284.         #endif
  285.     #endif
  286.     if(v>0){
  287.         if(fractionalVersion)sprintf(version,"%.2f ",v);
  288.         else sprintf(version,"%.0f ",v);
  289.     }else sprintf(version,"");
  290.     if(GENERATING68020)longs="68020";
  291.     else{
  292.         #if GENERATINGPOWERPC
  293.             longs="PowerPC";
  294.         #else
  295.             longs="68000";
  296.         #endif
  297.     }
  298.     if(GENERATING68881)floating=" and 68881";
  299.     else floating="";
  300.     universal="";
  301.     if(sizeof(double)==12){
  302.         #if THINK_C
  303.             #if !__option(native_fp)
  304.                 universal="\"universal\"-format ";
  305.             #endif
  306.         #endif
  307.     }
  308.     sprintf(string,"Compiled by %s %sfor %s%s, %ldB int and %s%ldB double."
  309.         ,compiler,version,longs,floating,sizeof(int),universal,sizeof(double));
  310.     return string;
  311. }
  312.  
  313. char *IdentifyModel(void)
  314. {
  315.     int error;
  316.     long machine=0,value;
  317.     int machines=sizeof(machineName)/sizeof(machineName[0]);
  318.     static char string[64];
  319.     char cpuName[32]="";
  320.     long cpuHz=0,busHz=0;
  321.     Boolean hasL2Cache,powerMgrExists;
  322.     
  323.     #if 0
  324.         /* Before System 7.5 the MacOS supplied the model name. Now it merely supplies a
  325.             generic name, like "Power Macintosh". */
  326.         if(!error){
  327.             GetIndString((unsigned char *)string,kMachineNameStrID,machine);
  328.             p2cstr((unsigned char *)string);
  329.             if(strlen(string)==0){
  330.                 if(machine<0 || machine>=machines)machine=0;
  331.                 sprintf(string,"%s",machineName[machine]);
  332.             }
  333.         }
  334.     #endif
  335.     /* The model names in the machineName[] table include clock speed, where appropriate,
  336.     for the pre-PCI Macs, e.g. 6100/60, but omit clock speed for the PCI Macs, since we
  337.     hope to get the clock speed from the Name Registry. */
  338.     error=Gestalt(gestaltMachineType,&machine);
  339.     strcpy(string,machineName[machine]);
  340.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    // useful only on PCI Macs
  341.     error=Gestalt(gestaltBusClockSpeed,&busHz);
  342.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);
  343.     Gestalt(gestaltPowerMgrAttr,&value);
  344.     powerMgrExists=value&(1L<<gestaltPMgrExists);
  345. //    if(TrapAvailable(0xA09E))sprintf(string,"%s/%ld",string,(long)MaximumProcessorSpeed());
  346.     if(powerMgrExists)sprintf(string,"%s/%ld",string,(long)GetCPUSpeed());
  347.     if(cpuHz>0)sprintf(string,"%s/%ld",string,cpuHz/1000000);
  348.  
  349.     return string;
  350. }
  351.  
  352. char *IdentifyVideo(GDHandle device)
  353. // E.g. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM)"
  354. {
  355.     static char string[256];
  356.     long quickDraw;
  357.     char *slotName;
  358.  
  359.     string[0]=0;
  360.     Gestalt(gestaltQuickdrawVersion,&quickDraw);
  361.     if(quickDraw<gestalt8BitQD){
  362.         sprintf(string,"%s ",IdentifyModel());
  363.         sprintf(string,"%s\"%s\"",string,"1-bit QuickDraw");
  364.     }else{
  365.         slotName=GetDeviceSlotName(device);
  366.         if(strlen(slotName)==0 || strcmp(slotName,"0")==0)sprintf(string,"%s ",IdentifyModel());
  367.         sprintf(string,"%s\"%s\"",string,GDCardName(device));
  368.         if(GDVersion(device)==0)sprintf(string,"%s (%s)",string,GDNameStr(device));
  369.         else sprintf(string,"%s (%s version %d)"
  370.             ,string,GDNameStr(device),(int)GDVersion(device));
  371.         if(strlen(slotName)>0 && strcmp(slotName,"0")!=0)sprintf(string,"%s slot %s",string,slotName);
  372.     }
  373.     return string;
  374. }
  375.  
  376. char *IdentifyOwner(void)
  377. {
  378.     static char string[64];
  379.     unsigned char **owner;
  380.     
  381.     string[0]=0;
  382.     owner=GetString(-16096);    // Get owner's name from System file
  383.     if(owner!=NULL && *owner[0]>0){
  384.         memcpy(string,*owner,(*owner)[0]+1);
  385.         p2cstr((unsigned char *)string);
  386.     }
  387.     return string;
  388. }
  389.  
  390. char *IdentifyApplication(void)    // Returns name of application.
  391. {
  392.     static char string[32];
  393.  
  394.     memcpy(string,LMGetCurApName(),32);
  395.     p2cstr((unsigned char *)string);
  396.     return string;
  397. }
  398.  
  399. char *IdentifyMachine(void)
  400. {
  401.     int error;
  402.     long fpu,processor=-1,system,value,sysArchitecture;
  403.     int fpus=sizeof(fpuName)/sizeof(fpuName[0]);
  404.     static char string[256];
  405.     Boolean cacheData=1,cacheInstructions=1;
  406.     long romSize,romVersion,qD;
  407.     char *processorName,*owner,fpuString[32];
  408.     char cpuName[32]="";
  409.     long cpuHz=0,busHz=0;
  410.     Boolean hasL2Cache;
  411.     char cache[32]="";
  412.     
  413.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    /* works only on PCI Macs */
  414.     error=Gestalt(gestaltBusClockSpeed,&busHz);        /* works only on PCI Macs */
  415.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);        /* works only on PCI Macs */
  416.     string[0]=0;
  417.     error=Gestalt(gestaltSystemVersion,&system);
  418.     if(error)return string;                        /* Gestalt not available */
  419.     sysArchitecture=gestalt68k;                    /* default */
  420.     error=Gestalt(gestaltSysArchitecture,&sysArchitecture);
  421.     switch(sysArchitecture){
  422.         case gestalt68k:
  423.             error=Gestalt(gestaltProcessorType,&processor);
  424.             switch(processor){
  425.                 case 1: processorName="68000";break;
  426.                 case 2: processorName="68010";break;
  427.                 case 3: processorName="68020";break;
  428.                 case 4: processorName="68030";break;
  429.                 case 5: processorName="68040";break;
  430.                 default: processorName="unknown processor";break;
  431.             }
  432.             break;
  433.         case gestaltPowerPC:
  434.             error=Gestalt(gestaltNativeCPUtype,&processor);
  435.             switch(processor){
  436.                 case gestaltCPU601: processorName="ppc601";break;
  437.                 case gestaltCPU603: processorName="ppc603";break;
  438.                  case 0x106:         processorName="ppc603e";break;/* PowerBook 5300 */
  439.                 case gestaltCPU604: processorName="ppc604";break;
  440.                 default: processorName="unknown processor";break;
  441.             }
  442.             break;
  443.         default:
  444.             processorName="unknown processor";
  445.             break;
  446.     }
  447.     Gestalt(gestaltFPUType,&fpu);
  448.     if(fpu<0 || fpu>=fpus)fpu=fpus-2;
  449.     if(fpu==1 && Gestalt('FPUE',&value)==0)fpu=fpus-1;// Software FPU by John Neil & Assoc.
  450.     if(fpu>0)sprintf(fpuString,", %s",fpuName[fpu]);
  451.     else fpuString[0]=0;
  452.     owner=IdentifyOwner();
  453.     if(strlen(owner)>0)sprintf(string,"%s's ",owner);
  454.     sprintf(string,"%s%s",string,IdentifyModel());
  455.     if(strlen(cpuName)>0){
  456.         if(hasL2Cache)strcpy(cache,", L2 cache");
  457.         else strcpy(cache,"");
  458.     }
  459.     sprintf(string,"%s, %s%s%s, System %lx.%lx"
  460.         ,string,processorName,cache,fpuString,system/0x100,system%0x100/0x10);
  461.     system%=0x10;
  462.     if(system)sprintf(string,"%s%lx",string,system);
  463.     sprintf(string,"%s.",string);
  464.     if(0){    // The rest is boring, so I've disabled it.
  465.         Gestalt(gestaltROMSize,&romSize);
  466.         Gestalt(gestaltROMVersion,&romVersion);
  467.         sprintf(string,"%s, %ldK ROM version %ld+%ld*256."
  468.             ,string,romSize/1024,romVersion%256,romVersion/256);
  469.         if(TrapAvailable(_HWPriv)){
  470.             #if !GENERATINGPOWERPC    /* apparently these functions don't work on PowerPC */
  471.                 cacheData=SwapDataCache(1);
  472.                 SwapDataCache(cacheData);
  473.                 cacheInstructions=SwapInstructionCache(1);
  474.                 SwapInstructionCache(cacheInstructions);
  475.                 if(cacheData || cacheInstructions)sprintf(string,"%s Caching",string);
  476.                 else sprintf(string,"%s No caching",string);
  477.                 if(cacheData)sprintf(string,"%s data",string);
  478.                 if(cacheData && cacheInstructions)sprintf(string,"%s &",string);
  479.                 if(cacheInstructions)sprintf(string,"%s instructions",string);
  480.                 sprintf(string,"%s.",string);
  481.             #endif
  482.         }
  483.         value=0;
  484.         Gestalt(gestaltAddressingModeAttr,&value);
  485.         {
  486.             if(value&1L<<gestalt32BitAddressing)sprintf(string,"%s 32-bit addressing.",string);
  487.             else sprintf(string,"%s 24-bit addressing.",string);
  488.         }
  489.         Gestalt(gestaltQuickdrawVersion,&qD);
  490.         switch(qD/0x100){
  491.             case 0:
  492.                 sprintf(string,"%s 1-bit QuickDraw.",string);
  493.                 break;
  494.             case 1:
  495.                 sprintf(string,"%s 8-bit QuickDraw.",string);
  496.                 break;
  497.             case 2:
  498.                 sprintf(string,"%s 32-bit QuickDraw 1.%02lx.",string,qD%0x100);
  499.                 break;
  500.             default:
  501.                 sprintf(string,"%s QuickDraw 0x%lx.",string,qD);
  502.                 break;
  503.         }
  504.     }
  505.     return string;
  506. }
  507.  
  508. char *IdentifyVM(void)
  509. {
  510.     // Based on gestalt-selectors-29.etx, available from Info-Mac.
  511.     Boolean vm;            // VM is on, possibly due to RAM Doubler
  512.     Boolean ramDoubler;    // RAM Doubler is on.
  513.     long value;
  514.     int error;
  515.     char *s="";
  516.     
  517.     error=Gestalt(gestaltVMAttr,&value);
  518.     vm=!error && value&(1L<<gestaltVMPresent);
  519.     error=Gestalt('vmem',&value);
  520.     ramDoubler=!error && value=='RaM2';
  521.     if(vm){
  522.         if(!ramDoubler)s="Using Virtual Memory. ";
  523.         else s="Using RAM Doubler. ";
  524.     }
  525.     return s;
  526. }
  527.  
  528. char *IdentifyGreenwichMeanTime(void)
  529. {
  530.     MachineLocation location;
  531.     long t;
  532.     double hours;
  533.     static char s[64];
  534.  
  535.     ReadLocation(&location);
  536.     if(location.latitude!=0 || location.longitude!=0){
  537.         #if UNIVERSAL_HEADERS>1
  538.             t=location.u.gmtDelta & 0x00FFFFFF;
  539.         #else
  540.             t=location.gmtFlags.gmtDelta & 0x00FFFFFF;
  541.         #endif
  542.         if(t & 0x800000)t|=0xFF000000;
  543.         hours=fabs(t/3600.0);
  544.         if(hours==floor(hours))sprintf(s,"%.0f",hours);
  545.         else sprintf(s,"%.1f",hours);
  546.         if(t<0)sprintf(s,"%s hours west of Greenwich mean time.",s);
  547.         else sprintf(s,"%s hours east of Greenwich mean time.",s);
  548.     }else s[0]=0;
  549.     return s;
  550. }
  551.  
  552. /************************************************************************/
  553.  
  554. /*
  555. Use NameRegistry
  556.  
  557. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  558. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  559.  
  560. Two routines that use the new Name Registry (available only on PCI PowerMacs) to
  561. determine the cpu clock rate, presence of L2 cache, and the name of the PCI video
  562. card. The routines return zeroes if the Name Registry is not available (i.e. on
  563. non-PCI Macs), or if compiled as 68k code.
  564.  
  565. WARNING: to effectively use Apple's NameRegistry in your project you MUST have Apple's
  566. NameRegistryLib, which contains the glue code to access the MacOS. (Add that file
  567. to your project and set that library for "weak" linking.) However, as of 10/13/95 Apple has
  568. distributed NameRegistry.h and NameRegistryLib only as part of the Apple
  569. Developer Mailing ($250/year from APDA). These two files are not included with
  570. Metrowerks CodeWarrior 7. As a quick fix you may set the flag
  571. NO_NAME_REGISTRY_LIB to true, which will disable the rest of this file, but
  572. allows you to link and run your programs.
  573.  
  574. Once compiled and linked with the NameRegistryLib glue this code will run on any
  575. PCI PowerMac. A temporary #if section takes care of the missing header file, so
  576. you'll be able to compile, but I don't think you'll be able to link this unless
  577. you have NameRegistryLib. I confess to being somewhat unsure on the last point
  578. since there are suggestions in the original source files that NameRegistryLib
  579. could be installed in the System:Extensions folder of the target machine instead
  580. of being linked into the application itself. That is apparently what the call
  581. below to Code Fragment Manager is for. However, I don't really understand this
  582. arcane stuff (i.e. "weak linking"), and it didn't seem worth learning for the
  583. routines in this file. Metrowerks has a "weak-linking" option, but limited
  584. experimentation suggests that you have to have the library in order to weak-link
  585. to it, which is no help at all for someone who doesn't have the library.
  586.  
  587. HISTORY:
  588. 10/13/95 dgp wrote GetVideoProperties() and GetCPUProperties(), based on general
  589.         advice from Apple (below) and 
  590.         the PCISlotsUtils.cp source file for Apple's "PCI Slot Peek" demo,
  591.         in the PCI developer's kit included on the September '95 Developer's CD. 
  592.  
  593. Hi Denis
  594.  
  595. Thank you for your link (ID# 151426) concerning how to detect a L2 cache and
  596. get the processor speed.
  597.  
  598. Let's look at the cache first.  Before the introduction of the PCI Power
  599. Macintosh computers there was no API to detect the presents of this cache.  On
  600. a PCI Macintosh there is the Name Registry.  This registry contains this
  601. information in the form of a property.  The property name is "name" and if
  602. there is a L2 cache the property value is "l2-cache".  Look at the Developer CD
  603. for Sept 95.  In the What's New folder there is the PCI DDK.  There is a
  604. program called Display Name Registry that will show what I have just explained.
  605. Of course it must run on a PCI machine.  There is also a document called
  606. "Designing PCI Cards&Drivers...." that shows examples of using the Name
  607. Registry to search for system resources.
  608.  
  609. Now the CPU speed.  Like the last case, before PCI Macs there was no direct
  610. call that could be made to determine CPU speed.  There are Gestalt calls for
  611. CPU type if that's any help.  However, there are CPU accelerator cards so that
  612. would present a problem.  The two calls you mentioned (GetCPUSPeed &
  613. MaximumProcessSpeed) are both for portable Macs that use the Power Manager.
  614. The traps for these calls are not implemented on other Macs.  At least not the
  615. 9500 that I tried.  However, PCI Macs and the Name Registry contain this
  616. information also.  Again go to Display Name Registry and look at the
  617. Devices:device-tree;PowerPC,604 property.  Expand it and there is an additional
  618. sub property called "clock-frequency".  My 9500 show 132000000.
  619.  
  620. So you have what you want but starting with PCI Macs.
  621.  
  622. Regards
  623.  
  624. Wayne Flansburg
  625. Developer Technical Support
  626. Apple Computer
  627. 12 October 1995
  628. */
  629.  
  630. #if UNIVERSAL_HEADERS>1
  631.     #ifndef __CODEFRAGMENTS__
  632.         #include <CodeFragments.h>
  633.     #endif
  634. #else
  635.     #undef NO_NAME_REGISTRY_LIB
  636.     #define NO_NAME_REGISTRY_LIB 1
  637. #endif
  638. #if 0
  639.     #ifndef __NAMEREGISTRY__
  640.         #include <NameRegistry.h>
  641.     #endif
  642. #else
  643.     #ifndef NO_NAME_REGISTRY_LIB
  644.         // As of 10/13/95 Apple has distributed NameRegistry.h and NameRegistryLib only to 
  645.         // Apple Developers. Neither is included with Metrowerks CodeWarrior 7.
  646.         // The following definitions and prototypes, copied from NameRegistry.h, are just 
  647.         // enough to allow compilation of this file without the header file. However, 
  648.         // you'll still need NameRegistryLib in order to link.
  649.         typedef UInt32 RegIterationOp;
  650.         typedef RegIterationOp RegEntryIterationOp;
  651.         struct RegEntryIter {
  652.             void *opaque;
  653.         };
  654.         typedef struct RegEntryIter *RegEntryIterPtr;
  655.         typedef struct RegEntryIter RegEntryIter;
  656.         struct RegEntryID {
  657.             UInt8 opaque[16];
  658.         };
  659.         typedef struct RegEntryID RegEntryID, *RegEntryIDPtr;
  660.         typedef char RegCStrPathName;
  661.         typedef UInt32 RegPathNameSize;
  662.         enum {
  663.             kRegMaximumPropertyNameLength = 31,                            /* Max length of Property Name */
  664.             kRegPropertyNameTerminator    = 0x00,                            /* '\0' */
  665.             kRegIterContinue = 0x1UL
  666.         };
  667.         typedef char RegPropertyName, *RegPropertyNamePtr, RegPropertyNameBuf[kRegMaximumPropertyNameLength + 1];
  668.         typedef UInt32 RegPropertyValueSize;
  669.         extern OSStatus RegistryEntryIterateCreate(RegEntryIter *cookie);
  670.         extern OSStatus RegistryEntryIterateDispose(RegEntryIter *cookie);
  671.         extern OSStatus RegistryEntryIterate(RegEntryIter *cookie, RegEntryIterationOp relationship, RegEntryID *foundEntry, Boolean *done);
  672.         extern OSStatus RegistryEntryToPathSize(const RegEntryID *entryID, RegPathNameSize *pathSize);
  673.         extern OSStatus RegistryCStrEntryToPath(const RegEntryID *entryID, RegCStrPathName *pathName, RegPathNameSize pathSize);
  674.         extern OSStatus RegistryPropertyGetSize(const RegEntryID *entryID, const RegPropertyName *propertyName, RegPropertyValueSize *propertySize);
  675.         extern OSStatus RegistryPropertyGet(const RegEntryID *entryID, const RegPropertyName *propertyName, void *propertyValue, RegPropertyValueSize *propertySize);
  676.         extern OSStatus RegistryEntryIDDispose(RegEntryID *id);
  677.     #endif
  678. #endif
  679. #include <stdio.h>
  680. #include <string.h>
  681. /*
  682.     RegPropertyModifiers propertyModifiers;
  683.     if(0 && !error){
  684.         error=RegistryPropertyGetMod(&entry,foundProperty,&propertyModifiers);
  685.     }
  686. */
  687.  
  688. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  689. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  690.  
  691. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel)
  692. {
  693. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  694.     device;
  695.     slotName[0]=0;
  696.     cardName[0]=0;
  697.     cardModel[0]=0;
  698.     return;
  699. #else
  700.     /*
  701.     Assume we're running native on ppc.
  702.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  703.     would require intervention by the Mixed Mode Manager,
  704.     and I decided not to bother with that complication, since there seems little point in
  705.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  706.     binary, so it always runs native.
  707.     */
  708.     OSStatus                    error;
  709.     RegEntryIter            cookie;
  710.     Boolean                    done;
  711.     RegEntryIterationOp    op;
  712.     RegEntryID                entry;
  713.     RegCStrPathName        *pathName;
  714.     RegPathNameSize        pathNameSize;
  715.     RegPropertyNameBuf    foundProperty;
  716.     RegPropertyValueSize    propertySize;
  717.     long templong;
  718.     short driverRef;
  719.     
  720.     slotName[0]=0;
  721.     cardName[0]=0;
  722.     cardModel[0]=0;
  723.     if(device==NULL)return;
  724.     if(1){
  725.         // If the NameRegistryLib is weak-linked, and it is missing, we need to
  726.         // check for the library, to prevent a crash if we try to access the library's exports;
  727.         // currently Finder reports the library is missing
  728.         CFragConnectionID        connID;
  729.         Ptr                        mainAddr;
  730.         Str255                    errName;
  731.         
  732.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  733.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  734.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  735.         if(error)return;    // No NameRegistryLib
  736.     }
  737.     error=Gestalt(gestaltNameRegistryVersion,&templong);
  738.     if(error)return;    // No PCI slots
  739.     op=kRegIterContinue;
  740.     error=RegistryEntryIterateCreate(&cookie);
  741.     if(!error){
  742.         done=FALSE;
  743.         while (!error && !done){
  744.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  745.             if(!error && !done){
  746.                 pathName=NULL;
  747.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  748.                 if(!error){
  749.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  750.                     if(pathName==NULL)error=MemError();
  751.                 }
  752.                 if(!error){
  753.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  754.                     //printf("%s\n",pathName);    // debugging
  755.                 }
  756.                 if(!error){
  757.                     // Found a video device
  758.                     strcpy(foundProperty,"driver-ref");
  759.                     propertySize=0;
  760.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  761.                     if(!error){
  762.                         assert(propertySize==sizeof(driverRef));
  763.                         error=RegistryPropertyGet(&entry,foundProperty,&driverRef,&propertySize);
  764.                         if(!error && driverRef==(*device)->gdRefNum){
  765.                             done=TRUE; // we found the correct driver
  766.                             strcpy(foundProperty,"name");
  767.                             propertySize=0;
  768.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  769.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardName,&propertySize);
  770.                             strcpy(foundProperty,"model");
  771.                             propertySize=0;
  772.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  773.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardModel,&propertySize);
  774.                             propertySize=0;
  775.                             strcpy(foundProperty,"AAPL,slot-name");
  776.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  777.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,slotName,&propertySize);
  778.                             // PCI slot names on Power Macintosh currently are of form : A1,B1,C1,D2,E2,F2
  779.                         }
  780.                     }else error=noErr;    // there is a card but NO display attached (no open driver)
  781.                 }
  782.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  783.                 RegistryEntryIDDispose(&entry);
  784.             }
  785.         }
  786.         RegistryEntryIterateDispose(&cookie);
  787.     }
  788.     return;
  789. #endif
  790. }    /* GetVideoProperties */
  791.  
  792. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache)
  793. {
  794. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  795.     cpuName[0]=0;
  796.     *cpuHz=0;
  797.     *hasL2Cache=0;
  798.     return;
  799. #else
  800.     /*
  801.     Assume we're running native on ppc.
  802.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  803.     would require intervention by the Mixed Mode Manager,
  804.     and I decided not to bother with that complication, since there seems little point in
  805.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  806.     binary, so it always runs native.
  807.     */
  808.     OSStatus error;
  809.     RegEntryIter cookie;
  810.     Boolean done;
  811.     RegEntryIterationOp    op;
  812.     RegEntryID entry;
  813.     RegCStrPathName *pathName;
  814.     RegPathNameSize pathNameSize;
  815.     RegPropertyNameBuf foundProperty;
  816.     RegPropertyValueSize propertySize;
  817.     long templong;
  818.     char *propertyValue[256];
  819.     
  820.     cpuName[0]=0;
  821.     *cpuHz=0;
  822.     *hasL2Cache=0;
  823.     if(1){
  824.         // If the NameRegistryLib is weak-linked, and it is missing, we need to
  825.         // check for the library, to prevent a crash if we try to access the library's exports;
  826.         // currently Finder reports the library is missing
  827.         CFragConnectionID        connID;
  828.         Ptr                        mainAddr;
  829.         Str255                    errName;
  830.         
  831.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  832.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  833.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  834.         if(error)return;    // No NameRegistryLib
  835.     }
  836.     error=Gestalt(gestaltNameRegistryVersion,&templong);
  837.     if(error)return;    // No PCI slots
  838.  
  839.     op=kRegIterContinue;
  840.     error=RegistryEntryIterateCreate(&cookie);
  841.     if(!error){
  842.         done=FALSE;
  843.         while (!error && !done){
  844.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  845.             if(!error && !done){
  846.                 pathName=NULL;
  847.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  848.                 if(!error){
  849.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  850.                     if(pathName==NULL)error=MemError();
  851.                 }
  852.                 if(!error){
  853.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  854.                     //printf("%s\n",pathName);    // debugging
  855.                 }
  856.                 if(!error){
  857.                     propertySize=0;
  858.                     strcpy(foundProperty,"device_type");
  859.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  860.                     if(!error)assert(propertySize<=sizeof(propertyValue));
  861.                     if(!error){
  862.                         error=RegistryPropertyGet(&entry,foundProperty,propertyValue,&propertySize);
  863.                         // Is it what we want?
  864.                         if(!error && strcmp("cpu",(char *)propertyValue)!=0)error=-1; // no
  865.                     }
  866.                     if(!error){
  867.                         strcpy(foundProperty,"clock-frequency");
  868.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  869.                     }
  870.                     if(!error)assert(propertySize==sizeof(*cpuHz));
  871.                     if(!error){
  872.                         error=RegistryPropertyGet(&entry,foundProperty,cpuHz,&propertySize);
  873.                     }
  874.                     if(!error){
  875.                         strcpy(foundProperty,"name");
  876.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  877.                     }
  878.                     if(!error)assert(propertySize<256);
  879.                     if(!error){
  880.                         error=RegistryPropertyGet(&entry,foundProperty,cpuName,&propertySize);
  881.                     }
  882.                     if(!error){
  883.                         strcpy(foundProperty,"l2-cache");
  884.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  885.                         if(!error)*hasL2Cache=1;
  886.                         else *hasL2Cache=0;
  887.                     }
  888.                     error=0;
  889.                 }
  890.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  891.                 RegistryEntryIDDispose(&entry);
  892.             }
  893.         }
  894.         RegistryEntryIterateDispose(&cookie);
  895.     }
  896.     return;
  897. #endif
  898. }    /* GetCPUProperties */
  899.