BackendWindow.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <stdint.h>
  26. #include <memory>
  27. #include "InputEvent.h"
  28. #include "../image/ImageRgbaU8.h"
  29. #include "../api/imageAPI.h"
  30. #include "../api/stringAPI.h"
  31. #include "../collection/List.h"
  32. namespace dsr {
  33. // The class to use when porting the window manager to another operating system.
  34. // A simple interface for the most basic operations that a window can do.
  35. // * Show an image over the whole window
  36. // * Take input events
  37. // Minimalism reduces the cost of porting core functionality to new operating systems.
  38. // All other features should be optional.
  39. class BackendWindow {
  40. public:
  41. String title;
  42. // Events
  43. List<InputEvent*> eventQueue;
  44. void queueInputEvent(InputEvent* event) {
  45. this->eventQueue.push(event);
  46. }
  47. private:
  48. int requestingResize = false;
  49. int requestedWidth = 0;
  50. int requestedHeight = 0;
  51. public:
  52. // Request to resize the window.
  53. // When the implementation receives a resize, call receiveWindowResize with the new dimensions.
  54. // If requestingResize is already true, it will just overwrite the old request.
  55. // Next call to executeEvents will then use it to resize the canvas.
  56. void receivedWindowResize(int width, int height) {
  57. this->requestingResize = true;
  58. this->requestedWidth = width;
  59. this->requestedHeight = height;
  60. }
  61. public:
  62. BackendWindow() {}
  63. virtual ~BackendWindow() {}
  64. virtual void setFullScreen(bool enabled) = 0;
  65. virtual bool isFullScreen() = 0;
  66. virtual int getWidth() const = 0;
  67. virtual int getHeight() const = 0;
  68. public:
  69. // Back-end interface
  70. // Responsible for adding events to eventQueue
  71. virtual void prefetchEvents() = 0;
  72. public:
  73. // Canvas interface
  74. virtual AlignedImageRgbaU8 getCanvas() = 0;
  75. virtual void showCanvas() = 0;
  76. virtual void resizeCanvas(int width, int height) = 0;
  77. virtual String getTitle() { return this->title; }
  78. virtual void setTitle(const String &newTitle) = 0;
  79. // Each callback declaration has a public variable and a public getter and setter
  80. DECLARE_CALLBACK(closeEvent, emptyCallback);
  81. DECLARE_CALLBACK(resizeEvent, sizeCallback);
  82. DECLARE_CALLBACK(keyboardEvent, keyboardCallback);
  83. DECLARE_CALLBACK(mouseEvent, mouseCallback);
  84. // Call executeEvents to run all callbacks collected in eventQueue
  85. // Returns true if any event was processed
  86. bool executeEvents();
  87. };
  88. }
  89. #endif