BackendWindow.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_BACKEND_WINDOW
  24. #define DFPSR_BACKEND_WINDOW
  25. #include <cstdint>
  26. #include <memory>
  27. #include "InputEvent.h"
  28. #include "../api/imageAPI.h"
  29. #include "../api/stringAPI.h"
  30. #include "../collection/List.h"
  31. namespace dsr {
  32. // The class to use when porting the window manager to another operating system.
  33. // A simple interface for the most basic operations that a window can do.
  34. // * Show an image over the whole window
  35. // * Take input events
  36. // Minimalism reduces the cost of porting core functionality to new operating systems.
  37. // All other features should be optional.
  38. class BackendWindow {
  39. public:
  40. String title;
  41. // Events
  42. List<InputEvent*> eventQueue;
  43. void queueInputEvent(InputEvent* event) {
  44. this->eventQueue.push(event);
  45. }
  46. private:
  47. int requestingResize = false;
  48. int requestedWidth = 0;
  49. int requestedHeight = 0;
  50. public:
  51. // Request to resize the window.
  52. // When the implementation receives a resize, call receiveWindowResize with the new dimensions.
  53. // If requestingResize is already true, it will just overwrite the old request.
  54. // Next call to executeEvents will then use it to resize the canvas.
  55. void receivedWindowResize(int width, int height) {
  56. this->requestingResize = true;
  57. this->requestedWidth = width;
  58. this->requestedHeight = height;
  59. }
  60. public:
  61. BackendWindow() {}
  62. virtual ~BackendWindow() {}
  63. virtual void setFullScreen(bool enabled) = 0;
  64. virtual bool isFullScreen() = 0;
  65. virtual int getWidth() const = 0;
  66. virtual int getHeight() const = 0;
  67. public:
  68. // Back-end interface
  69. // Responsible for adding events to eventQueue
  70. virtual void prefetchEvents() = 0;
  71. public:
  72. // Canvas interface
  73. virtual AlignedImageRgbaU8 getCanvas() = 0;
  74. virtual void showCanvas() = 0;
  75. virtual void resizeCanvas(int width, int height) = 0;
  76. virtual String getTitle() { return this->title; }
  77. virtual void setTitle(const String &newTitle) = 0;
  78. public:
  79. // Cursor interface
  80. bool visibleCursor = true; // Written to by setCursorVisibility on success.
  81. virtual bool setCursorVisibility(bool visible) { return false; } // Returns true on success.
  82. virtual void setCursorPosition(int x, int y) {} // Does nothing unless implemented.
  83. public:
  84. // Clipboard interface
  85. // If none is replaced, both default implementations will use an internal variable.
  86. // If both are implemented, the system's clipboard should be accessed.
  87. // Partial implementations with only loadFromClipboard or saveToClipboard are not allowed.
  88. // Load from the clipboard, waiting at most timeoutInMilliseconds milliseconds.
  89. virtual ReadableString loadFromClipboard(double timeoutInSeconds = 0.5);
  90. // Save text to the clipboard.
  91. virtual void saveToClipboard(const ReadableString &text, double timeoutInSeconds = 0.5);
  92. public:
  93. // Each callback declaration has a public variable and a public getter and setter
  94. DECLARE_CALLBACK(closeEvent, emptyCallback);
  95. DECLARE_CALLBACK(resizeEvent, sizeCallback);
  96. DECLARE_CALLBACK(keyboardEvent, keyboardCallback);
  97. DECLARE_CALLBACK(mouseEvent, mouseCallback);
  98. // Call executeEvents to run all callbacks collected in eventQueue
  99. // Returns true if any event was processed
  100. bool executeEvents();
  101. };
  102. }
  103. #endif