logo

inaban

Distrustful Wayland Compositor (inspired by XMonad and dwm) git clone https://hacktivis.me/git/inaban.git

inaban.c (34990B)


  1. // SPDX-FileCopyrightText: 2019 tinyWL Authors
  2. // SPDX-License-Identifier: CC0-1.0
  3. //
  4. // SPDX-FileCopyrightText: 2019-2022 inaban Authors <https://hacktivis.me/git/inaban>
  5. // SPDX-License-Identifier: BSD-3-Clause
  6. #include "inaban.h"
  7. #include "config.h"
  8. #include <assert.h>
  9. #include <getopt.h>
  10. #include <signal.h> /* signal(), SIGTERM */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <unistd.h> /* execvp() */
  15. #include <unistd.h>
  16. #define LENGTH(X) (sizeof X / sizeof X[0])
  17. #define MAX_STARTUP_CMD 256
  18. struct inaban_server server = {0};
  19. // xdg-decoration.c
  20. extern void handle_xdg_toplevel_decoration(struct wl_listener *listener, void *data);
  21. /* event raised when a modifier key, such as shift or alt, is pressed. */
  22. static void
  23. keyboard_handle_modifiers(struct wl_listener *listener, void *data)
  24. {
  25. (void)data;
  26. struct inaban_keyboard *keyboard = wl_container_of(listener, keyboard, modifiers);
  27. struct wlr_seat *seat = keyboard->server->seat;
  28. if(server.locked == true)
  29. {
  30. // needs to be written
  31. }
  32. else
  33. {
  34. wlr_seat_set_keyboard(seat, keyboard->device);
  35. wlr_seat_keyboard_notify_modifiers(seat, &keyboard->device->keyboard->modifiers);
  36. }
  37. }
  38. /* event raised when a key is pressed or released. */
  39. static void
  40. keyboard_handle_key(struct wl_listener *listener, void *data)
  41. {
  42. struct inaban_keyboard *keyboard = wl_container_of(listener, keyboard, key);
  43. struct inaban_server *server = keyboard->server;
  44. struct wlr_event_keyboard_key *event = data;
  45. struct wlr_seat *seat = server->seat;
  46. /* Translate libinput keycode -> xkbcommon */
  47. uint32_t keycode = event->keycode + 8;
  48. xkb_keysym_t keysym = xkb_state_key_get_one_sym(keyboard->device->keyboard->xkb_state, keycode);
  49. bool handled = false;
  50. uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard);
  51. if(server->locked == true)
  52. {
  53. if((event->state == WL_KEYBOARD_KEY_STATE_PRESSED) &&
  54. (keysym >= XKB_KEY_XF86Switch_VT_1 && keysym <= XKB_KEY_XF86Switch_VT_12))
  55. {
  56. struct wlr_session *session = wlr_backend_get_session(server->backend);
  57. if(session)
  58. {
  59. unsigned vt = keysym - XKB_KEY_XF86Switch_VT_1 + 1;
  60. wlr_session_change_vt(session, vt);
  61. }
  62. }
  63. return;
  64. }
  65. switch(event->state)
  66. {
  67. case WL_KEYBOARD_KEY_STATE_PRESSED:
  68. wlr_log(WLR_DEBUG,
  69. "key_pressed: {modifiers: %x, keycode: %x, keysym: %x}",
  70. modifiers,
  71. keycode,
  72. keysym);
  73. if(keysym >= XKB_KEY_XF86Switch_VT_1 && keysym <= XKB_KEY_XF86Switch_VT_12)
  74. {
  75. struct wlr_session *session = wlr_backend_get_session(server->backend);
  76. if(session)
  77. {
  78. unsigned vt = keysym - XKB_KEY_XF86Switch_VT_1 + 1;
  79. wlr_session_change_vt(session, vt);
  80. }
  81. }
  82. for(size_t i = 0; i < LENGTH(shortcuts); i++)
  83. if((keysym == shortcuts[i].keysym) && (modifiers == shortcuts[i].mod) && shortcuts[i].func)
  84. {
  85. shortcuts[i].func(&(shortcuts[i].arg));
  86. handled = true;
  87. }
  88. if(!handled && keysym == ModKey)
  89. {
  90. wlr_log(WLR_DEBUG, "InputMode: ModKey");
  91. server->input_mode = INABAN_INPUT_MODKEY;
  92. handled = true;
  93. }
  94. break;
  95. case WL_KEYBOARD_KEY_STATE_RELEASED:
  96. if(keysym == ModKey && server->input_mode == INABAN_INPUT_MODKEY)
  97. {
  98. wlr_log(WLR_DEBUG, "InputMode: Normal");
  99. server->input_mode = INABAN_INPUT_NORMAL;
  100. handled = true;
  101. }
  102. break;
  103. }
  104. /* Otherwise, we pass it along to the client. */
  105. if(!handled)
  106. {
  107. wlr_seat_set_keyboard(seat, keyboard->device);
  108. wlr_seat_keyboard_notify_key(seat, event->time_msec, event->keycode, event->state);
  109. }
  110. }
  111. static void
  112. server_new_keyboard(struct inaban_server *server, struct wlr_input_device *device)
  113. {
  114. struct inaban_keyboard *keyboard = calloc(1, sizeof(struct inaban_keyboard));
  115. keyboard->server = server;
  116. keyboard->device = device;
  117. /* We need to prepare an XKB keymap and assign it to the keyboard. This
  118. * assumes the defaults (e.g. layout = "us"). */
  119. struct xkb_rule_names rules = {0};
  120. struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  121. struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
  122. wlr_keyboard_set_keymap(device->keyboard, keymap);
  123. xkb_keymap_unref(keymap);
  124. xkb_context_unref(context);
  125. wlr_keyboard_set_repeat_info(device->keyboard, 25, 600);
  126. /* Here we set up listeners for keyboard events. */
  127. keyboard->modifiers.notify = keyboard_handle_modifiers;
  128. wl_signal_add(&device->keyboard->events.modifiers, &keyboard->modifiers);
  129. keyboard->key.notify = keyboard_handle_key;
  130. wl_signal_add(&device->keyboard->events.key, &keyboard->key);
  131. wlr_seat_set_keyboard(server->seat, device);
  132. /* And add the keyboard to our list of keyboards */
  133. wl_list_insert(&server->keyboards, &keyboard->link);
  134. }
  135. static void
  136. server_new_pointer(struct inaban_server *server, struct wlr_input_device *device)
  137. {
  138. /* We don't do anything special with pointers. All of our pointer handling
  139. * is proxied through wlr_cursor. On another compositor, you might take this
  140. * opportunity to do libinput configuration on the device to set
  141. * acceleration, etc. */
  142. wlr_cursor_attach_input_device(server->cursor, device);
  143. }
  144. /* event raised by the backend when a new input device becomes available */
  145. static void
  146. server_new_input(struct wl_listener *listener, void *data)
  147. {
  148. struct inaban_server *server = wl_container_of(listener, server, new_input);
  149. struct wlr_input_device *device = data;
  150. switch(device->type)
  151. {
  152. case WLR_INPUT_DEVICE_KEYBOARD: server_new_keyboard(server, device); break;
  153. case WLR_INPUT_DEVICE_POINTER: server_new_pointer(server, device); break;
  154. default: break;
  155. }
  156. /* We need to let the wlr_seat know what our capabilities are, which is
  157. * communiciated to the client. In TinyWL we always have a cursor, even if
  158. * there are no pointer devices, so we always include that capability. */
  159. uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
  160. if(!wl_list_empty(&server->keyboards)) caps |= WL_SEAT_CAPABILITY_KEYBOARD;
  161. wlr_seat_set_capabilities(server->seat, caps);
  162. }
  163. /* event raised by the seat when a client provides a cursor image */
  164. static void
  165. seat_request_cursor(struct wl_listener *listener, void *data)
  166. {
  167. struct inaban_server *server = wl_container_of(listener, server, request_cursor);
  168. struct wlr_seat_pointer_request_set_cursor_event *event = data;
  169. struct wlr_seat_client *focused_client = server->seat->pointer_state.focused_client;
  170. if(focused_client == event->seat_client)
  171. wlr_cursor_set_surface(server->cursor, event->surface, event->hotspot_x, event->hotspot_y);
  172. }
  173. static bool
  174. view_at(struct inaban_view *view,
  175. double lx,
  176. double ly,
  177. struct wlr_surface **surface,
  178. double *sx,
  179. double *sy)
  180. {
  181. /*
  182. * XDG toplevels may have nested surfaces, such as popup windows for context
  183. * menus or tooltips. This function tests if any of those are underneath the
  184. * coordinates lx and ly (in output Layout Coordinates). If so, it sets the
  185. * surface pointer to that wlr_surface and the sx and sy coordinates to the
  186. * coordinates relative to that surface's top-left corner.
  187. */
  188. double view_sx = lx - view->x;
  189. double view_sy = ly - view->y;
  190. double _sx, _sy;
  191. struct wlr_surface *_surface = NULL;
  192. _surface = wlr_xdg_surface_surface_at(view->xdg_surface, view_sx, view_sy, &_sx, &_sy);
  193. if(_surface != NULL)
  194. {
  195. *sx = _sx;
  196. *sy = _sy;
  197. *surface = _surface;
  198. return true;
  199. }
  200. return false;
  201. }
  202. /* iterates over all of our surfaces and attempts to find one under the cursor,
  203. * relies on server->views being ordered from top-to-bottom */
  204. static struct inaban_view *
  205. desktop_view_at(struct inaban_server *server,
  206. double lx,
  207. double ly,
  208. struct wlr_surface **surface,
  209. double *sx,
  210. double *sy)
  211. {
  212. struct inaban_view *view;
  213. wl_list_for_each(view, &server->views, link)
  214. {
  215. if(view_at(view, lx, ly, surface, sx, sy)) return view;
  216. }
  217. return NULL;
  218. }
  219. static void
  220. process_cursor_motion(struct inaban_server *server, uint32_t time)
  221. {
  222. if(server->cursor_mode == INABAN_CURSOR_MOVE)
  223. {
  224. struct inaban_view *view = server->grabbed_view;
  225. view->x = (int)server->cursor->x - (int)server->grab_x;
  226. view->y = (int)server->cursor->y - (int)server->grab_y;
  227. }
  228. else if(server->cursor_mode == INABAN_CURSOR_RESIZE)
  229. {
  230. struct inaban_view *view = server->grabbed_view;
  231. uint32_t width = (uint32_t)server->cursor->x - (uint32_t)server->grab_x;
  232. uint32_t height = (uint32_t)server->cursor->y - (uint32_t)server->grab_y;
  233. wlr_xdg_toplevel_set_size(view->xdg_surface, width, height);
  234. }
  235. else
  236. {
  237. double sx, sy;
  238. struct wlr_seat *seat = server->seat;
  239. struct wlr_surface *surface = NULL;
  240. struct inaban_view *view =
  241. desktop_view_at(server, server->cursor->x, server->cursor->y, &surface, &sx, &sy);
  242. /* If there's no view under the cursor, set the cursor image to a
  243. * default. This is what makes the cursor image appear when you move it
  244. * around the screen, not over any views. */
  245. if(!view) wlr_xcursor_manager_set_cursor_image(server->cursor_mgr, "left_ptr", server->cursor);
  246. if(surface)
  247. {
  248. bool focus_changed = seat->pointer_state.focused_surface != surface;
  249. /*
  250. * "Enter" the surface if necessary. This lets the client know that the
  251. * cursor has entered one of its surfaces.
  252. *
  253. * Note that this gives the surface "pointer focus", which is distinct
  254. * from keyboard focus. You get pointer focus by moving the pointer over
  255. * a window.
  256. */
  257. wlr_seat_pointer_notify_enter(seat, surface, sx, sy);
  258. /* The enter event contains coordinates, so we only need to notify
  259. * on motion if the focus did not change. */
  260. if(!focus_changed) wlr_seat_pointer_notify_motion(seat, time, sx, sy);
  261. }
  262. else
  263. {
  264. /* Clear pointer focus so future button events and such are not sent to
  265. * the last client to have the cursor over it. */
  266. wlr_seat_pointer_clear_focus(seat);
  267. }
  268. }
  269. }
  270. static void
  271. server_cursor_motion(struct wl_listener *listener, void *data)
  272. {
  273. if(server.locked == true)
  274. {
  275. return;
  276. }
  277. /* This event is forwarded by the cursor when a pointer emits a _relative_
  278. * pointer motion event (i.e. a delta) */
  279. struct inaban_server *server = wl_container_of(listener, server, cursor_motion);
  280. struct wlr_event_pointer_motion *event = data;
  281. /* The cursor doesn't move unless we tell it to. The cursor automatically
  282. * handles constraining the motion to the output layout, as well as any
  283. * special configuration applied for the specific input device which
  284. * generated the event. You can pass NULL for the device if you want to move
  285. * the cursor around without any input. */
  286. wlr_cursor_move(server->cursor, event->device, event->delta_x, event->delta_y);
  287. process_cursor_motion(server, event->time_msec);
  288. }
  289. static void
  290. server_cursor_motion_absolute(struct wl_listener *listener, void *data)
  291. {
  292. if(server.locked == true)
  293. {
  294. return;
  295. }
  296. /* This event is forwarded by the cursor when a pointer emits an _absolute_
  297. * motion event, from 0..1 on each axis. This happens, for example, when
  298. * wlroots is running under a Wayland window rather than KMS+DRM, and you
  299. * move the mouse over the window. You could enter the window from any edge,
  300. * so we have to warp the mouse there. There is also some hardware which
  301. * emits these events. */
  302. struct inaban_server *server = wl_container_of(listener, server, cursor_motion_absolute);
  303. struct wlr_event_pointer_motion_absolute *event = data;
  304. wlr_cursor_warp_absolute(server->cursor, event->device, event->x, event->y);
  305. process_cursor_motion(server, event->time_msec);
  306. }
  307. /* event forwarded by the cursor when a pointer emits a button event */
  308. static void
  309. server_cursor_button(struct wl_listener *listener, void *data)
  310. {
  311. if(server.locked == true)
  312. {
  313. return;
  314. }
  315. struct inaban_server *server = wl_container_of(listener, server, cursor_button);
  316. struct wlr_event_pointer_button *event = data;
  317. double sx, sy;
  318. struct wlr_surface *surface = NULL;
  319. struct inaban_view *view =
  320. desktop_view_at(server, server->cursor->x, server->cursor->y, &surface, &sx, &sy);
  321. if((server->input_mode == INABAN_INPUT_MODKEY) && (server->cursor_mode == INABAN_CURSOR_NORMAL) &&
  322. (event->state == WLR_BUTTON_PRESSED) && (view != NULL))
  323. {
  324. struct wlr_box geo_box;
  325. wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box);
  326. switch(event->button)
  327. {
  328. case 272: /* for some reason this is button 1 */
  329. server->cursor_mode = INABAN_CURSOR_MOVE;
  330. server->grabbed_view = view;
  331. server->grab_x = server->cursor->x - view->x;
  332. server->grab_y = server->cursor->y - view->y;
  333. wlr_log(WLR_DEBUG, "server->cursor_mode = move");
  334. break;
  335. case 273: /* for some reason this is button 2 */
  336. server->cursor_mode = INABAN_CURSOR_RESIZE;
  337. server->grabbed_view = view;
  338. server->grab_x = view->x;
  339. server->grab_y = view->y;
  340. server->grab_width = geo_box.width;
  341. server->grab_height = geo_box.height;
  342. wlr_log(WLR_DEBUG, "server->cursor_mode = resize");
  343. break;
  344. }
  345. }
  346. else if((server->cursor_mode != INABAN_CURSOR_NORMAL) && (event->state == WLR_BUTTON_RELEASED))
  347. {
  348. server->cursor_mode = INABAN_CURSOR_NORMAL;
  349. }
  350. else
  351. {
  352. /* Notify the client with pointer focus that a button press has occurred */
  353. wlr_seat_pointer_notify_button(server->seat, event->time_msec, event->button, event->state);
  354. /* Focus that client if the button was _pressed_ */
  355. focus_view(view, surface);
  356. }
  357. }
  358. static void
  359. server_cursor_axis(struct wl_listener *listener, void *data)
  360. {
  361. if(server.locked == true)
  362. {
  363. return;
  364. }
  365. /* This event is forwarded by the cursor when a pointer emits an axis event,
  366. * for example when you move the scroll wheel. */
  367. struct inaban_server *server = wl_container_of(listener, server, cursor_axis);
  368. struct wlr_event_pointer_axis *event = data;
  369. /* Notify the client with pointer focus of the axis event. */
  370. wlr_seat_pointer_notify_axis(server->seat,
  371. event->time_msec,
  372. event->orientation,
  373. event->delta,
  374. event->delta_discrete,
  375. event->source);
  376. }
  377. static void
  378. server_cursor_frame(struct wl_listener *listener, void *data)
  379. {
  380. if(server.locked == true)
  381. {
  382. return;
  383. }
  384. (void)data;
  385. /* This event is forwarded by the cursor when a pointer emits an frame
  386. * event. Frame events are sent after regular pointer events to group
  387. * multiple events together. For instance, two axis events may happen at the
  388. * same time, in which case a frame event won't be sent in between. */
  389. struct inaban_server *server = wl_container_of(listener, server, cursor_frame);
  390. /* Notify the client with pointer focus of the frame event. */
  391. wlr_seat_pointer_notify_frame(server->seat);
  392. }
  393. /* called for every surface that needs to be rendered */
  394. static void
  395. render_surface(struct wlr_surface *surface, int sx, int sy, void *data)
  396. {
  397. struct wlr_box border_box;
  398. struct render_data *rdata = data;
  399. struct inaban_view *view = rdata->view;
  400. struct wlr_output *output = rdata->output;
  401. /* We first obtain a wlr_texture, which is a GPU resource. wlroots
  402. * automatically handles negotiating these with the client. The underlying
  403. * resource could be an opaque handle passed from the client, or the client
  404. * could have sent a pixel buffer which we copied to the GPU, or a few other
  405. * means. You don't have to worry about this, wlroots takes care of it. */
  406. struct wlr_texture *texture = wlr_surface_get_texture(surface);
  407. if(texture == NULL) return;
  408. /* The view has a position in layout coordinates. If you have two displays,
  409. * one next to the other, both 1080p, a view on the rightmost display might
  410. * have layout coordinates of 2000,100. We need to translate that to
  411. * output-local coordinates, or (2000 - 1920). */
  412. double ox = 0, oy = 0;
  413. wlr_output_layout_output_coords(view->server->output_layout, output, &ox, &oy);
  414. ox += view->x + sx, oy += view->y + sy;
  415. /* We also have to apply the scale factor for HiDPI outputs. This is only
  416. * part of the puzzle, TinyWL does not fully support HiDPI. */
  417. struct wlr_box box = {
  418. .x = (int)(ox * output->scale),
  419. .y = (int)(oy * output->scale),
  420. .width = (int)(surface->current.width * output->scale),
  421. .height = (int)(surface->current.height * output->scale),
  422. };
  423. /*
  424. * Those familiar with OpenGL are also familiar with the role of matricies
  425. * in graphics programming. We need to prepare a matrix to render the view
  426. * with. wlr_matrix_project_box is a helper which takes a box with a desired
  427. * x, y coordinates, width and height, and an output geometry, then
  428. * prepares an orthographic projection and multiplies the necessary
  429. * transforms to produce a model-view-projection matrix.
  430. *
  431. * Naturally you can do this any way you like, for example to make a 3D
  432. * compositor.
  433. */
  434. float matrix[9];
  435. enum wl_output_transform transform = wlr_output_transform_invert(surface->current.transform);
  436. wlr_matrix_project_box(matrix, &box, transform, 0, output->transform_matrix);
  437. /* top border */
  438. border_box.x = box.x - BORDER_SIZE;
  439. border_box.y = box.y - BORDER_SIZE;
  440. border_box.width = box.width + BORDER_SIZE * 2;
  441. border_box.height = BORDER_SIZE;
  442. wlr_render_rect(rdata->renderer, &border_box, border_color, output->transform_matrix);
  443. /* bottom border */
  444. border_box.y = box.y + box.height;
  445. wlr_render_rect(rdata->renderer, &border_box, border_color, output->transform_matrix);
  446. /* left border */
  447. border_box.x = box.x - BORDER_SIZE;
  448. border_box.y = box.y - BORDER_SIZE;
  449. border_box.width = BORDER_SIZE;
  450. border_box.height = box.height + BORDER_SIZE * 2;
  451. wlr_render_rect(rdata->renderer, &border_box, border_color, output->transform_matrix);
  452. /* right border */
  453. border_box.x = box.x + box.width;
  454. wlr_render_rect(rdata->renderer, &border_box, border_color, output->transform_matrix);
  455. /* This takes our matrix, the texture, and an alpha, and performs the actual
  456. * rendering on the GPU. */
  457. wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1);
  458. /* This lets the client know that we've displayed that frame and it can
  459. * prepare another one now if it likes. */
  460. wlr_surface_send_frame_done(surface, rdata->when);
  461. }
  462. /* called every time an output is ready to display a frame,
  463. * generally at the output's refresh rate (e.g. 60Hz). */
  464. static void
  465. output_frame(struct wl_listener *listener, void *data)
  466. {
  467. (void)data;
  468. struct inaban_output *output = wl_container_of(listener, output, frame);
  469. struct wlr_renderer *renderer = output->server->renderer;
  470. struct timespec now;
  471. clock_gettime(CLOCK_MONOTONIC, &now);
  472. /* wlr_output_attach_render makes the OpenGL context current. */
  473. if(!wlr_output_attach_render(output->wlr_output, NULL))
  474. {
  475. return;
  476. }
  477. /* The "effective" resolution can change if you rotate your outputs. */
  478. int width, height;
  479. wlr_output_effective_resolution(output->wlr_output, &width, &height);
  480. /* Begin the renderer (calls glViewport and some other GL sanity checks) */
  481. wlr_renderer_begin(renderer, (uint32_t)width, (uint32_t)height);
  482. if(output->server->locked == true)
  483. {
  484. // cursor isn't draw because the cursor motion is ignored
  485. wlr_renderer_clear(renderer, locked_color);
  486. wlr_renderer_end(renderer);
  487. wlr_output_commit(output->wlr_output);
  488. return;
  489. }
  490. wlr_renderer_clear(renderer, background_color);
  491. /* Each subsequent window we render is rendered on top of the last. Because
  492. * our view list is ordered front-to-back, we iterate over it backwards. */
  493. struct inaban_view *view;
  494. wl_list_for_each_reverse(view, &output->server->views, link)
  495. {
  496. if(!view->mapped) continue; /* An unmapped view should not be rendered. */
  497. struct render_data rdata = {
  498. .output = output->wlr_output,
  499. .view = view,
  500. .renderer = renderer,
  501. .when = &now,
  502. };
  503. /* This calls our render_surface function for each surface among the
  504. * xdg_surface's toplevel and popups. */
  505. wlr_xdg_surface_for_each_surface(view->xdg_surface, render_surface, &rdata);
  506. }
  507. /* Hardware cursors are rendered by the GPU on a separate plane, and can be
  508. * moved around without re-rendering what's beneath them - which is more
  509. * efficient. However, not all hardware supports hardware cursors. For this
  510. * reason, wlroots provides a software fallback, which we ask it to render
  511. * here. wlr_cursor handles configuring hardware vs software cursors for you,
  512. * and this function is a no-op when hardware cursors are in use. */
  513. wlr_output_render_software_cursors(output->wlr_output, NULL);
  514. /* Conclude rendering and swap the buffers, showing the final frame
  515. * on-screen. */
  516. wlr_renderer_end(renderer);
  517. wlr_output_commit(output->wlr_output);
  518. }
  519. static void
  520. handle_output_mode(struct wl_listener *listener, void *data)
  521. {
  522. (void)data;
  523. struct inaban_output *output = wl_container_of(listener, output, mode);
  524. struct wlr_output *wlr_output = output->wlr_output;
  525. struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
  526. if(mode)
  527. {
  528. wlr_output_set_mode(wlr_output, mode);
  529. }
  530. wlr_output_enable(wlr_output, true);
  531. wlr_output_commit(wlr_output);
  532. }
  533. /* raised by the backend when a new output (aka display/monitor) becomes available */
  534. static void
  535. server_new_output(struct wl_listener *listener, void *data)
  536. {
  537. struct inaban_server *server = wl_container_of(listener, server, new_output);
  538. struct wlr_output *wlr_output = data;
  539. /* Allocates and configures our state for this output */
  540. struct inaban_output *output = calloc(1, sizeof(struct inaban_output));
  541. if(output == NULL)
  542. {
  543. wlr_log_errno(WLR_ERROR, "Unable to allocate inaban output");
  544. return;
  545. }
  546. struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
  547. if(mode)
  548. {
  549. wlr_output_set_mode(wlr_output, mode);
  550. }
  551. wlr_output_init_render(wlr_output, server->allocator, server->renderer);
  552. output->wlr_output = wlr_output;
  553. output->server = server;
  554. /* Sets up a listener for the frame notify event. */
  555. output->frame.notify = output_frame;
  556. wl_signal_add(&wlr_output->events.frame, &output->frame);
  557. wl_list_insert(&server->outputs, &output->link);
  558. output->mode.notify = handle_output_mode;
  559. wl_signal_add(&wlr_output->events.mode, &output->mode);
  560. /* Adds this to the output layout. The add_auto function arranges outputs
  561. * from left-to-right in the order they appear. A more sophisticated
  562. * compositor would let the user configure the arrangement of outputs in the
  563. * layout. */
  564. wlr_output_layout_add_auto(server->output_layout, wlr_output);
  565. /* Creating the global adds a wl_output global to the display, which Wayland
  566. * clients can see to find out information about the output (such as
  567. * DPI, scale factor, manufacturer, etc). */
  568. wlr_output_create_global(wlr_output);
  569. wlr_output_enable(wlr_output, true);
  570. wlr_output_commit(wlr_output);
  571. }
  572. /* Called when the surface is mapped, or ready to display on-screen. */
  573. static void
  574. xdg_surface_map(struct wl_listener *listener, void *data)
  575. {
  576. (void)data;
  577. struct inaban_view *view = wl_container_of(listener, view, map);
  578. view->mapped = true;
  579. focus_view(view, view->xdg_surface->surface);
  580. }
  581. /* Called when the surface is unmapped, and should no longer be shown. */
  582. static void
  583. xdg_surface_unmap(struct wl_listener *listener, void *data)
  584. {
  585. (void)data;
  586. struct inaban_view *view = wl_container_of(listener, view, unmap);
  587. view->mapped = false;
  588. }
  589. /* Called when the surface is destroyed and should never be shown again. */
  590. static void
  591. xdg_surface_destroy(struct wl_listener *listener, void *data)
  592. {
  593. (void)data;
  594. struct inaban_view *view = wl_container_of(listener, view, destroy);
  595. wl_list_remove(&view->link);
  596. free(view);
  597. }
  598. static void
  599. xdg_deny_request(struct wl_listener *listener, void *data)
  600. {
  601. (void)listener;
  602. (void)data;
  603. }
  604. /* raised when wlr_xdg_shell receives a new xdg surface from a client,
  605. * either a toplevel (application window) or popup. */
  606. static void
  607. server_new_xdg_surface(struct wl_listener *listener, void *data)
  608. {
  609. struct inaban_server *server = wl_container_of(listener, server, new_xdg_surface);
  610. struct wlr_xdg_surface *xdg_surface = data;
  611. if(xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) return;
  612. /* Allocate a inaban_view for this surface */
  613. struct inaban_view *view = calloc(1, sizeof(struct inaban_view));
  614. view->server = server;
  615. view->xdg_surface = xdg_surface;
  616. /* Listen to the various events it can emit */
  617. view->map.notify = xdg_surface_map;
  618. wl_signal_add(&xdg_surface->events.map, &view->map);
  619. view->unmap.notify = xdg_surface_unmap;
  620. wl_signal_add(&xdg_surface->events.unmap, &view->unmap);
  621. view->destroy.notify = xdg_surface_destroy;
  622. wl_signal_add(&xdg_surface->events.destroy, &view->destroy);
  623. /* cotd */
  624. struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel;
  625. view->request_move.notify = xdg_deny_request;
  626. wl_signal_add(&toplevel->events.request_move, &view->request_move);
  627. view->request_resize.notify = xdg_deny_request;
  628. wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
  629. /* Add it to the list of views. */
  630. wl_list_insert(&server->views, &view->link);
  631. }
  632. static void
  633. handle_request_set_primary_selection(struct wl_listener *listener, void *data)
  634. {
  635. if(server.locked == true)
  636. {
  637. return;
  638. }
  639. struct inaban_server *server = wl_container_of(listener, server, request_set_primary_selection);
  640. struct wlr_seat_request_set_primary_selection_event *event = data;
  641. wlr_seat_set_primary_selection(server->seat, event->source, event->serial);
  642. }
  643. static void
  644. handle_request_set_selection(struct wl_listener *listener, void *data)
  645. {
  646. if(server.locked == true)
  647. {
  648. return;
  649. }
  650. struct inaban_server *server = wl_container_of(listener, server, request_set_selection);
  651. struct wlr_seat_request_set_selection_event *event = data;
  652. wlr_seat_set_selection(server->seat, event->source, event->serial);
  653. }
  654. void
  655. sigterm_handler(int signal)
  656. {
  657. (void)signal;
  658. wl_display_terminate(server.wl_display);
  659. }
  660. void
  661. usage(char *argv0)
  662. {
  663. printf("Usage: %s [-s startup command]\n", argv0);
  664. }
  665. int
  666. main(int argc, char *argv[])
  667. {
  668. wlr_log_init(WLR_DEBUG, NULL);
  669. char *startup_cmdv[MAX_STARTUP_CMD] = {NULL};
  670. int startup_cmdc = 0;
  671. int ret = 0;
  672. struct wlr_server_decoration_manager *server_decoration_manager = NULL;
  673. struct wlr_xdg_decoration_manager_v1 *xdg_decoration_manager = NULL;
  674. server.locked = false;
  675. if((getuid() * geteuid() * getgid() * getegid()) == 0)
  676. {
  677. wlr_log(WLR_ERROR, "running as root, refusing to continue");
  678. return 1;
  679. }
  680. // handle SIGTERM signals
  681. signal(SIGTERM, sigterm_handler);
  682. int c;
  683. while((c = getopt(argc, argv, "s:h")) != -1)
  684. {
  685. switch(c)
  686. {
  687. case 's':
  688. if(startup_cmdc < MAX_STARTUP_CMD)
  689. {
  690. startup_cmdv[startup_cmdc] = optarg;
  691. startup_cmdc++;
  692. }
  693. else
  694. {
  695. wlr_log(WLR_INFO,
  696. "Ignoring ā€˜-s %sā€™, max number of startup commands (%d) reached.",
  697. optarg,
  698. MAX_STARTUP_CMD);
  699. }
  700. break;
  701. default: usage(argv[0]); return 0;
  702. }
  703. }
  704. if(optind < argc)
  705. {
  706. usage(argv[0]);
  707. return 0;
  708. }
  709. char *terminal = getenv("TERMINAL");
  710. if(NULL != terminal)
  711. {
  712. termcmd[0] = terminal;
  713. }
  714. server.wl_display = wl_display_create();
  715. if(!server.wl_display)
  716. {
  717. wlr_log(WLR_ERROR, "Cannot allocate a Wayland display");
  718. return 1;
  719. }
  720. server.backend = wlr_backend_autocreate(server.wl_display);
  721. if(!server.backend)
  722. {
  723. wlr_log(WLR_ERROR, "Unable to create the wlroots backend");
  724. ret = 1;
  725. goto end;
  726. }
  727. /* If we don't provide a renderer, autocreate makes a GLES2 renderer for us.
  728. * The renderer is responsible for defining the various pixel formats it
  729. * supports for shared memory, this configures that for clients. */
  730. server.renderer = wlr_renderer_autocreate(server.backend);
  731. if(!server.renderer)
  732. {
  733. wlr_log(WLR_ERROR, "Failed to create renderer");
  734. ret = 1;
  735. goto end;
  736. }
  737. server.allocator = wlr_allocator_autocreate(server.backend, server.renderer);
  738. wlr_renderer_init_wl_display(server.renderer, server.wl_display);
  739. /* This creates some hands-off wlroots interfaces. The compositor is
  740. * necessary for clients to allocate surfaces and the data device manager
  741. * handles the clipboard. Each of these wlroots interfaces has room for you
  742. * to dig your fingers in and play with their behavior if you want. */
  743. if(!wlr_compositor_create(server.wl_display, server.renderer))
  744. {
  745. wlr_log(WLR_ERROR, "Unable to create the wlroots compositor");
  746. ret = 1;
  747. goto end;
  748. }
  749. if(!wlr_data_device_manager_create(server.wl_display))
  750. {
  751. wlr_log(WLR_ERROR, "Unable to create the data device manager");
  752. ret = 1;
  753. goto end;
  754. }
  755. /* Creates an output layout, which a wlroots utility for working with an
  756. * arrangement of screens in a physical layout. */
  757. server.output_layout = wlr_output_layout_create();
  758. if(!server.output_layout)
  759. {
  760. wlr_log(WLR_ERROR, "Unable to create output layout");
  761. ret = 1;
  762. goto end;
  763. }
  764. /* Configure a listener to be notified when new outputs are available on the
  765. * backend. */
  766. wl_list_init(&server.outputs);
  767. server.new_output.notify = server_new_output;
  768. assert(server.backend);
  769. wl_signal_add(&server.backend->events.new_output, &server.new_output);
  770. /* Set up our list of views and the xdg-shell. The xdg-shell is a Wayland
  771. * protocol which is used for application windows. For more detail on
  772. * shells, refer to my article:
  773. *
  774. * https://drewdevault.com/2018/07/29/Wayland-shells.html
  775. */
  776. wl_list_init(&server.views);
  777. server.xdg_shell = wlr_xdg_shell_create(server.wl_display);
  778. server.new_xdg_surface.notify = server_new_xdg_surface;
  779. wl_signal_add(&server.xdg_shell->events.new_surface, &server.new_xdg_surface);
  780. server_decoration_manager = wlr_server_decoration_manager_create(server.wl_display);
  781. if(!server_decoration_manager)
  782. {
  783. wlr_log(WLR_ERROR, "Unable to create the server decoration manager");
  784. ret = 1;
  785. goto end;
  786. }
  787. wlr_server_decoration_manager_set_default_mode(server_decoration_manager,
  788. WLR_SERVER_DECORATION_MANAGER_MODE_SERVER);
  789. xdg_decoration_manager = wlr_xdg_decoration_manager_v1_create(server.wl_display);
  790. wl_signal_add(&xdg_decoration_manager->events.new_toplevel_decoration,
  791. &server.xdg_toplevel_decoration);
  792. server.xdg_toplevel_decoration.notify = handle_xdg_toplevel_decoration;
  793. /*
  794. * Creates a cursor, which is a wlroots utility for tracking the cursor
  795. * image shown on screen.
  796. */
  797. server.cursor = wlr_cursor_create();
  798. wlr_cursor_attach_output_layout(server.cursor, server.output_layout);
  799. /* Creates an xcursor manager, another wlroots utility which loads up
  800. * Xcursor themes to source cursor images from and makes sure that cursor
  801. * images are available at all scale factors on the screen (necessary for
  802. * HiDPI support). We add a cursor theme at scale factor 1 to begin with. */
  803. server.cursor_mgr = wlr_xcursor_manager_create(NULL, 24);
  804. wlr_xcursor_manager_load(server.cursor_mgr, 1);
  805. /*
  806. * wlr_cursor *only* displays an image on screen. It does not move around
  807. * when the pointer moves. However, we can attach input devices to it, and
  808. * it will generate aggregate events for all of them. In these events, we
  809. * can choose how we want to process them, forwarding them to clients and
  810. * moving the cursor around. More detail on this process is described in my
  811. * input handling blog post:
  812. *
  813. * https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html
  814. *
  815. * And more comments are sprinkled throughout the notify functions above.
  816. */
  817. server.cursor_motion.notify = server_cursor_motion;
  818. wl_signal_add(&server.cursor->events.motion, &server.cursor_motion);
  819. server.cursor_motion_absolute.notify = server_cursor_motion_absolute;
  820. wl_signal_add(&server.cursor->events.motion_absolute, &server.cursor_motion_absolute);
  821. server.cursor_button.notify = server_cursor_button;
  822. wl_signal_add(&server.cursor->events.button, &server.cursor_button);
  823. server.cursor_axis.notify = server_cursor_axis;
  824. wl_signal_add(&server.cursor->events.axis, &server.cursor_axis);
  825. server.cursor_frame.notify = server_cursor_frame;
  826. wl_signal_add(&server.cursor->events.frame, &server.cursor_frame);
  827. /*
  828. * Configures a seat, which is a single "seat" at which a user sits and
  829. * operates the computer. This conceptually includes up to one keyboard,
  830. * pointer, touch, and drawing tablet device. We also rig up a listener to
  831. * let us know when new input devices are available on the backend.
  832. */
  833. wl_list_init(&server.keyboards);
  834. server.new_input.notify = server_new_input;
  835. wl_signal_add(&server.backend->events.new_input, &server.new_input);
  836. server.seat = wlr_seat_create(server.wl_display, "seat0");
  837. if(!server.seat)
  838. {
  839. wlr_log_errno(WLR_ERROR, "Unable to create wlroots seat");
  840. ret = 1;
  841. goto end;
  842. }
  843. server.request_cursor.notify = seat_request_cursor;
  844. wl_signal_add(&server.seat->events.request_set_cursor, &server.request_cursor);
  845. /* Clipboard protocols */
  846. wlr_data_control_manager_v1_create(server.wl_display);
  847. wlr_primary_selection_v1_device_manager_create(server.wl_display);
  848. /* Clipboard requests handlers */
  849. server.request_set_selection.notify = handle_request_set_selection;
  850. wl_signal_add(&server.seat->events.request_set_selection, &server.request_set_selection);
  851. server.request_set_primary_selection.notify = handle_request_set_primary_selection;
  852. wl_signal_add(&server.seat->events.request_set_primary_selection,
  853. &server.request_set_primary_selection);
  854. /* Add a Unix socket to the Wayland display. */
  855. const char *socket = wl_display_add_socket_auto(server.wl_display);
  856. if(!socket)
  857. {
  858. wlr_log_errno(WLR_ERROR, "Unable to open Wayland socket");
  859. wlr_backend_destroy(server.backend);
  860. ret = 1;
  861. goto end;
  862. }
  863. /* Start the backend. This will enumerate outputs and inputs, become the DRM
  864. * master, etc */
  865. if(!wlr_backend_start(server.backend))
  866. {
  867. wlr_log(WLR_ERROR, "Unable to start the wlroots backend");
  868. wlr_backend_destroy(server.backend);
  869. wl_display_destroy(server.wl_display);
  870. ret = 1;
  871. goto end;
  872. }
  873. /* Set the WAYLAND_DISPLAY environment variable to our socket and run the
  874. * startup command if requested. */
  875. unsetenv("DISPLAY");
  876. setenv("WAYLAND_DISPLAY", socket, true);
  877. for(int i = 0; i < startup_cmdc; i++)
  878. {
  879. if(fork() == 0)
  880. {
  881. execl("/bin/sh", "/bin/sh", "-c", startup_cmdv[i], (void *)NULL);
  882. }
  883. }
  884. /* Run the Wayland event loop. This does not return until you exit the
  885. * compositor. Starting the backend rigged up all of the necessary event
  886. * loop configuration to listen to libinput events, DRM events, generate
  887. * frame events at the refresh rate, and so on. */
  888. wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s", socket);
  889. wl_display_run(server.wl_display);
  890. end:
  891. if(server.seat) wlr_seat_destroy(server.seat);
  892. wl_display_destroy_clients(server.wl_display);
  893. wl_display_destroy(server.wl_display);
  894. return ret;
  895. }