ProcessGUI.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "Process.h"
  2. #include "MemoryManager.h"
  3. #include <LibC/errno_numbers.h>
  4. #include <Widgets/AbstractScreen.h>
  5. #include <Widgets/FrameBuffer.h>
  6. #include <Widgets/EventLoop.h>
  7. #include <Widgets/Font.h>
  8. #include <Widgets/Button.h>
  9. #include <Widgets/Label.h>
  10. #include <Widgets/Widget.h>
  11. #include <Widgets/Window.h>
  12. #include <Widgets/WindowManager.h>
  13. void Process::initialize_gui_statics()
  14. {
  15. Font::initialize();
  16. FrameBuffer::initialize();
  17. EventLoop::initialize();
  18. WindowManager::initialize();
  19. AbstractScreen::initialize();
  20. new EventLoop;
  21. }
  22. int Process::make_window_id()
  23. {
  24. int new_id = m_next_window_id++;
  25. while (!new_id || m_windows.contains(new_id)) {
  26. new_id = m_next_window_id++;
  27. if (new_id < 0)
  28. new_id = 1;
  29. }
  30. return new_id;
  31. }
  32. static void wait_for_gui_server()
  33. {
  34. // FIXME: Time out after a while and return an error.
  35. while (!EventLoop::main().running())
  36. sleep(10);
  37. }
  38. int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
  39. {
  40. wait_for_gui_server();
  41. if (!validate_read_typed(user_params))
  42. return -EFAULT;
  43. auto params = *user_params;
  44. Rect rect = params.rect;
  45. if (rect.is_empty())
  46. return -EINVAL;
  47. ProcessPagingScope scope(EventLoop::main().server_process());
  48. int window_id = make_window_id();
  49. if (!window_id)
  50. return -ENOMEM;
  51. auto window = make<Window>(*this, window_id);
  52. if (!window)
  53. return -ENOMEM;
  54. window->setTitle(params.title);
  55. window->setRect(rect);
  56. m_windows.set(window_id, move(window));
  57. dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, rect.x(), rect.y(), rect.width(), rect.height());
  58. return window_id;
  59. }
  60. int Process::gui$destroy_window(int window_id)
  61. {
  62. dbgprintf("%s<%u> gui$destroy_window (window_id=%d)\n", name().characters(), pid(), window_id);
  63. if (window_id < 0)
  64. return -EINVAL;
  65. auto it = m_windows.find(window_id);
  66. if (it == m_windows.end())
  67. return -EBADWINDOW;
  68. m_windows.remove(window_id);
  69. return 0;
  70. }
  71. int Process::gui$get_window_backing_store(int window_id, GUI_WindowBackingStoreInfo* info)
  72. {
  73. dbgprintf("%s<%u> gui$get_window_backing_store (window_id=%d, info=%p)\n", name().characters(), pid(), window_id, info);
  74. if (!validate_write_typed(info))
  75. return -EFAULT;
  76. if (window_id < 0)
  77. return -EINVAL;
  78. auto it = m_windows.find(window_id);
  79. if (it == m_windows.end())
  80. return -EBADWINDOW;
  81. auto& window = *(*it).value;
  82. info->bpp = sizeof(RGBA32);
  83. info->pitch = window.backing()->pitch();
  84. info->size = window.backing()->size();
  85. info->pixels = reinterpret_cast<RGBA32*>(window.backing()->client_region()->linearAddress.asPtr());
  86. return 0;
  87. }
  88. int Process::gui$invalidate_window(int window_id)
  89. {
  90. dbgprintf("%s<%u> gui$invalidate_window (window_id=%d)\n", name().characters(), pid(), window_id);
  91. if (window_id < 0)
  92. return -EINVAL;
  93. auto it = m_windows.find(window_id);
  94. if (it == m_windows.end())
  95. return -EBADWINDOW;
  96. auto& window = *(*it).value;
  97. // FIXME: This should queue up a message that the window server process can read.
  98. // Poking into its data structures is not good.
  99. InterruptDisabler disabler;
  100. WindowManager::the().invalidate(window);
  101. return 0;
  102. }