MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / SysGet

Method SysGet

Source/script2.cpp:3877–4011  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3875
3876
3877ResultType Line::SysGet(char *aCmd, char *aValue)
3878// Thanks to Gregory F. Hogg of Hogg's Software for providing sample code on which this function
3879// is based.
3880{
3881 // For simplicity and array look-up performance, this is done even for sub-commands that output to an array:
3882 Var *output_var = ResolveVarOfArg(0);
3883 if (!output_var)
3884 return FAIL;
3885
3886 SysGetCmds cmd = ConvertSysGetCmd(aCmd);
3887 // Since command names are validated at load-time, this only happens if the command name
3888 // was contained in a variable reference. But for simplicity of design here, return
3889 // failure in this case (unlike other functions similar to this one):
3890 if (cmd == SYSGET_CMD_INVALID)
3891 return LineError(ERR_SYSGET ERR_ABORT, FAIL, aCmd);
3892
3893 MonitorInfoPackage mip = {0}; // Improves maintainability to initialize unconditionally, here.
3894 mip.monitor_info_ex.cbSize = sizeof(MONITORINFOEX); // Also improves maintainability.
3895
3896 // EnumDisplayMonitors() must be dynamically loaded; otherwise, the app won't launch at all on Win95/NT.
3897 typedef BOOL (WINAPI* EnumDisplayMonitorsType)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
3898 static EnumDisplayMonitorsType MyEnumDisplayMonitors = (EnumDisplayMonitorsType)
3899 GetProcAddress(GetModuleHandle("user32.dll"), "EnumDisplayMonitors");
3900
3901 switch(cmd)
3902 {
3903 case SYSGET_CMD_METRICS: // In this case, aCmd is the value itself.
3904 return output_var->Assign(GetSystemMetrics(ATOI(aCmd))); // Input and output are both signed integers.
3905
3906 // For the next few cases, I'm not sure if it is possible to have zero monitors. Obviously it's possible
3907 // to not have a monitor turned on or not connected at all. But it seems likely that these various API
3908 // functions will provide a "default monitor" in the absence of a physical monitor connected to the
3909 // system. To be safe, all of the below will assume that zero is possible, at least on some OSes or
3910 // under some conditions. However, on Win95/NT, "1" is assumed since there is probably no way to tell
3911 // for sure if there are zero monitors except via GetSystemMetrics(SM_CMONITORS), which is a different
3912 // animal as described below.
3913 case SYSGET_CMD_MONITORCOUNT:
3914 // Don't use GetSystemMetrics(SM_CMONITORS) because of this:
3915 // MSDN: "GetSystemMetrics(SM_CMONITORS) counts only display monitors. This is different from
3916 // EnumDisplayMonitors, which enumerates display monitors and also non-display pseudo-monitors."
3917 if (!MyEnumDisplayMonitors) // Since system only supports 1 monitor, the first must be primary.
3918 return output_var->Assign(1); // Assign as 1 vs. "1" to use hexidecimal display if that is in effect.
3919 mip.monitor_number_to_find = COUNT_ALL_MONITORS;
3920 MyEnumDisplayMonitors(NULL, NULL, EnumMonitorProc, (LPARAM)&mip);
3921 return output_var->Assign(mip.count); // Will assign zero if the API ever returns a legitimate zero.
3922
3923 // Even if the first monitor to be retrieved by the EnumProc is always the primary (which is doubtful
3924 // since there's no mention of this in the MSDN docs) it seems best to have this sub-cmd in case that
3925 // policy ever changes:
3926 case SYSGET_CMD_MONITORPRIMARY:
3927 if (!MyEnumDisplayMonitors) // Since system only supports 1 monitor, the first must be primary.
3928 return output_var->Assign(1); // Assign as 1 vs. "1" to use hexidecimal display if that is in effect.
3929 // The mip struct's values have already initalized correctly for the below:
3930 MyEnumDisplayMonitors(NULL, NULL, EnumMonitorProc, (LPARAM)&mip);
3931 return output_var->Assign(mip.count); // Will assign zero if the API ever returns a legitimate zero.
3932
3933 case SYSGET_CMD_MONITORAREA:
3934 case SYSGET_CMD_MONITORWORKAREA:

Callers

nothing calls this directly

Calls 4

ATOIFunction · 0.85
snprintfFunction · 0.85
AssignMethod · 0.80
FindOrAddVarMethod · 0.80

Tested by

no test coverage detected