2
0

ApplicationWindowLinux.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Window/ApplicationWindowLinux.h>
  6. #include <Utils/Log.h>
  7. ApplicationWindowLinux::~ApplicationWindowLinux()
  8. {
  9. if (mDisplay)
  10. {
  11. XDestroyWindow(mDisplay, mWindow);
  12. XCloseDisplay(mDisplay);
  13. }
  14. }
  15. void ApplicationWindowLinux::Initialize(const char *inTitle)
  16. {
  17. // Open connection to X server
  18. mDisplay = XOpenDisplay(nullptr);
  19. if (!mDisplay)
  20. FatalError("Failed to open X display");
  21. // Create a simple window
  22. int screen = DefaultScreen(mDisplay);
  23. mWindow = XCreateSimpleWindow(mDisplay, RootWindow(mDisplay, screen), 0, 0, mWindowWidth, mWindowHeight, 1, BlackPixel(mDisplay, screen), WhitePixel(mDisplay, screen));
  24. // Select input events
  25. XSelectInput(mDisplay, mWindow, ExposureMask | StructureNotifyMask | KeyPressMask);
  26. // Set window title
  27. XStoreName(mDisplay, mWindow, inTitle);
  28. // Register WM_DELETE_WINDOW to handle the close button
  29. mWmDeleteWindow = XInternAtom(mDisplay, "WM_DELETE_WINDOW", false);
  30. XSetWMProtocols(mDisplay, mWindow, &mWmDeleteWindow, 1);
  31. // Map the window (make it visible)
  32. XMapWindow(mDisplay, mWindow);
  33. // Flush the display to ensure commands are executed
  34. XFlush(mDisplay);
  35. }
  36. void ApplicationWindowLinux::MainLoop(RenderCallback inRenderCallback)
  37. {
  38. for (;;)
  39. {
  40. while (XPending(mDisplay) > 0)
  41. {
  42. XEvent event;
  43. XNextEvent(mDisplay, &event);
  44. if (event.type == ClientMessage && static_cast<Atom>(event.xclient.data.l[0]) == mWmDeleteWindow)
  45. {
  46. // Handle quit events
  47. return;
  48. }
  49. else if (event.type == ConfigureNotify)
  50. {
  51. // Handle window resize events
  52. XConfigureEvent xce = event.xconfigure;
  53. if (xce.width != mWindowWidth || xce.height != mWindowHeight)
  54. OnWindowResized(xce.width, xce.height);
  55. }
  56. else
  57. mEventListener(event);
  58. }
  59. // Call the render callback
  60. if (!inRenderCallback())
  61. return;
  62. }
  63. }