]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sys_shared.c
sys: commenting and tidying
[xonotic/darkplaces.git] / sys_shared.c
index 0bb69c2576d2cffc77006a1743cb9e831d46cac9..286ff7e5a80f1e13df408a3d3e470ea3b9ded7e1 100644 (file)
@@ -4,17 +4,14 @@
 # endif
 #endif
 
-#include "quakedef.h"
-#include "taskqueue.h"
-#include "thread.h"
-#include "libcurl.h"
-
 #define SUPPORTDLL
 
 #ifdef WIN32
 # include <windows.h>
 # include <mmsystem.h> // timeGetTime
 # include <time.h> // localtime
+# include <conio.h> // _kbhit, _getch, _putch
+# include <io.h> // write; Include this BEFORE darkplaces.h because it uses strncpy which trips DP_STATIC_ASSERT
 #ifdef _MSC_VER
 #pragma comment(lib, "winmm.lib")
 #endif
@@ -22,6 +19,9 @@
 # ifdef __FreeBSD__
 #  include <sys/sysctl.h>
 # endif
+# ifdef __ANDROID__
+#  include <android/log.h>
+# endif
 # include <unistd.h>
 # include <fcntl.h>
 # include <sys/time.h>
 
 #include <signal.h>
 
+#include "quakedef.h"
+#include "taskqueue.h"
+#include "thread.h"
+#include "libcurl.h"
+
+sys_t sys;
+
 static char sys_timestring[128];
 char *Sys_TimeString(const char *timeformat)
 {
@@ -60,6 +67,15 @@ void Sys_Quit (int returnvalue)
                Sys_AllowProfiling(false);
        host.state = host_shutdown;
        Host_Shutdown();
+
+#ifdef __ANDROID__
+       Sys_AllowProfiling(false);
+#endif
+#ifndef WIN32
+       fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
+#endif
+       fflush(stdout);
+
        exit(returnvalue);
 }
 
@@ -483,7 +499,7 @@ double Sys_Sleep(double time)
        }
 
        if(sys_debugsleep.integer)
-               Sys_Printf("sys_debugsleep: requesting %u ", microseconds);
+               Con_Printf("sys_debugsleep: requesting %u ", microseconds);
        dt = Sys_DirtyTime();
 
        // less important on newer libcurl so no need to disturb dedicated servers
@@ -538,10 +554,54 @@ double Sys_Sleep(double time)
 
        dt = Sys_DirtyTime() - dt;
        if(sys_debugsleep.integer)
-               Sys_Printf(" got %u oversleep %d\n", (unsigned int)(dt * 1000000), (unsigned int)(dt * 1000000) - microseconds);
+               Con_Printf(" got %u oversleep %d\n", (unsigned int)(dt * 1000000), (unsigned int)(dt * 1000000) - microseconds);
        return (dt < 0 || dt >= 1800) ? 0 : dt;
 }
 
+
+/*
+===============================================================================
+
+STDIO
+
+===============================================================================
+*/
+
+void Sys_Print(const char *text)
+{
+#ifdef __ANDROID__
+       if (developer.integer > 0)
+       {
+               __android_log_write(ANDROID_LOG_DEBUG, sys.argv[0], text);
+       }
+#else
+       if(sys.outfd < 0)
+               return;
+  #ifndef WIN32
+       // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
+       // this is because both go to /dev/tty by default!
+       {
+               int origflags = fcntl (sys.outfd, F_GETFL, 0);
+               fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
+  #else
+    #define write _write
+  #endif
+               while(*text)
+               {
+                       fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
+                       if(written <= 0)
+                               break; // sorry, I cannot do anything about this error - without an output
+                       text += written;
+               }
+  #ifndef WIN32
+               fcntl (sys.outfd, F_SETFL, origflags);
+       }
+  #endif
+       //fprintf(stdout, "%s", text);
+#endif
+}
+
+/// for the console to report failures inside Con_Printf()
 void Sys_Printf(const char *fmt, ...)
 {
        va_list argptr;
@@ -554,6 +614,100 @@ void Sys_Printf(const char *fmt, ...)
        Sys_Print(msg);
 }
 
+/// Reads a line from POSIX stdin or the Windows console
+char *Sys_ConsoleInput(void)
+{
+       static char text[MAX_INPUTLINE];
+       static unsigned int len = 0;
+#ifdef WIN32
+       int c;
+
+       // read a line out
+       while (_kbhit ())
+       {
+               c = _getch ();
+               if (c == '\r')
+               {
+                       text[len] = '\0';
+                       _putch ('\n');
+                       len = 0;
+                       return text;
+               }
+               if (c == '\b')
+               {
+                       if (len)
+                       {
+                               _putch (c);
+                               _putch (' ');
+                               _putch (c);
+                               len--;
+                       }
+                       continue;
+               }
+               if (len < sizeof (text) - 1)
+               {
+                       _putch (c);
+                       text[len] = c;
+                       len++;
+               }
+       }
+#else
+       fd_set fdset;
+       struct timeval timeout;
+       FD_ZERO(&fdset);
+       FD_SET(0, &fdset); // stdin
+       timeout.tv_sec = 0;
+       timeout.tv_usec = 0;
+       if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
+       {
+               len = read (0, text, sizeof(text) - 1);
+               if (len >= 1)
+               {
+                       // rip off the \n and terminate
+                       // div0: WHY? console code can deal with \n just fine
+                       // this caused problems with pasting stuff into a terminal window
+                       // so, not ripping off the \n, but STILL keeping a NUL terminator
+                       text[len] = 0;
+                       return text;
+               }
+       }
+#endif
+       return NULL;
+}
+
+
+/*
+===============================================================================
+
+Startup and Shutdown
+
+===============================================================================
+*/
+
+void Sys_Error (const char *error, ...)
+{
+       va_list argptr;
+       char string[MAX_INPUTLINE];
+
+// change stdin to non blocking
+#ifndef WIN32
+       fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
+#endif
+
+       va_start (argptr,error);
+       dpvsnprintf (string, sizeof (string), error, argptr);
+       va_end (argptr);
+
+       Con_Printf(CON_ERROR "Engine Error: %s\n", string);
+
+       // don't want a dead window left blocking the OS UI or the crash dialog
+       Host_Shutdown();
+
+       Sys_SDL_Dialog("Engine Error", string);
+
+       exit (1);
+}
+
 #ifndef WIN32
 static const char *Sys_FindInPATH(const char *name, char namesep, const char *PATH, char pathsep, char *buf, size_t bufsize)
 {