/*

  Screensaver wraparound program to start the alternate screensaver.

  German Version by Markus Hörl  ||  http://jkdefrag.8qm.de or http://www.jkdefrag.de

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  For the full text of the license see the "License gpl.txt" file.



  Jeroen C. Kessels
  Internet Engineer
  http://www.kessels.com/

*/




#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <time.h>
#include <sys/timeb.h>

// Only needed for pre-2008 compiler
#if _MSC_VER < 1500
#include "x64glue.h"
#endif




/* Include the variables used by the defragger library. */
//#include "Common1.h"
#define NO     0
#define YES    1




/* Search case-insensitive for a substring. */
WCHAR *stristr(WCHAR *Haystack, WCHAR *Needle) {
  WCHAR *p1;
  register int i;

  if ((Haystack == NULL) || (Needle == NULL)) return(NULL);

  p1 = Haystack;
  i = (int)wcslen(Needle);
  while (*p1 != 0) {
    if (_wcsnicmp(p1,Needle,i) == 0) return(p1);
    p1++;
    }

  return(NULL);
  }




void ReadKey(WCHAR *Path, WCHAR *Name, WCHAR *Output) {
  HKEY Key;
  DWORD KeyDisposition;
  int Result;
  DWORD Length;

  *Output = 0;
  Result = RegCreateKeyExW(HKEY_CURRENT_USER,Path,0,NULL,REG_OPTION_NON_VOLATILE,
    KEY_READ,NULL,&Key,&KeyDisposition);
  if (Result == ERROR_SUCCESS) {
    Length = sizeof(WCHAR) * BUFSIZ;
    Result = RegQueryValueExW(Key,Name,NULL,NULL,(BYTE *)Output,&Length);
    RegCloseKey(Key);
    }
  if (Result != ERROR_SUCCESS) *Output = 0;
  }




int __stdcall WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow) {
  WCHAR MyName[MAX_PATH];
  WCHAR MyShortName[MAX_PATH];
  WCHAR DefragPath[MAX_PATH];
  WCHAR AlternatePath[MAX_PATH];
  int Preview;
  int argc;
  WCHAR **argv;
  WCHAR **MyArgv;
  intptr_t Result;
  WCHAR *p1;
  WCHAR w1[MAX_PATH];
  int i;

  /* Fetch the name of this program. */
  GetModuleFileNameW(NULL,MyName,MAX_PATH);
  GetShortPathNameW(MyName,MyShortName,MAX_PATH);
  GetLongPathNameW(MyShortName,MyName,MAX_PATH);

  /* Determine path to the JkDefragScreenSaver program. */
  wcscpy_s(DefragPath,MAX_PATH,MyName);
  p1 = stristr(DefragPath,L"1.exe");
  if (p1 == NULL) p1 = stristr(DefragPath,L".exe");
  if (p1 == NULL) p1 = stristr(DefragPath,L"1.scr");
  if (p1 == NULL) p1 = stristr(DefragPath,L".scr");
  if (p1 != NULL) {
      *p1 = '\0';
      wcscat_s(DefragPath,MAX_PATH,L".exe");
    } else {
      *DefragPath = 0;
      }

  /* Make a local copy of the argv array, all values except argv[0]. */
  Preview = NO;
  argv = CommandLineToArgvW(GetCommandLineW(),&argc);
  MyArgv = (WCHAR **)malloc((argc + 1) * sizeof(WCHAR *));
  for (i = 0; i < argc; i++) {
    MyArgv[i] = argv[i];
    if (_wcsicmp(argv[i],L"/p") == 0) Preview = YES;
    }
  MyArgv[argc] = NULL;

  /* Run the JkDefrag screensaver. */
  if (Preview == NO) {
    swprintf_s(w1,MAX_PATH,L"\"%s\"",DefragPath);
    MyArgv[0] = w1;
    Result = _wspawnv(P_WAIT,DefragPath,MyArgv);

    /* If the screensaver did not return 1 then exit. */
    if (Result != 1) return(0);
    }

  /* Fetch the alternate screensaver setting from the registry. */
  ReadKey(L"Control Panel\\Screen Saver.JkDefrag",L"AlternateScreenSaver",AlternatePath);
  if ((*AlternatePath != '\0') && (_wcsicmp(AlternatePath,MyName) == 0)) {
    *AlternatePath = '\0';
    }
  if ((*AlternatePath != '\0') && (_wcsicmp(AlternatePath,MyShortName) == 0)) {
    *AlternatePath = '\0';
    }
  if (*AlternatePath == '\0') return(0);

  /* Run the alternate screensaver. */
  swprintf_s(w1,MAX_PATH,L"\"%s\"",AlternatePath);
  MyArgv[0] = w1;
  if (Preview == YES) {
      Result = _wexecv(AlternatePath,MyArgv);
    } else {
      Result = _wspawnv(P_WAIT,AlternatePath,MyArgv);
      }

  return(0);
  }
/*
  German Version 3.36 by Markus Hörl  ||  http://jkdefrag.8qm.de or http://www.jkdefrag.de
*/