ProcessGUI.cpp 8.9 KB

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