Bug 678030. gfxinfo: Refactor id parsing. r=ajuma
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Sat, 20 Aug 2011 19:01:11 -0400
changeset 75610 c51e35b364e662459866a3f53dbb627b8d97f9e0
parent 75609 53f198cfbf478a26fb0a9c00e0a87f5a2d019ce4
child 75611 e6fc16452e5273dacee60363a8909d8f8419d539
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersajuma
bugs678030
milestone9.0a1
Bug 678030. gfxinfo: Refactor id parsing. r=ajuma This will make it easier to reuse when we parse subsystem ids.
widget/src/windows/GfxInfo.cpp
--- a/widget/src/windows/GfxInfo.cpp
+++ b/widget/src/windows/GfxInfo.cpp
@@ -301,17 +301,31 @@ typedef BOOL (WINAPI*SetupDiGetDeviceReg
   PBYTE PropertyBuffer,
   DWORD PropertyBufferSize,
   PDWORD RequiredSize
 );
 typedef BOOL (WINAPI*SetupDiDestroyDeviceInfoListFunc)(
   HDEVINFO DeviceInfoSet
 );
 
-
+// The device ID is a string like PCI\VEN_15AD&DEV_0405&SUBSYS_040515AD
+// this function is used to extract the id's out of it
+PRUint32
+ParseIDFromDeviceID(const nsAString &key, const char *prefix, int length)
+{
+  nsAutoString id(key);
+  ToUpperCase(id);
+  PRInt32 start = id.Find(prefix);
+  if (start != -1) {
+    id.Cut(0, start + strlen(prefix));
+    id.Truncate(length);
+  }
+  nsresult err;
+  return id.ToInteger(&err, 16);
+}
 
 /* Other interesting places for info:
  *   IDXGIAdapter::GetDesc()
  *   IDirectDraw7::GetAvailableVidMem()
  *   e->GetAvailableTextureMem()
  * */
 
 #define DEVICE_KEY_PREFIX L"\\Registry\\Machine\\"
@@ -452,53 +466,21 @@ GfxInfo::Init()
 
         setupDestroyDeviceInfoList(devinfo);
       }
     }
 
     FreeLibrary(setupapi);
   }
 
-  nsAutoString vendor(mDeviceID);
-  ToUpperCase(vendor);
-  PRInt32 start = vendor.Find(NS_LITERAL_CSTRING("VEN_"));
-  if (start != -1) {
-    vendor.Cut(0, start + strlen("VEN_"));
-    vendor.Truncate(4);
-  }
-  nsresult err;
-  mAdapterVendorID = vendor.ToInteger(&err, 16);
-  
-  vendor = mDeviceID2;
-  ToUpperCase(vendor);
-  start = vendor.Find(NS_LITERAL_CSTRING("VEN_"));
-  if (start != -1) {
-    vendor.Cut(0, start + strlen("VEN_"));
-    vendor.Truncate(4);
-  }
-  mAdapterVendorID2 = vendor.ToInteger(&err, 16);
+  mAdapterVendorID  = ParseIDFromDeviceID(mDeviceID,  "VEN_", 4);
+  mAdapterVendorID2 = ParseIDFromDeviceID(mDeviceID2, "VEN_", 4);
+  mAdapterDeviceID  = ParseIDFromDeviceID(mDeviceID,  "&DEV_", 4);
+  mAdapterDeviceID2 = ParseIDFromDeviceID(mDeviceID2, "&DEV_", 4);
 
-  nsAutoString device(mDeviceID);
-  ToUpperCase(device);
-  start = device.Find(NS_LITERAL_CSTRING("&DEV_"));
-  if (start != -1) {
-    device.Cut(0, start + strlen("&DEV_"));
-    device.Truncate(4);
-  }
-  mAdapterDeviceID = device.ToInteger(&err, 16);
-  
-  device = mDeviceID2;
-  ToUpperCase(device);
-  start = device.Find(NS_LITERAL_CSTRING("&DEV_"));
-  if (start != -1) {
-    device.Cut(0, start + strlen("&DEV_"));
-    device.Truncate(4);
-  }
-  mAdapterDeviceID2 = device.ToInteger(&err, 16);
-  
   const char *spoofedDriverVersionString = PR_GetEnv("MOZ_GFX_SPOOF_DRIVER_VERSION");
   if (spoofedDriverVersionString) {
     mDriverVersion.AssignASCII(spoofedDriverVersionString);
   }
 
   const char *spoofedVendor = PR_GetEnv("MOZ_GFX_SPOOF_VENDOR_ID");
   if (spoofedVendor) {
      PR_sscanf(spoofedVendor, "%x", &mAdapterVendorID);