/* xdragon main program */

#include "xdragon.h"
#include <windows.h>

DWORD WINAPI eventTopLevel();
DWORD WINAPI recordTopLevel();

errorExit(msg)
{
  LPVOID lpMsgBuf;

  FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		0, (LPTSTR) &lpMsgBuf, 0, NULL );
  fprintf(stderr,"%s: %s\n", msg, lpMsgBuf);
  exit(1);
}

int main(int argc, char **argv) {

  if (argc != 2) {
    fprintf(stderr, "Usage: %s x-display\n", argv[0]); exit(1);}
  
  xOpen(argv[1]);		/* Open connection to X11 server */
  setupHotkey();
  createWindow();
  setupRecord();
  /* Create thread to handle events.  This thread reads from the inputDisplay,
     so we have to be done with anything else we're doing on that connection */
  if (CreateThread(NULL, 0, eventTopLevel, NULL, 0, NULL) == NULL)
    errorExit("Can't create thread");
  if (CreateThread(NULL, 0, recordTopLevel, NULL, 0, NULL) == NULL)
    errorExit("Can't create record thread");
  handleWindow();
}
