Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "MessageHandler.h" 21 : #include "gui/CGUI.h" 22 : #include "gui/GUIManager.h" 23 : #include "lib/external_libraries/libsdl.h" 24 : #include "lib/path.h" 25 : #include "maths/MathUtil.h" 26 : #include "ps/Game.h" 27 : #include "ps/GameSetup/Config.h" 28 : #include "ps/GameSetup/GameSetup.h" 29 : #include "ps/Input.h" 30 : #include "ps/VideoMode.h" 31 : #include "renderer/Renderer.h" 32 : #include "scriptinterface/Interface.h" 33 : #include "simulation2/components/ICmpSoundManager.h" 34 : #include "simulation2/system/Component.h" 35 : #include "simulation2/system/Entity.h" 36 : #include "tools/atlas/GameInterface/MessagePasser.h" 37 : #include "tools/atlas/GameInterface/MessagePasserImpl.h" 38 : #include "tools/atlas/GameInterface/Messages.h" 39 : #include "tools/atlas/GameInterface/Shareable.h" 40 : #include "tools/atlas/GameInterface/SharedTypes.h" 41 : #include "tools/atlas/GameInterface/View.h" 42 : 43 : #include <SDL_events.h> 44 : #include <SDL_keyboard.h> 45 : #include <SDL_keycode.h> 46 : #include <cstddef> 47 : #include <js/CallArgs.h> 48 : #include <js/RootingAPI.h> 49 : #include <memory> 50 : #include <string> 51 : 52 : extern void (*Atlas_GLSwapBuffers)(void* context); 53 : 54 : namespace AtlasMessage 55 : { 56 : 57 0 : MESSAGEHANDLER(MessageTrace) 58 : { 59 0 : ((MessagePasserImpl*)g_MessagePasser)->SetTrace(msg->enable); 60 0 : } 61 : 62 0 : MESSAGEHANDLER(Screenshot) 63 : { 64 0 : if (msg->big) 65 0 : g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::BIG); 66 : else 67 0 : g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::DEFAULT); 68 0 : } 69 : 70 0 : QUERYHANDLER(Ping) 71 : { 72 0 : } 73 : 74 0 : MESSAGEHANDLER(SimStopMusic) 75 : { 76 0 : CmpPtr<ICmpSoundManager> cmpSoundManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); 77 0 : if (cmpSoundManager) 78 0 : cmpSoundManager->StopMusic(); 79 0 : } 80 : 81 0 : MESSAGEHANDLER(SimStateSave) 82 : { 83 0 : AtlasView::GetView_Game()->SaveState(*msg->label); 84 0 : } 85 : 86 0 : MESSAGEHANDLER(SimStateRestore) 87 : { 88 0 : AtlasView::GetView_Game()->RestoreState(*msg->label); 89 0 : } 90 : 91 0 : QUERYHANDLER(SimStateDebugDump) 92 : { 93 0 : msg->dump = AtlasView::GetView_Game()->DumpState(msg->binary); 94 0 : } 95 : 96 0 : MESSAGEHANDLER(SimPlay) 97 : { 98 0 : AtlasView::GetView_Game()->SetSpeedMultiplier(msg->speed); 99 0 : AtlasView::GetView_Game()->SetTesting(msg->simTest); 100 0 : } 101 : 102 0 : MESSAGEHANDLER(SetSmoothFramerate) 103 : { 104 0 : AtlasView::GetView_Game()->SetSmoothFramerate(msg->enabled); 105 0 : AtlasView::GetView_Actor()->SetSmoothFramerate(msg->enabled); 106 0 : } 107 : 108 0 : MESSAGEHANDLER(JavaScript) 109 : { 110 0 : g_GUI->GetActiveGUI()->GetScriptInterface()->LoadGlobalScript(L"Atlas", *msg->command); 111 0 : } 112 : 113 0 : MESSAGEHANDLER(GuiSwitchPage) 114 : { 115 0 : g_GUI->SwitchPage(*msg->page, NULL, JS::UndefinedHandleValue); 116 0 : } 117 : 118 0 : MESSAGEHANDLER(GuiMouseButtonEvent) 119 : { 120 0 : if (!g_VideoMode.IsInitialized()) 121 0 : return; 122 : 123 0 : SDL_Event ev{}; 124 0 : ev.type = msg->pressed ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP; 125 0 : ev.button.button = msg->button; 126 0 : ev.button.state = msg->pressed ? SDL_PRESSED : SDL_RELEASED; 127 0 : ev.button.clicks = msg->clicks; 128 0 : float x, y; 129 0 : msg->pos->GetScreenSpace(x, y); 130 0 : ev.button.x = static_cast<std::uint16_t>(Clamp<int>(x, 0, g_VideoMode.GetWindowWidth())); 131 0 : ev.button.y = static_cast<std::uint16_t>(Clamp<int>(y, 0, g_VideoMode.GetWindowHeight())); 132 0 : g_VideoMode.m_InputManager.DispatchEvent(ev); 133 : } 134 : 135 0 : MESSAGEHANDLER(GuiMouseMotionEvent) 136 : { 137 0 : if (!g_VideoMode.IsInitialized()) 138 0 : return; 139 : 140 0 : SDL_Event ev{}; 141 0 : ev.type = SDL_MOUSEMOTION; 142 0 : float x, y; 143 0 : msg->pos->GetScreenSpace(x, y); 144 0 : ev.motion.x = static_cast<std::uint16_t>(Clamp<int>(x, 0, g_VideoMode.GetWindowWidth())); 145 0 : ev.motion.y = static_cast<std::uint16_t>(Clamp<int>(y, 0, g_VideoMode.GetWindowHeight())); 146 0 : g_VideoMode.m_InputManager.DispatchEvent(ev); 147 : } 148 : 149 0 : MESSAGEHANDLER(GuiKeyEvent) 150 : { 151 0 : if (!g_VideoMode.IsInitialized()) 152 0 : return; 153 : 154 0 : SDL_Event ev{}; 155 0 : ev.type = msg->pressed ? SDL_KEYDOWN : SDL_KEYUP; 156 0 : ev.key.keysym.sym = static_cast<SDL_Keycode>(static_cast<int>(msg->sdlkey)); 157 0 : ev.key.keysym.scancode = SDL_GetScancodeFromKey(static_cast<SDL_Keycode>(static_cast<int>(msg->sdlkey))); 158 0 : g_VideoMode.m_InputManager.DispatchEvent(ev); 159 : } 160 : 161 0 : MESSAGEHANDLER(GuiCharEvent) 162 : { 163 0 : if (!g_VideoMode.IsInitialized()) 164 0 : return; 165 : 166 : // Simulate special 'text input' events in the SDL 167 : // This isn't quite compatible with WXWidget's handling, 168 : // so to avoid trouble we only send 'letter-like' ASCII input. 169 0 : SDL_Event ev{}; 170 0 : ev.type = SDL_TEXTEDITING; 171 0 : ev.text.type = SDL_TEXTEDITING; 172 0 : ev.text.text[0] = static_cast<char>(msg->sdlkey); 173 0 : ev.text.text[1] = '\0'; 174 0 : g_VideoMode.m_InputManager.DispatchEvent(ev); 175 : 176 0 : ev.type = SDL_TEXTINPUT; 177 0 : ev.text.type = SDL_TEXTINPUT; 178 0 : ev.text.text[0] = static_cast<char>(msg->sdlkey); 179 0 : ev.text.text[1] = '\0'; 180 0 : g_VideoMode.m_InputManager.DispatchEvent(ev); 181 : } 182 : 183 : } // namespace AtlasMessage