/*

        Earth Screen Saver for Windows

				Definitions

*/

#define ScreenSaver
#define INTERNET_LINKS			// Include Internet-based help

#ifndef RC_INVOKED
#include <crtdbg.h>
#define _CRTDBG_MAP_ALLOC
#endif
#include <windows.h>
#include <winbase.h>
#include <scrnsave.h>
#include <string.h>
#include <time.h>
#ifndef RC_INVOKED
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef INTERNET_LINKS
#include <urlmon.h>
#endif
#include <math.h>
#include <search.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include "jpeglib.h"
#endif

#define _fmemcpy memcpy
#define _fstrlen strlen
#define _fstrstr strstr
typedef RGBQUAD *LPRGBQUAD;

//	Define bullshit Windows memory calls to use flat memory malloc()/free()

#define GlobalAlloc(bullshit, size) malloc(size)
#define GlobalFree(buf) free(buf)
#define GlobalLock(handle) ( NO!!! NO!!! }
#define GlobalUnlock(handle) { NO!!! NO!!! }

#include "resource.h"

//      String codes in resource file

#define IDS_DAY_NAMES           16          	// Day name base

#define IDS_APPNAME				103				// Application name/title

#define IDS_FORMATS            200          	// Format base
#define Format(n)   rstring(IDS_FORMATS + (n))

//      Resource ID numbers

//	Definitions for screen saver unique objects

#define ID_STARTUP					102
#define ID_HELP                     110

#define PI 3.14159265358979323846  /* Assume not near black hole nor in
                                      Tennessee */

#define V       (void)

#define EOS     '\0'

/*  Handy mathematical functions  */

#ifdef abs
#undef abs
#endif

#define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))       /* Extract sign */
#define abs(x) ((x) < 0 ? (-(x)) : (x))                   /* Absolute val */
#define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle    */
#define fixangr(a)  ((a) - (PI*2) * (floor((a) / (PI*2))))/* Fix angle in radians*/
#define dtr(x) ((x) * (PI / 180.0))                       /* Degree->Radian */
#define rtd(x) ((x) / (PI / 180.0))                       /* Radian->Degree */
/* #define min(x, y) ((x) < (y) ? (x) : (y))				  /* Minimum */
/* #define max(x, y) ((x) > (y) ? (x) : (y))				  /* Minimum */

//  Determine number of elements in an array

#define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))

//  Assertion checking for Windows

#ifndef NDEBUG
#define assert(x) { if (!(x)) { char erm[128]; wsprintf((LPSTR) erm, \
        (LPSTR) "Assertion %s failed at line %d of file %s.", (LPCSTR) #x, __LINE__,(LPSTR) __FILE__); \
        FatalAppExit(0, (LPCSTR) erm); }}
#else
#define assert(x)
#endif

//	Make sure no debug output remains in production builds: use Honk(x) for debug MessageBeep(x)

#ifdef NDEBUG
#ifdef OutputDebugString
#undef OutputDebugString
#endif
#define OutputDebugString(x) DebugOutputInProductionBuild()
#define Honk(x) DebugOutputInProductionBuild()
#else
#define Honk(x)	MessageBeep(x)
#endif

extern char szString[128];		// Variable to load resource strings
#define hInst	 hMainInstance  // Redefinitions for screen saver lib compatibility
#define hWndMain hMainWindow
extern HINSTANCE hInst;			// Class instance pointer
extern HWND hWndMain;			// Main window pointer
extern double faketime;         // Time for simulation / testing
extern int ImageWid, ImageHgt;	// Image width and height
extern int showDateTime;		// Show date and time ?
extern int forcePalette;		// Force palette even if > 8 bit display
extern int usePalette;			// Should palette be used ?
extern int currentImage;		// Current image in file list
extern char *currentFileName;	// Current slide file name, if any
extern char *currentSoundName;	// Current sound file name, if any
extern char *loadError;			// Error message for image load
extern int showFileName;		// Show file name ?
extern int changeTime;			// Change slides after this number of seconds
extern char slidePath[MAX_PATH];// Directory containing slides
extern int shufflePlay;			// Show slides in random order ?
extern int randomPosition;		// Show slides in random screen position ?
extern int soundMode;			// Sound play mode
#define		SOUND_NEVER		 0		// Ignore sound files
#define		SOUND_CONTINUOUS 1		// Continuous play
#define		SOUND_SYNC		 2		// Synchronised with slides
#define		SOUND_KEY		 3		// Play when Enter/Return key pressed
extern int soundPause;			// Pause between sounds in seconds
extern TEXTMETRIC tmx;			// Standard font text metric
extern COLORREF skycolour;		// Colour for legends

#define WindowProc(x) LONG FAR PASCAL x(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)

/* Dialogue service functions. */

#define DialogueProc(name) BOOL FAR PASCAL name(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

//  Calculation functions from ASTRO.C (in DLL)

extern double jtime(struct tm FAR *t);
extern void jyear(double td, long FAR *yy, int FAR *mm, int FAR *dd),
            jhms(double j, int FAR *h, int FAR *m, int FAR *s);
extern double ucttoj(long year, int mon, int mday,
                     int hour, int min, int sec);

//	From FILELIST.C

struct fileListItem {
	int age;
	char filename[0];
};

extern struct fileListItem **fileList;	// File list
extern int fileListLength;				// Number of files in list
extern struct fileListItem **soundFileList;	// Sound file list
extern int soundFileListLength;				// Number of files in list
extern int fileListStatus;				// Status from building file list
#define		FL_OK		0				// List built OK (but may be empty)
#define		FL_BADDIR	1				// Directory could not be read
#define		FL_NOMEM	2				// Insufficient memory to build list
extern void fileListDestroy(void);
extern int fileListBuild(char *path);
extern void fileListDump(FILE *df);
extern void fileListSort(void);
extern void fileListShuffleImages(void);
extern void fileListShuffleSounds(void);

//	From LOADBMP.C

extern int loadBMPimage(FILE *input_file, HGLOBAL *LimageBitmap,
						int *LsizeX, int *LsizeY);

//	From LOADGIF.C

extern int loadGIFimage(FILE *input_file, HGLOBAL *LimageBitmap,
						int *LsizeX, int *LsizeY);

//	From LOADIMAGE.C

extern int loadImage(FILE *input_file, HGLOBAL *LimageBitmap,
					 int *LsizeX, int *LsizeY);

//	From LOADJPEG.C

extern int loadJPEGimage(FILE *input_file, HGLOBAL *LimageBitmap,
						 int *LsizeX, int *LsizeY);

//	From LOADPNG.C

extern int loadPNGimage(FILE *input_file, HGLOBAL *LimageBitmap,
						int *LsizeX, int *LsizeY);

//	From PAINTSCR.C

extern void needImageBitmap(void);
extern void release_image(void);
extern void updviewfrom(HWND hWnd, BOOL repositioned);

//	From QUANTIFY.c

extern HGLOBAL quantifyBMP(HGLOBAL imageBitmapIn);

//  From RANDOM.C

extern void srandom(unsigned long seed);
extern long random(void);

//	From SCRNSAVE.C

extern void GetIniEntries(void);
extern void GetIniSettings(void);

//	From SCRNWIND.C

#ifdef TRACY
extern void Blooie(char *s);
#endif
extern void set_tm_time(struct tm *t, BOOL islocal);
extern void findThemeFile(void);

//	From UTIL.C

extern void ssavetime(HDC hDC, struct tm lt, RECT tr, COLORREF tcol);
extern void localtimeformat(void);
extern char *rstring(int resid);
