inaban.c 33 KB

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