123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using GpuFanControl.MsiAfterburnerWrap;
- using GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs;
- using System;
- namespace GpuFanControl
- {
- internal class Program
- {
- private static int Main(string[] args)
- {
- if (args.Length == 0)
- {
- PrintUsage();
- return -1;
- }
- if (args[0].ToLower().StartsWith("-q"))
- {
- int? index = null;
- if (int.TryParse(args[0].Substring(2), out int parsedIndex))
- {
- index = parsedIndex;
- }
- var cm = ConnectToAfterburner(out var errCode);
- if (cm == null) return errCode;
- void PrintSpeedTableHeader() => Console.WriteLine("index\tmode\tmin\tval\tmax\t%");
- void PrintGpuFanSpeed(MACM_SHARED_MEMORY_GPU_ENTRY g, int gpuIndex)
- {
- if (!g.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
- {
- Console.WriteLine($"{gpuIndex}\t** No support fan speed **");
- }
- else
- {
- var gfr = g.FanSpeedMax - g.FanSpeedMin;
- Console.WriteLine($"{gpuIndex}\t{g.FanFlagsCur}\t{g.FanSpeedMin}\t{g.FanSpeedCur}\t{g.FanSpeedMax}\t{((float)g.FanSpeedCur - g.FanSpeedMin) / gfr * 100:N2}%");
- }
- }
- if (index.HasValue)
- {
- var g = cm.GetGpuEntry(index.Value, out var err);
- if (g.HasValue)
- {
- //show specified
- PrintSpeedTableHeader();
- PrintGpuFanSpeed(g.Value, index.Value);
- }
- else
- {
- Console.WriteLine("Argument Error: err");
- return -1;
- }
- }
- else
- {
- //show all
- PrintSpeedTableHeader();
- for (int i = 0; i < cm.GetNumberOfGpu(); i++)
- {
- var entry = cm.GetGpuEntry(i, out var err);
- if (entry.HasValue)
- {
- PrintGpuFanSpeed(entry.Value, i);
- }
- else
- {
- Console.WriteLine($"{i}\tErr:{err}");
- }
- }
- }
- }
- else if (args[0].ToLower().StartsWith("-s"))
- {
- float? value = null;
- var sParam = args[0].Substring(2);
- if (sParam.ToLower() == "auto")
- {
- }
- else if (float.TryParse(sParam, out var parsedValue))
- {
- value = parsedValue;
- }
- else
- {
- Console.WriteLine("Argument Error: -s ");
- return -1;
- }
- int? index = null;
- if (args.Length > 1 && args[1].ToLower().StartsWith("-i") && int.TryParse(args[1].Substring(2), out int parsedIndex))
- {
- index = parsedIndex;
- }
- var cm = ConnectToAfterburner(out var errCode);
- if (cm == null) return errCode;
- void SetGpuFanSpeed(MACM_SHARED_MEMORY_GPU_ENTRY g, int gpuIndex)
- {
- if (g.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
- {
- Console.WriteLine($"Set parameter for GPU {gpuIndex}");
- if (value.HasValue)
- {
- g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
- var gfr = g.FanSpeedMax - g.FanSpeedMin;
- var targetValue = (uint)Math.Round(g.FanSpeedMin + gfr * value.Value / 100);
- if (targetValue < g.FanSpeedMin) g.FanSpeedCur = g.FanSpeedMin;
- else if (targetValue > g.FanSpeedMax) g.FanSpeedCur = g.FanSpeedMax;
- else g.FanSpeedCur = targetValue;
- Console.WriteLine($"G{gpuIndex} min:{g.FanSpeedMin} max:{g.FanSpeedMax} set:{g.FanSpeedCur}");
- }
- else
- {
- g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.AUTO;
- Console.WriteLine($"G{gpuIndex} set:AUTO");
- }
- cm.SetGpuEntry(g, gpuIndex);
- }
- else
- {
- Console.WriteLine($"No fan speed support on GPU {gpuIndex}");
- }
- }
- if (index.HasValue)
- {
- var g = cm.GetGpuEntry(index.Value, out var err);
- if (g.HasValue)
- {
- //set specified
- SetGpuFanSpeed(g.Value, index.Value);
- }
- else
- {
- Console.WriteLine("Argument Error: index out of GPU entries");
- return -1;
- }
- }
- else
- {
- //set all
- for (int i = 0; i < cm.GetNumberOfGpu(); i++)
- {
- var entry = cm.GetGpuEntry(i, out var err);
- if (entry.HasValue)
- {
- SetGpuFanSpeed(entry.Value, i);
- }
- else
- {
- Console.WriteLine($"{i}\tErr:{err}");
- }
- }
- }
- Console.WriteLine("Commit changes...");
- cm.Commit();
- Console.WriteLine("Success.");
- cm.Disconnect();
- }
- else
- {
- PrintUsage();
- return -1;
- }
- return 0;
- }
- private static MsiAfterBurnControlWrap ConnectToAfterburner(out int error)
- {
- Console.Write("Connecting to MSI Afterburner...");
- try
- {
- var cm = new MsiAfterBurnControlWrap();
- cm.Connect();
- cm.Refresh();
- try
- {
- var l = Console.CursorLeft;
- Console.CursorLeft = 0;
- Console.Write("".PadLeft(l));
- Console.CursorLeft = 0;
- }
- catch (Exception e)
- {
- Console.WriteLine();
- }
- error = 0;
- return cm;
- }
- catch (Exception e)
- {
- Console.WriteLine();
- Console.WriteLine(e.Message);
- error = -2;
- return null;
- }
- }
- private static void PrintUsage()
- {
- Console.WriteLine("GPU Fan Control Utility");
- Console.WriteLine("\tBased on MSI Afterburner 2.1 or later");
- Console.WriteLine();
- Console.WriteLine("Query GPU Fan Speed");
- Console.WriteLine("\t-q[index] blank for all");
- Console.WriteLine("\toutput: index\tmode\tmin\tval(%)\tmax");
- Console.WriteLine();
- Console.WriteLine("Set GPU Fan Speed");
- Console.WriteLine("\t-s<auto|percent>");
- Console.WriteLine("\t[-i<index>] blank for all");
- Console.WriteLine();
- Console.WriteLine("Exit Code:");
- Console.WriteLine("\tSuccess:\t0");
- Console.WriteLine("\tArgument Error:\t-1");
- Console.WriteLine("\tConnect Fail:\t-2");
- Console.WriteLine("\tUnknown Fail:\t-3");
- }
- }
- }
|