//
// File: display.cpp
// All functions related to the display should be placed here
// Author: J. Dumay
// Created: 10/07/2001
// 
// Modification history:
// FA   10/07/2001   Use SE macros. Load into separate 'Display' package.
// FA   01/08/2001   Put Windows specific code between #if defined(VERSION_WIN)/#endif 
//
// Notes:
//  It may be the case that subsequent calls to _ENUMdisplaySettings may cause
//  a crash on Windows '98


#include "display.h"
#include "../macros.h"
extern "C" {
# include "../loadpak.h"
# include "../include/vscol.h"
}
#if defined(VERSION_WIN)
# include <windows.h>
#endif


static NativeDefinition defs[] = {
    { "_ENUMdisplaySettings", 0, "fun [] [[I I I] r1]", _ENUMdisplaySettings }
};


int _ENUMdisplaySettings(mmachine m)
{
#if defined(VERSION_WIN)
    // Retrieve current display frequency
    DEVMODE mode;
	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &mode);
    DWORD freq = mode.dmDisplayFrequency;

	// Construct list of display resolutions
    SECHECK(SEPUSH(m, NIL));
    for (int i = 0; EnumDisplaySettings(NULL, i, &mode); i++) {
	    if (mode.dmDisplayFrequency == freq) {
			SECHECK(SEPUSH(m, SEI2W(mode.dmPelsWidth)));
			SECHECK(SEPUSH(m, SEI2W(mode.dmPelsHeight)));
			SECHECK(SEPUSH(m, SEI2W(mode.dmBitsPerPel)));
			SECHECK(SEPUSH(m, SEI2W(3)));
			SECHECK(SENEWTUPLE(m));
			SESWAP(m);
			SECHECK(SEPUSH(m, SEI2W(2)));
			SECHECK(SENEWTUPLE(m));
		}
    }
#else
    SEPUSH(m, NIL);
#endif
	return MERROK;
}


int loadPackageDisplay(mmachine m)
{
  return PKhardpak2(m, "Display", sizeof(defs)/sizeof(defs[0]), defs);
}
