]> sjero.net Git - wget/blobdiff - src/mswindows.c
[svn] Windows fixes by Gisle Vanem.
[wget] / src / mswindows.c
index 95a0099fac5d4708060049fc5a7a38b11cb7109f..a3a186a8e97fd0bf97cce5d23d8fb1d65428f4ed 100644 (file)
@@ -57,11 +57,24 @@ so, delete this exception statement from your version.  */
 extern int errno;
 #endif
 
+#ifndef ES_SYSTEM_REQUIRED
+#define ES_SYSTEM_REQUIRED  0x00000001
+#endif
+
+#ifndef ES_CONTINUOUS
+#define ES_CONTINUOUS       0x80000000
+#endif
+
+
 /* Defined in log.c.  */
 void log_request_redirect_output PARAMS ((const char *));
 
+static DWORD set_sleep_mode (DWORD mode);
+
+static DWORD pwr_mode = 0;
 static int windows_nt_p;
 
+#ifndef HAVE_SLEEP
 
 /* Emulation of Unix sleep.  */
 
@@ -70,11 +83,14 @@ sleep (unsigned seconds)
 {
   return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
 }
+#endif
 
+#ifndef HAVE_USLEEP
 /* Emulation of Unix usleep().  This has a granularity of
    milliseconds, but that's ok because:
 
-   a) Wget is only using it with milliseconds;
+   a) Wget is only using it with milliseconds [not anymore, but b)
+      still applies];
 
    b) You can't rely on usleep's granularity anyway.  If a caller
    expects usleep to respect every microsecond, he's in for a
@@ -86,21 +102,7 @@ usleep (unsigned long usec)
   SleepEx (usec / 1000, TRUE);
   return 0;
 }
-
-static char *
-read_registry (HKEY hkey, char *subkey, char *valuename, char *buf, int *len)
-{
-  HKEY result;
-  DWORD size = *len;
-  DWORD type = REG_SZ;
-  if (RegOpenKeyEx (hkey, subkey, NULL, KEY_READ, &result) != ERROR_SUCCESS)
-    return NULL;
-  if (RegQueryValueEx (result, valuename, NULL, &type, buf, &size) != ERROR_SUCCESS)
-    buf = NULL;
-  *len = size;
-  RegCloseKey (result);
-  return buf;
-}
+#endif  /* HAVE_USLEEP */
 
 void
 windows_main_junk (int *argc, char **argv, char **exec_name)
@@ -120,6 +122,9 @@ static void
 ws_cleanup (void)
 {
   WSACleanup ();
+  if (pwr_mode)
+     set_sleep_mode (pwr_mode);
+  pwr_mode = 0;
 }
 
 static void
@@ -136,7 +141,7 @@ fork_to_background (void)
 
   if (!opt.lfilename)
     {
-      opt.lfilename = unique_name (DEFAULT_LOGFILE);
+      opt.lfilename = unique_name (DEFAULT_LOGFILE, 0);
       changedp = 1;
     }
   printf (_("Continuing in background.\n"));
@@ -165,7 +170,7 @@ ws_handler (DWORD dwEvent)
     case CTRL_CLOSE_EVENT:
     case CTRL_LOGOFF_EVENT:
     default:
-      WSACleanup ();
+      ws_cleanup ();
       return FALSE;
     }
   return TRUE;
@@ -261,6 +266,7 @@ ws_startup (void)
       exit (1);
     }
   atexit (ws_cleanup);
+  pwr_mode = set_sleep_mode (0);
   SetConsoleCtrlHandler (ws_handler, TRUE);
 }
 
@@ -290,3 +296,31 @@ int borland_utime(const char *path, const struct utimbuf *times)
   return res;
 }
 #endif
+
+/*
+ * Prevent Windows entering sleep/hibernation-mode while wget is doing a lengthy transfer.
+ * Windows does by default not consider network activity in console-programs as activity !
+ * Works on Win-98/ME/2K and up.
+ */
+static
+DWORD set_sleep_mode (DWORD mode)
+{
+  HMODULE mod = LoadLibrary ("kernel32.dll");
+  DWORD (*_SetThreadExecutionState) (DWORD) = NULL;
+  DWORD rc = (DWORD)-1;
+
+  if (mod)
+     (void*)_SetThreadExecutionState = GetProcAddress ((HINSTANCE)mod, "SetThreadExecutionState");
+
+  if (_SetThreadExecutionState)
+    {
+      if (mode == 0)  /* first time */
+         mode = (ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
+      rc = (*_SetThreadExecutionState) (mode);
+    }
+  if (mod)
+     FreeLibrary (mod);
+  DEBUGP (("set_sleep_mode(): mode 0x%08lX, rc 0x%08lX\n", mode, rc));
+  return (rc);
+}
+