]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sys_shared.c
Fix client version of ambientsound
[xonotic/darkplaces.git] / sys_shared.c
index 970d920404c844ee59501632ffd3851181717f46..84e210aa433c3a7690264aad022dce4787c575a0 100644 (file)
@@ -5,6 +5,7 @@
 #endif
 
 #include "quakedef.h"
+#include "taskqueue.h"
 #include "thread.h"
 
 #define SUPPORTDLL
@@ -17,6 +18,9 @@
 #pragma comment(lib, "winmm.lib")
 #endif
 #else
+# ifdef __FreeBSD__
+#  include <sys/sysctl.h>
+# endif
 # include <unistd.h>
 # include <fcntl.h>
 # include <sys/time.h>
@@ -41,16 +45,19 @@ char *Sys_TimeString(const char *timeformat)
 }
 
 
-extern qboolean host_shuttingdown;
 void Sys_Quit (int returnvalue)
 {
        // Unlock mutexes because the quit command may jump directly here, causing a deadlock
-       Cbuf_UnlockThreadMutex();
+       if (cmd_client.text_lock)
+               Cbuf_Unlock(&cmd_client);
+       if (cmd_server.text_lock)
+               Cbuf_Unlock(&cmd_server);
        SV_UnlockThreadMutex();
+       TaskQueue_Frame(true);
 
        if (COM_CheckParm("-profilegameonly"))
                Sys_AllowProfiling(false);
-       host_shuttingdown = true;
+       host.state = host_shutdown;
        Host_Shutdown();
        exit(returnvalue);
 }
@@ -69,7 +76,7 @@ void Sys_AllowProfiling(qboolean enable)
        else
                moncleanup();
 #endif
-#elif defined(__linux__) || defined(__FreeBSD__)
+#elif (defined(__linux__) && (defined(__GLIBC__) || defined(__GNU_LIBRARY__))) || defined(__FreeBSD__)
        extern int moncontrol(int);
        moncontrol(enable);
 #endif
@@ -156,7 +163,7 @@ notfound:
 #else
                dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
 #endif
-               if (Sys_LoadLibraryFunctions(dllhandle, fcts, true, (dllnames[i+1] != NULL) || (strrchr(com_argv[0], '/'))))
+               if (Sys_LoadLibraryFunctions(dllhandle, fcts, true, (dllnames[i+1] != NULL) || (strrchr(sys.argv[0], '/'))))
                        break;
                else
                        Sys_UnloadLibrary (&dllhandle);
@@ -164,10 +171,10 @@ notfound:
 
        // see if the names can be loaded relative to the executable path
        // (this is for Mac OSX which does not check next to the executable)
-       if (!dllhandle && strrchr(com_argv[0], '/'))
+       if (!dllhandle && strrchr(sys.argv[0], '/'))
        {
                char path[MAX_OSPATH];
-               strlcpy(path, com_argv[0], sizeof(path));
+               strlcpy(path, sys.argv[0], sizeof(path));
                strrchr(path, '/')[1] = 0;
                for (i = 0; dllnames[i] != NULL; i++)
                {
@@ -238,11 +245,10 @@ void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
 # define HAVE_Sleep 1
 #endif
 
+#ifndef WIN32
 #if defined(CLOCK_MONOTONIC) || defined(CLOCK_HIRES)
 # define HAVE_CLOCKGETTIME 1
 #endif
-
-#ifndef WIN32
 // FIXME improve this check, manpage hints to DST_NONE
 # define HAVE_GETTIMEOFDAY 1
 #endif
@@ -261,17 +267,17 @@ void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
 #endif
 
 // this one is referenced elsewhere
-cvar_t sys_usenoclockbutbenchmark = {CVAR_SAVE, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."};
+cvar_t sys_usenoclockbutbenchmark = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."};
 
 // these are not
-static cvar_t sys_debugsleep = {0, "sys_debugsleep", "0", "write requested and attained sleep times to standard output, to be used with gnuplot"};
-static cvar_t sys_usesdlgetticks = {CVAR_SAVE, "sys_usesdlgetticks", "0", "use SDL_GetTicks() timer (less accurate, for debugging)"};
-static cvar_t sys_usesdldelay = {CVAR_SAVE, "sys_usesdldelay", "0", "use SDL_Delay() (less accurate, for debugging)"};
+static cvar_t sys_debugsleep = {CVAR_CLIENT | CVAR_SERVER, "sys_debugsleep", "0", "write requested and attained sleep times to standard output, to be used with gnuplot"};
+static cvar_t sys_usesdlgetticks = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "sys_usesdlgetticks", "0", "use SDL_GetTicks() timer (less accurate, for debugging)"};
+static cvar_t sys_usesdldelay = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "sys_usesdldelay", "0", "use SDL_Delay() (less accurate, for debugging)"};
 #if HAVE_QUERYPERFORMANCECOUNTER
-static cvar_t sys_usequeryperformancecounter = {CVAR_SAVE, "sys_usequeryperformancecounter", "0", "use windows QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power) for timing rather than timeGetTime function (which has issues on some motherboards)"};
+static cvar_t sys_usequeryperformancecounter = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "sys_usequeryperformancecounter", "0", "use windows QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power) for timing rather than timeGetTime function (which has issues on some motherboards)"};
 #endif
 #if HAVE_CLOCKGETTIME
