]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
deduplicate Sys_ConsoleInput()
[xonotic/darkplaces.git] / sys_sdl.c
1 #ifdef WIN32
2 #include <io.h> // Include this BEFORE darkplaces.h because it uses strncpy which trips DP_STATIC_ASSERT
3 #else
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/time.h>
7 #endif
8
9 #ifdef __ANDROID__
10 #include <android/log.h>
11 #endif
12
13 /*
14  * Include this BEFORE darkplaces.h because it breaks wrapping
15  * _Static_assert. Cloudwalk has no idea how or why so don't ask.
16  */
17 #include <SDL.h>
18
19 #include "darkplaces.h"
20
21 #ifdef WIN32
22 #ifdef _MSC_VER
23 #pragma comment(lib, "sdl2.lib")
24 #pragma comment(lib, "sdl2main.lib")
25 #endif
26 #endif
27
28 sys_t sys;
29
30 // =======================================================================
31 // General routines
32 // =======================================================================
33
34 void Sys_Shutdown (void)
35 {
36 #ifdef __ANDROID__
37         Sys_AllowProfiling(false);
38 #endif
39 #ifndef WIN32
40         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
41 #endif
42         fflush(stdout);
43         SDL_Quit();
44 }
45
46 // Sys_Error early in startup might screw with automated
47 // workflows or something if we show the dialog by default.
48 static qbool nocrashdialog = true;
49 void Sys_Error (const char *error, ...)
50 {
51         va_list argptr;
52         char string[MAX_INPUTLINE];
53
54 // change stdin to non blocking
55 #ifndef WIN32
56         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
57 #endif
58
59         va_start (argptr,error);
60         dpvsnprintf (string, sizeof (string), error, argptr);
61         va_end (argptr);
62
63         Con_Printf(CON_ERROR "Engine Error: %s\n", string);
64         
65         // don't want a dead window left blocking the OS UI or the crash dialog
66         Host_Shutdown();
67
68         if(!nocrashdialog)
69                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Engine Error", string, NULL);
70
71         exit (1);
72 }
73
74 void Sys_Print(const char *text)
75 {
76 #ifdef __ANDROID__
77         if (developer.integer > 0)
78         {
79                 __android_log_write(ANDROID_LOG_DEBUG, sys.argv[0], text);
80         }
81 #else
82         if(sys.outfd < 0)
83                 return;
84 #ifndef WIN32
85         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
86         // this is because both go to /dev/tty by default!
87         {
88                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
89                 fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
90 #endif
91 #ifdef WIN32
92 #define write _write
93 #endif
94                 while(*text)
95                 {
96                         fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
97                         if(written <= 0)
98                                 break; // sorry, I cannot do anything about this error - without an output
99                         text += written;
100                 }
101 #ifndef WIN32
102                 fcntl (sys.outfd, F_SETFL, origflags);
103         }
104 #endif
105         //fprintf(stdout, "%s", text);
106 #endif
107 }
108
109 char *Sys_GetClipboardData (void)
110 {
111         char *data = NULL;
112         char *cliptext;
113
114         cliptext = SDL_GetClipboardText();
115         if (cliptext != NULL) {
116                 size_t allocsize;
117                 allocsize = min(MAX_INPUTLINE, strlen(cliptext) + 1);
118                 data = (char *)Z_Malloc (allocsize);
119                 strlcpy (data, cliptext, allocsize);
120                 SDL_free(cliptext);
121         }
122
123         return data;
124 }
125
126 void Sys_SDL_Init(void)
127 {
128         // we don't know which systems we'll want to init, yet...
129         if (SDL_Init(0) < 0)
130                 Sys_Error("SDL_Init failed: %s\n", SDL_GetError());
131
132         // COMMANDLINEOPTION: sdl: -nocrashdialog disables "Engine Error" crash dialog boxes
133         if(!Sys_CheckParm("-nocrashdialog"))
134                 nocrashdialog = false;
135 }
136
137 qbool sys_supportsdlgetticks = true;
138 unsigned int Sys_SDL_GetTicks (void)
139 {
140         return SDL_GetTicks();
141 }
142 void Sys_SDL_Delay (unsigned int milliseconds)
143 {
144         SDL_Delay(milliseconds);
145 }