31 # define STDIN 0 /* file descriptor for standard input */ |
31 # define STDIN 0 /* file descriptor for standard input */ |
32 |
32 |
33 /** |
33 /** |
34 * Switches OpenTTD to a console app at run-time, instead of a PM app |
34 * Switches OpenTTD to a console app at run-time, instead of a PM app |
35 * Necessary to see stdout, etc. */ |
35 * Necessary to see stdout, etc. */ |
36 static void OS2_SwitchToConsoleMode(void) |
36 static void OS2_SwitchToConsoleMode() |
37 { |
37 { |
38 PPIB pib; |
38 PPIB pib; |
39 PTIB tib; |
39 PTIB tib; |
40 |
40 |
41 DosGetInfoBlocks(&tib, &pib); |
41 DosGetInfoBlocks(&tib, &pib); |
72 static HANDLE _hInputReady, _hWaitForInputHandling; |
72 static HANDLE _hInputReady, _hWaitForInputHandling; |
73 static HANDLE _hThread; // Thread to close |
73 static HANDLE _hThread; // Thread to close |
74 static char _win_console_thread_buffer[200]; |
74 static char _win_console_thread_buffer[200]; |
75 |
75 |
76 /* Windows Console thread. Just loop and signal when input has been received */ |
76 /* Windows Console thread. Just loop and signal when input has been received */ |
77 static void WINAPI CheckForConsoleInput(void) |
77 static void WINAPI CheckForConsoleInput() |
78 { |
78 { |
|
79 DWORD nb; |
|
80 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); |
79 while (true) { |
81 while (true) { |
80 fgets(_win_console_thread_buffer, lengthof(_win_console_thread_buffer), stdin); |
82 ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, NULL); |
81 /* Signal input waiting that input is read and wait for it being handled |
83 /* Signal input waiting that input is read and wait for it being handled |
82 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */ |
84 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */ |
83 SetEvent(_hInputReady); |
85 SetEvent(_hInputReady); |
84 WaitForSingleObject(_hWaitForInputHandling, INFINITE); |
86 WaitForSingleObject(_hWaitForInputHandling, INFINITE); |
85 } |
87 } |
86 } |
88 } |
87 |
89 |
88 static void CreateWindowsConsoleThread(void) |
90 static void CreateWindowsConsoleThread() |
89 { |
91 { |
90 DWORD dwThreadId; |
92 DWORD dwThreadId; |
91 /* Create event to signal when console input is ready */ |
93 /* Create event to signal when console input is ready */ |
92 _hInputReady = CreateEvent(NULL, false, false, NULL); |
94 _hInputReady = CreateEvent(NULL, false, false, NULL); |
93 _hWaitForInputHandling = CreateEvent(NULL, false, false, NULL); |
95 _hWaitForInputHandling = CreateEvent(NULL, false, false, NULL); |
97 if (_hThread == NULL) error("Cannot create console thread!"); |
99 if (_hThread == NULL) error("Cannot create console thread!"); |
98 |
100 |
99 DEBUG(driver, 2, "Windows console thread started"); |
101 DEBUG(driver, 2, "Windows console thread started"); |
100 } |
102 } |
101 |
103 |
102 static void CloseWindowsConsoleThread(void) |
104 static void CloseWindowsConsoleThread() |
103 { |
105 { |
104 CloseHandle(_hThread); |
106 CloseHandle(_hThread); |
105 CloseHandle(_hInputReady); |
107 CloseHandle(_hInputReady); |
106 CloseHandle(_hWaitForInputHandling); |
108 CloseHandle(_hWaitForInputHandling); |
107 DEBUG(driver, 2, "Windows console thread shut down"); |
109 DEBUG(driver, 2, "Windows console thread shut down"); |
151 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {} |
153 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {} |
152 static bool DedicatedVideoChangeRes(int w, int h) { return false; } |
154 static bool DedicatedVideoChangeRes(int w, int h) { return false; } |
153 static void DedicatedVideoFullScreen(bool fs) {} |
155 static void DedicatedVideoFullScreen(bool fs) {} |
154 |
156 |
155 #if defined(UNIX) || defined(__OS2__) || defined(PSP) |
157 #if defined(UNIX) || defined(__OS2__) || defined(PSP) |
156 static bool InputWaiting(void) |
158 static bool InputWaiting() |
157 { |
159 { |
158 struct timeval tv; |
160 struct timeval tv; |
159 fd_set readfds; |
161 fd_set readfds; |
160 |
162 |
161 tv.tv_sec = 0; |
163 tv.tv_sec = 0; |
166 |
168 |
167 /* don't care about writefds and exceptfds: */ |
169 /* don't care about writefds and exceptfds: */ |
168 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0; |
170 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0; |
169 } |
171 } |
170 |
172 |
171 static uint32 GetTime(void) |
173 static uint32 GetTime() |
172 { |
174 { |
173 struct timeval tim; |
175 struct timeval tim; |
174 |
176 |
175 gettimeofday(&tim, NULL); |
177 gettimeofday(&tim, NULL); |
176 return tim.tv_usec / 1000 + tim.tv_sec * 1000; |
178 return tim.tv_usec / 1000 + tim.tv_sec * 1000; |
177 } |
179 } |
178 |
180 |
179 #else |
181 #else |
180 |
182 |
181 static bool InputWaiting(void) |
183 static bool InputWaiting() |
182 { |
184 { |
183 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0; |
185 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0; |
184 } |
186 } |
185 |
187 |
186 static uint32 GetTime(void) |
188 static uint32 GetTime() |
187 { |
189 { |
188 return GetTickCount(); |
190 return GetTickCount(); |
189 } |
191 } |
190 |
192 |
191 #endif |
193 #endif |
192 |
194 |
193 static void DedicatedHandleKeyInput(void) |
195 static void DedicatedHandleKeyInput() |
194 { |
196 { |
195 static char input_line[200] = ""; |
197 static char input_line[200] = ""; |
196 |
198 |
197 if (!InputWaiting()) return; |
199 if (!InputWaiting()) return; |
198 |
200 |
223 } |
225 } |
224 |
226 |
225 IConsoleCmdExec(input_line); // execute command |
227 IConsoleCmdExec(input_line); // execute command |
226 } |
228 } |
227 |
229 |
228 static void DedicatedVideoMainLoop(void) |
230 static void DedicatedVideoMainLoop() |
229 { |
231 { |
230 uint32 cur_ticks = GetTime(); |
232 uint32 cur_ticks = GetTime(); |
231 uint32 next_tick = cur_ticks + 30; |
233 uint32 next_tick = cur_ticks + 30; |
232 |
234 |
233 /* Signal handlers */ |
235 /* Signal handlers */ |