-static cvar_t sys_useclockgettime = {CVAR_SAVE, "sys_useclockgettime", "0", "use POSIX clock_gettime function (which has issues if the system clock speed is far off, as it can't get fixed by NTP) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
+static cvar_t sys_useclockgettime = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "sys_useclockgettime", "1", "use POSIX clock_gettime function (not adjusted by NTP on some older Linux kernels) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
 #endif
 
 static double benchmark_time; // actually always contains an integer amount of milliseconds, will eventually "overflow"
@@ -311,7 +317,7 @@ double Sys_DirtyTime(void)
 #if HAVE_QUERYPERFORMANCECOUNTER
        if (sys_usequeryperformancecounter.integer)
        {
-               // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
+               // LadyHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
                // QueryPerformanceCounter
                // platform:
                // Windows 95/98/ME/NT/2000/XP
@@ -489,12 +495,17 @@ static const char *Sys_FindInPATH(const char *name, char namesep, const char *PA
 static const char *Sys_FindExecutableName(void)
 {
 #if defined(WIN32)
-       return com_argv[0];
+       return sys.argv[0];
 #else
        static char exenamebuf[MAX_OSPATH+1];
        ssize_t n = -1;
 #if defined(__FreeBSD__)
-       n = readlink("/proc/curproc/file", exenamebuf, sizeof(exenamebuf)-1);
+       int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
+       size_t exenamebuflen = sizeof(exenamebuf)-1;
+       if (sysctl(mib, 4, exenamebuf, &exenamebuflen, NULL, 0) == 0)
+       {
+               n = exenamebuflen;
+       }
 #elif defined(__linux__)
        n = readlink("/proc/self/exe", exenamebuf, sizeof(exenamebuf)-1);
 #endif
@@ -503,18 +514,18 @@ static const char *Sys_FindExecutableName(void)
                exenamebuf[n] = 0;
                return exenamebuf;
        }
-       if(strchr(com_argv[0], '/'))
-               return com_argv[0]; // possibly a relative path
+       if(strchr(sys.argv[0], '/'))
+               return sys.argv[0]; // possibly a relative path
        else
-               return Sys_FindInPATH(com_argv[0], '/', getenv("PATH"), ':', exenamebuf, sizeof(exenamebuf));
+               return Sys_FindInPATH(sys.argv[0], '/', getenv("PATH"), ':', exenamebuf, sizeof(exenamebuf));
 #endif
 }
 
 void Sys_ProvideSelfFD(void)
 {
-       if(com_selffd != -1)
+       if(sys.selffd != -1)
                return;
-       com_selffd = FS_SysOpenFD(Sys_FindExecutableName(), "rb", false);
+       sys.selffd = FS_SysOpenFD(Sys_FindExecutableName(), "rb", false);
 }
 
 // for x86 cpus only...  (x64 has SSE2_PRESENT)
@@ -589,17 +600,15 @@ qboolean Sys_HaveSSE2(void)
 #if defined(__linux__)
 #include <sys/resource.h>
 #include <errno.h>
-static int nicelevel;
-static qboolean nicepossible;
-static qboolean isnice;
+
 void Sys_InitProcessNice (void)
 {
        struct rlimit lim;
-       nicepossible = false;
+       sys.nicepossible = false;
        if(COM_CheckParm("-nonice"))
                return;
        errno = 0;
-       nicelevel = getpriority(PRIO_PROCESS, 0);
+       sys.nicelevel = getpriority(PRIO_PROCESS, 0);
        if(errno)
        {
                Con_Printf("Kernel does not support reading process priority - cannot use niceness\n");
@@ -610,35 +619,35 @@ void Sys_InitProcessNice (void)
                Con_Printf("Kernel does not support lowering nice level again - cannot use niceness\n");
                return;
        }
-       if(lim.rlim_cur != RLIM_INFINITY && nicelevel < (int) (20 - lim.rlim_cur))
+       if(lim.rlim_cur != RLIM_INFINITY && sys.nicelevel < (int) (20 - lim.rlim_cur))
        {
                Con_Printf("Current nice level is below the soft limit - cannot use niceness\n");
                return;
        }
-       nicepossible = true;
-       isnice = false;
+       sys.nicepossible = true;
+       sys.isnice = false;
 }
 void Sys_MakeProcessNice (void)
 {
-       if(!nicepossible)
+       if(!sys.nicepossible)
                return;
-       if(isnice)
+       if(sys.isnice)
                return;
        Con_DPrintf("Process is becoming 'nice'...\n");
        if(setpriority(PRIO_PROCESS, 0, 19))
-               Con_Printf("Failed to raise nice level to %d\n", 19);
-       isnice = true;
+               Con_Errorf("Failed to raise nice level to %d\n", 19);
+       sys.isnice = true;
 }
 void Sys_MakeProcessMean (void)
 {
-       if(!nicepossible)
+       if(!sys.nicepossible)
                return;
-       if(!isnice)
+       if(!sys.isnice)
                return;
        Con_DPrintf("Process is becoming 'mean'...\n");
-       if(setpriority(PRIO_PROCESS, 0, nicelevel))
-               Con_Printf("Failed to lower nice level to %d\n", nicelevel);
-       isnice = false;
+       if(setpriority(PRIO_PROCESS, 0, sys.nicelevel))
+               Con_Errorf("Failed to lower nice level to %d\n", sys.nicelevel);
+       sys.isnice = false;
 }
 #else
 void Sys_InitProcessNice (void)