]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_shared.c
fixed bug where vid_dgamouse cvar broke support for absolute mouse
[divverent/darkplaces.git] / sys_shared.c
1 #include "quakedef.h"
2 # include <time.h>
3 #ifndef WIN32
4 # include <unistd.h>
5 # include <fcntl.h>
6 # include <dlfcn.h>
7 #endif
8
9 static char sys_timestring[128];
10 char *Sys_TimeString(const char *timeformat)
11 {
12         time_t mytime = time(NULL);
13         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
14         return sys_timestring;
15 }
16
17
18 extern qboolean host_shuttingdown;
19 void Sys_Quit (int returnvalue)
20 {
21         if (COM_CheckParm("-profilegameonly"))
22                 Sys_AllowProfiling(false);
23         host_shuttingdown = true;
24         Host_Shutdown();
25         exit(returnvalue);
26 }
27
28 void Sys_AllowProfiling(qboolean enable)
29 {
30 #if defined(__linux__) || defined(__FreeBSD__)
31 int moncontrol(int);
32         moncontrol(enable);
33 #endif
34 }
35
36
37 /*
38 ===============================================================================
39
40 DLL MANAGEMENT
41
42 ===============================================================================
43 */
44
45 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
46 {
47         const dllfunction_t *func;
48         dllhandle_t dllhandle = 0;
49         unsigned int i;
50
51         if (handle == NULL)
52                 return false;
53
54 #ifndef WIN32
55 #ifdef PREFER_PRELOAD
56         dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
57         if(dllhandle)
58         {
59                 for (func = fcts; func && func->name != NULL; func++)
60                         if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
61                         {
62                                 dlclose(dllhandle);
63                                 goto notfound;
64                         }
65                 Con_Printf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]);
66                 *handle = dllhandle;
67                 return true;
68         }
69 notfound:
70 #endif
71 #endif
72
73         // Initializations
74         for (func = fcts; func && func->name != NULL; func++)
75                 *func->funcvariable = NULL;
76
77         // Try every possible name
78         Con_Printf ("Trying to load library...");
79         for (i = 0; dllnames[i] != NULL; i++)
80         {
81                 Con_Printf (" \"%s\"", dllnames[i]);
82 #ifdef WIN32
83                 dllhandle = LoadLibrary (dllnames[i]);
84 #else
85                 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
86 #endif
87                 if (dllhandle)
88                         break;
89         }
90
91         // see if the names can be loaded relative to the executable path
92         // (this is for Mac OSX which does not check next to the executable)
93         if (!dllhandle && strrchr(com_argv[0], '/'))
94         {
95                 char path[MAX_OSPATH];
96                 strlcpy(path, com_argv[0], sizeof(path));
97                 strrchr(path, '/')[1] = 0;
98                 for (i = 0; dllnames[i] != NULL; i++)
99                 {
100                         char temp[MAX_OSPATH];
101                         strlcpy(temp, path, sizeof(temp));
102                         strlcat(temp, dllnames[i], sizeof(temp));
103                         Con_Printf (" \"%s\"", temp);
104 #ifdef WIN32
105                         dllhandle = LoadLibrary (temp);
106 #else
107                         dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
108 #endif
109                         if (dllhandle)
110                                 break;
111                 }
112         }
113
114         // No DLL found
115         if (! dllhandle)
116         {
117                 Con_Printf(" - failed.\n");
118                 return false;
119         }
120
121         Con_Printf(" - loaded.\n");
122
123         // Get the function adresses
124         for (func = fcts; func && func->name != NULL; func++)
125                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
126                 {
127                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
128                         Sys_UnloadLibrary (&dllhandle);
129                         return false;
130                 }
131
132         *handle = dllhandle;
133         return true;
134 }
135
136 void Sys_UnloadLibrary (dllhandle_t* handle)
137 {
138         if (handle == NULL || *handle == NULL)
139                 return;
140
141 #ifdef WIN32
142         FreeLibrary (*handle);
143 #else
144         dlclose (*handle);
145 #endif
146
147         *handle = NULL;
148 }
149
150 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
151 {
152 #ifdef WIN32
153         return (void *)GetProcAddress (handle, name);
154 #else
155         return (void *)dlsym (handle, name);
156 #endif
157 }
158