UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unix_shared.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include <unistd.h>
27 #include <sys/time.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <pwd.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <locale.h>
35 #include <signal.h>
36 #include <dirent.h>
37 
38 #include "../../common/common.h"
39 #include "../system.h"
40 
41 int Sys_Milliseconds (void)
42 {
43  struct timeval tp;
44  struct timezone tzp;
45  static int secbase = 0;
46 
47  gettimeofday(&tp, &tzp);
48 
49  if (!secbase) {
50  secbase = tp.tv_sec;
51  return tp.tv_usec / 1000;
52  }
53 
54  return (tp.tv_sec - secbase) * 1000 + tp.tv_usec / 1000;
55 }
56 
60 int Sys_Setenv (const char* name, const char* value)
61 {
62  if (value && value[0] != '\0')
63  return setenv(name, value, 1);
64  else
65  return unsetenv(name);
66 }
67 
68 void Sys_Sleep (int milliseconds)
69 {
70 #if 0
71  struct timespec sleep, remaining;
72  sleep.tv_sec = (long) milliseconds / 1000;
73  sleep.tv_nsec = 1000000 * (milliseconds - (long) milliseconds);
74  while (nanosleep(&sleep, &remaining) < 0 && errno == EINTR)
75  /* If nanosleep has been interrupted by a signal, adjust the
76  * sleeping period and return to sleep. */
77  sleep = remaining;
78 #endif
79  if (milliseconds < 1)
80  milliseconds = 1;
81  usleep(milliseconds * 1000);
82 }
83 
84 void Sys_Breakpoint (void)
85 {
86  raise(SIGTRAP);
87 }
88 
89 #ifdef COMPILE_UFO
90 const char* Sys_SetLocale (const char* localeID)
91 {
92  const char* locale;
93 
94 # ifndef __sun
95  unsetenv("LANGUAGE");
96 # endif /* __sun */
97 # ifdef __APPLE__
98  if (localeID[0] != '\0') {
99  if (Sys_Setenv("LANGUAGE", localeID) != 0)
100  Com_Printf("...setenv for LANGUAGE failed: %s\n", localeID);
101  if (Sys_Setenv("LC_ALL", localeID) != 0)
102  Com_Printf("...setenv for LC_ALL failed: %s\n", localeID);
103  }
104 # endif /* __APPLE__ */
105 
106  Sys_Setenv("LC_NUMERIC", "C");
107  setlocale(LC_NUMERIC, "C");
108 
109  /* set to system default */
110  setlocale(LC_ALL, "C");
111  locale = setlocale(LC_MESSAGES, localeID);
112  if (!locale) {
113  if (Sys_Setenv("LANGUAGE", localeID) != 0) {
114  locale = localeID;
115  }
116  }
117  if (!locale) {
118  Com_DPrintf(DEBUG_CLIENT, "...could not set to language: %s\n", localeID);
119  locale = setlocale(LC_MESSAGES, "");
120  if (!locale) {
121  Com_DPrintf(DEBUG_CLIENT, "...could not set to system language\n");
122  }
123  return nullptr;
124  }
125 
126  Com_Printf("...using language: %s\n", locale);
127  return locale;
128 }
129 
130 const char* Sys_GetLocale (void)
131 {
132  /* Calling with nullptr param should return current system settings. */
133  const char* currentLocale = setlocale(LC_MESSAGES, nullptr);
134  if (Q_strvalid(currentLocale))
135  return currentLocale;
136  return "C";
137 }
138 #endif
139 
140 #ifdef COMPILE_UFO
141 void Sys_SetAffinityAndPriority (void)
142 {
143  if (sys_affinity->modified) {
144  sys_affinity->modified = false;
145  }
146 
147  if (sys_priority->modified) {
148  sys_priority->modified = false;
149  }
150 }
151 #endif
152 
153 #ifdef USE_SIGNALS
154 
157 static void Sys_Signal (int s)
158 {
159  switch (s) {
160  case SIGHUP:
161  case SIGTERM:
162  case SIGQUIT:
163 #ifndef COMPILE_UFO
164  case SIGINT:
165 #endif
166  Com_Printf("Received signal %d, quitting..\n", s);
167  Sys_Quit();
168  break;
169 #ifdef COMPILE_UFO
170  case SIGINT:
171  Com_Printf("Received signal %d, quitting..\n", s);
172  Com_Quit();
173  break;
174 #endif
175  default:
176  Sys_Error("Received signal %d.", s);
177  break;
178  }
179 }
180 #endif
181 
182 void Sys_InitSignals (void)
183 {
184 #ifdef USE_SIGNALS
185  signal(SIGHUP, Sys_Signal);
186  signal(SIGINT, Sys_Signal);
187  signal(SIGQUIT, Sys_Signal);
188  signal(SIGILL, Sys_Signal);
189  signal(SIGABRT, Sys_Signal);
190  signal(SIGFPE, Sys_Signal);
191  signal(SIGSEGV, Sys_Signal);
192  signal(SIGTERM, Sys_Signal);
193 #endif
194 }
195 
196 void Sys_OpenURL (const char* url)
197 {
198  char buf[512];
199 #ifdef __APPLE__
200  Com_sprintf(buf, sizeof(buf), "open \"%s\"", url);
201 #else
202  Com_sprintf(buf, sizeof(buf), "xdg-open \"%s\"", url);
203 #endif
204  system(buf) || 0;
205 }
void Sys_Error(const char *error,...)
Definition: g_main.cpp:421
void Sys_SetAffinityAndPriority(void)
cvar_t * sys_affinity
Definition: common.cpp:60
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
void Com_Quit(void)
Definition: common.cpp:511
void Sys_Sleep(int milliseconds)
Definition: unix_shared.cpp:68
const char * Sys_SetLocale(const char *localeID)
Definition: win_shared.cpp:147
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void Sys_OpenURL(const char *url)
voidpf void * buf
Definition: ioapi.h:42
#define Q_strvalid(string)
Definition: shared.h:141
#define DEBUG_CLIENT
Definition: defines.h:59
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition: common.cpp:398
const char * Sys_GetLocale(void)
Definition: win_shared.cpp:156
void Sys_InitSignals(void)
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
int Sys_Milliseconds(void)
Definition: unix_shared.cpp:41
int Sys_Setenv(const char *name, const char *value)
set/unset environment variables (empty value removes it)
Definition: unix_shared.cpp:60
bool modified
Definition: cvar.h:79
void Sys_Quit(void)
cvar_t * sys_priority
Definition: common.cpp:59
void Sys_Breakpoint(void)
Definition: unix_shared.cpp:84