FS2_Open
Open source remastering of the Freespace 2 engine
osapi_unix.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef WIN32 // Goober5000
13 
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <stdarg.h>
17 
18 #include "cmdline/cmdline.h"
19 #include "globalincs/pstypes.h"
20 #include "io/key.h"
21 #include "io/mouse.h"
22 #include "osapi/outwnd.h"
23 #include "io/joy.h"
24 #include "io/joy_ff.h"
25 #include "osapi/osregistry.h"
26 #include "graphics/2d.h"
27 #include "freespace2/freespace.h"
28 
29 #define THREADED // to use the proper set of macros
30 #include "osapi/osapi.h"
31 
32 // ----------------------------------------------------------------------------------------------------
33 // OSAPI DEFINES/VARS
34 //
35 
36 // os-wide globals
37 static int fAppActive = 1;
38 static char szWinTitle[128];
39 static char szWinClass[128];
40 static int Os_inited = 0;
41 
42 static CRITICAL_SECTION Os_lock;
43 
45 
46 // ----------------------------------------------------------------------------------------------------
47 // OSAPI FORWARD DECLARATIONS
48 //
49 
50 DWORD unix_process(DWORD lparam);
51 
52 // called at shutdown. Makes sure all thread processing terminates.
53 void os_deinit();
54 
55 // ----------------------------------------------------------------------------------------------------
56 // OSAPI FUNCTIONS
57 //
58 
59 // initialization/shutdown functions -----------------------------------------------
60 
61 // detect users home directory
62 const char *detect_home(void)
63 {
64  return (getenv("HOME"));
65 }
66 
67 // If app_name is NULL or ommited, then TITLE is used
68 // for the app name, which is where registry keys are stored.
69 void os_init(const char * wclass, const char * title, const char *app_name, const char *version_string )
70 {
71  // create default ini entries for the user
72  if (os_config_read_string(NULL, NOX("VideocardFs2open"), NULL) == NULL)
73  os_config_write_string(NULL, NOX("VideocardFs2open"), NOX("OGL -(1024x768)x32 bit"));
74 
75  os_init_registry_stuff(Osreg_company_name, title, version_string);
76 
77  strcpy_s( szWinTitle, title );
78  strcpy_s( szWinClass, wclass );
79 
80  INITIALIZE_CRITICAL_SECTION( Os_lock );
81 
82  unix_process(0);
83 
84  // initialized
85  Os_inited = 1;
86 
87  atexit(os_deinit);
88 }
89 
90 // set the main window title
91 void os_set_title( const char *title )
92 {
93  strcpy_s( szWinTitle, title );
94 
95  SDL_WM_SetCaption( szWinTitle, NULL );
96 }
97 
98 extern void gr_opengl_shutdown();
99 
100 // call at program end
102 {
104 
105 #ifndef NDEBUG
106  outwnd_close();
107 #endif
108 }
109 
110 
111 // window management -----------------------------------------------------------------
112 
113 // Returns 1 if app is not the foreground app.
115 {
116  return fAppActive;
117 }
118 
119 // Returns the handle to the main window
120 /*uint os_get_window()
121 {
122  // not used
123  return 0;
124 }*/
125 
126 
127 // process management -----------------------------------------------------------------
128 
129 // Sleeps for n milliseconds or until app becomes active.
130 void os_sleep(int ms)
131 {
132  Sleep(ms);
133 }
134 
135 // Used to stop message processing
137 {
138  ENTER_CRITICAL_SECTION( Os_lock );
139 }
140 
141 // resume message processing
142 void os_resume()
143 {
144  LEAVE_CRITICAL_SECTION( Os_lock );
145 }
146 
147 
148 // ----------------------------------------------------------------------------------------------------
149 // OSAPI FORWARD DECLARATIONS
150 //
151 
152 extern int SDLtoFS2[SDLK_LAST];
153 extern void joy_set_button_state(int button, int state);
154 extern void joy_set_hat_state(int position);
155 
157 {
158  SDL_Event event;
159 
160  while( SDL_PollEvent(&event) ) {
161  switch(event.type) {
162  case SDL_ACTIVEEVENT:
163  if( (event.active.state & SDL_APPACTIVE) || (event.active.state & SDL_APPINPUTFOCUS) ) {
164  if (fAppActive != event.active.gain) {
166  {
167  if (fAppActive)
168  game_pause();
169  else
170  game_unpause();
171  }
172  }
173  fAppActive = event.active.gain;
174  gr_activate(fAppActive);
175  }
176  break;
177 
178  case SDL_KEYDOWN:
179  /*if( (event.key.keysym.mod & KMOD_ALT) && (event.key.keysym.sym == SDLK_RETURN) ) {
180  Gr_screen_mode_switch = 1;
181  gr_activate(1);
182  break;
183  }*/
184 
185  if( SDLtoFS2[event.key.keysym.sym] ) {
186  key_mark( SDLtoFS2[event.key.keysym.sym], 1, 0 );
187  }
188  break;
189 
190  case SDL_KEYUP:
191  /*if( (event.key.keysym.mod & KMOD_ALT) && (event.key.keysym.sym == SDLK_RETURN) ) {
192  Gr_screen_mode_switch = 0;
193  break;
194  }*/
195 
196  if (SDLtoFS2[event.key.keysym.sym]) {
197  key_mark( SDLtoFS2[event.key.keysym.sym], 0, 0 );
198  }
199  break;
200 
201  case SDL_MOUSEBUTTONDOWN:
202  case SDL_MOUSEBUTTONUP:
203  if (event.button.button == SDL_BUTTON_LEFT)
204  mouse_mark_button( MOUSE_LEFT_BUTTON, event.button.state );
205  else if (event.button.button == SDL_BUTTON_MIDDLE)
206  mouse_mark_button( MOUSE_MIDDLE_BUTTON, event.button.state );
207  else if (event.button.button == SDL_BUTTON_RIGHT)
208  mouse_mark_button( MOUSE_RIGHT_BUTTON, event.button.state );
209 
210  break;
211 
212  case SDL_JOYHATMOTION:
213  joy_set_hat_state( event.jhat.value );
214  break;
215 
216  case SDL_JOYBUTTONDOWN:
217  case SDL_JOYBUTTONUP:
218  if (event.jbutton.button < JOY_NUM_BUTTONS) {
219  joy_set_button_state( event.jbutton.button, event.jbutton.state );
220  }
221  break;
222  }
223  }
224 
225  return 0;
226 }
227 
228 
229 // called at shutdown. Makes sure all thread processing terminates.
230 void os_deinit()
231 {
232  DELETE_CRITICAL_SECTION( Os_lock );
233 
234  SDL_Quit();
235 }
236 
237 void os_poll()
238 {
239  unix_process(0);
240 }
241 
242 void debug_int3(char *file, int line)
243 {
244  mprintf(("Int3(): From %s at line %d\n", file, line));
245 
246  // we have to call os_deinit() before abort() so we make sure that SDL gets
247  // closed out and we don't lose video/input control
248  os_deinit();
249 
250  abort();
251 }
252 
253 #endif // Goober5000 - #ifndef WIN32
void key_mark(uint code, int state, uint latency)
Definition: key.cpp:697
void os_deinit()
Definition: osapi_unix.cpp:230
int SDLtoFS2[SDLK_LAST]
#define DELETE_CRITICAL_SECTION(csc)
Definition: osapi.h:41
#define MOUSE_LEFT_BUTTON
Definition: mouse.h:43
bool Cmdline_no_unfocus_pause
Definition: cmdline.cpp:513
#define mprintf(args)
Definition: pstypes.h:238
#define MOUSE_MIDDLE_BUTTON
Definition: mouse.h:45
void os_init_registry_stuff(const char *company, const char *app, const char *version)
Definition: osregistry.cpp:423
SDL_mutex * CRITICAL_SECTION
Definition: config.h:213
const char * os_config_read_string(const char *section, const char *name, const char *default_value)
Definition: osregistry.cpp:322
void outwnd_close()
Definition: outwnd.cpp:1316
void os_poll()
Definition: osapi_unix.cpp:237
void os_init(const char *wclass, const char *title, const char *app_name, const char *version_string)
Definition: osapi_unix.cpp:69
void joy_set_button_state(int button, int state)
Definition: joy-unix.cpp:365
void os_config_write_string(const char *section, const char *name, const char *value)
Definition: osregistry.cpp:221
void joy_set_hat_state(int position)
Definition: joy-unix.cpp:394
void os_set_title(const char *title)
Definition: osapi_unix.cpp:91
void os_cleanup()
Definition: osapi_unix.cpp:101
#define ENTER_CRITICAL_SECTION(csc)
Definition: osapi.h:42
unsigned long DWORD
Definition: config.h:90
DWORD unix_process(DWORD lparam)
Definition: osapi_unix.cpp:156
#define NOX(s)
Definition: pstypes.h:473
void debug_int3(char *file, int line)
Definition: osapi_unix.cpp:242
const char * Osreg_company_name
Definition: osregistry.cpp:28
#define MOUSE_RIGHT_BUTTON
Definition: mouse.h:44
#define LEAVE_CRITICAL_SECTION(csc)
Definition: osapi.h:43
void os_suspend()
Definition: osapi_unix.cpp:136
int os_foreground()
Definition: osapi_unix.cpp:114
#define INITIALIZE_CRITICAL_SECTION(csc)
Definition: osapi.h:40
void os_resume()
Definition: osapi_unix.cpp:142
const char * detect_home(void)
Definition: osapi_unix.cpp:62
void Sleep(int mili)
struct _cl_event * event
Definition: Glext.h:7296
void game_unpause()
Definition: fredstubs.cpp:222
#define JOY_NUM_BUTTONS
Definition: joy.h:15
void gr_activate(int)
Definition: 2d.cpp:1122
void gr_opengl_shutdown()
Definition: gropengl.cpp:1316
int Os_debugger_running
Definition: osapi_unix.cpp:44
void os_sleep(int ms)
Definition: osapi_unix.cpp:130
void game_pause()
Definition: fredstubs.cpp:221
void mouse_mark_button(uint flags, int set)
Definition: mouse.cpp:156
#define strcpy_s(...)
Definition: safe_strings.h:67