├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── gtk_fix.sh └── tinywl.c /.gitignore: -------------------------------------------------------------------------------- 1 | tinywl 2 | xdg-shell-protocol.h 3 | xdg-shell-protocol.c 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This work is licensed under CC0, which effectively puts it in the public domain. 2 | 3 | --- 4 | 5 | Creative Commons Legal Code 6 | 7 | CC0 1.0 Universal 8 | 9 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 10 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 11 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 12 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 13 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 14 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 15 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 16 | HEREUNDER. 17 | 18 | Statement of Purpose 19 | 20 | The laws of most jurisdictions throughout the world automatically confer 21 | exclusive Copyright and Related Rights (defined below) upon the creator 22 | and subsequent owner(s) (each and all, an "owner") of an original work of 23 | authorship and/or a database (each, a "Work"). 24 | 25 | Certain owners wish to permanently relinquish those rights to a Work for 26 | the purpose of contributing to a commons of creative, cultural and 27 | scientific works ("Commons") that the public can reliably and without fear 28 | of later claims of infringement build upon, modify, incorporate in other 29 | works, reuse and redistribute as freely as possible in any form whatsoever 30 | and for any purposes, including without limitation commercial purposes. 31 | These owners may contribute to the Commons to promote the ideal of a free 32 | culture and the further production of creative, cultural and scientific 33 | works, or to gain reputation or greater distribution for their Work in 34 | part through the use and efforts of others. 35 | 36 | For these and/or other purposes and motivations, and without any 37 | expectation of additional consideration or compensation, the person 38 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 39 | is an owner of Copyright and Related Rights in the Work, voluntarily 40 | elects to apply CC0 to the Work and publicly distribute the Work under its 41 | terms, with knowledge of his or her Copyright and Related Rights in the 42 | Work and the meaning and intended legal effect of CC0 on those rights. 43 | 44 | 1. Copyright and Related Rights. A Work made available under CC0 may be 45 | protected by copyright and related or neighboring rights ("Copyright and 46 | Related Rights"). Copyright and Related Rights include, but are not 47 | limited to, the following: 48 | 49 | i. the right to reproduce, adapt, distribute, perform, display, 50 | communicate, and translate a Work; 51 | ii. moral rights retained by the original author(s) and/or performer(s); 52 | iii. publicity and privacy rights pertaining to a person's image or 53 | likeness depicted in a Work; 54 | iv. rights protecting against unfair competition in regards to a Work, 55 | subject to the limitations in paragraph 4(a), below; 56 | v. rights protecting the extraction, dissemination, use and reuse of data 57 | in a Work; 58 | vi. database rights (such as those arising under Directive 96/9/EC of the 59 | European Parliament and of the Council of 11 March 1996 on the legal 60 | protection of databases, and under any national implementation 61 | thereof, including any amended or successor version of such 62 | directive); and 63 | vii. other similar, equivalent or corresponding rights throughout the 64 | world based on applicable law or treaty, and any national 65 | implementations thereof. 66 | 67 | 2. Waiver. To the greatest extent permitted by, but not in contravention 68 | of, applicable law, Affirmer hereby overtly, fully, permanently, 69 | irrevocably and unconditionally waives, abandons, and surrenders all of 70 | Affirmer's Copyright and Related Rights and associated claims and causes 71 | of action, whether now known or unknown (including existing as well as 72 | future claims and causes of action), in the Work (i) in all territories 73 | worldwide, (ii) for the maximum duration provided by applicable law or 74 | treaty (including future time extensions), (iii) in any current or future 75 | medium and for any number of copies, and (iv) for any purpose whatsoever, 76 | including without limitation commercial, advertising or promotional 77 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 78 | member of the public at large and to the detriment of Affirmer's heirs and 79 | successors, fully intending that such Waiver shall not be subject to 80 | revocation, rescission, cancellation, termination, or any other legal or 81 | equitable action to disrupt the quiet enjoyment of the Work by the public 82 | as contemplated by Affirmer's express Statement of Purpose. 83 | 84 | 3. Public License Fallback. Should any part of the Waiver for any reason 85 | be judged legally invalid or ineffective under applicable law, then the 86 | Waiver shall be preserved to the maximum extent permitted taking into 87 | account Affirmer's express Statement of Purpose. In addition, to the 88 | extent the Waiver is so judged Affirmer hereby grants to each affected 89 | person a royalty-free, non transferable, non sublicensable, non exclusive, 90 | irrevocable and unconditional license to exercise Affirmer's Copyright and 91 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 92 | maximum duration provided by applicable law or treaty (including future 93 | time extensions), (iii) in any current or future medium and for any number 94 | of copies, and (iv) for any purpose whatsoever, including without 95 | limitation commercial, advertising or promotional purposes (the 96 | "License"). The License shall be deemed effective as of the date CC0 was 97 | applied by Affirmer to the Work. Should any part of the License for any 98 | reason be judged legally invalid or ineffective under applicable law, such 99 | partial invalidity or ineffectiveness shall not invalidate the remainder 100 | of the License, and in such case Affirmer hereby affirms that he or she 101 | will not (i) exercise any of his or her remaining Copyright and Related 102 | Rights in the Work or (ii) assert any associated claims and causes of 103 | action with respect to the Work, in either case contrary to Affirmer's 104 | express Statement of Purpose. 105 | 106 | 4. Limitations and Disclaimers. 107 | 108 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 109 | surrendered, licensed or otherwise affected by this document. 110 | b. Affirmer offers the Work as-is and makes no representations or 111 | warranties of any kind concerning the Work, express, implied, 112 | statutory or otherwise, including without limitation warranties of 113 | title, merchantability, fitness for a particular purpose, non 114 | infringement, or the absence of latent or other defects, accuracy, or 115 | the present or absence of errors, whether or not discoverable, all to 116 | the greatest extent permissible under applicable law. 117 | c. Affirmer disclaims responsibility for clearing rights of other persons 118 | that may apply to the Work or any use thereof, including without 119 | limitation any person's Copyright and Related Rights in the Work. 120 | Further, Affirmer disclaims responsibility for obtaining any necessary 121 | consents, permissions or other rights required for any use of the 122 | Work. 123 | d. Affirmer understands and acknowledges that Creative Commons is not a 124 | party to this document and has no duty or obligation with respect to 125 | this CC0 or use of the Work. 126 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | WAYLAND_PROTOCOLS=$(shell pkg-config --variable=pkgdatadir wayland-protocols) 2 | WAYLAND_SCANNER=$(shell pkg-config --variable=wayland_scanner wayland-scanner) 3 | LIBS=\ 4 | $(shell pkg-config --cflags --libs wlroots) \ 5 | $(shell pkg-config --cflags --libs wayland-server) \ 6 | $(shell pkg-config --cflags --libs pangocairo) \ 7 | $(shell pkg-config --cflags --libs xkbcommon) 8 | 9 | # wayland-scanner is a tool which generates C headers and rigging for Wayland 10 | # protocols, which are specified in XML. wlroots requires you to rig these up 11 | # to your build system yourself and provide them in the include path. 12 | xdg-shell-protocol.h: 13 | $(WAYLAND_SCANNER) server-header \ 14 | $(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@ 15 | 16 | xdg-shell-protocol.c: xdg-shell-protocol.h 17 | $(WAYLAND_SCANNER) private-code \ 18 | $(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@ 19 | 20 | tinywl: tinywl.c xdg-shell-protocol.h xdg-shell-protocol.c 21 | $(CC) $(CFLAGS) \ 22 | -g -Werror -I. \ 23 | -DWLR_USE_UNSTABLE \ 24 | -o $@ $< \ 25 | $(LIBS) 26 | 27 | clean: 28 | rm -f tinywl xdg-shell-protocol.h xdg-shell-protocol.c 29 | 30 | .DEFAULT_GOAL=tinywl 31 | .PHONY: clean 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tinywl+ 2 | A simple stacking wayland compositor based on tinywl. Where features can be applied to [wlroot's tinywl](https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/0.15/tinywl) example. 3 | 4 | ### Inspiration 5 | - It was hard to find an easy way to add functionality to tinywl without having to understand very different code bases such as sway, etc. This was made so people can start developing and adding, what I think should be part of tinywl or at least in the examples, things to their compositor or contribute here for common use cases. 6 | - There is not enough stacking window managers for wayland that are simple to start from. 7 | - JWM ♥ 8 | 9 | ### Goals 10 | - Each commit should encapture an idea. Bug fixes etc should be amended to the relevant commit. 11 | - Keep things simple and as easy and basic as possible. 12 | - Maybe have patch sets that are optional or larger in seperate patches. 13 | 14 | ### Notes 15 | - Starting tinywl+ will get you a black screen so one might want to start using the `-s ` ie `./tinywl -s sakura` to have it start an application when it starts. 16 | - Would be nice to have [fcft](https://codeberg.org/dnkl/fcft) to render fonts to be more lightweight. 17 | - GTK does not play well with server side decorations(SSD). However, we can sorta force it to behave with some hacks included in `gtk_fix.sh`. 18 | - Not as many protocols supported as [dwl](https://github.com/djpohly/dwl), but tinywl+ comes in lighter with lines of code(LOS) than dwl :) 19 | 20 | ### Build instructions and requirements 21 | The requirements and build instructions are much like they are for original tinywl. ie: 22 |
Install these dependencies: 23 | - wlroots 24 | - wayland-protocols 25 | 26 | And run `make`. 27 | -------------------------------------------------------------------------------- /gtk_fix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This adds a custom css rules to remove the curved corners and drop shadows 4 | echo '/** Some apps use titlebar class and some window */ 5 | .titlebar, 6 | /** Remove curved windows */ 7 | window { 8 | border-radius: 0; 9 | box-shadow: none; 10 | } 11 | 12 | /** Remove shadows */ 13 | decoration { 14 | box-shadow: none; 15 | margin: 0; 16 | } 17 | decoration:backdrop { 18 | box-shadow: none; 19 | } 20 | ' >> ~/.config/gtk-3.0/gtk.css 21 | 22 | # In wayalnd the settings.ini file is not used so have to set with gsettings instead 23 | # Remove the CSD close etc buttons 24 | gsettings set org.gnome.desktop.wm.preferences button-layout menu:none 25 | # Don't do anything window manager specific when clicking the headder bar 26 | gsettings set org.gnome.desktop.wm.preferences action-double-click-titlebar none 27 | gsettings set org.gnome.desktop.wm.preferences action-right-click-titlebar none 28 | gsettings set org.gnome.desktop.wm.preferences action-middle-click-titlebar none 29 | -------------------------------------------------------------------------------- /tinywl.c: -------------------------------------------------------------------------------- 1 | #define _POSIX_C_SOURCE 200112L 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | /* For brevity's sake, struct members are annotated where they are used. */ 34 | enum tinywl_cursor_mode { 35 | TINYWL_CURSOR_PASSTHROUGH, 36 | TINYWL_CURSOR_MOVE, 37 | TINYWL_CURSOR_RESIZE, 38 | TINYWL_CURSOR_PRESSED, 39 | }; 40 | 41 | struct tinywl_server { 42 | struct wl_display *wl_display; 43 | struct wlr_backend *backend; 44 | struct wlr_renderer *renderer; 45 | struct wlr_allocator *allocator; 46 | struct wlr_scene *scene; 47 | 48 | struct wlr_xdg_shell *xdg_shell; 49 | struct wl_listener new_xdg_surface; 50 | struct wl_list views; 51 | 52 | struct wlr_cursor *cursor; 53 | struct wlr_xcursor_manager *cursor_mgr; 54 | struct wl_listener cursor_motion; 55 | struct wl_listener cursor_motion_absolute; 56 | struct wl_listener cursor_button; 57 | struct wl_listener cursor_axis; 58 | struct wl_listener cursor_frame; 59 | 60 | struct wlr_seat *seat; 61 | struct wl_listener new_input; 62 | struct wl_listener request_cursor; 63 | struct wl_listener request_set_selection; 64 | struct wl_list keyboards; 65 | enum tinywl_cursor_mode cursor_mode; 66 | struct tinywl_view *grabbed_view; 67 | double grab_x, grab_y; 68 | struct wlr_box grab_geobox; 69 | uint32_t resize_edges; 70 | struct wlr_scene_tree *view_menu; 71 | struct tinywl_view *opened_menu_view; 72 | struct wlr_scene_rect *selected_menu_item; 73 | 74 | struct wlr_output_layout *output_layout; 75 | struct wl_list outputs; 76 | struct wl_listener new_output; 77 | }; 78 | 79 | struct tinywl_output { 80 | struct wl_list link; 81 | struct tinywl_server *server; 82 | struct wlr_output *wlr_output; 83 | struct wl_listener frame; 84 | struct wlr_scene_rect *background; 85 | }; 86 | 87 | struct previous_geo { 88 | int x, y, width, height; 89 | }; 90 | 91 | struct title { 92 | struct wlr_scene_buffer *buffer; 93 | int original_width, current_width; 94 | }; 95 | 96 | struct tinywl_view { 97 | struct wl_list link; 98 | struct tinywl_server *server; 99 | struct wlr_xdg_surface *xdg_surface; 100 | struct wlr_scene_node *scene_node; 101 | struct wlr_scene_rect *border; 102 | struct wlr_scene_rect *titlebar; 103 | struct wlr_scene_rect *close_button; 104 | struct title title; 105 | struct wl_listener map; 106 | struct wl_listener unmap; 107 | struct wl_listener destroy; 108 | struct wl_listener commit; 109 | struct wl_listener request_move; 110 | struct wl_listener request_resize; 111 | struct wl_listener request_maximize; 112 | struct wl_listener set_title; 113 | struct previous_geo saved_geometry; 114 | int x, y; 115 | }; 116 | 117 | struct tinywl_keyboard { 118 | struct wl_list link; 119 | struct tinywl_server *server; 120 | struct wlr_input_device *device; 121 | 122 | struct wl_listener modifiers; 123 | struct wl_listener key; 124 | }; 125 | 126 | enum tinywl_node_type { 127 | NONE, 128 | TITLEBAR, 129 | BORDER, 130 | CLOSE_BUTTON, 131 | MENU, 132 | }; 133 | 134 | struct tinywl_node_details { 135 | enum tinywl_node_type type; 136 | void *owner; 137 | struct tinywl_view *view; 138 | int index; 139 | struct wl_listener destroy; 140 | }; 141 | 142 | // These are not runtime configurable yet so make them 'const's 143 | typedef struct Global_config { 144 | char *font_description; 145 | const int edge_margin; 146 | const int titlebar_padding; 147 | const int border_size; 148 | const int doubleclick_interval; 149 | const int deco_button_size; 150 | const float background_rgba[4]; 151 | const float active_window_rgba[4]; 152 | const float inactive_window_rgba[4]; 153 | }Global_config; 154 | const Global_config CONFIG = { 155 | "Sans 12", 2, 2, 3, 500, 16, 156 | { 0.2f, 0.2f, 0.25f, 1.0f }, 157 | { 0.0f, 0.47f, 0.8f, 1.0f }, 158 | { 0.33f, 0.33f, 0.33f, 1.0f } 159 | }; 160 | int TITLEBAR_HEIGHT; 161 | 162 | // Inspired from sway/labwc node.c/h 163 | static void node_destroy(struct tinywl_node_details *tinywl_node_details) { 164 | wl_list_remove(&tinywl_node_details->destroy.link); 165 | free(tinywl_node_details); 166 | } 167 | 168 | static void node_destroy_notify(struct wl_listener *listener, void *data) { 169 | struct tinywl_node_details *tinywl_node_details = 170 | wl_container_of(listener, tinywl_node_details, destroy); 171 | node_destroy(tinywl_node_details); 172 | } 173 | 174 | struct tinywl_node_details *node_init(enum tinywl_node_type type, 175 | void *owner, struct tinywl_view *view, int index) { 176 | struct tinywl_node_details *tinywl_node_details = 177 | calloc(1, sizeof(struct tinywl_node_details)); 178 | tinywl_node_details->type = type; 179 | tinywl_node_details->owner = owner; 180 | tinywl_node_details->view = view; 181 | tinywl_node_details->index = index; 182 | 183 | tinywl_node_details->destroy.notify = node_destroy_notify; 184 | if (view){ 185 | wl_signal_add(&view->xdg_surface->events.destroy, &tinywl_node_details->destroy); 186 | } // For nodes that don't have a view (menu) this should be attached to something else 187 | 188 | return tinywl_node_details; 189 | } 190 | 191 | // From hopalong, is there a better way? 192 | static struct tinywl_view *tinywl_view_from_wlr_surface( 193 | struct tinywl_server *server, struct wlr_surface *surface) { 194 | struct tinywl_view *view; 195 | 196 | wl_list_for_each(view, &server->views, link) 197 | { 198 | if (surface == view->xdg_surface->surface) 199 | return view; 200 | } 201 | 202 | return NULL; 203 | } 204 | 205 | static void focus_view(struct tinywl_view *view, struct wlr_surface *surface) { 206 | /* Note: this function only deals with keyboard focus. */ 207 | if (view == NULL) { 208 | return; 209 | } 210 | struct tinywl_server *server = view->server; 211 | struct wlr_seat *seat = server->seat; 212 | struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface; 213 | if (prev_surface == surface) { 214 | /* Don't re-focus an already focused surface. */ 215 | return; 216 | } 217 | if (prev_surface) { 218 | /* 219 | * Deactivate the previously focused surface. This lets the client know 220 | * it no longer has focus and the client will repaint accordingly, e.g. 221 | * stop displaying a caret. 222 | */ 223 | struct wlr_xdg_surface *previous = wlr_xdg_surface_from_wlr_surface( 224 | seat->keyboard_state.focused_surface); 225 | wlr_xdg_toplevel_set_activated(previous, false); 226 | 227 | /* Update the border to inactive color */ 228 | struct tinywl_view *focused_view = tinywl_view_from_wlr_surface( 229 | server, prev_surface); 230 | if (focused_view && focused_view->border){ 231 | wlr_scene_rect_set_color(focused_view->border, CONFIG.inactive_window_rgba); 232 | wlr_scene_rect_set_color(focused_view->titlebar, CONFIG.inactive_window_rgba); 233 | } 234 | } 235 | struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); 236 | /* Move the view to the front */ 237 | wlr_scene_node_raise_to_top(view->scene_node); 238 | wl_list_remove(&view->link); 239 | wl_list_insert(&server->views, &view->link); 240 | /* Activate the new surface */ 241 | wlr_xdg_toplevel_set_activated(view->xdg_surface, true); 242 | /* Update the border to active color */ 243 | if (view->border){ 244 | wlr_scene_rect_set_color(view->border, CONFIG.active_window_rgba); 245 | wlr_scene_rect_set_color(view->titlebar, CONFIG.active_window_rgba); 246 | } 247 | /* 248 | * Tell the seat to have the keyboard enter this surface. wlroots will keep 249 | * track of this and automatically send key events to the appropriate 250 | * clients without additional work on your part. 251 | */ 252 | wlr_seat_keyboard_notify_enter(seat, view->xdg_surface->surface, 253 | keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers); 254 | } 255 | 256 | static void save_view_geometry(struct tinywl_view *view){ 257 | struct wlr_box view_geometry; 258 | wlr_xdg_surface_get_geometry(view->xdg_surface, &view_geometry); 259 | 260 | view->saved_geometry.x = view->x; 261 | view->saved_geometry.y = view->y; 262 | view->saved_geometry.height = view_geometry.height; 263 | view->saved_geometry.width = view_geometry.width; 264 | } 265 | 266 | bool maximize_view(struct tinywl_view *view, enum wlr_edges edge){ 267 | // Return false if the view is already maximized 268 | if (view->xdg_surface->toplevel->current.maximized){ 269 | return false; 270 | } else { 271 | /* Now that we can move from one maximized edge to another we don't want to 272 | * save the state in that case and just continue to use the old geometry */ 273 | if (view->server->cursor_mode != TINYWL_CURSOR_MOVE){ 274 | save_view_geometry(view); 275 | }; 276 | 277 | struct wlr_output *output = 278 | wlr_output_layout_output_at(view->server->output_layout, 279 | view->server->cursor->x, view->server->cursor->y); 280 | if (!output){ return false; } 281 | 282 | int x, y, width, height; 283 | x = CONFIG.border_size; 284 | y = TITLEBAR_HEIGHT + CONFIG.border_size; 285 | width = output->width - CONFIG.border_size*2; 286 | height = output->height - (TITLEBAR_HEIGHT + CONFIG.border_size*2); 287 | 288 | switch (edge) { 289 | case WLR_EDGE_LEFT: 290 | width = output->width/2 - CONFIG.border_size*2; 291 | break; 292 | case WLR_EDGE_RIGHT: 293 | x = output->width/2 + CONFIG.border_size; 294 | width = output->width/2 - CONFIG.border_size*2; 295 | break; 296 | case WLR_EDGE_BOTTOM: 297 | y = output->height/2 + (TITLEBAR_HEIGHT + CONFIG.border_size); 298 | height = output->height/2 - (TITLEBAR_HEIGHT + CONFIG.border_size*2); 299 | break; 300 | case WLR_EDGE_TOP: 301 | height = output->height/2 - (TITLEBAR_HEIGHT + CONFIG.border_size*2); 302 | break; 303 | } 304 | 305 | view->x = x; 306 | view->y = y; 307 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 308 | wlr_xdg_toplevel_set_size(view->xdg_surface, width, height); 309 | wlr_xdg_toplevel_set_maximized(view->xdg_surface, true); 310 | } 311 | return true; 312 | } 313 | 314 | bool unmaximize_view(struct tinywl_view *view){ 315 | // Return false if the view is not maximized 316 | if (view->xdg_surface->toplevel->current.maximized){ 317 | view->x = view->saved_geometry.x; 318 | view->y = view->saved_geometry.y; 319 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 320 | wlr_xdg_toplevel_set_size(view->xdg_surface, view->saved_geometry.width, view->saved_geometry.height); 321 | wlr_xdg_toplevel_set_maximized(view->xdg_surface, false); 322 | } else { 323 | return false; 324 | } 325 | return true; 326 | } 327 | 328 | void toggle_maximize(struct tinywl_view *view){ 329 | if (!unmaximize_view(view)) 330 | maximize_view(view, WLR_EDGE_NONE); 331 | } 332 | 333 | // Buffer logic from cagebreak 334 | struct text_buffer { 335 | struct wlr_buffer base; 336 | void *data; 337 | uint32_t format; 338 | size_t stride; 339 | }; 340 | 341 | static void text_buffer_destroy(struct wlr_buffer *wlr_buffer) { 342 | struct text_buffer *buffer = wl_container_of(wlr_buffer, buffer, base); 343 | free(buffer->data); 344 | free(buffer); 345 | } 346 | 347 | static bool text_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer, 348 | uint32_t flags, void **data, uint32_t *format, size_t *stride) { 349 | struct text_buffer *buffer = wl_container_of(wlr_buffer, buffer, base); 350 | if(data != NULL) { 351 | *data = (void *)buffer->data; 352 | } 353 | if(format != NULL) { 354 | *format = buffer->format; 355 | } 356 | if(stride != NULL) { 357 | *stride = buffer->stride; 358 | } 359 | return true; 360 | } 361 | 362 | static void text_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) { 363 | // This space is intentionally left blank 364 | } 365 | 366 | static const struct wlr_buffer_impl text_buffer_impl = { 367 | .destroy = text_buffer_destroy, 368 | .begin_data_ptr_access = text_buffer_begin_data_ptr_access, 369 | .end_data_ptr_access = text_buffer_end_data_ptr_access, 370 | }; 371 | 372 | static struct text_buffer *text_buffer_create(uint32_t width, uint32_t height, uint32_t stride) { 373 | struct text_buffer *buffer = calloc(1, sizeof(*buffer)); 374 | if (buffer == NULL) { 375 | return NULL; 376 | } 377 | 378 | wlr_buffer_init(&buffer->base, &text_buffer_impl, width, height); 379 | buffer->format = DRM_FORMAT_ARGB8888; 380 | buffer->stride = stride; 381 | 382 | buffer->data = malloc(buffer->stride * height); 383 | if (buffer->data == NULL) { 384 | free(buffer); 385 | return NULL; 386 | } 387 | 388 | return buffer; 389 | } 390 | 391 | static void get_text_size(char *text, char *font_str, int *width, int *height){ 392 | cairo_surface_t *surface = cairo_image_surface_create( 393 | CAIRO_FORMAT_ARGB32, 0, 0); 394 | cairo_status_t status = cairo_surface_status(surface); 395 | 396 | cairo_t *cr = cairo_create(surface); 397 | PangoLayout *layout; 398 | PangoFontDescription *desc; 399 | 400 | /* Create Pango layout. */ 401 | layout = pango_cairo_create_layout (cr); 402 | desc = pango_font_description_from_string (font_str); 403 | pango_layout_set_font_description (layout, desc); 404 | pango_font_description_free (desc); 405 | pango_layout_set_text (layout, text, -1); 406 | /* Set width and height to text size */ 407 | pango_layout_get_pixel_size(layout, width, height); 408 | 409 | /* Cleanup */ 410 | cairo_surface_destroy(surface); 411 | g_object_unref (layout); 412 | cairo_destroy(cr); 413 | 414 | } 415 | 416 | static struct text_buffer * create_text_buffer(char* text, int width, int height) { 417 | cairo_surface_t *surface = cairo_image_surface_create( 418 | CAIRO_FORMAT_ARGB32, width, height); 419 | cairo_status_t status = cairo_surface_status(surface); 420 | if (status != CAIRO_STATUS_SUCCESS) { 421 | wlr_log(WLR_ERROR, "cairo_image_surface_create failed: %s", 422 | cairo_status_to_string(status)); 423 | return NULL; 424 | } 425 | 426 | cairo_t *cr = cairo_create(surface); 427 | PangoLayout *layout; 428 | PangoFontDescription *desc; 429 | 430 | /* Set background to be transparent */ 431 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0); 432 | cairo_paint (cr); 433 | 434 | /* Create Pango layout. */ 435 | layout = pango_cairo_create_layout (cr); 436 | desc = pango_font_description_from_string (CONFIG.font_description); 437 | pango_layout_set_font_description (layout, desc); 438 | pango_font_description_free (desc); 439 | pango_layout_set_text (layout, text, -1); 440 | pango_layout_set_width (layout, width * PANGO_SCALE); 441 | pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_MIDDLE); 442 | 443 | /* Draw layout. */ 444 | cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); 445 | pango_cairo_show_layout (cr, layout); 446 | 447 | //--- 448 | unsigned char *data = cairo_image_surface_get_data(surface); 449 | int stride = cairo_image_surface_get_stride(surface); 450 | struct text_buffer *buf=text_buffer_create(width, height, stride); 451 | void *data_ptr; 452 | 453 | if(!wlr_buffer_begin_data_ptr_access(&buf->base, WLR_BUFFER_DATA_PTR_ACCESS_WRITE, 454 | &data_ptr, NULL, NULL)) { 455 | wlr_log(WLR_ERROR, "Failed to get pointer access to text buffer"); 456 | return NULL; 457 | } 458 | memcpy(data_ptr, data, stride*height); 459 | wlr_buffer_end_data_ptr_access(&buf->base); 460 | cairo_surface_destroy(surface); 461 | //----- 462 | 463 | /* free the layout object */ 464 | g_object_unref (layout); 465 | cairo_destroy(cr); 466 | return buf; 467 | } 468 | 469 | static void view_title_update(struct tinywl_view *view, 470 | char* title_str){ 471 | if (view->title.buffer) 472 | wlr_scene_node_destroy(&view->title.buffer->node); 473 | 474 | int width, height; 475 | get_text_size(title_str, CONFIG.font_description, &width, &height); 476 | TITLEBAR_HEIGHT = height + CONFIG.titlebar_padding * 2; 477 | view->title.original_width = width; 478 | 479 | int pending_width = 480 | view->xdg_surface->surface->current.width - CONFIG.border_size - CONFIG.deco_button_size; 481 | if (pending_width > 0 && width > pending_width) 482 | width = pending_width; 483 | view->title.current_width = width; 484 | 485 | struct text_buffer *buf = create_text_buffer(title_str, width, height); 486 | struct wlr_scene_buffer *text_scene_buffer = malloc(sizeof(struct wlr_scene_buffer)); 487 | view->title.buffer = wlr_scene_buffer_create(view->scene_node, &buf->base); 488 | view->title.buffer->node.data = node_init(TITLEBAR, 489 | (void *)&view->titlebar->node, view, 0); 490 | 491 | wlr_scene_node_set_position(&view->title.buffer->node, 492 | CONFIG.titlebar_padding, 493 | CONFIG.titlebar_padding - TITLEBAR_HEIGHT); 494 | } 495 | 496 | static void xdg_toplevel_set_title(struct wl_listener *listener, void *data){ 497 | struct tinywl_view *view = wl_container_of(listener, view, set_title); 498 | view_title_update(view, view->xdg_surface->toplevel->title); 499 | } 500 | 501 | static void position_view_centered(struct tinywl_view *view){ 502 | int main_width, main_height; 503 | if (view->xdg_surface->toplevel->parent){ 504 | struct wlr_box geo_box; 505 | wlr_xdg_surface_get_geometry(view->xdg_surface->toplevel->parent, &geo_box); 506 | main_width = geo_box.width; 507 | main_height = geo_box.height; 508 | } else { 509 | struct wlr_output *output = 510 | wlr_output_layout_output_at(view->server->output_layout, 511 | view->server->cursor->x, view->server->cursor->y); 512 | main_width = output->width; 513 | main_height = output->height; 514 | } 515 | 516 | if (main_width){ 517 | struct wlr_box view_geometry; 518 | wlr_xdg_surface_get_geometry(view->xdg_surface, &view_geometry); 519 | view->x = main_width/2 - view_geometry.width/2; 520 | view->y = main_height/2 - view_geometry.height/2; 521 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 522 | }; 523 | } 524 | 525 | static void keyboard_handle_modifiers( 526 | struct wl_listener *listener, void *data) { 527 | /* This event is raised when a modifier key, such as shift or alt, is 528 | * pressed. We simply communicate this to the client. */ 529 | struct tinywl_keyboard *keyboard = 530 | wl_container_of(listener, keyboard, modifiers); 531 | /* 532 | * A seat can only have one keyboard, but this is a limitation of the 533 | * Wayland protocol - not wlroots. We assign all connected keyboards to the 534 | * same seat. You can swap out the underlying wlr_keyboard like this and 535 | * wlr_seat handles this transparently. 536 | */ 537 | wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device); 538 | /* Send modifiers to the client. */ 539 | wlr_seat_keyboard_notify_modifiers(keyboard->server->seat, 540 | &keyboard->device->keyboard->modifiers); 541 | } 542 | 543 | static bool handle_keybinding(struct tinywl_server *server, xkb_keysym_t sym) { 544 | /* 545 | * Here we handle compositor keybindings. This is when the compositor is 546 | * processing keys, rather than passing them on to the client for its own 547 | * processing. 548 | * 549 | * This function assumes Alt is held down. 550 | */ 551 | switch (sym) { 552 | case XKB_KEY_Escape: 553 | wl_display_terminate(server->wl_display); 554 | break; 555 | case XKB_KEY_F1: 556 | /* Cycle to the next view */ 557 | if (wl_list_length(&server->views) < 2) { 558 | break; 559 | } 560 | struct tinywl_view *next_view = wl_container_of( 561 | server->views.prev, next_view, link); 562 | focus_view(next_view, next_view->xdg_surface->surface); 563 | break; 564 | default: 565 | return false; 566 | } 567 | return true; 568 | } 569 | 570 | static void keyboard_handle_key( 571 | struct wl_listener *listener, void *data) { 572 | /* This event is raised when a key is pressed or released. */ 573 | struct tinywl_keyboard *keyboard = 574 | wl_container_of(listener, keyboard, key); 575 | struct tinywl_server *server = keyboard->server; 576 | struct wlr_event_keyboard_key *event = data; 577 | struct wlr_seat *seat = server->seat; 578 | 579 | /* Translate libinput keycode -> xkbcommon */ 580 | uint32_t keycode = event->keycode + 8; 581 | /* Get a list of keysyms based on the keymap for this keyboard */ 582 | const xkb_keysym_t *syms; 583 | int nsyms = xkb_state_key_get_syms( 584 | keyboard->device->keyboard->xkb_state, keycode, &syms); 585 | 586 | bool handled = false; 587 | uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); 588 | if ((modifiers & WLR_MODIFIER_ALT) && 589 | event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { 590 | /* If alt is held down and this button was _pressed_, we attempt to 591 | * process it as a compositor keybinding. */ 592 | for (int i = 0; i < nsyms; i++) { 593 | handled = handle_keybinding(server, syms[i]); 594 | } 595 | } 596 | 597 | if (!handled) { 598 | /* Otherwise, we pass it along to the client. */ 599 | wlr_seat_set_keyboard(seat, keyboard->device); 600 | wlr_seat_keyboard_notify_key(seat, event->time_msec, 601 | event->keycode, event->state); 602 | } 603 | } 604 | 605 | static void server_new_keyboard(struct tinywl_server *server, 606 | struct wlr_input_device *device) { 607 | struct tinywl_keyboard *keyboard = 608 | calloc(1, sizeof(struct tinywl_keyboard)); 609 | keyboard->server = server; 610 | keyboard->device = device; 611 | 612 | /* We need to prepare an XKB keymap and assign it to the keyboard. This 613 | * assumes the defaults (e.g. layout = "us"). */ 614 | struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); 615 | struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, NULL, 616 | XKB_KEYMAP_COMPILE_NO_FLAGS); 617 | 618 | wlr_keyboard_set_keymap(device->keyboard, keymap); 619 | xkb_keymap_unref(keymap); 620 | xkb_context_unref(context); 621 | wlr_keyboard_set_repeat_info(device->keyboard, 25, 600); 622 | 623 | /* Here we set up listeners for keyboard events. */ 624 | keyboard->modifiers.notify = keyboard_handle_modifiers; 625 | wl_signal_add(&device->keyboard->events.modifiers, &keyboard->modifiers); 626 | keyboard->key.notify = keyboard_handle_key; 627 | wl_signal_add(&device->keyboard->events.key, &keyboard->key); 628 | 629 | wlr_seat_set_keyboard(server->seat, device); 630 | 631 | /* And add the keyboard to our list of keyboards */ 632 | wl_list_insert(&server->keyboards, &keyboard->link); 633 | } 634 | 635 | static void server_new_pointer(struct tinywl_server *server, 636 | struct wlr_input_device *device) { 637 | /* We don't do anything special with pointers. All of our pointer handling 638 | * is proxied through wlr_cursor. On another compositor, you might take this 639 | * opportunity to do libinput configuration on the device to set 640 | * acceleration, etc. */ 641 | wlr_cursor_attach_input_device(server->cursor, device); 642 | } 643 | 644 | static void server_new_input(struct wl_listener *listener, void *data) { 645 | /* This event is raised by the backend when a new input device becomes 646 | * available. */ 647 | struct tinywl_server *server = 648 | wl_container_of(listener, server, new_input); 649 | struct wlr_input_device *device = data; 650 | switch (device->type) { 651 | case WLR_INPUT_DEVICE_KEYBOARD: 652 | server_new_keyboard(server, device); 653 | break; 654 | case WLR_INPUT_DEVICE_POINTER: 655 | server_new_pointer(server, device); 656 | break; 657 | default: 658 | break; 659 | } 660 | /* We need to let the wlr_seat know what our capabilities are, which is 661 | * communiciated to the client. In TinyWL we always have a cursor, even if 662 | * there are no pointer devices, so we always include that capability. */ 663 | uint32_t caps = WL_SEAT_CAPABILITY_POINTER; 664 | if (!wl_list_empty(&server->keyboards)) { 665 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; 666 | } 667 | wlr_seat_set_capabilities(server->seat, caps); 668 | } 669 | 670 | static void seat_request_cursor(struct wl_listener *listener, void *data) { 671 | struct tinywl_server *server = wl_container_of( 672 | listener, server, request_cursor); 673 | /* This event is raised by the seat when a client provides a cursor image */ 674 | struct wlr_seat_pointer_request_set_cursor_event *event = data; 675 | struct wlr_seat_client *focused_client = 676 | server->seat->pointer_state.focused_client; 677 | /* This can be sent by any client, so we check to make sure this one is 678 | * actually has pointer focus first. */ 679 | if (focused_client == event->seat_client) { 680 | /* Once we've vetted the client, we can tell the cursor to use the 681 | * provided surface as the cursor image. It will set the hardware cursor 682 | * on the output that it's currently on and continue to do so as the 683 | * cursor moves between outputs. */ 684 | wlr_cursor_set_surface(server->cursor, event->surface, 685 | event->hotspot_x, event->hotspot_y); 686 | } 687 | } 688 | 689 | static void seat_request_set_selection(struct wl_listener *listener, void *data) { 690 | /* This event is raised by the seat when a client wants to set the selection, 691 | * usually when the user copies something. wlroots allows compositors to 692 | * ignore such requests if they so choose, but in tinywl we always honor 693 | */ 694 | struct tinywl_server *server = wl_container_of( 695 | listener, server, request_set_selection); 696 | struct wlr_seat_request_set_selection_event *event = data; 697 | wlr_seat_set_selection(server->seat, event->source, event->serial); 698 | } 699 | 700 | static struct tinywl_view *desktop_view_at(struct tinywl_server *server, 701 | double lx, double ly, double *sx, double *sy, void **scene_node, 702 | struct tinywl_node_details **tinywl_node_details) { 703 | /* This returns the topmost node in the scene at the given layout coords. */ 704 | struct wlr_scene_node *node = wlr_scene_node_at( 705 | &server->scene->node, lx, ly, sx, sy); 706 | 707 | *scene_node = node; 708 | 709 | if (node == NULL || (!node->data && node->type != WLR_SCENE_NODE_SURFACE)) { 710 | return NULL; 711 | } else if (node->type == WLR_SCENE_NODE_SURFACE){ 712 | //*surface = wlr_scene_surface_from_node(node)->surface; 713 | /* Find the node corresponding to the tinywl_view at the root of this 714 | * surface tree, it is the only one for which we set the data field. */ 715 | while (node != NULL && node->data == NULL) { 716 | node = node->parent; 717 | } 718 | return node->data; 719 | } 720 | 721 | struct tinywl_node_details *details = node->data; 722 | if (details){ 723 | *tinywl_node_details = details; 724 | if (!details->view && server->opened_menu_view) 725 | return server->opened_menu_view; 726 | return details->view; 727 | } else { 728 | return NULL; 729 | } 730 | } 731 | 732 | static void process_cursor_move(struct tinywl_server *server, uint32_t time) { 733 | struct tinywl_view *view = server->grabbed_view; 734 | struct wlr_output *output = 735 | wlr_output_layout_output_at(view->server->output_layout, 736 | view->server->cursor->x, view->server->cursor->y); 737 | if (!output){ return; } 738 | 739 | if (server->cursor->x <= CONFIG.edge_margin) 740 | maximize_view(view, WLR_EDGE_LEFT); 741 | else if (server->cursor->x >= output->width - CONFIG.edge_margin) 742 | maximize_view(view, WLR_EDGE_RIGHT); 743 | else if (server->cursor->y >= output->height - CONFIG.edge_margin) 744 | maximize_view(view, WLR_EDGE_BOTTOM); 745 | else if (server->cursor->y <= CONFIG.edge_margin) 746 | maximize_view(view, WLR_EDGE_TOP); 747 | else{ 748 | // Unmaximize the window if it is maximized. 749 | unmaximize_view(view); 750 | 751 | /* Move the grabbed view to the new position. */ 752 | view->x = server->cursor->x - server->grab_x; 753 | view->y = server->cursor->y - server->grab_y; 754 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 755 | } 756 | } 757 | 758 | static void process_cursor_resize(struct tinywl_server *server, uint32_t time) { 759 | /* 760 | * Resizing the grabbed view can be a little bit complicated, because we 761 | * could be resizing from any corner or edge. This not only resizes the view 762 | * on one or two axes, but can also move the view if you resize from the top 763 | * or left edges (or top-left corner). 764 | * 765 | * Note that I took some shortcuts here. In a more fleshed-out compositor, 766 | * you'd wait for the client to prepare a buffer at the new size, then 767 | * commit any movement that was prepared. 768 | */ 769 | struct tinywl_view *view = server->grabbed_view; 770 | double border_x = server->cursor->x - server->grab_x; 771 | double border_y = server->cursor->y - server->grab_y; 772 | int new_left = server->grab_geobox.x; 773 | int new_right = server->grab_geobox.x + server->grab_geobox.width; 774 | int new_top = server->grab_geobox.y; 775 | int new_bottom = server->grab_geobox.y + server->grab_geobox.height; 776 | 777 | if (server->resize_edges & WLR_EDGE_TOP) { 778 | new_top = border_y; 779 | if (new_top >= new_bottom) { 780 | new_top = new_bottom - 1; 781 | } 782 | } else if (server->resize_edges & WLR_EDGE_BOTTOM) { 783 | new_bottom = border_y; 784 | if (new_bottom <= new_top) { 785 | new_bottom = new_top + 1; 786 | } 787 | } 788 | if (server->resize_edges & WLR_EDGE_LEFT) { 789 | new_left = border_x; 790 | if (new_left >= new_right) { 791 | new_left = new_right - 1; 792 | } 793 | } else if (server->resize_edges & WLR_EDGE_RIGHT) { 794 | new_right = border_x; 795 | if (new_right <= new_left) { 796 | new_right = new_left + 1; 797 | } 798 | } 799 | 800 | struct wlr_box geo_box; 801 | wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); 802 | view->x = new_left - geo_box.x; 803 | view->y = new_top - geo_box.y; 804 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 805 | 806 | int new_width = new_right - new_left; 807 | int new_height = new_bottom - new_top; 808 | wlr_xdg_toplevel_set_size(view->xdg_surface, new_width, new_height); 809 | } 810 | 811 | enum wlr_edges find_resize_edge(struct tinywl_view *view, 812 | struct wlr_surface *surface) { 813 | enum wlr_edges edge = 0; 814 | if (view->server->cursor->x < view->x + CONFIG.border_size) { 815 | edge |= WLR_EDGE_LEFT; 816 | } 817 | if (view->server->cursor->y < view->y + CONFIG.border_size) { 818 | edge |= WLR_EDGE_TOP; 819 | } 820 | if (view->server->cursor->x >= 821 | view->x + surface->pending.width - CONFIG.border_size) { 822 | edge |= WLR_EDGE_RIGHT; 823 | } 824 | if (view->server->cursor->y >= 825 | view->y + surface->pending.height - CONFIG.border_size) { 826 | edge |= WLR_EDGE_BOTTOM; 827 | } 828 | return edge; 829 | } 830 | 831 | // Forward declare, alternatively this function could be moved here. 832 | static void begin_interactive(struct tinywl_view *view, 833 | enum tinywl_cursor_mode mode, uint32_t edges); 834 | 835 | static void process_cursor_motion(struct tinywl_server *server, uint32_t time) { 836 | /* If the mode is non-passthrough, delegate to those functions. */ 837 | if (server->cursor_mode == TINYWL_CURSOR_MOVE) { 838 | process_cursor_move(server, time); 839 | return; 840 | } else if (server->cursor_mode == TINYWL_CURSOR_RESIZE) { 841 | process_cursor_resize(server, time); 842 | return; 843 | } 844 | 845 | /* Otherwise, find the view under the pointer and send the event along. */ 846 | double sx, sy; 847 | struct wlr_seat *seat = server->seat; 848 | void *scene_node; 849 | struct tinywl_node_details *tinywl_node_details = NULL; 850 | struct tinywl_view *view = desktop_view_at(server, 851 | server->cursor->x, server->cursor->y, &sx, &sy, 852 | &scene_node, &tinywl_node_details); 853 | 854 | if (tinywl_node_details && tinywl_node_details->type == MENU){ 855 | wlr_xcursor_manager_set_cursor_image( 856 | server->cursor_mgr, "left_ptr", server->cursor); 857 | if (tinywl_node_details->owner && 858 | server->selected_menu_item != tinywl_node_details->owner){ 859 | if (server->selected_menu_item){ 860 | wlr_scene_rect_set_color(server->selected_menu_item, CONFIG.inactive_window_rgba); 861 | } 862 | wlr_scene_rect_set_color(tinywl_node_details->owner, CONFIG.active_window_rgba); 863 | server->selected_menu_item = tinywl_node_details->owner; 864 | } 865 | } else if (server->selected_menu_item){ 866 | wlr_scene_rect_set_color(server->selected_menu_item, CONFIG.inactive_window_rgba); 867 | server->selected_menu_item = NULL; 868 | } 869 | 870 | if ((!view || tinywl_node_details && 871 | (tinywl_node_details->type == TITLEBAR || 872 | tinywl_node_details->type == CLOSE_BUTTON || 873 | tinywl_node_details->type == MENU) && 874 | server->cursor_mode != TINYWL_CURSOR_PRESSED)) { 875 | /* If there's no view under the cursor, set the cursor image to a 876 | * default. This is what makes the cursor image appear when you move it 877 | * around the screen, not over any views. */ 878 | wlr_xcursor_manager_set_cursor_image( 879 | server->cursor_mgr, "left_ptr", server->cursor); 880 | } else if(tinywl_node_details && 881 | (tinywl_node_details->type == TITLEBAR && 882 | server->cursor_mode == TINYWL_CURSOR_PRESSED)){ 883 | wlr_xcursor_manager_set_cursor_image( 884 | server->cursor_mgr, "move", server->cursor); 885 | server->seat->pointer_state.focused_surface = view->xdg_surface->surface; 886 | begin_interactive(view, TINYWL_CURSOR_MOVE, 0); 887 | } else if (tinywl_node_details && tinywl_node_details->type == BORDER){ 888 | enum wlr_edges edge = find_resize_edge(view, view->xdg_surface->surface); 889 | wlr_xcursor_manager_set_cursor_image( 890 | server->cursor_mgr, wlr_xcursor_get_resize_name(edge), server->cursor); 891 | } 892 | 893 | if (server->cursor_mode == TINYWL_CURSOR_PRESSED && view != server->grabbed_view) { 894 | // Send pointer events to the view which the mouse button is pressed on. 895 | view = server->grabbed_view; 896 | sx = server->cursor->x - view->x; 897 | sy = server->cursor->y - view->y; 898 | wlr_seat_pointer_notify_motion(seat, time, sx, sy); 899 | } else if (view && view->xdg_surface->surface) { 900 | /* 901 | * Send pointer enter and motion events. 902 | * 903 | * The enter event gives the surface "pointer focus", which is distinct 904 | * from keyboard focus. You get pointer focus by moving the pointer over 905 | * a window. 906 | * 907 | * Note that wlroots will avoid sending duplicate enter/motion events if 908 | * the surface has already has pointer focus or if the client is already 909 | * aware of the coordinates passed. 910 | */ 911 | if (!tinywl_node_details){ 912 | wlr_seat_pointer_notify_enter(seat, view->xdg_surface->surface, sx, sy); 913 | wlr_seat_pointer_notify_motion(seat, time, sx, sy); 914 | } else if (tinywl_node_details && server->cursor_mode != TINYWL_CURSOR_PRESSED){ 915 | wlr_seat_pointer_clear_focus(seat); 916 | } 917 | } else { 918 | /* Clear pointer focus so future button events and such are not sent to 919 | * the last client to have the cursor over it. */ 920 | wlr_seat_pointer_clear_focus(seat); 921 | } 922 | } 923 | 924 | static void server_cursor_motion(struct wl_listener *listener, void *data) { 925 | /* This event is forwarded by the cursor when a pointer emits a _relative_ 926 | * pointer motion event (i.e. a delta) */ 927 | struct tinywl_server *server = 928 | wl_container_of(listener, server, cursor_motion); 929 | struct wlr_event_pointer_motion *event = data; 930 | /* The cursor doesn't move unless we tell it to. The cursor automatically 931 | * handles constraining the motion to the output layout, as well as any 932 | * special configuration applied for the specific input device which 933 | * generated the event. You can pass NULL for the device if you want to move 934 | * the cursor around without any input. */ 935 | wlr_cursor_move(server->cursor, event->device, 936 | event->delta_x, event->delta_y); 937 | process_cursor_motion(server, event->time_msec); 938 | } 939 | 940 | static void server_cursor_motion_absolute( 941 | struct wl_listener *listener, void *data) { 942 | /* This event is forwarded by the cursor when a pointer emits an _absolute_ 943 | * motion event, from 0..1 on each axis. This happens, for example, when 944 | * wlroots is running under a Wayland window rather than KMS+DRM, and you 945 | * move the mouse over the window. You could enter the window from any edge, 946 | * so we have to warp the mouse there. There is also some hardware which 947 | * emits these events. */ 948 | struct tinywl_server *server = 949 | wl_container_of(listener, server, cursor_motion_absolute); 950 | struct wlr_event_pointer_motion_absolute *event = data; 951 | wlr_cursor_warp_absolute(server->cursor, event->device, event->x, event->y); 952 | process_cursor_motion(server, event->time_msec); 953 | } 954 | 955 | static int number_of_clicks(uint32_t button, uint32_t time_msec){ 956 | // Count number of clicks in a doubleclick_interval 957 | static int clicked = 1; 958 | static uint32_t last_button; 959 | static uint32_t last_time_pressed; 960 | 961 | if (button == last_button && 962 | (time_msec - last_time_pressed) < CONFIG.doubleclick_interval){ 963 | clicked++; 964 | } else { 965 | clicked = 1; 966 | } 967 | 968 | last_button = button; 969 | last_time_pressed = time_msec; 970 | return clicked; 971 | } 972 | 973 | static void server_cursor_button(struct wl_listener *listener, void *data) { 974 | /* This event is forwarded by the cursor when a pointer emits a button 975 | * event. */ 976 | struct tinywl_server *server = 977 | wl_container_of(listener, server, cursor_button); 978 | struct wlr_event_pointer_button *event = data; 979 | /* Notify the client with pointer focus that a button press has occurred */ 980 | wlr_seat_pointer_notify_button(server->seat, 981 | event->time_msec, event->button, event->state); 982 | double sx, sy; 983 | void *scene_node; 984 | struct tinywl_node_details *tinywl_node_details = NULL; 985 | struct tinywl_view *view = desktop_view_at(server, 986 | server->cursor->x, server->cursor->y, &sx, &sy, 987 | &scene_node, &tinywl_node_details); 988 | 989 | if (event->state == WLR_BUTTON_RELEASED) { 990 | /* If you released any buttons, we exit interactive move/resize mode. */ 991 | server->cursor_mode = TINYWL_CURSOR_PASSTHROUGH; 992 | 993 | if (server->opened_menu_view){ 994 | if (tinywl_node_details && tinywl_node_details->type == MENU) { 995 | switch(tinywl_node_details->index) { 996 | case 0: // Toggle maximize 997 | toggle_maximize(server->opened_menu_view); 998 | break; 999 | 1000 | case 1: // Close 1001 | wlr_xdg_toplevel_send_close(server->opened_menu_view->xdg_surface); 1002 | break; 1003 | } 1004 | } 1005 | 1006 | wlr_scene_node_set_enabled(&server->view_menu->node, false); 1007 | server->opened_menu_view = NULL; 1008 | return; 1009 | } 1010 | 1011 | // Handle button events on titlebar portion 1012 | int clicked = number_of_clicks(event->button, event->time_msec); 1013 | if (tinywl_node_details && tinywl_node_details->type == TITLEBAR){ 1014 | if (event->button == BTN_LEFT && clicked == 2){ 1015 | toggle_maximize(view); 1016 | } else if (event->button == BTN_MIDDLE){ 1017 | wlr_xdg_toplevel_send_close(view->xdg_surface); 1018 | } else if (event->button == BTN_RIGHT){ 1019 | wlr_scene_node_set_position(&server->view_menu->node, 1020 | server->cursor->x, server->cursor->y); 1021 | wlr_scene_node_set_enabled(&server->view_menu->node, true); 1022 | wlr_scene_node_raise_to_top(&server->view_menu->node); 1023 | server->opened_menu_view = view; 1024 | } 1025 | } else if (tinywl_node_details && tinywl_node_details->type == CLOSE_BUTTON){ 1026 | wlr_xdg_toplevel_send_close(view->xdg_surface); 1027 | } 1028 | 1029 | // The view might have changed (maximized) thus simulate move to update cursor 1030 | process_cursor_motion(server, event->time_msec); 1031 | } else { 1032 | if (server->opened_menu_view){ 1033 | return; 1034 | } 1035 | 1036 | if (view){ 1037 | /* Focus that client if the button was _pressed_ */ 1038 | focus_view(view, view->xdg_surface->surface); 1039 | server->grabbed_view = view; 1040 | if (tinywl_node_details && tinywl_node_details->type == BORDER){ 1041 | /* If we are clicking the border, then the surface is pointer focus is 1042 | * cleared and we need to manually set the focused surface without 1043 | * calling an enter, which would change the cursor image. */ 1044 | server->seat->pointer_state.focused_surface = view->xdg_surface->surface; 1045 | begin_interactive(view, TINYWL_CURSOR_RESIZE, 1046 | find_resize_edge(view, view->xdg_surface->surface)); 1047 | } else if (event->button == BTN_LEFT) { 1048 | server->cursor_mode = TINYWL_CURSOR_PRESSED; 1049 | } 1050 | } 1051 | } 1052 | } 1053 | 1054 | static void server_cursor_axis(struct wl_listener *listener, void *data) { 1055 | /* This event is forwarded by the cursor when a pointer emits an axis event, 1056 | * for example when you move the scroll wheel. */ 1057 | struct tinywl_server *server = 1058 | wl_container_of(listener, server, cursor_axis); 1059 | struct wlr_event_pointer_axis *event = data; 1060 | /* Notify the client with pointer focus of the axis event. */ 1061 | wlr_seat_pointer_notify_axis(server->seat, 1062 | event->time_msec, event->orientation, event->delta, 1063 | event->delta_discrete, event->source); 1064 | } 1065 | 1066 | static void server_cursor_frame(struct wl_listener *listener, void *data) { 1067 | /* This event is forwarded by the cursor when a pointer emits an frame 1068 | * event. Frame events are sent after regular pointer events to group 1069 | * multiple events together. For instance, two axis events may happen at the 1070 | * same time, in which case a frame event won't be sent in between. */ 1071 | struct tinywl_server *server = 1072 | wl_container_of(listener, server, cursor_frame); 1073 | /* Notify the client with pointer focus of the frame event. */ 1074 | wlr_seat_pointer_notify_frame(server->seat); 1075 | } 1076 | 1077 | static void output_frame(struct wl_listener *listener, void *data) { 1078 | /* This function is called every time an output is ready to display a frame, 1079 | * generally at the output's refresh rate (e.g. 60Hz). */ 1080 | struct tinywl_output *output = wl_container_of(listener, output, frame); 1081 | struct wlr_scene *scene = output->server->scene; 1082 | 1083 | struct wlr_scene_output *scene_output = wlr_scene_get_scene_output( 1084 | scene, output->wlr_output); 1085 | 1086 | /* Render the scene if needed and commit the output */ 1087 | wlr_scene_output_commit(scene_output); 1088 | 1089 | struct timespec now; 1090 | clock_gettime(CLOCK_MONOTONIC, &now); 1091 | wlr_scene_output_send_frame_done(scene_output, &now); 1092 | } 1093 | 1094 | static void server_new_output(struct wl_listener *listener, void *data) { 1095 | /* This event is raised by the backend when a new output (aka a display or 1096 | * monitor) becomes available. */ 1097 | struct tinywl_server *server = 1098 | wl_container_of(listener, server, new_output); 1099 | struct wlr_output *wlr_output = data; 1100 | 1101 | /* Configures the output created by the backend to use our allocator 1102 | * and our renderer. Must be done once, before commiting the output */ 1103 | wlr_output_init_render(wlr_output, server->allocator, server->renderer); 1104 | 1105 | /* Some backends don't have modes. DRM+KMS does, and we need to set a mode 1106 | * before we can use the output. The mode is a tuple of (width, height, 1107 | * refresh rate), and each monitor supports only a specific set of modes. We 1108 | * just pick the monitor's preferred mode, a more sophisticated compositor 1109 | * would let the user configure it. */ 1110 | if (!wl_list_empty(&wlr_output->modes)) { 1111 | struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output); 1112 | wlr_output_set_mode(wlr_output, mode); 1113 | wlr_output_enable(wlr_output, true); 1114 | if (!wlr_output_commit(wlr_output)) { 1115 | return; 1116 | } 1117 | } 1118 | 1119 | /* Allocates and configures our state for this output */ 1120 | struct tinywl_output *output = 1121 | calloc(1, sizeof(struct tinywl_output)); 1122 | output->wlr_output = wlr_output; 1123 | output->server = server; 1124 | /* Sets up a listener for the frame notify event. */ 1125 | output->frame.notify = output_frame; 1126 | wl_signal_add(&wlr_output->events.frame, &output->frame); 1127 | wl_list_insert(&server->outputs, &output->link); 1128 | 1129 | output->background = wlr_scene_rect_create( 1130 | &server->scene->node, 1131 | output->wlr_output->width, output->wlr_output->height, 1132 | CONFIG.background_rgba); 1133 | 1134 | /* Adds this to the output layout. The add_auto function arranges outputs 1135 | * from left-to-right in the order they appear. A more sophisticated 1136 | * compositor would let the user configure the arrangement of outputs in the 1137 | * layout. 1138 | * 1139 | * The output layout utility automatically adds a wl_output global to the 1140 | * display, which Wayland clients can see to find out information about the 1141 | * output (such as DPI, scale factor, manufacturer, etc). 1142 | */ 1143 | wlr_output_layout_add_auto(server->output_layout, wlr_output); 1144 | } 1145 | 1146 | static void xdg_toplevel_map(struct wl_listener *listener, void *data) { 1147 | /* Called when the surface is mapped, or ready to display on-screen. */ 1148 | struct tinywl_view *view = wl_container_of(listener, view, map); 1149 | 1150 | position_view_centered(view); 1151 | 1152 | wl_list_insert(&view->server->views, &view->link); 1153 | 1154 | focus_view(view, view->xdg_surface->surface); 1155 | } 1156 | 1157 | static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) { 1158 | /* Called when the surface is unmapped, and should no longer be shown. */ 1159 | struct tinywl_view *view = wl_container_of(listener, view, unmap); 1160 | 1161 | wl_list_remove(&view->link); 1162 | 1163 | // Destroy commit listener and node for decorations 1164 | wl_list_remove(&view->commit.link); 1165 | wlr_scene_node_destroy(view->scene_node); 1166 | } 1167 | 1168 | static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) { 1169 | /* Called when the surface is destroyed and should never be shown again. */ 1170 | struct tinywl_view *view = wl_container_of(listener, view, destroy); 1171 | 1172 | wl_list_remove(&view->map.link); 1173 | wl_list_remove(&view->unmap.link); 1174 | wl_list_remove(&view->destroy.link); 1175 | wl_list_remove(&view->request_move.link); 1176 | wl_list_remove(&view->request_resize.link); 1177 | wl_list_remove(&view->request_maximize.link); 1178 | wl_list_remove(&view->set_title.link); 1179 | 1180 | free(view); 1181 | } 1182 | 1183 | static void xdg_toplevel_commit(struct wl_listener *listener, void *data) { 1184 | struct tinywl_view *view = wl_container_of(listener, view, commit); 1185 | 1186 | int pending_width = view->xdg_surface->pending.geometry.width; 1187 | int pending_height = view->xdg_surface->pending.geometry.height; 1188 | 1189 | // Only render a new title if the width of the view is different than title 1190 | if (pending_width - CONFIG.deco_button_size < view->title.current_width || 1191 | (view->title.current_width != view->title.original_width && 1192 | view->title.current_width != pending_width - CONFIG.border_size - CONFIG.deco_button_size)){ 1193 | view_title_update(view, view->xdg_surface->toplevel->title); 1194 | } 1195 | 1196 | // This needs to be done here otherwise the border/titlebar move faster/slower 1197 | // than the surface when the size is changed thus causing a lag effect. 1198 | if (view->border && (pending_width != view->border->width || 1199 | pending_height != view->border->height - TITLEBAR_HEIGHT - CONFIG.border_size)){ 1200 | wlr_scene_rect_set_size(view->border, pending_width + (CONFIG.border_size*2), 1201 | pending_height + TITLEBAR_HEIGHT + (CONFIG.border_size*2)); 1202 | wlr_scene_rect_set_size(view->titlebar, pending_width, 1203 | TITLEBAR_HEIGHT); 1204 | } 1205 | 1206 | if (view->close_button){ 1207 | wlr_scene_node_set_position(&view->close_button->node, 1208 | view->xdg_surface->pending.geometry.width - view->close_button->width, 1209 | TITLEBAR_HEIGHT/2 - view->close_button->height/2); 1210 | } 1211 | } 1212 | 1213 | /* This function is from labwc that calulates the view/window 1214 | * position under the mouse proportional to when it was maximized. */ 1215 | static int max_move_scale(double pos_cursor, double pos_current, 1216 | double size_current, double size_orig) 1217 | { 1218 | double anchor_frac = (pos_cursor - pos_current) / size_current; 1219 | int pos_new = pos_cursor - (size_orig * anchor_frac); 1220 | if (pos_new < pos_current) { 1221 | /* Clamp by using the old offsets of the maximized window */ 1222 | pos_new = pos_current; 1223 | } 1224 | return pos_new; 1225 | } 1226 | 1227 | static void begin_interactive(struct tinywl_view *view, 1228 | enum tinywl_cursor_mode mode, uint32_t edges) { 1229 | /* This function sets up an interactive move or resize operation, where the 1230 | * compositor stops propegating pointer events to clients and instead 1231 | * consumes them itself, to move or resize windows. */ 1232 | struct tinywl_server *server = view->server; 1233 | struct wlr_surface *focused_surface = 1234 | server->seat->pointer_state.focused_surface; 1235 | if (view->xdg_surface->surface != 1236 | wlr_surface_get_root_surface(focused_surface)) { 1237 | /* Deny move/resize requests from unfocused clients. */ 1238 | return; 1239 | } 1240 | server->grabbed_view = view; 1241 | server->cursor_mode = mode; 1242 | 1243 | if (mode == TINYWL_CURSOR_MOVE) { 1244 | if (view->xdg_surface->toplevel->current.maximized){ 1245 | // Calculate where the window should be under the cursor 1246 | int new_x = max_move_scale(server->cursor->x, view->x, 1247 | focused_surface->pending.width, view->saved_geometry.width); 1248 | int new_y = max_move_scale(server->cursor->y, view->y, 1249 | focused_surface->pending.height, view->saved_geometry.height); 1250 | view->x = new_x; 1251 | view->y = new_y; 1252 | } else { 1253 | // Save geometry before start of a move since we can maxmize with a move 1254 | save_view_geometry(view); 1255 | } 1256 | server->grab_x = server->cursor->x - view->x; 1257 | server->grab_y = server->cursor->y - view->y; 1258 | } else { 1259 | struct wlr_box geo_box; 1260 | wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); 1261 | 1262 | double border_x = (view->x + geo_box.x) + 1263 | ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0); 1264 | double border_y = (view->y + geo_box.y) + 1265 | ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0); 1266 | server->grab_x = server->cursor->x - border_x; 1267 | server->grab_y = server->cursor->y - border_y; 1268 | 1269 | server->grab_geobox = geo_box; 1270 | server->grab_geobox.x += view->x; 1271 | server->grab_geobox.y += view->y; 1272 | 1273 | server->resize_edges = edges; 1274 | } 1275 | } 1276 | 1277 | static void xdg_toplevel_request_move( 1278 | struct wl_listener *listener, void *data) { 1279 | /* This event is raised when a client would like to begin an interactive 1280 | * move, typically because the user clicked on their client-side 1281 | * decorations. Note that a more sophisticated compositor should check the 1282 | * provided serial against a list of button press serials sent to this 1283 | * client, to prevent the client from requesting this whenever they want. */ 1284 | struct tinywl_view *view = wl_container_of(listener, view, request_move); 1285 | begin_interactive(view, TINYWL_CURSOR_MOVE, 0); 1286 | } 1287 | 1288 | static void xdg_toplevel_request_resize( 1289 | struct wl_listener *listener, void *data) { 1290 | /* This event is raised when a client would like to begin an interactive 1291 | * resize, typically because the user clicked on their client-side 1292 | * decorations. Note that a more sophisticated compositor should check the 1293 | * provided serial against a list of button press serials sent to this 1294 | * client, to prevent the client from requesting this whenever they want. */ 1295 | struct wlr_xdg_toplevel_resize_event *event = data; 1296 | struct tinywl_view *view = wl_container_of(listener, view, request_resize); 1297 | begin_interactive(view, TINYWL_CURSOR_RESIZE, event->edges); 1298 | } 1299 | 1300 | static void xdg_toplevel_request_maximize(struct wl_listener *listener, void *data){ 1301 | struct tinywl_view *view = wl_container_of(listener, view, request_maximize); 1302 | toggle_maximize(view); 1303 | } 1304 | 1305 | static void server_new_xdg_surface(struct wl_listener *listener, void *data) { 1306 | /* This event is raised when wlr_xdg_shell receives a new xdg surface from a 1307 | * client, either a toplevel (application window) or popup. */ 1308 | struct tinywl_server *server = 1309 | wl_container_of(listener, server, new_xdg_surface); 1310 | struct wlr_xdg_surface *xdg_surface = data; 1311 | 1312 | /* We must add xdg popups to the scene graph so they get rendered. The 1313 | * wlroots scene graph provides a helper for this, but to use it we must 1314 | * provide the proper parent scene node of the xdg popup. To enable this, 1315 | * we always set the user data field of xdg_surfaces to the corresponding 1316 | * scene node. */ 1317 | if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { 1318 | struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface( 1319 | xdg_surface->popup->parent); 1320 | struct wlr_scene_node *parent_node = parent->data; 1321 | xdg_surface->data = wlr_scene_xdg_surface_create( 1322 | parent_node, xdg_surface); 1323 | return; 1324 | } 1325 | assert(xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); 1326 | 1327 | /* Allocate a tinywl_view for this surface */ 1328 | struct tinywl_view *view = 1329 | calloc(1, sizeof(struct tinywl_view)); 1330 | view->server = server; 1331 | view->xdg_surface = xdg_surface; 1332 | /* If the new surface has a parent create it as part of the parent. Doing 1333 | * this will ensure that a dialog will be seen when it's parent is focused.*/ 1334 | if (xdg_surface->toplevel->parent != 0) { 1335 | view->scene_node = wlr_scene_xdg_surface_create( 1336 | xdg_surface->toplevel->parent->data, view->xdg_surface); 1337 | } else { 1338 | view->scene_node = &wlr_scene_tree_create(&view->server->scene->node)->node; 1339 | // Create the border 1340 | view->border = wlr_scene_rect_create( 1341 | view->scene_node, 0, 0, CONFIG.inactive_window_rgba); 1342 | view->border->node.data = node_init(BORDER, NULL, view, 0); 1343 | // Create the titlebar and title text 1344 | view->titlebar = wlr_scene_rect_create( 1345 | &view->border->node, 0, 0, CONFIG.inactive_window_rgba); 1346 | view->titlebar->node.data = node_init(TITLEBAR, NULL, view, 0); 1347 | view_title_update(view, view->xdg_surface->toplevel->title); 1348 | // Create the close button 1349 | view->close_button = wlr_scene_rect_create( 1350 | &view->titlebar->node, 0, 0, (float [4]){0.8f, 0.22f, 0.0f, 1.0f}); 1351 | view->close_button->node.data = node_init(CLOSE_BUTTON, NULL, view, 0); 1352 | wlr_scene_xdg_surface_create(view->scene_node, view->xdg_surface); 1353 | // Set the decoration position. The size is handled by the commit handler 1354 | wlr_scene_node_set_position(&view->border->node, -CONFIG.border_size, 1355 | -(TITLEBAR_HEIGHT + CONFIG.border_size)); 1356 | wlr_scene_node_set_position(&view->titlebar->node, CONFIG.border_size, 1357 | CONFIG.border_size); 1358 | int size = TITLEBAR_HEIGHT - CONFIG.border_size; 1359 | size = (size <= CONFIG.deco_button_size) ? size : CONFIG.deco_button_size; 1360 | wlr_scene_rect_set_size(view->close_button, size, size); 1361 | } 1362 | view->scene_node->data = view; 1363 | xdg_surface->data = view->scene_node; 1364 | 1365 | /* Listen to the various events it can emit */ 1366 | view->map.notify = xdg_toplevel_map; 1367 | wl_signal_add(&xdg_surface->events.map, &view->map); 1368 | view->unmap.notify = xdg_toplevel_unmap; 1369 | wl_signal_add(&xdg_surface->events.unmap, &view->unmap); 1370 | view->destroy.notify = xdg_toplevel_destroy; 1371 | wl_signal_add(&xdg_surface->events.destroy, &view->destroy); 1372 | view->commit.notify = xdg_toplevel_commit; 1373 | wl_signal_add(&xdg_surface->surface->events.commit, &view->commit); 1374 | 1375 | /* cotd */ 1376 | struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel; 1377 | view->request_move.notify = xdg_toplevel_request_move; 1378 | wl_signal_add(&toplevel->events.request_move, &view->request_move); 1379 | view->request_resize.notify = xdg_toplevel_request_resize; 1380 | wl_signal_add(&toplevel->events.request_resize, &view->request_resize); 1381 | view->request_maximize.notify = xdg_toplevel_request_maximize; 1382 | wl_signal_add(&toplevel->events.request_maximize, &view->request_maximize); 1383 | view->set_title.notify = xdg_toplevel_set_title; 1384 | wl_signal_add(&toplevel->events.set_title, &view->set_title); 1385 | } 1386 | 1387 | static struct wlr_scene_tree *generate_menu(struct tinywl_server *server){ 1388 | const int margin = 5; 1389 | char *menu_items[] = {"Maximize Toggle", "Close"}; 1390 | int menu_size = sizeof menu_items / sizeof *menu_items; 1391 | 1392 | struct wlr_scene_tree *menu = wlr_scene_tree_create(&server->scene->node); 1393 | int width, height, largest_width = 0; 1394 | for (int i = 0; i < menu_size; i++) { 1395 | get_text_size(menu_items[i], CONFIG.font_description, &width, &height); 1396 | if (width > largest_width) 1397 | largest_width = width; 1398 | } 1399 | 1400 | for (int i = 0; i < menu_size; i++) { 1401 | struct wlr_scene_rect *container = wlr_scene_rect_create( 1402 | &menu->node, largest_width + margin*2, height + margin*2, 1403 | CONFIG.inactive_window_rgba); 1404 | /* We added an index field to node_init to use to determine which menu item 1405 | we click later. One could skip doing this and loop through and compare the node 1406 | with the list of menu item nodes instead if so desired. */ 1407 | container->node.data = node_init(MENU, NULL, NULL, i); 1408 | wlr_scene_node_set_position(&container->node, 0, (height + margin*2) * i); 1409 | struct text_buffer *buf = create_text_buffer(menu_items[i], largest_width, height); 1410 | struct wlr_scene_buffer *text_scene_buffer = malloc(sizeof(struct wlr_scene_buffer)); 1411 | struct wlr_scene_buffer *bb = wlr_scene_buffer_create(&container->node, &buf->base); 1412 | bb->node.data = node_init(MENU, container, NULL, i); 1413 | wlr_scene_node_set_position(&bb->node, margin, margin); 1414 | } 1415 | 1416 | wlr_scene_node_set_enabled(&menu->node, false); 1417 | server->opened_menu_view = NULL; 1418 | server->selected_menu_item = NULL; 1419 | return menu; 1420 | } 1421 | 1422 | int main(int argc, char *argv[]) { 1423 | wlr_log_init(WLR_DEBUG, NULL); 1424 | char *startup_cmd = NULL; 1425 | 1426 | int c; 1427 | while ((c = getopt(argc, argv, "s:h")) != -1) { 1428 | switch (c) { 1429 | case 's': 1430 | startup_cmd = optarg; 1431 | break; 1432 | default: 1433 | printf("Usage: %s [-s startup command]\n", argv[0]); 1434 | return 0; 1435 | } 1436 | } 1437 | if (optind < argc) { 1438 | printf("Usage: %s [-s startup command]\n", argv[0]); 1439 | return 0; 1440 | } 1441 | 1442 | struct tinywl_server server; 1443 | 1444 | /* The Wayland display is managed by libwayland. It handles accepting 1445 | * clients from the Unix socket, manging Wayland globals, and so on. */ 1446 | server.wl_display = wl_display_create(); 1447 | /* The backend is a wlroots feature which abstracts the underlying input and 1448 | * output hardware. The autocreate option will choose the most suitable 1449 | * backend based on the current environment, such as opening an X11 window 1450 | * if an X11 server is running. */ 1451 | server.backend = wlr_backend_autocreate(server.wl_display); 1452 | 1453 | /* Autocreates a renderer, either Pixman, GLES2 or Vulkan for us. The user 1454 | * can also specify a renderer using the WLR_RENDERER env var. 1455 | * The renderer is responsible for defining the various pixel formats it 1456 | * supports for shared memory, this configures that for clients. */ 1457 | server.renderer = wlr_renderer_autocreate(server.backend); 1458 | wlr_renderer_init_wl_display(server.renderer, server.wl_display); 1459 | 1460 | /* Autocreates an allocator for us. 1461 | * The allocator is the bridge between the renderer and the backend. It 1462 | * handles the buffer creation, allowing wlroots to render onto the 1463 | * screen */ 1464 | server.allocator = wlr_allocator_autocreate(server.backend, 1465 | server.renderer); 1466 | 1467 | /* This creates some hands-off wlroots interfaces. The compositor is 1468 | * necessary for clients to allocate surfaces and the data device manager 1469 | * handles the clipboard. Each of these wlroots interfaces has room for you 1470 | * to dig your fingers in and play with their behavior if you want. Note that 1471 | * the clients cannot set the selection directly without compositor approval, 1472 | * see the handling of the request_set_selection event below.*/ 1473 | wlr_compositor_create(server.wl_display, server.renderer); 1474 | wlr_data_device_manager_create(server.wl_display); 1475 | 1476 | /* Creates an output layout, which a wlroots utility for working with an 1477 | * arrangement of screens in a physical layout. */ 1478 | server.output_layout = wlr_output_layout_create(); 1479 | 1480 | /* Configure a listener to be notified when new outputs are available on the 1481 | * backend. */ 1482 | wl_list_init(&server.outputs); 1483 | server.new_output.notify = server_new_output; 1484 | wl_signal_add(&server.backend->events.new_output, &server.new_output); 1485 | 1486 | /* Create a scene graph. This is a wlroots abstraction that handles all 1487 | * rendering and damage tracking. All the compositor author needs to do 1488 | * is add things that should be rendered to the scene graph at the proper 1489 | * positions and then call wlr_scene_output_commit() to render a frame if 1490 | * necessary. 1491 | */ 1492 | server.scene = wlr_scene_create(); 1493 | wlr_scene_attach_output_layout(server.scene, server.output_layout); 1494 | 1495 | /* Use decoration protocols to negotiate server-side decorations */ 1496 | wlr_server_decoration_manager_set_default_mode( 1497 | wlr_server_decoration_manager_create(server.wl_display), 1498 | WLR_SERVER_DECORATION_MANAGER_MODE_SERVER); 1499 | wlr_xdg_decoration_manager_v1_create(server.wl_display); 1500 | 1501 | /* Set up the xdg-shell. The xdg-shell is a Wayland protocol which is used 1502 | * for application windows. For more detail on shells, refer to my article: 1503 | * 1504 | * https://drewdevault.com/2018/07/29/Wayland-shells.html 1505 | */ 1506 | wl_list_init(&server.views); 1507 | server.xdg_shell = wlr_xdg_shell_create(server.wl_display); 1508 | server.new_xdg_surface.notify = server_new_xdg_surface; 1509 | wl_signal_add(&server.xdg_shell->events.new_surface, 1510 | &server.new_xdg_surface); 1511 | 1512 | /* 1513 | * Creates a cursor, which is a wlroots utility for tracking the cursor 1514 | * image shown on screen. 1515 | */ 1516 | server.cursor = wlr_cursor_create(); 1517 | wlr_cursor_attach_output_layout(server.cursor, server.output_layout); 1518 | 1519 | /* Creates an xcursor manager, another wlroots utility which loads up 1520 | * Xcursor themes to source cursor images from and makes sure that cursor 1521 | * images are available at all scale factors on the screen (necessary for 1522 | * HiDPI support). We add a cursor theme at scale factor 1 to begin with. */ 1523 | server.cursor_mgr = wlr_xcursor_manager_create(NULL, 24); 1524 | wlr_xcursor_manager_load(server.cursor_mgr, 1); 1525 | 1526 | /* 1527 | * wlr_cursor *only* displays an image on screen. It does not move around 1528 | * when the pointer moves. However, we can attach input devices to it, and 1529 | * it will generate aggregate events for all of them. In these events, we 1530 | * can choose how we want to process them, forwarding them to clients and 1531 | * moving the cursor around. More detail on this process is described in my 1532 | * input handling blog post: 1533 | * 1534 | * https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html 1535 | * 1536 | * And more comments are sprinkled throughout the notify functions above. 1537 | */ 1538 | server.cursor_motion.notify = server_cursor_motion; 1539 | wl_signal_add(&server.cursor->events.motion, &server.cursor_motion); 1540 | server.cursor_motion_absolute.notify = server_cursor_motion_absolute; 1541 | wl_signal_add(&server.cursor->events.motion_absolute, 1542 | &server.cursor_motion_absolute); 1543 | server.cursor_button.notify = server_cursor_button; 1544 | wl_signal_add(&server.cursor->events.button, &server.cursor_button); 1545 | server.cursor_axis.notify = server_cursor_axis; 1546 | wl_signal_add(&server.cursor->events.axis, &server.cursor_axis); 1547 | server.cursor_frame.notify = server_cursor_frame; 1548 | wl_signal_add(&server.cursor->events.frame, &server.cursor_frame); 1549 | 1550 | /* 1551 | * Configures a seat, which is a single "seat" at which a user sits and 1552 | * operates the computer. This conceptually includes up to one keyboard, 1553 | * pointer, touch, and drawing tablet device. We also rig up a listener to 1554 | * let us know when new input devices are available on the backend. 1555 | */ 1556 | wl_list_init(&server.keyboards); 1557 | server.new_input.notify = server_new_input; 1558 | wl_signal_add(&server.backend->events.new_input, &server.new_input); 1559 | server.seat = wlr_seat_create(server.wl_display, "seat0"); 1560 | server.request_cursor.notify = seat_request_cursor; 1561 | wl_signal_add(&server.seat->events.request_set_cursor, 1562 | &server.request_cursor); 1563 | server.request_set_selection.notify = seat_request_set_selection; 1564 | wl_signal_add(&server.seat->events.request_set_selection, 1565 | &server.request_set_selection); 1566 | 1567 | /* Add a Unix socket to the Wayland display. */ 1568 | const char *socket = wl_display_add_socket_auto(server.wl_display); 1569 | if (!socket) { 1570 | wlr_backend_destroy(server.backend); 1571 | return 1; 1572 | } 1573 | 1574 | /* Start the backend. This will enumerate outputs and inputs, become the DRM 1575 | * master, etc */ 1576 | if (!wlr_backend_start(server.backend)) { 1577 | wlr_backend_destroy(server.backend); 1578 | wl_display_destroy(server.wl_display); 1579 | return 1; 1580 | } 1581 | 1582 | /* Generate menus */ 1583 | server.view_menu = generate_menu(&server); 1584 | 1585 | /* Set the WAYLAND_DISPLAY environment variable to our socket and run the 1586 | * startup command if requested. */ 1587 | setenv("WAYLAND_DISPLAY", socket, true); 1588 | if (startup_cmd) { 1589 | if (fork() == 0) { 1590 | execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL); 1591 | } 1592 | } 1593 | /* Run the Wayland event loop. This does not return until you exit the 1594 | * compositor. Starting the backend rigged up all of the necessary event 1595 | * loop configuration to listen to libinput events, DRM events, generate 1596 | * frame events at the refresh rate, and so on. */ 1597 | wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s", 1598 | socket); 1599 | wl_display_run(server.wl_display); 1600 | 1601 | /* Once wl_display_run returns, we shut down the server. */ 1602 | wl_display_destroy_clients(server.wl_display); 1603 | wl_display_destroy(server.wl_display); 1604 | return 0; 1605 | } 1606 | --------------------------------------------------------------------------------