Hardware/ATI/ADL.cs
changeset 1 361e324a0ed4
child 3 1a0928afac6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/ATI/ADL.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,251 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Runtime.InteropServices;
    1.44 +
    1.45 +namespace OpenHardwareMonitor.Hardware.ATI {
    1.46 +  
    1.47 +  [StructLayout(LayoutKind.Sequential)]
    1.48 +  public struct ADLAdapterInfo {
    1.49 +    public int Size;
    1.50 +    public int AdapterIndex;
    1.51 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.52 +    public string UDID;
    1.53 +    public int BusNumber;
    1.54 +    public int DriverNumber;
    1.55 +    public int FunctionNumber;
    1.56 +    public int VendorID;
    1.57 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.58 +    public string AdapterName;
    1.59 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.60 +    public string DisplayName;
    1.61 +    public int Present;
    1.62 +    public int Exist;
    1.63 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.64 +    public string DriverPath;
    1.65 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.66 +    public string DriverPathExt;
    1.67 +    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ADL.ADL_MAX_PATH)]
    1.68 +    public string PNPString;
    1.69 +    public int OSDisplayIndex;
    1.70 +  }
    1.71 +
    1.72 +  [StructLayout(LayoutKind.Sequential)]
    1.73 +  public struct ADLPMActivity {
    1.74 +    public int Size;
    1.75 +    public int EngineClock;
    1.76 +    public int MemoryClock;
    1.77 +    public int Vddc;
    1.78 +    public int ActivityPercent;
    1.79 +    public int CurrentPerformanceLevel;
    1.80 +    public int CurrentBusSpeed;
    1.81 +    public int CurrentBusLanes;
    1.82 +    public int MaximumBusLanes;
    1.83 +    public int Reserved;
    1.84 +  }
    1.85 +
    1.86 +  [StructLayout(LayoutKind.Sequential)]
    1.87 +  public struct ADLTemperature {
    1.88 +    public int Size;
    1.89 +    public int Temperature;
    1.90 +  }
    1.91 +
    1.92 +  [StructLayout(LayoutKind.Sequential)]
    1.93 +  public struct ADLFanSpeedValue {
    1.94 +    public int Size;
    1.95 +    public int SpeedType;
    1.96 +    public int FanSpeed;
    1.97 +    public int Flags;
    1.98 +  }
    1.99 +
   1.100 +  [StructLayout(LayoutKind.Sequential)]
   1.101 +  public struct ADLFanSpeedInfo {
   1.102 +    public int Size;
   1.103 +    public int Flags;
   1.104 +    public int MinPercent;
   1.105 +    public int MaxPercent;
   1.106 +    public int MinRPM;
   1.107 +    public int MaxRPM;
   1.108 +  }
   1.109 +
   1.110 +  public class ADL {
   1.111 +    public const int ADL_MAX_PATH = 256;
   1.112 +    public const int ADL_MAX_ADAPTERS = 40;
   1.113 +    public const int ADL_MAX_DISPLAYS = 40;
   1.114 +    public const int ADL_MAX_DEVICENAME = 32;
   1.115 +    public const int ADL_SUCCESS = 0;
   1.116 +    public const int ADL_FAIL = -1;
   1.117 +    public const int ADL_DRIVER_OK = 0;
   1.118 +    public const int ADL_MAX_GLSYNC_PORTS = 8;
   1.119 +    public const int ADL_MAX_GLSYNC_PORT_LEDS = 8;
   1.120 +    public const int ADL_MAX_NUM_DISPLAYMODES = 1024;
   1.121 +
   1.122 +    public const int ADL_DL_FANCTRL_SPEED_TYPE_PERCENT = 1;
   1.123 +    public const int ADL_DL_FANCTRL_SPEED_TYPE_RPM = 2;
   1.124 +
   1.125 +    public const int ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ = 1;
   1.126 +    public const int ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE = 2;
   1.127 +    public const int ADL_DL_FANCTRL_SUPPORTS_RPM_READ = 4;
   1.128 +    public const int ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE = 8;
   1.129 +
   1.130 +    private delegate int ADL_Main_Control_CreateDelegate(
   1.131 +      ADL_Main_Memory_AllocDelegate callback, int enumConnectedAdapters);
   1.132 +    private delegate int ADL_Adapter_AdapterInfo_GetDelegate(IntPtr info,
   1.133 +      int size);
   1.134 +
   1.135 +    public delegate int ADL_Main_Control_DestroyDelegate();
   1.136 +    public delegate int ADL_Adapter_NumberOfAdapters_GetDelegate(
   1.137 +      ref int numAdapters);    
   1.138 +    public delegate int ADL_Adapter_ID_GetDelegate(int adapterIndex,
   1.139 +      out int adapterID);
   1.140 +    public delegate int ADL_Adapter_Active_GetDelegate(int adapterIndex,
   1.141 +      out int status);
   1.142 +    public delegate int ADL_Overdrive5_CurrentActivity_GetDelegate(
   1.143 +      int iAdapterIndex, ref ADLPMActivity activity);
   1.144 +    public delegate int ADL_Overdrive5_Temperature_GetDelegate(int adapterIndex,
   1.145 +        int thermalControllerIndex, ref ADLTemperature temperature);
   1.146 +    public delegate int ADL_Overdrive5_FanSpeed_GetDelegate(int adapterIndex,
   1.147 +        int thermalControllerIndex, ref	ADLFanSpeedValue fanSpeedValue);
   1.148 +    public delegate int ADL_Overdrive5_FanSpeedInfo_GetDelegate(
   1.149 +      int adapterIndex, int thermalControllerIndex,
   1.150 +      ref ADLFanSpeedInfo fanSpeedInfo);
   1.151 +
   1.152 +    private static ADL_Main_Control_CreateDelegate
   1.153 +      _ADL_Main_Control_Create;
   1.154 +    private static ADL_Adapter_AdapterInfo_GetDelegate
   1.155 +      _ADL_Adapter_AdapterInfo_Get;
   1.156 +
   1.157 +    public static ADL_Main_Control_DestroyDelegate
   1.158 +      ADL_Main_Control_Destroy;
   1.159 +    public static ADL_Adapter_NumberOfAdapters_GetDelegate
   1.160 +      ADL_Adapter_NumberOfAdapters_Get;
   1.161 +    public static ADL_Adapter_ID_GetDelegate
   1.162 +      ADL_Adapter_ID_Get;
   1.163 +    public static ADL_Adapter_Active_GetDelegate
   1.164 +      ADL_Adapter_Active_Get;
   1.165 +    public static ADL_Overdrive5_CurrentActivity_GetDelegate
   1.166 +      ADL_Overdrive5_CurrentActivity_Get;
   1.167 +    public static ADL_Overdrive5_Temperature_GetDelegate
   1.168 +      ADL_Overdrive5_Temperature_Get;
   1.169 +    public static ADL_Overdrive5_FanSpeed_GetDelegate
   1.170 +      ADL_Overdrive5_FanSpeed_Get;
   1.171 +    public static ADL_Overdrive5_FanSpeedInfo_GetDelegate
   1.172 +      ADL_Overdrive5_FanSpeedInfo_Get;
   1.173 +
   1.174 +    private static string GetDllName() {
   1.175 +      int p = (int)System.Environment.OSVersion.Platform;
   1.176 +      if ((p == 4) || (p == 128)) {
   1.177 +        if (IntPtr.Size == 4) {
   1.178 +          return "atiadlxy.so";
   1.179 +        } else {
   1.180 +          return "atiadlxx.so";
   1.181 +        }
   1.182 +      } else {
   1.183 +        if (IntPtr.Size == 4) {
   1.184 +          return "atiadlxy.dll";
   1.185 +        } else {
   1.186 +          return "atiadlxx.dll";
   1.187 +        }
   1.188 +      }
   1.189 +    }
   1.190 +
   1.191 +    private static void GetDelegate<T>(string entryPoint, out T newDelegate)
   1.192 +      where T : class 
   1.193 +    {
   1.194 +      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   1.195 +      attribute.CallingConvention = CallingConvention.StdCall;
   1.196 +      attribute.PreserveSig = true;
   1.197 +      attribute.EntryPoint = entryPoint;
   1.198 +      PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   1.199 +    }
   1.200 +
   1.201 +    static ADL() {
   1.202 +       GetDelegate("ADL_Main_Control_Create", 
   1.203 +         out _ADL_Main_Control_Create);
   1.204 +       GetDelegate("ADL_Adapter_AdapterInfo_Get", 
   1.205 +         out _ADL_Adapter_AdapterInfo_Get);
   1.206 +       GetDelegate("ADL_Main_Control_Destroy", 
   1.207 +         out ADL_Main_Control_Destroy);
   1.208 +       GetDelegate("ADL_Adapter_NumberOfAdapters_Get", 
   1.209 +         out ADL_Adapter_NumberOfAdapters_Get);
   1.210 +       GetDelegate("ADL_Adapter_ID_Get", 
   1.211 +         out ADL_Adapter_ID_Get);
   1.212 +       GetDelegate("ADL_Adapter_Active_Get", 
   1.213 +         out ADL_Adapter_Active_Get);
   1.214 +       GetDelegate("ADL_Overdrive5_CurrentActivity_Get", 
   1.215 +         out ADL_Overdrive5_CurrentActivity_Get);
   1.216 +       GetDelegate("ADL_Overdrive5_Temperature_Get", 
   1.217 +         out ADL_Overdrive5_Temperature_Get);
   1.218 +       GetDelegate("ADL_Overdrive5_FanSpeed_Get", 
   1.219 +         out ADL_Overdrive5_FanSpeed_Get);
   1.220 +       GetDelegate("ADL_Overdrive5_FanSpeedInfo_Get",
   1.221 +          out ADL_Overdrive5_FanSpeedInfo_Get);
   1.222 +    }
   1.223 +
   1.224 +    private ADL() { }
   1.225 +
   1.226 +    public static int ADL_Main_Control_Create(int enumConnectedAdapters) {
   1.227 +      return _ADL_Main_Control_Create(Main_Memory_Alloc, enumConnectedAdapters);
   1.228 +    }
   1.229 +
   1.230 +    public static int ADL_Adapter_AdapterInfo_Get(ADLAdapterInfo[] info) {
   1.231 +      int elementSize = Marshal.SizeOf(typeof(ADLAdapterInfo));
   1.232 +      int size = info.Length * elementSize;
   1.233 +      IntPtr ptr = Marshal.AllocHGlobal(size);
   1.234 +      int result = _ADL_Adapter_AdapterInfo_Get(ptr, size);
   1.235 +      for (int i = 0; i < info.Length; i++)
   1.236 +        info[i] = (ADLAdapterInfo)
   1.237 +          Marshal.PtrToStructure((IntPtr)((long)ptr + i * elementSize),
   1.238 +          typeof(ADLAdapterInfo));
   1.239 +      Marshal.FreeHGlobal(ptr);
   1.240 +      return result;
   1.241 +    }
   1.242 +
   1.243 +    private delegate IntPtr ADL_Main_Memory_AllocDelegate(int size);
   1.244 +
   1.245 +    private static IntPtr Main_Memory_Alloc(int size) {
   1.246 +      return Marshal.AllocHGlobal(size);;
   1.247 +    }
   1.248 +
   1.249 +    private static void Main_Memory_Free(IntPtr buffer) {
   1.250 +      if (IntPtr.Zero != buffer)
   1.251 +        Marshal.FreeHGlobal(buffer);
   1.252 +    }
   1.253 +  }
   1.254 +}