12#include "../gfx_func.h"
13#include "../error_func.h"
14#include "../network/network.h"
15#include "../network/network_internal.h"
16#include "../console_func.h"
17#include "../genworld.h"
18#include "../fileio_type.h"
20#include "../blitter/factory.hpp"
21#include "../company_func.h"
22#include "../core/random_func.hpp"
23#include "../saveload/saveload.h"
25#include "../window_func.h"
31# include <sys/types.h>
37static void DedicatedSignalHandler(
int sig)
41 signal(sig, DedicatedSignalHandler);
50# include "../os/windows/win32.h"
52static HANDLE _hInputReady, _hWaitForInputHandling;
53static HANDLE _hThread;
54static std::string _win_console_thread_buffer;
57static void WINAPI CheckForConsoleInput()
62 std::getline(std::cin, _win_console_thread_buffer);
65 SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
69static void CreateWindowsConsoleThread()
73 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
74 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
75 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr) UserError(
"Cannot create console event!");
77 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
78 if (_hThread ==
nullptr) UserError(
"Cannot create console thread!");
80 Debug(driver, 2,
"Windows console thread started");
83static void CloseWindowsConsoleThread()
85 CloseHandle(_hThread);
86 CloseHandle(_hInputReady);
87 CloseHandle(_hWaitForInputHandling);
88 Debug(driver, 2,
"Windows console thread shut down");
93#include "../safeguards.h"
96static void *_dedicated_video_mem;
115 _screen.dst_ptr = _dedicated_video_mem;
122 CreateWindowsConsoleThread();
123 SetConsoleTitle(L
"OpenTTD Dedicated Server");
128 _set_error_mode(_OUT_TO_STDERR);
129 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
130 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
133 Debug(driver, 1,
"Loading dedicated server");
140 CloseWindowsConsoleThread();
142 free(_dedicated_video_mem);
150static bool InputWaiting()
159 FD_SET(STDIN, &readfds);
162 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
167static bool InputWaiting()
169 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
174static void DedicatedHandleKeyInput()
176 if (!InputWaiting())
return;
178 if (_exit_game)
return;
180 std::string input_line;
182 if (!std::getline(std::cin, input_line))
return;
185 std::swap(input_line, _win_console_thread_buffer);
186 SetEvent(_hWaitForInputHandling);
190 auto p = input_line.find_last_not_of(
"\r\n");
191 if (p != std::string::npos) p++;
199 signal(SIGTERM, DedicatedSignalHandler);
200 signal(SIGINT, DedicatedSignalHandler);
201 signal(SIGQUIT, DedicatedSignalHandler);
214 this->is_game_threaded =
false;
218 while (!_exit_game) {
219 if (!_dedicated_forks) DedicatedHandleKeyInput();
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual void PostResize()
Post resize event.
Factory for the dedicated server video driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
void MainLoop() override
Perform the actual drawing.
void Stop() override
Stop this driver.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void DrainCommandQueue()
Execute all queued commands.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
CompanyID _current_company
Company currently doing an action.
@ COMPANY_SPECTATOR
The client is spectating.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Base for the dedicated video driver.
Dimension _cur_resolution
The current resolution.
SaveLoadOperation
Operation performed on the file.
DetailedFileType
Kinds of files in each AbstractFileType.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
static const uint32_t GENERATE_NEW_SEED
Create a new random seed.
void StartNewGameWithoutGUI(uint32_t seed)
Start a normal game without the GUI.
SwitchMode _switch_mode
The next mainloop command.
bool _is_network_server
Does this client wants to be a network-server?
bool _network_dedicated
are we a dedicated server?
@ SM_START_HEIGHTMAP
Load a heightmap and start a new game from it.
@ SM_LOAD_GAME
Load game, Play Scenario.
GameMode
Mode which defines the state of the game.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
ClientSettings _settings_client
The current settings for this game.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
std::vector< std::string > StringList
Type for a list of strings.
GUISettings gui
settings related to the GUI
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?...
Interface for filtering a savegame till it is loaded.
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.