/**
 * emacs-run.c
 *  Launch a WinXP executable from within EMACS using (w32-shell-execute ...)
 *  (after unsetting the SHELL and TERM variables).
 *
 *  http://the-brown-dragon.com/
 */
#define _WIN32_WINNT 0x0501     /* To use XP functions. */
#include <windows.h>

/**
 * Initializes the size of the launched window
 * to a reasonble value that I like.
 */
void
_set_fullscreensize (LPSTARTUPINFO si)
{
        HWND        dw;
        RECT        dr;

    dw  =   GetDesktopWindow ();
    GetClientRect (dw, &dr);
    si->dwFlags |= STARTF_USESIZE;
    si->dwXSize  = 640;
    si->dwYSize  = dr.bottom - dr.top;
}

/**
 * Main entry point.
 */
int WINAPI
WinMain (HINSTANCE hInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpCmdLine,
         int nCmdShow)
{
        STARTUPINFO          si;
        PROCESS_INFORMATION  pi;
        char                 cmd[1024];

    ZeroMemory (&si, sizeof (si));
    si.cb      = sizeof (si);
    _set_fullscreensize (&si);
    ZeroMemory (&pi, sizeof (pi));

    strncpy (cmd, lpCmdLine, sizeof (cmd) - 1);

    SetEnvironmentVariable ("SHELL", NULL);
    SetEnvironmentVariable ("TERM", NULL);
    if (!CreateProcess (NULL,
                   cmd,
                   NULL,
                   NULL,
                   FALSE,
                   CREATE_NEW_CONSOLE,
                   NULL,
                   NULL,
                   &si,
                   &pi)) {
            char    e[1024];

        sprintf (e, "emacs-run.exe: CreateProcess Failed! (%d)", GetLastError ());
        MessageBox (NULL, e, "emacs-run", MB_ICONERROR);
        return EXIT_FAILURE;
    }

    WaitForSingleObject (pi.hProcess, INFINITE);
    CloseHandle (pi.hProcess);
    CloseHandle (pi.hThread);

    return EXIT_SUCCESS;
}

/**
 * NB
 *   - Will not handle very long command lines (should not be needed
 *     for a simple launcher).
 *   - Compiles with: cl /DWIN32 emacs-run.c user32.lib
 */


