private FREObject SetDisplayResolution(FREContext ctx, uint argc, FREObject[] argv)
        {
            var key            = argv[0].AsString();
            var newWidth       = argv[1].AsInt();
            var newHeight      = argv[2].AsInt();
            var newRefreshRate = argv[3].AsInt();

            if (string.IsNullOrEmpty(key))
            {
                return(FREObject.Zero);
            }
            var device = _displayDeviceMap[key];
            var dm     = new Devmode();

            dm.dmSize = (short)Marshal.SizeOf(dm);

            if (WinApi.EnumDisplaySettings(device.DeviceName, WinApi.EnumCurrentSettings, ref dm) == 0)
            {
                return(false.ToFREObject());
            }

            dm.dmPelsWidth  = newWidth;
            dm.dmPelsHeight = newHeight;

            var flgs = DevModeFlags.DM_PELSWIDTH | DevModeFlags.DM_PELSHEIGHT;

            if (newRefreshRate > 0)
            {
                flgs |= DevModeFlags.DM_DISPLAYFREQUENCY;
                dm.dmDisplayFrequency = newRefreshRate;
            }

            dm.dmFields = (int)flgs;

            return(WinApi.ChangeDisplaySettings(ref dm, (int)ChangeDisplaySettingsFlags.CdsTest) != 0
                ? false.ToFREObject()
                : (WinApi.ChangeDisplaySettings(ref dm, 0) == 0).ToFREObject());
        }
        public FREObject SetDisplayResolution(FREContext ctx, uint argc, FREObject[] argv)
        {
            var key            = Convert.ToString(new FreObjectSharp(argv[0]).Value);
            var newWidth       = Convert.ToInt32(new FreObjectSharp(argv[1]).Value);
            var newHeight      = Convert.ToInt32(new FreObjectSharp(argv[2]).Value);
            var newRefreshRate = Convert.ToInt32(new FreObjectSharp(argv[3]).Value);

            if (!string.IsNullOrEmpty(key))
            {
                var device = _displayDeviceMap[key];
                var dm     = new Devmode();
                dm.dmSize = (short)Marshal.SizeOf(dm);

                if (WinApi.EnumDisplaySettings(device.DeviceName, WinApi.EnumCurrentSettings, ref dm) == 0)
                {
                    return(new FreObjectSharp(false).RawValue);
                }

                dm.dmPelsWidth  = newWidth;
                dm.dmPelsHeight = newHeight;

                var flgs = DevModeFlags.DM_PELSWIDTH | DevModeFlags.DM_PELSHEIGHT;

                if (newRefreshRate > 0)
                {
                    flgs |= DevModeFlags.DM_DISPLAYFREQUENCY;
                    dm.dmDisplayFrequency = newRefreshRate;
                }

                dm.dmFields = (int)flgs;

                return(WinApi.ChangeDisplaySettings(ref dm, (int)ChangeDisplaySettingsFlags.CdsTest) != 0
                    ? new FreObjectSharp(false).RawValue
                    : new FreObjectSharp(WinApi.ChangeDisplaySettings(ref dm, 0) == 0).RawValue);
            }
            return(FREObject.Zero);
        }
        private FREObject GetDisplayDevices(FREContext ctx, uint argc, FREObject[] argv)
        {
            var vecDisplayDevices = new FREArray("com.tuarua.DisplayDevice");

            var dd = new DisplayDevice();

            dd.cb = Marshal.SizeOf(dd);

            _displayDeviceMap.Clear();

            try {
                uint index = 0;
                while (WinApi.EnumDisplayDevices(null, index++, ref dd, 0))
                {
                    dynamic displayDevice  = new FreObjectSharp("com.tuarua.DisplayDevice");
                    dynamic displayMonitor = new FreObjectSharp("com.tuarua.Monitor");

                    displayDevice.isPrimary       = dd.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice);
                    displayDevice.isActive        = dd.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop);
                    displayDevice.isRemovable     = dd.StateFlags.HasFlag(DisplayDeviceStateFlags.Removable);
                    displayDevice.isVgaCampatible = dd.StateFlags.HasFlag(DisplayDeviceStateFlags.VgaCompatible);

                    var monitor = new DisplayDevice();
                    monitor.cb = Marshal.SizeOf(monitor);

                    if (!WinApi.EnumDisplayDevices(dd.DeviceName, index - 1, ref monitor, 0))
                    {
                        continue;
                    }

                    var dm = new Devmode();
                    dm.dmSize = (short)Marshal.SizeOf(dm);
                    if (WinApi.EnumDisplaySettings(dd.DeviceName, WinApi.EnumCurrentSettings, ref dm) == 0)
                    {
                        continue;
                    }

                    var availdm = new Devmode();
                    availdm.dmSize = (short)Marshal.SizeOf(availdm);
                    IList <DisplaySettings> availableDisplaySettings = new List <DisplaySettings>();

                    var freAvailableDisplaySettings = displayDevice.availableDisplaySettings;
                    for (var iModeNum = 0;
                         WinApi.EnumDisplaySettings(dd.DeviceName, iModeNum, ref availdm) != 0;
                         iModeNum++)
                    {
                        var settings = new DisplaySettings {
                            Width       = availdm.dmPelsWidth,
                            Height      = availdm.dmPelsHeight,
                            BitDepth    = Convert.ToInt32(availdm.dmBitsPerPel),
                            RefreshRate = availdm.dmDisplayFrequency
                        };

                        if (HasDisplaySetting(availableDisplaySettings, settings))
                        {
                            continue;
                        }
                        availableDisplaySettings.Add(settings);

                        dynamic displaySettings = new FreObjectSharp("com.tuarua.DisplaySettings");
                        displaySettings.width       = availdm.dmPelsWidth;
                        displaySettings.height      = availdm.dmPelsHeight;
                        displaySettings.refreshRate = availdm.dmDisplayFrequency;
                        displaySettings.bitDepth    = Convert.ToInt32(availdm.dmBitsPerPel);
                        freAvailableDisplaySettings.Push(displaySettings);
                    }

                    displayMonitor.friendlyName = monitor.DeviceString;
                    displayMonitor.name         = monitor.DeviceName;
                    displayMonitor.id           = monitor.DeviceID;
                    displayMonitor.key          = monitor.DeviceKey;

                    displayDevice.friendlyName = dd.DeviceString;
                    displayDevice.name         = dd.DeviceName;
                    displayDevice.id           = dd.DeviceID;
                    displayDevice.key          = dd.DeviceKey;

                    dynamic currentDisplaySettings = new FreObjectSharp("com.tuarua.DisplaySettings");
                    currentDisplaySettings.width       = dm.dmPelsWidth;
                    currentDisplaySettings.height      = dm.dmPelsHeight;
                    currentDisplaySettings.refreshRate = dm.dmDisplayFrequency;
                    currentDisplaySettings.bitDepth    = Convert.ToInt32(dm.dmBitsPerPel);

                    displayDevice.currentDisplaySettings = currentDisplaySettings;
                    displayDevice.monitor = displayMonitor;
                    vecDisplayDevices.Push(displayDevice);

                    _displayDeviceMap.Add(dd.DeviceKey, dd);
                }
            }
            catch (Exception e) {
                Trace(e.Message);
                Trace(e.Source);
                Trace(e.StackTrace);
            }

            return(vecDisplayDevices.RawValue);
        }
        public FREObject GetDisplayDevices(FREContext ctx, uint argc, FREObject[] argv)
        {
            var tmp = new FREObject().Init("Vector.<com.tuarua.DisplayDevice>", null);
            var vecDisplayDevices = new FREArray(tmp);

            var dd = new DisplayDevice();

            dd.cb = Marshal.SizeOf(dd);

            _displayDeviceMap.Clear();

            try {
                uint index = 0;
                uint cnt   = 0;
                while (WinApi.EnumDisplayDevices(null, index++, ref dd, 0))
                {
                    var displayDevice  = new FREObject().Init("com.tuarua.DisplayDevice", null);
                    var displayMonitor = new FREObject().Init("com.tuarua.Monitor", null);

                    displayDevice.SetProp("isPrimary",
                                          dd.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice));
                    displayDevice.SetProp("isActive",
                                          dd.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop));
                    displayDevice.SetProp("isRemovable", dd.StateFlags.HasFlag(DisplayDeviceStateFlags.Removable));
                    displayDevice.SetProp("isVgaCampatible",
                                          dd.StateFlags.HasFlag(DisplayDeviceStateFlags.VgaCompatible));

                    var monitor = new DisplayDevice();
                    monitor.cb = Marshal.SizeOf(monitor);

                    if (!WinApi.EnumDisplayDevices(dd.DeviceName, index - 1, ref monitor, 0))
                    {
                        continue;
                    }

                    var dm = new Devmode();
                    dm.dmSize = (short)Marshal.SizeOf(dm);
                    if (WinApi.EnumDisplaySettings(dd.DeviceName, WinApi.EnumCurrentSettings, ref dm) == 0)
                    {
                        continue;
                    }

                    var availdm = new Devmode();
                    availdm.dmSize = (short)Marshal.SizeOf(availdm);
                    IList <DisplaySettings> availableDisplaySettings = new List <DisplaySettings>();

                    var freAvailableDisplaySettings = new FREArray(displayDevice.GetProp("availableDisplaySettings"));

                    uint cntAvailableSettings = 0;
                    for (var iModeNum = 0;
                         WinApi.EnumDisplaySettings(dd.DeviceName, iModeNum, ref availdm) != 0;
                         iModeNum++)
                    {
                        var settings = new DisplaySettings {
                            Width       = availdm.dmPelsWidth,
                            Height      = availdm.dmPelsHeight,
                            BitDepth    = Convert.ToInt32(availdm.dmBitsPerPel),
                            RefreshRate = availdm.dmDisplayFrequency
                        };

                        if (HasDisplaySetting(availableDisplaySettings, settings))
                        {
                            continue;
                        }
                        availableDisplaySettings.Add(settings);

                        var displaySettings = new FREObject().Init("com.tuarua.DisplaySettings", null);

                        displaySettings.SetProp("width", availdm.dmPelsWidth);
                        displaySettings.SetProp("height", availdm.dmPelsHeight);
                        displaySettings.SetProp("refreshRate", availdm.dmDisplayFrequency);
                        displaySettings.SetProp("bitDepth", Convert.ToInt32(availdm.dmBitsPerPel));
                        freAvailableDisplaySettings.Set(cntAvailableSettings, displaySettings);
                        cntAvailableSettings++;
                    }

                    displayMonitor.SetProp("friendlyName", monitor.DeviceString);
                    displayMonitor.SetProp("name", monitor.DeviceName);
                    displayMonitor.SetProp("id", monitor.DeviceID);
                    displayMonitor.SetProp("key", monitor.DeviceKey);

                    displayDevice.SetProp("friendlyName", dd.DeviceString);
                    displayDevice.SetProp("name", dd.DeviceName);
                    displayDevice.SetProp("id", dd.DeviceID);
                    displayDevice.SetProp("key", dd.DeviceKey);

                    var currentDisplaySettings = new FREObject().Init("com.tuarua.DisplaySettings", null);

                    currentDisplaySettings.SetProp("width", dm.dmPelsWidth);
                    currentDisplaySettings.SetProp("height", dm.dmPelsHeight);
                    currentDisplaySettings.SetProp("refreshRate", dm.dmDisplayFrequency);
                    currentDisplaySettings.SetProp("bitDepth", Convert.ToInt32(dm.dmBitsPerPel));

                    displayDevice.SetProp("currentDisplaySettings", currentDisplaySettings);
                    displayDevice.SetProp("monitor", displayMonitor);

                    vecDisplayDevices.Set(cnt, displayDevice);

                    _displayDeviceMap.Add(dd.DeviceKey, dd);

                    cnt++;
                }
            }
            catch (Exception e) {
                Trace("ERROR: " + e);
            }

            return(vecDisplayDevices.RawValue);
        }