make it possible to override CXX from the command line
[rofl0r-df-libgraphics.git] / g_src / win32_compat.cpp
blob30c65683dcd5d988446349d52a077f59f1f394d3
1 #include <string>
3 #include "enabler.h"
4 #include "platform.h"
5 #ifndef WIN32
6 # include <sys/types.h>
7 # include <sys/stat.h>
8 # include <sys/time.h>
9 # include <signal.h>
10 # include <errno.h>
11 # include <stdio.h>
12 # include <string.h>
13 # include <unistd.h>
14 # ifdef __APPLE__
15 # include "osx_messagebox.h"
16 # elif defined(unix) && defined(HAVE_GTK2)
17 # include <gtk/gtk.h>
18 # endif
19 #endif
21 #ifndef WIN32
22 BOOL CreateDirectory(const char* pathname, void*)
24 if (mkdir(pathname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
25 if (errno != EEXIST) {
26 std::string emsg = "mkdir(";
27 emsg.append(pathname);
28 emsg.append(") failed");
29 perror(emsg.c_str());
31 return FALSE;
32 } else {
33 return TRUE;
37 BOOL DeleteFile(const char* filename)
39 return !unlink(filename);
42 void ZeroMemory(void* dest, int len)
44 memset(dest, 0, len);
47 /* Returns milliseconds since 1970
48 * Wraps every 24 days (assuming 32-bit signed dwords)
50 DWORD GetTickCount()
52 struct timeval tp;
53 gettimeofday(&tp, NULL);
54 return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
57 char* itoa(int value, char* result, int base)
59 // check that the base is valid
60 if (base < 2 || base > 16) { *result = 0; return result; }
62 char* out = result;
63 int quot = value;
67 *out = "0123456789abcdef"[ /*std::*/abs(quot % base) ];
68 ++out;
69 quot /= base;
71 while (quot);
73 if (value < 0) *out++ = '-';
75 std::reverse(result, out);
76 *out = 0;
77 return result;
80 // Fills performanceCount with microseconds passed since 1970
81 // Wraps in twenty-nine thousand years or so
82 BOOL QueryPerformanceCounter(LARGE_INTEGER* performanceCount)
84 struct timeval tp;
85 gettimeofday(&tp, NULL);
86 performanceCount->QuadPart = ((long long)tp.tv_sec * 1000000) + tp.tv_usec;
87 return TRUE;
90 BOOL QueryPerformanceFrequency(LARGE_INTEGER* performanceCount)
92 /* A constant, 10^6, as we give microseconds since 1970 in
93 * QueryPerformanceCounter. */
94 performanceCount->QuadPart = 1000000;
96 return TRUE;
99 int MessageBox(HWND *dummy, const char *text, const char *caption, UINT type)
101 bool toggle_screen = false;
102 int ret = IDOK;
103 if (enabler.is_fullscreen()) {
104 enabler.toggle_fullscreen();
105 toggle_screen = true;
107 # ifdef __APPLE__ // Cocoa code
108 if (type & MB_YESNO) {
109 ret = CocoaAlertPanel(caption, text, "Yes", "No", NULL);
110 ret = (ret == 0 ? IDNO : IDYES);
111 } else {
112 CocoaAlertPanel(caption, text, "OK", NULL, NULL);
114 # else // GTK code
115 # ifdef HAVE_GTK2
116 if (getenv("DISPLAY")) {
117 // Have X, will dialog
118 GtkWidget *dialog = gtk_message_dialog_new(NULL,
119 GTK_DIALOG_DESTROY_WITH_PARENT,
120 type & MB_YESNO ?
121 GTK_MESSAGE_QUESTION :
122 GTK_MESSAGE_ERROR,
123 type & MB_YESNO ?
124 GTK_BUTTONS_YES_NO :
125 GTK_BUTTONS_OK,
126 "%s", text);
127 gtk_window_set_position((GtkWindow*)dialog, GTK_WIN_POS_CENTER_ALWAYS);
128 gtk_window_set_title((GtkWindow*)dialog, caption);
129 gint dialog_ret = gtk_dialog_run(GTK_DIALOG(dialog));
130 gtk_widget_destroy(dialog);
131 while (gtk_events_pending())
132 gtk_main_iteration();
134 if (type & MB_YESNO) {
135 switch (dialog_ret) {
136 default:
137 case GTK_RESPONSE_DELETE_EVENT:
138 case GTK_RESPONSE_NO:
139 ret = IDNO;
140 break;
141 case GTK_RESPONSE_YES:
142 ret = IDYES;
143 break;
146 } else {
147 #elif CURSES
148 // Use curses
149 init_curses();
150 erase();
151 gps.force_full_display_count = 1;
152 wattrset(*stdscr_p, A_NORMAL | COLOR_PAIR(1));
154 mvwaddstr(*stdscr_p, 0, 5, caption);
155 mvwaddstr(*stdscr_p, 2, 2, text);
156 nodelay(*stdscr_p, false);
157 if (type & MB_YESNO) {
158 mvwaddstr(*stdscr_p, 5, 0, "Press 'y' or 'n'.");
159 refresh();
160 while (1) {
161 char i = wgetch(*stdscr_p);
162 if (i == 'y') {
163 ret = IDYES;
164 break;
166 else if (i == 'n') {
167 ret = IDNO;
168 break;
172 else {
173 mvwaddstr(*stdscr_p, 5, 0, "Press any key to continue.");
174 refresh();
175 wgetch(*stdscr_p);
177 nodelay(*stdscr_p, -1);
178 # else /* not APPLE, not GTK, not curses - use stdio */
179 dprintf(2, "Alert %s:\n%s\n", caption ? caption : "", text ? text : "");
180 if (type & MB_YESNO) {
181 while(ret == IDOK) {
182 dprintf(2, "please answer with 'yes' or 'no'\n");
183 char buf[16];
184 fgets(buf, sizeof buf, stdin);
185 if(!strncmp(buf, "yes", 3)) ret = IDYES;
186 else if(!strncmp(buf, "no", 2)) ret = IDNO;
189 # endif //end ifdef HAVE_GTK2 / CURSES
190 # ifdef HAVE_GTK2
192 # endif
193 # endif
195 if (toggle_screen) {
196 enabler.toggle_fullscreen();
199 return ret;
201 #endif