]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_sdl.c
deduplicate Sys_Error()
[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_SDL_Dialog(const char *title, const char *string)
50 {
51         if(!nocrashdialog)
52                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, string, NULL);
53 }
54
55 void Sys_Print(const char *text)
56 {
57 #ifdef __ANDROID__
58         if (developer.integer > 0)
59         {
60                 __android_log_write(ANDROID_LOG_DEBUG, sys.argv[0], text);
61         }
62 #else
63         if(sys.outfd < 0)
64                 return;
65 #ifndef WIN32
66         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
67         // this is because both go to /dev/tty by default!
68         {
69                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
70                 fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
71 #endif
72 #ifdef WIN32
73 #define write _write
74 #endif
75                 while(*text)
76                 {
77                         fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
78                         if(written <= 0)
79                                 break; // sorry, I cannot do anything about this error - without an output
80                         text += written;
81                 }
82 #ifndef WIN32
83                 fcntl (sys.outfd, F_SETFL, origflags);
84         }
85 #endif
86         //fprintf(stdout, "%s", text);
87 #endif
88 }
89
90 char *Sys_GetClipboardData (void)
91 {
92         char *data = NULL;
93         char *cliptext;
94
95         cliptext = SDL_GetClipboardText();
96         if (cliptext != NULL) {
97                 size_t allocsize;
98                 allocsize = min(MAX_INPUTLINE, strlen(cliptext) + 1);
99                 data = (char *)Z_Malloc (allocsize);
100                 strlcpy (data, cliptext, allocsize);
101                 SDL_free(cliptext);
102         }
103
104         return data;
105 }
106
107 void Sys_SDL_Init(void)
108 {
109         // we don't know which systems we'll want to init, yet...
110         if (SDL_Init(0) < 0)
111                 Sys_Error("SDL_Init failed: %s\n", SDL_GetError());
112
113         // COMMANDLINEOPTION: sdl: -nocrashdialog disables "Engine Error" crash dialog boxes
114         if(!Sys_CheckParm("-nocrashdialog"))
115                 nocrashdialog = false;
116 }
117
118 qbool sys_supportsdlgetticks = true;
119 unsigned int Sys_SDL_GetTicks (void)
120 {
121         return SDL_GetTicks();
122 }
123 void Sys_SDL_Delay (unsigned int milliseconds)
124 {
125         SDL_Delay(milliseconds);
126 }