summaryrefslogtreecommitdiff
path: root/g_src/win32_compat.cpp
blob: 44d6ace091336996fc674ef9669302d93c468a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <string>

#include "enabler.h"
#include "platform.h"
#ifndef WIN32
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/time.h>
# include <signal.h>
# include <errno.h>
# include <stdio.h>
# include <string.h>
# ifdef __APPLE__
#  include "osx_messagebox.h"
# elif defined(unix)
#  include <gtk/gtk.h>
# endif
#endif

#ifndef WIN32
BOOL CreateDirectory(const char* pathname, void*)
{
  if (mkdir(pathname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
    if (errno != EEXIST) {
      std::string emsg = "mkdir(";
      emsg.append(pathname);
      emsg.append(") failed");
      perror(emsg.c_str());
    }
    return FALSE;
  } else {
    return TRUE;
  }
}

BOOL DeleteFile(const char* filename)
{
  return !unlink(filename);
}

void ZeroMemory(void* dest, int len)
{
  memset(dest, 0, len);
}

/* Returns milliseconds since 1970
 * Wraps every 24 days (assuming 32-bit signed dwords)
 */
DWORD GetTickCount()
{
  struct timeval tp;
  gettimeofday(&tp, NULL);
  return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
}

char* itoa(int value, char* result, int base)
{
  // check that the base is valid
  if (base < 2 || base > 16) { *result = 0; return result; }
	
  char* out = result;
  int quot = value;
	
  do
    {
      *out = "0123456789abcdef"[ /*std::*/abs(quot % base) ];
      ++out;
      quot /= base;
    }
  while (quot);
	
  if (value < 0) *out++ = '-';
	
  std::reverse(result, out);
  *out = 0;
  return result;
}

// Fills performanceCount with microseconds passed since 1970
// Wraps in twenty-nine thousand years or so
BOOL QueryPerformanceCounter(LARGE_INTEGER* performanceCount)
{
  struct timeval tp;
  gettimeofday(&tp, NULL);
  performanceCount->QuadPart = ((long long)tp.tv_sec * 1000000) + tp.tv_usec;
  return TRUE;
}

BOOL QueryPerformanceFrequency(LARGE_INTEGER* performanceCount)
{
  /* A constant, 10^6, as we give microseconds since 1970 in
   * QueryPerformanceCounter. */
  performanceCount->QuadPart = 1000000;
  
  return TRUE;
}

int MessageBox(HWND *dummy, const char *text, const char *caption, UINT type)
{
  bool toggle_screen = false;
  int ret = IDOK;
  if (enabler.is_fullscreen()) {
    enabler.toggle_fullscreen();
    toggle_screen = true;
  }
# ifdef __APPLE__ // Cocoa code
  if (type & MB_YESNO) {
    ret = CocoaAlertPanel(caption, text, "Yes", "No", NULL);
    ret = (ret == 0 ? IDNO : IDYES);
  } else {
    CocoaAlertPanel(caption, text, "OK", NULL, NULL);
  }
# else // GTK code
  if (getenv("DISPLAY")) {
    // Have X, will dialog
    GtkWidget *dialog = gtk_message_dialog_new(NULL,
                                               GTK_DIALOG_DESTROY_WITH_PARENT,
                                               type & MB_YESNO ?
                                               GTK_MESSAGE_QUESTION :
                                               GTK_MESSAGE_ERROR,
                                               type & MB_YESNO ?
                                               GTK_BUTTONS_YES_NO :
                                               GTK_BUTTONS_OK,
                                               "%s", text);
    gtk_window_set_position((GtkWindow*)dialog, GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_title((GtkWindow*)dialog, caption);
    gint dialog_ret = gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    while (gtk_events_pending())
      gtk_main_iteration();
    
    if (type & MB_YESNO) {
      switch (dialog_ret) {
      default:
      case GTK_RESPONSE_DELETE_EVENT:
      case GTK_RESPONSE_NO:
        ret = IDNO;
        break;
      case GTK_RESPONSE_YES:
        ret = IDYES;
        break;
      }
    }
  } else {
    // Use curses
    init_curses();
    erase();
    gps.force_full_display_count = 1;
    wattrset(*stdscr_p, A_NORMAL | COLOR_PAIR(1));
    
    mvwaddstr(*stdscr_p, 0, 5, caption);
    mvwaddstr(*stdscr_p, 2, 2, text);
    nodelay(*stdscr_p, false);
    if (type & MB_YESNO) {
      mvwaddstr(*stdscr_p, 5, 0, "Press 'y' or 'n'.");
      refresh();
      while (1) {
        char i = wgetch(*stdscr_p);
        if (i == 'y') {
          ret = IDYES;
          break;
        }
        else if (i == 'n') {
          ret = IDNO;
          break;
        }
      }
    }
    else {
      mvwaddstr(*stdscr_p, 5, 0, "Press any key to continue.");
      refresh();
      wgetch(*stdscr_p);
    }
    nodelay(*stdscr_p, -1);
  }
# endif
  
  if (toggle_screen) {
    enabler.toggle_fullscreen();
  }
	
  return ret;
}
#endif