/* Crafter.CppDOM Copyright (C) 2025 Catcrafts Catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ export module Crafter.CppDOM:BindingsExport; import std; import :EventTypes; export namespace Crafter::CppDOMBindings { std::int32_t clickHandlerMaxId = 0; std::unordered_map>* clickHandlers = new std::unordered_map>(); std::int32_t mouseOverHandlerMaxId = 0; std::unordered_map>* mouseOverHandlers = new std::unordered_map>(); std::int32_t mouseOutHandlerMaxId = 0; std::unordered_map>* mouseOutHandlers = new std::unordered_map>(); std::int32_t mouseMoveHandlerMaxId = 0; std::unordered_map>* mouseMoveHandlers = new std::unordered_map>(); std::int32_t mouseDownHandlerMaxId = 0; std::unordered_map>* mouseDownHandlers = new std::unordered_map>(); std::int32_t mouseUpHandlerMaxId = 0; std::unordered_map>* mouseUpHandlers = new std::unordered_map>(); std::int32_t focusHandlerMaxId = 0; std::unordered_map>* focusHandlers = new std::unordered_map>(); std::int32_t blurHandlerMaxId = 0; std::unordered_map>* blurHandlers = new std::unordered_map>(); std::int32_t keyDownHandlerMaxId = 0; std::unordered_map>* keyDownHandlers = new std::unordered_map>(); std::int32_t keyUpHandlerMaxId = 0; std::unordered_map>* keyUpHandlers = new std::unordered_map>(); std::int32_t keyPressHandlerMaxId = 0; std::unordered_map>* keyPressHandlers = new std::unordered_map>(); std::int32_t changeHandlerMaxId = 0; std::unordered_map>* changeHandlers = new std::unordered_map>(); std::int32_t submitHandlerMaxId = 0; std::unordered_map>* submitHandlers = new std::unordered_map>(); std::int32_t inputHandlerMaxId = 0; std::unordered_map>* inputHandlers = new std::unordered_map>(); std::int32_t resizeHandlerMaxId = 0; std::unordered_map>* resizeHandlers = new std::unordered_map>(); std::int32_t scrollHandlerMaxId = 0; std::unordered_map>* scrollHandlers = new std::unordered_map>(); std::int32_t contextMenuHandlerMaxId = 0; std::unordered_map>* contextMenuHandlers = new std::unordered_map>(); std::int32_t dragStartHandlerMaxId = 0; std::unordered_map>* dragStartHandlers = new std::unordered_map>(); std::int32_t dragEndHandlerMaxId = 0; std::unordered_map>* dragEndHandlers = new std::unordered_map>(); std::int32_t dropHandlerMaxId = 0; std::unordered_map>* dropHandlers = new std::unordered_map>(); std::int32_t wheelHandlerMaxId = 0; std::unordered_map>* wheelHandlers = new std::unordered_map>(); std::int32_t dragOverHandlerMaxId = 0; std::unordered_map>* dragOverHandlers = new std::unordered_map>(); std::int32_t dragEnterHandlerMaxId = 0; std::unordered_map>* dragEnterHandlers = new std::unordered_map>(); std::int32_t dragLeaveHandlerMaxId = 0; std::unordered_map>* dragLeaveHandlers = new std::unordered_map>(); std::int32_t fetchHandlerMaxId = 0; std::unordered_map>* fetchHandlers = new std::unordered_map>(); } extern "C" { __attribute__((export_name("WasmAlloc"))) void* WasmAlloc(std::int32_t size) { return std::malloc(size); } __attribute__((export_name("WasmFree"))) void WasmFree(void* ptr) { std::free(ptr); } __attribute__((export_name("deleteElement"))) void DeleteElement(void* ptr) { // This will be implemented in JavaScript } __attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::clickHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseOverHandler"))) void ExecuteMouseOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseOverHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseOutHandler"))) void ExecuteMouseOutHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseOutHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseMoveHandler"))) void ExecuteMouseMoveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseMoveHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseDownHandler"))) void ExecuteMouseDownHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button,std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseDownHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseUpHandler"))) void ExecuteMouseUpHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseUpHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteFocusHandler"))) void ExecuteFocusHandler(std::int32_t handlerID, void* target, void* relatedTarget) { Crafter::CppDOMBindings::focusHandlers->find(handlerID)->second(Crafter::FocusEvent(target, relatedTarget)); } __attribute__((export_name("ExecuteBlurHandler"))) void ExecuteBlurHandler(std::int32_t handlerID, void* target, void* relatedTarget) { Crafter::CppDOMBindings::blurHandlers->find(handlerID)->second(Crafter::FocusEvent(target, relatedTarget)); } __attribute__((export_name("ExecuteKeyDownHandler"))) void ExecuteKeyDownHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyDownHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteKeyUpHandler"))) void ExecuteKeyUpHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyUpHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteKeyPressHandler"))) void ExecuteKeyPressHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyPressHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteChangeHandler"))) void ExecuteChangeHandler(std::int32_t handlerID, const char* value) { Crafter::CppDOMBindings::changeHandlers->find(handlerID)->second(Crafter::ChangeEvent(value)); } __attribute__((export_name("ExecuteSubmitHandler"))) void ExecuteSubmitHandler(std::int32_t handlerID) { Crafter::CppDOMBindings::submitHandlers->find(handlerID)->second(); } __attribute__((export_name("ExecuteInputHandler"))) void ExecuteInputHandler(std::int32_t handlerID, const char* data, bool isComposing) { Crafter::CppDOMBindings::inputHandlers->find(handlerID)->second(Crafter::InputEvent(data, isComposing)); } __attribute__((export_name("ExecuteResizeHandler"))) void ExecuteResizeHandler(std::int32_t handlerID, std::int32_t width, std::int32_t height) { Crafter::CppDOMBindings::resizeHandlers->find(handlerID)->second(Crafter::ResizeEvent(width, height)); } __attribute__((export_name("ExecuteScrollHandler"))) void ExecuteScrollHandler(std::int32_t handlerID, double scrollX, double scrollY) { Crafter::CppDOMBindings::scrollHandlers->find(handlerID)->second(Crafter::ScrollEvent(scrollX, scrollY)); } __attribute__((export_name("ExecuteContextMenuHandler"))) void ExecuteContextMenuHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::contextMenuHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragStartHandler"))) void ExecuteDragStartHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragStartHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragEndHandler"))) void ExecuteDragEndHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragEndHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDropHandler"))) void ExecuteDropHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dropHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteWheelHandler"))) void ExecuteWheelHandler(std::int32_t handlerID, double deltaX, double deltaY, double deltaZ, std::int32_t deltaMode, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::wheelHandlers->find(handlerID)->second(Crafter::WheelEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey, deltaX, deltaY, deltaZ)); } __attribute__((export_name("ExecuteDragOverHandler"))) void ExecuteDragOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragOverHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragEnterHandler"))) void ExecuteDragEnterHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragEnterHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragLeaveHandler"))) void ExecuteDragLeaveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragLeaveHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteFetchHandler"))) void ExecuteFetchHandler(std::int32_t handlerID, const char* response) { std::cout << handlerID << std::endl; Crafter::CppDOMBindings::fetchHandlers->find(handlerID)->second(response); Crafter::CppDOMBindings::fetchHandlers->erase(handlerID); } } export namespace Crafter::CppDOMBindings { __attribute__((import_module("env"), import_name("fetch"))) void Fetch(const char* url, std::int32_t urlLenght, std::int32_t handlerID); void Fetch(const std::string_view url, std::function callback) { std::int32_t id = fetchHandlerMaxId++; CppDOMBindings::fetchHandlers->insert({id, callback}); Fetch(url.data(), url.size(), id); } }