30# include <sys/types.h>
36std::atomic<bool> _dedicated_exit_requested;
39static void DedicatedSignalHandler(
int sig)
41 _dedicated_exit_requested =
true;
42 signal(sig, DedicatedSignalHandler);
53static HANDLE _hInputReady, _hWaitForInputHandling;
54static HANDLE _hThread;
55static std::string _win_console_thread_buffer;
58static void WINAPI CheckForConsoleInput()
63 std::getline(std::cin, _win_console_thread_buffer);
66 SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
70static void CreateWindowsConsoleThread()
74 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
75 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
76 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr) UserError(
"Cannot create console event!");
78 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
79 if (_hThread ==
nullptr) UserError(
"Cannot create console thread!");
81 Debug(driver, 2,
"Windows console thread started");
84static void CloseWindowsConsoleThread()
86 CloseHandle(_hThread);
87 CloseHandle(_hInputReady);
88 CloseHandle(_hWaitForInputHandling);
89 Debug(driver, 2,
"Windows console thread shut down");
97static std::unique_ptr<uint8_t[]> _dedicated_video_mem;
112 if (bpp != 0) _dedicated_video_mem = std::make_unique<uint8_t[]>(
static_cast<size_t>(
_cur_resolution.width) *
_cur_resolution.height * (bpp / 8));
116 _screen.dst_ptr = _dedicated_video_mem.get();
123 CreateWindowsConsoleThread();
124 SetConsoleTitle(L
"OpenTTD Dedicated Server");
129 _set_error_mode(_OUT_TO_STDERR);
130 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
131 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
134 Debug(driver, 1,
"Loading dedicated server");
141 CloseWindowsConsoleThread();
143 _dedicated_video_mem =
nullptr;
151static bool InputWaiting()
160 FD_SET(STDIN, &readfds);
163 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
168static bool InputWaiting()
170 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
175static void DedicatedHandleKeyInput()
177 if (!InputWaiting())
return;
179 if (_exit_game)
return;
181 std::string input_line;
183 if (!std::getline(std::cin, input_line))
return;
186 std::swap(input_line, _win_console_thread_buffer);
187 SetEvent(_hWaitForInputHandling);
191 auto p = input_line.find_last_not_of(
"\r\n");
192 if (p != std::string::npos) p++;
200 signal(SIGTERM, DedicatedSignalHandler);
201 signal(SIGINT, DedicatedSignalHandler);
202 signal(SIGQUIT, DedicatedSignalHandler);
215 this->is_game_threaded =
false;
219 while (!_exit_game) {
227 if (_dedicated_exit_requested) {
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.
Functions related to companies.
static constexpr CompanyID COMPANY_SPECTATOR
The client is spectating.
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
Console functions used outside of the console code.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
bool _dedicated_forks
Whether a fork has been done.
Base for the dedicated video driver.
Dimension _cur_resolution
The current resolution.
Error reporting related functions.
Factory to 'query' all available blitters.
Types for standard in/out file operations.
SaveLoadOperation
Operation performed on the file.
DetailedFileType
Kinds of files in each AbstractFileType.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Declarations for savegames operations.
Functions related to world/map generation.
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.
Functions related to the gfx engine.
bool _is_network_server
Does this client wants to be a network-server?
bool _network_dedicated
are we a dedicated server?
Basic functions/variables used all over the place.
bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, std::shared_ptr< LoadFilter > lf)
Load the specified savegame but on error do different things.
Variables and function used internally.
GameMode
Mode which defines the state of the game.
@ LoadGame
Load game, Play Scenario.
@ StartHeightmap
Load a heightmap and start a new game from it.
A number of safeguards to prevent using unsafe methods.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit).
Functions/types related to saving and loading games.
ClientSettings _settings_client
The current settings for this game.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
std::vector< std::string > StringList
Type for a list of strings.
Interface for filtering a savegame till it is loaded.
void SetCurrentThreadName(const std::string &thread_name)
Name the thread this function is called on for the debugger.
Declarations of functions for MS windows systems.
Window functions not directly related to making/drawing windows.