ProcessGUI.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "Process.h"
  2. #include "MemoryManager.h"
  3. #include <LibC/errno_numbers.h>
  4. #include <SharedGraphics/Font.h>
  5. #include <WindowServer/WSScreen.h>
  6. #include <WindowServer/WSMessageLoop.h>
  7. #include <WindowServer/WSWindow.h>
  8. #include <WindowServer/WSWindowManager.h>
  9. //#define LOG_GUI_SYSCALLS
  10. void Process::initialize_gui_statics()
  11. {
  12. Font::initialize();
  13. WSMessageLoop::initialize();
  14. WSWindowManager::initialize();
  15. WSScreen::initialize();
  16. new WSMessageLoop;
  17. }
  18. int Process::make_window_id()
  19. {
  20. int new_id = m_next_window_id++;
  21. while (!new_id || m_windows.contains(new_id)) {
  22. new_id = m_next_window_id++;
  23. if (new_id < 0)
  24. new_id = 1;
  25. }
  26. return new_id;
  27. }
  28. static void wait_for_gui_server()
  29. {
  30. // FIXME: Time out after a while and return an error.
  31. while (!WSMessageLoop::the().running())
  32. sleep(10);
  33. }
  34. int Process::gui$create_window(const GUI_WindowParameters* user_params)
  35. {
  36. wait_for_gui_server();
  37. if (!validate_read_typed(user_params))
  38. return -EFAULT;
  39. auto params = *user_params;
  40. Rect rect = params.rect;
  41. if (rect.is_empty())
  42. return -EINVAL;
  43. ProcessPagingScope scope(WSMessageLoop::the().server_process());
  44. int window_id = make_window_id();
  45. if (!window_id)
  46. return -ENOMEM;
  47. auto window = make<WSWindow>(*this, window_id);
  48. if (!window)
  49. return -ENOMEM;
  50. window->set_title(params.title);
  51. window->set_rect(rect);
  52. m_windows.set(window_id, move(window));
  53. #ifdef LOG_GUI_SYSCALLS
  54. 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());
  55. #endif
  56. return window_id;
  57. }
  58. int Process::gui$destroy_window(int window_id)
  59. {
  60. #ifdef LOG_GUI_SYSCALLS
  61. dbgprintf("%s<%u> gui$destroy_window (window_id=%d)\n", name().characters(), pid(), window_id);
  62. #endif
  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. auto message = make<WSMessage>(WSMessage::WM_DestroyWindow);
  69. WSMessageLoop::the().post_message((*it).value.leakPtr(), move(message), true);
  70. m_windows.remove(window_id);
  71. return 0;
  72. }
  73. int Process::gui$get_window_backing_store(int window_id, GUI_WindowBackingStoreInfo* info)
  74. {
  75. #ifdef LOG_GUI_SYSCALLS
  76. dbgprintf("%s<%u> gui$get_window_backing_store (window_id=%d, info=%p)\n", name().characters(), pid(), window_id, info);
  77. #endif
  78. if (!validate_write_typed(info))
  79. return -EFAULT;
  80. if (window_id < 0)
  81. return -EINVAL;
  82. auto it = m_windows.find(window_id);
  83. if (it == m_windows.end())
  84. return -EBADWINDOW;
  85. auto& window = *(*it).value;
  86. WSWindowLocker locker(window);
  87. auto* backing_store = window.backing();
  88. #ifdef BACKING_STORE_DEBUG
  89. dbgprintf("%s<%u> +++ %p[%d] (%dx%d)\n", name().characters(), pid(), backing_store, backing_store->width(), backing_store->height());
  90. #endif
  91. m_retained_backing_stores.append(backing_store);
  92. info->backing_store_id = backing_store;
  93. info->bpp = sizeof(RGBA32);
  94. info->pitch = backing_store->pitch();
  95. info->size = backing_store->size();
  96. info->pixels = reinterpret_cast<RGBA32*>(backing_store->client_region()->laddr().as_ptr());
  97. return 0;
  98. }
  99. int Process::gui$release_window_backing_store(void* backing_store_id)
  100. {
  101. for (size_t i = 0; i < m_retained_backing_stores.size(); ++i) {
  102. if (m_retained_backing_stores[i].ptr() == backing_store_id) {
  103. #ifdef BACKING_STORE_DEBUG
  104. auto* backing_store = m_retained_backing_stores[i].ptr();
  105. dbgprintf("%s<%u> --- %p (%dx%d)\n", name().characters(), pid(), backing_store, backing_store->width(), backing_store->height());
  106. #endif
  107. m_retained_backing_stores.remove(i);
  108. return 0;
  109. }
  110. }
  111. return -EBADBACKING;
  112. }
  113. int Process::gui$invalidate_window(int window_id, const GUI_Rect* a_rect)
  114. {
  115. if (window_id < 0)
  116. return -EINVAL;
  117. if (a_rect && !validate_read_typed(a_rect))
  118. return -EFAULT;
  119. auto it = m_windows.find(window_id);
  120. if (it == m_windows.end())
  121. return -EBADWINDOW;
  122. #ifdef LOG_GUI_SYSCALLS
  123. if (!a_rect)
  124. dbgprintf("%s<%u> gui$invalidate_window (window_id=%d, rect=(entire))\n", name().characters(), pid(), window_id);
  125. else
  126. dbgprintf("%s<%u> gui$invalidate_window (window_id=%d, rect={%d,%d %dx%d})\n", name().characters(), pid(), window_id, a_rect->location.x, a_rect->location.y, a_rect->size.width, a_rect->size.height);
  127. #endif
  128. auto& window = *(*it).value;
  129. Rect rect;
  130. if (a_rect)
  131. rect = *a_rect;
  132. WSMessageLoop::the().post_message(&window, make<WSClientWantsToPaintMessage>(rect));
  133. WSMessageLoop::the().server_process().request_wakeup();
  134. return 0;
  135. }
  136. int Process::gui$notify_paint_finished(int window_id, const GUI_Rect* a_rect)
  137. {
  138. if (window_id < 0)
  139. return -EINVAL;
  140. if (a_rect && !validate_read_typed(a_rect))
  141. return -EFAULT;
  142. auto it = m_windows.find(window_id);
  143. if (it == m_windows.end())
  144. return -EBADWINDOW;
  145. #ifdef LOG_GUI_SYSCALLS
  146. if (!a_rect)
  147. dbgprintf("%s<%u> gui$notify_paint_finished (window_id=%d, rect=(entire))\n", name().characters(), pid(), window_id);
  148. else
  149. dbgprintf("%s<%u> gui$notify_paint_finished (window_id=%d, rect={%d,%d %dx%d})\n", name().characters(), pid(), window_id, a_rect->location.x, a_rect->location.y, a_rect->size.width, a_rect->size.height);
  150. #endif
  151. auto& window = *(*it).value;
  152. Rect rect;
  153. if (a_rect)
  154. rect = *a_rect;
  155. WSMessageLoop::the().post_message(&window, make<WSClientFinishedPaintMessage>(rect));
  156. WSMessageLoop::the().server_process().request_wakeup();
  157. return 0;
  158. }
  159. int Process::gui$get_window_title(int window_id, char* buffer, size_t size)
  160. {
  161. if (window_id < 0)
  162. return -EINVAL;
  163. if (!validate_write(buffer, size))
  164. return -EFAULT;
  165. auto it = m_windows.find(window_id);
  166. if (it == m_windows.end())
  167. return -EBADWINDOW;
  168. auto& window = *(*it).value;
  169. String title;
  170. {
  171. WSWindowLocker locker(window);
  172. title = window.title();
  173. }
  174. if (title.length() > size)
  175. return -ERANGE;
  176. memcpy(buffer, title.characters(), title.length());
  177. return title.length();
  178. }
  179. int Process::gui$set_window_title(int window_id, const char* title, size_t size)
  180. {
  181. if (window_id < 0)
  182. return -EINVAL;
  183. if (!validate_read(title, size))
  184. return -EFAULT;
  185. auto it = m_windows.find(window_id);
  186. if (it == m_windows.end())
  187. return -EBADWINDOW;
  188. auto& window = *(*it).value;
  189. String new_title(title, size);
  190. WSMessageLoop::the().post_message(&window, make<WSSetWindowTitleMessage>(move(new_title)));
  191. WSMessageLoop::the().server_process().request_wakeup();
  192. return 0;
  193. }
  194. int Process::gui$get_window_rect(int window_id, GUI_Rect* rect)
  195. {
  196. if (window_id < 0)
  197. return -EINVAL;
  198. if (!validate_write_typed(rect))
  199. return -EFAULT;
  200. auto it = m_windows.find(window_id);
  201. if (it == m_windows.end())
  202. return -EBADWINDOW;
  203. auto& window = *(*it).value;
  204. {
  205. WSWindowLocker locker(window);
  206. *rect = window.rect();
  207. }
  208. return 0;
  209. }
  210. int Process::gui$set_window_rect(int window_id, const GUI_Rect* rect)
  211. {
  212. if (window_id < 0)
  213. return -EINVAL;
  214. if (!validate_read_typed(rect))
  215. return -EFAULT;
  216. auto it = m_windows.find(window_id);
  217. if (it == m_windows.end())
  218. return -EBADWINDOW;
  219. auto& window = *(*it).value;
  220. Rect new_rect = *rect;
  221. WSMessageLoop::the().post_message(&window, make<WSSetWindowRectMessage>(new_rect));
  222. WSMessageLoop::the().server_process().request_wakeup();
  223. return 0;
  224. }
  225. int Process::gui$set_global_cursor_tracking_enabled(int window_id, bool enabled)
  226. {
  227. if (window_id < 0)
  228. return -EINVAL;
  229. auto it = m_windows.find(window_id);
  230. if (it == m_windows.end())
  231. return -EBADWINDOW;
  232. auto& window = *(*it).value;
  233. WSWindowLocker locker(window);
  234. window.set_global_cursor_tracking_enabled(enabled);
  235. return 0;
  236. }
  237. void Process::destroy_all_windows()
  238. {
  239. for (auto& it : m_windows) {
  240. auto message = make<WSMessage>(WSMessage::WM_DestroyWindow);
  241. WSMessageLoop::the().post_message(it.value.leakPtr(), move(message), true);
  242. }
  243. m_windows.clear();
  244. }