implemented support for GL_ARB_texture_compression - this is controlled
[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         host_shuttingdown = true;
22         Host_Shutdown();
23         exit(returnvalue);
24 }
25
26 /*
27 ===============================================================================
28
29 DLL MANAGEMENT
30
31 ===============================================================================
32 */
33
34 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
35 {
36         const dllfunction_t *func;
37         dllhandle_t dllhandle = 0;
38         unsigned int i;
39
40         if (handle == NULL)
41                 return false;
42
43 #ifndef WIN32
44 #ifdef PREFER_PRELOAD
45         dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
46         if(dllhandle)
47         {
48                 for (func = fcts; func && func->name != NULL; func++)
49                         if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
50                         {
51                                 dlclose(dllhandle);
52                                 goto notfound;
53                         }
54                 Con_Printf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]);
55                 *handle = dllhandle;
56                 return true;
57         }
58 notfound:
59 #endif
60 #endif
61
62         // Initializations
63         for (func = fcts; func && func->name != NULL; func++)
64                 *func->funcvariable = NULL;
65
66         // Try every possible name
67         Con_Printf ("Trying to load library...");
68         for (i = 0; dllnames[i] != NULL; i++)
69         {
70                 Con_Printf (" \"%s\"", dllnames[i]);
71 #ifdef WIN32
72                 dllhandle = LoadLibrary (dllnames[i]);
73 #else
74                 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
75 #endif
76                 if (dllhandle)
77                         break;
78         }
79
80         // see if the names can be loaded relative to the executable path
81         // (this is for Mac OSX which does not check next to the executable)
82         if (!dllhandle && strrchr(com_argv[0], '/'))
83         {
84                 char path[MAX_OSPATH];
85                 strlcpy(path, com_argv[0], sizeof(path));
86                 strrchr(path, '/')[1] = 0;
87                 for (i = 0; dllnames[i] != NULL; i++)
88                 {
89                         char temp[MAX_OSPATH];
90                         strlcpy(temp, path, sizeof(temp));
91                         strlcat(temp, dllnames[i], sizeof(temp));
92                         Con_Printf (" \"%s\"", temp);
93 #ifdef WIN32
94                         dllhandle = LoadLibrary (temp);
95 #else
96                         dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
97 #endif
98                         if (dllhandle)
99                                 break;
100                 }
101         }
102
103         // No DLL found
104         if (! dllhandle)
105         {
106                 Con_Printf(" - failed.\n");
107                 return false;
108         }
109
110         Con_Printf(" - loaded.\n");
111
112         // Get the function adresses
113         for (func = fcts; func && func->name != NULL; func++)
114                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
115                 {
116                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
117                         Sys_UnloadLibrary (&dllhandle);
118                         return false;
119                 }
120
121         *handle = dllhandle;
122         return true;
123 }
124
125 void Sys_UnloadLibrary (dllhandle_t* handle)
126 {
127         if (handle == NULL || *handle == NULL)
128                 return;
129
130 #ifdef WIN32
131         FreeLibrary (*handle);
132 #else
133         dlclose (*handle);
134 #endif
135
136         *handle = NULL;
137 }
138
139 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
140 {
141 #ifdef WIN32
142         return (void *)GetProcAddress (handle, name);
143 #else
144         return (void *)dlsym (handle, name);
145 #endif
146 }
147