├── .gitignore ├── meson.build ├── Makefile ├── README.md ├── flake.nix ├── flake.lock ├── LICENSE └── tinywl.c /.gitignore: -------------------------------------------------------------------------------- 1 | tinywl 2 | *-protocol.c 3 | *-protocol.h 4 | 5 | result 6 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | executable( 2 | 'tinywl', 3 | ['tinywl.c', protocols_client_header['xdg-shell']], 4 | dependencies: wlroots, 5 | ) 6 | -------------------------------------------------------------------------------- /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 xkbcommon) 7 | 8 | # wayland-scanner is a tool which generates C headers and rigging for Wayland 9 | # protocols, which are specified in XML. wlroots requires you to rig these up 10 | # to your build system yourself and provide them in the include path. 11 | xdg-shell-protocol.h: 12 | $(WAYLAND_SCANNER) server-header \ 13 | $(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@ 14 | 15 | xdg-shell-protocol.c: xdg-shell-protocol.h 16 | $(WAYLAND_SCANNER) private-code \ 17 | $(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@ 18 | 19 | tinywl: tinywl.c xdg-shell-protocol.h xdg-shell-protocol.c 20 | $(CC) $(CFLAGS) \ 21 | -g -Werror -I. \ 22 | -DWLR_USE_UNSTABLE \ 23 | -o $@ $< \ 24 | $(LIBS) 25 | 26 | clean: 27 | rm -f tinywl xdg-shell-protocol.h xdg-shell-protocol.c 28 | 29 | .DEFAULT_GOAL=tinywl 30 | .PHONY: clean 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wlroots-tinywl 2 | 3 | A wayland compositor based on wlroots and tinywl, written in c, packaged with 4 | nix. This is really just for me to hack on wayland. 5 | 6 | ## Building wlroots-tinywl 7 | 8 | Simply enter the nix development shell for the dependencies and run commands 9 | as normal. 10 | ``` 11 | nix develop 12 | make 13 | ``` 14 | 15 | Or build with nix directly (without entering a dev shell) 16 | ``` 17 | nix build 18 | ``` 19 | 20 | Or immediately build and run the compositor directly with 21 | ``` 22 | nix run 23 | ``` 24 | 25 | ## NVIDIA graphics card 26 | Some recent NVIDIA drivers support GBM (required for wlroots). It should be as 27 | of [experiemental driver 495.46](https://www.nvidia.com/download/driverResults.aspx/184248) 28 | and [stable driver 510.47.03](https://www.nvidia.com/download/driverResults.aspx/186156). 29 | 30 | If you are having some issues with the cursor showing up, try to start the 31 | compositor like this: 32 | ``` 33 | WLR_NO_HARDWARE_CURSORS=1 ./tinywl -s firefox 34 | ``` 35 | 36 | ## Debugging Wayland Compositors 37 | You can run a wayland compositor by switching to a tty and launching it directly. But for debugging 38 | it is much nicer to run it embedded in your current window manager. We can set up a nice environment 39 | quite easily that will work regardless of window manager, display server, or video card. This is a way 40 | get wlroots based compositors to play nice with NVIDIA graphics cards. 41 | ``` 42 | kwin_wayland --xwayland --socket wayland-1 --width 1920 --height 1080 43 | WAYLAND_DISPLAY=wayland-1 ./result/bin/tinywl -s alacritty 44 | ``` 45 | Now tinywl will be running on `wayland-0`, inside the kwin window. Note that it doesn't matter if you 46 | use KDE Plasma (or litterally anything else), it's just that kwin is a mature compositor with multiple 47 | backends that will let us run tinywl (or any other compositor/window manager) embdedded. The kwin display 48 | will be called `WL-1`, as opposed to something like `HDMI-A-1` if you need to access it (like from a sway 49 | config). 50 | 51 | # [TinyWL's readme](https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/master/tinywl) 52 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "wlroots tinywl c implementation"; 3 | 4 | inputs = { 5 | nixpkgs.url = "github:NixOS/nixpkgs"; 6 | flake-utils.url = "github:numtide/flake-utils"; 7 | flake-compat = { url = "github:edolstra/flake-compat"; flake = false; }; 8 | 9 | nixpkgs-wayland = { url = "github:nix-community/nixpkgs-wayland"; }; 10 | }; 11 | 12 | outputs = { self, nixpkgs, flake-utils, ... }@inputs: 13 | flake-utils.lib.eachSystem 14 | (with flake-utils.lib.system; [ 15 | x86_64-linux 16 | 17 | ]) 18 | (system: 19 | let 20 | pkgs = import nixpkgs { 21 | inherit system; 22 | }; 23 | 24 | nativeBuildInputs = with pkgs; [ pkg-config wayland-scanner ]; 25 | 26 | buildInputs = with pkgs; [ 27 | libxkbcommon 28 | pixman 29 | udev 30 | wayland 31 | wayland-protocols 32 | inputs.nixpkgs-wayland.packages.${system}.wlroots 33 | ]; 34 | 35 | packageName = "tinywl"; 36 | 37 | in 38 | { 39 | 40 | packages.${packageName} = pkgs.stdenv.mkDerivation { 41 | pname = packageName; 42 | version = "0.15.1"; 43 | src = ./.; 44 | 45 | nativeBuildInputs = nativeBuildInputs; 46 | buildInputs = buildInputs; 47 | 48 | installPhase = '' 49 | runHook preInstall 50 | 51 | mkdir -p $out/bin 52 | cp tinywl $out/bin 53 | 54 | runHook postInstall 55 | ''; 56 | }; 57 | 58 | devShells.dev = pkgs.mkShell { 59 | packages = with pkgs; [ 60 | clang 61 | clang-tools 62 | cppcheck 63 | 64 | rnix-lsp 65 | 66 | kwin 67 | ]; 68 | 69 | buildInputs = with pkgs; [ 70 | gnumake 71 | ] ++ nativeBuildInputs ++ buildInputs; 72 | 73 | shellHook = '' 74 | [ $STARSHIP_SHELL ] && exec $STARSHIP_SHELL 75 | ''; 76 | 77 | CURRENT_PROJECT = packageName; 78 | }; 79 | 80 | defaultPackage = self.packages.${system}.${packageName}; 81 | devShell = self.devShells.${system}.dev; 82 | 83 | }); 84 | 85 | } 86 | 87 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "cachix": { 4 | "locked": { 5 | "lastModified": 1642244250, 6 | "narHash": "sha256-vWpUEqQdVP4srj+/YLJRTN9vjpTs4je0cdWKXPbDItc=", 7 | "owner": "nixos", 8 | "repo": "nixpkgs", 9 | "rev": "0fd9ee1aa36ce865ad273f4f07fdc093adeb5c00", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "nixos", 14 | "ref": "nixos-21.05", 15 | "repo": "nixpkgs", 16 | "type": "github" 17 | } 18 | }, 19 | "flake-compat": { 20 | "flake": false, 21 | "locked": { 22 | "lastModified": 1641205782, 23 | "narHash": "sha256-4jY7RCWUoZ9cKD8co0/4tFARpWB+57+r1bLLvXNJliY=", 24 | "owner": "edolstra", 25 | "repo": "flake-compat", 26 | "rev": "b7547d3eed6f32d06102ead8991ec52ab0a4f1a7", 27 | "type": "github" 28 | }, 29 | "original": { 30 | "owner": "edolstra", 31 | "repo": "flake-compat", 32 | "type": "github" 33 | } 34 | }, 35 | "flake-compat_2": { 36 | "flake": false, 37 | "locked": { 38 | "lastModified": 1641205782, 39 | "narHash": "sha256-4jY7RCWUoZ9cKD8co0/4tFARpWB+57+r1bLLvXNJliY=", 40 | "owner": "edolstra", 41 | "repo": "flake-compat", 42 | "rev": "b7547d3eed6f32d06102ead8991ec52ab0a4f1a7", 43 | "type": "github" 44 | }, 45 | "original": { 46 | "owner": "edolstra", 47 | "repo": "flake-compat", 48 | "type": "github" 49 | } 50 | }, 51 | "flake-utils": { 52 | "locked": { 53 | "lastModified": 1644229661, 54 | "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", 55 | "owner": "numtide", 56 | "repo": "flake-utils", 57 | "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", 58 | "type": "github" 59 | }, 60 | "original": { 61 | "owner": "numtide", 62 | "repo": "flake-utils", 63 | "type": "github" 64 | } 65 | }, 66 | "nixpkgs": { 67 | "locked": { 68 | "lastModified": 1646358635, 69 | "narHash": "sha256-mRf6aVIvZZ2gIADHJNkWxN9jme8Z3Krp7VcRuImn8gI=", 70 | "owner": "NixOS", 71 | "repo": "nixpkgs", 72 | "rev": "9ff63d04ed4bc8f2fce2c537349f432e5ef298e7", 73 | "type": "github" 74 | }, 75 | "original": { 76 | "owner": "NixOS", 77 | "repo": "nixpkgs", 78 | "type": "github" 79 | } 80 | }, 81 | "nixpkgs-wayland": { 82 | "inputs": { 83 | "cachix": "cachix", 84 | "flake-compat": "flake-compat_2", 85 | "nixpkgs": "nixpkgs_2" 86 | }, 87 | "locked": { 88 | "lastModified": 1646329437, 89 | "narHash": "sha256-y2gB58LPSX2osdvZZ/7XP2n4vg122SZ38hd/r5cMz2g=", 90 | "owner": "nix-community", 91 | "repo": "nixpkgs-wayland", 92 | "rev": "227bbb5ca57f9052cb528a21727d81529422f168", 93 | "type": "github" 94 | }, 95 | "original": { 96 | "owner": "nix-community", 97 | "repo": "nixpkgs-wayland", 98 | "type": "github" 99 | } 100 | }, 101 | "nixpkgs_2": { 102 | "locked": { 103 | "lastModified": 1646254136, 104 | "narHash": "sha256-8nQx02tTzgYO21BP/dy5BCRopE8OwE8Drsw98j+Qoaw=", 105 | "owner": "nixos", 106 | "repo": "nixpkgs", 107 | "rev": "3e072546ea98db00c2364b81491b893673267827", 108 | "type": "github" 109 | }, 110 | "original": { 111 | "owner": "nixos", 112 | "ref": "nixos-unstable", 113 | "repo": "nixpkgs", 114 | "type": "github" 115 | } 116 | }, 117 | "root": { 118 | "inputs": { 119 | "flake-compat": "flake-compat", 120 | "flake-utils": "flake-utils", 121 | "nixpkgs": "nixpkgs", 122 | "nixpkgs-wayland": "nixpkgs-wayland" 123 | } 124 | } 125 | }, 126 | "root": "root", 127 | "version": 7 128 | } 129 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | 29 | /* For brevity's sake, struct members are annotated where they are used. */ 30 | enum tinywl_cursor_mode { 31 | TINYWL_CURSOR_PASSTHROUGH, 32 | TINYWL_CURSOR_MOVE, 33 | TINYWL_CURSOR_RESIZE, 34 | }; 35 | 36 | struct tinywl_server { 37 | struct wl_display *wl_display; 38 | struct wlr_backend *backend; 39 | struct wlr_renderer *renderer; 40 | struct wlr_allocator *allocator; 41 | struct wlr_scene *scene; 42 | 43 | struct wlr_xdg_shell *xdg_shell; 44 | struct wl_listener new_xdg_surface; 45 | struct wl_list views; 46 | 47 | struct wlr_cursor *cursor; 48 | struct wlr_xcursor_manager *cursor_mgr; 49 | struct wl_listener cursor_motion; 50 | struct wl_listener cursor_motion_absolute; 51 | struct wl_listener cursor_button; 52 | struct wl_listener cursor_axis; 53 | struct wl_listener cursor_frame; 54 | 55 | struct wlr_seat *seat; 56 | struct wl_listener new_input; 57 | struct wl_listener request_cursor; 58 | struct wl_listener request_set_selection; 59 | struct wl_list keyboards; 60 | enum tinywl_cursor_mode cursor_mode; 61 | struct tinywl_view *grabbed_view; 62 | double grab_x, grab_y; 63 | struct wlr_box grab_geobox; 64 | uint32_t resize_edges; 65 | 66 | struct wlr_output_layout *output_layout; 67 | struct wl_list outputs; 68 | struct wl_listener new_output; 69 | }; 70 | 71 | struct tinywl_output { 72 | struct wl_list link; 73 | struct tinywl_server *server; 74 | struct wlr_output *wlr_output; 75 | struct wl_listener frame; 76 | }; 77 | 78 | struct tinywl_view { 79 | struct wl_list link; 80 | struct tinywl_server *server; 81 | struct wlr_xdg_toplevel *xdg_toplevel; 82 | struct wlr_scene_node *scene_node; 83 | struct wl_listener map; 84 | struct wl_listener unmap; 85 | struct wl_listener destroy; 86 | struct wl_listener request_move; 87 | struct wl_listener request_resize; 88 | int x, y; 89 | }; 90 | 91 | struct tinywl_keyboard { 92 | struct wl_list link; 93 | struct tinywl_server *server; 94 | struct wlr_input_device *device; 95 | 96 | struct wl_listener modifiers; 97 | struct wl_listener key; 98 | }; 99 | 100 | static void focus_view(struct tinywl_view *view, struct wlr_surface *surface) { 101 | /* Note: this function only deals with keyboard focus. */ 102 | if (view == NULL) { 103 | return; 104 | } 105 | struct tinywl_server *server = view->server; 106 | struct wlr_seat *seat = server->seat; 107 | struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface; 108 | if (prev_surface == surface) { 109 | /* Don't re-focus an already focused surface. */ 110 | return; 111 | } 112 | if (prev_surface) { 113 | /* 114 | * Deactivate the previously focused surface. This lets the client know 115 | * it no longer has focus and the client will repaint accordingly, e.g. 116 | * stop displaying a caret. 117 | */ 118 | struct wlr_xdg_surface *previous = 119 | wlr_xdg_surface_from_wlr_surface(seat->keyboard_state.focused_surface); 120 | assert(previous->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); 121 | wlr_xdg_toplevel_set_activated(previous->toplevel, false); 122 | } 123 | struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); 124 | /* Move the view to the front */ 125 | wlr_scene_node_raise_to_top(view->scene_node); 126 | wl_list_remove(&view->link); 127 | wl_list_insert(&server->views, &view->link); 128 | /* Activate the new surface */ 129 | wlr_xdg_toplevel_set_activated(view->xdg_toplevel, true); 130 | /* 131 | * Tell the seat to have the keyboard enter this surface. wlroots will keep 132 | * track of this and automatically send key events to the appropriate 133 | * clients without additional work on your part. 134 | */ 135 | wlr_seat_keyboard_notify_enter(seat, view->xdg_toplevel->base->surface, 136 | keyboard->keycodes, keyboard->num_keycodes, 137 | &keyboard->modifiers); 138 | } 139 | 140 | static void keyboard_handle_modifiers(struct wl_listener *listener, 141 | void *data) { 142 | /* This event is raised when a modifier key, such as shift or alt, is 143 | * pressed. We simply communicate this to the client. */ 144 | struct tinywl_keyboard *keyboard = 145 | wl_container_of(listener, keyboard, modifiers); 146 | /* 147 | * A seat can only have one keyboard, but this is a limitation of the 148 | * Wayland protocol - not wlroots. We assign all connected keyboards to the 149 | * same seat. You can swap out the underlying wlr_keyboard like this and 150 | * wlr_seat handles this transparently. 151 | */ 152 | wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device); 153 | /* Send modifiers to the client. */ 154 | wlr_seat_keyboard_notify_modifiers(keyboard->server->seat, 155 | &keyboard->device->keyboard->modifiers); 156 | } 157 | 158 | static bool handle_keybinding(struct tinywl_server *server, xkb_keysym_t sym) { 159 | /* 160 | * Here we handle compositor keybindings. This is when the compositor is 161 | * processing keys, rather than passing them on to the client for its own 162 | * processing. 163 | * 164 | * This function assumes Alt is held down. 165 | */ 166 | switch (sym) { 167 | case XKB_KEY_Escape: 168 | wl_display_terminate(server->wl_display); 169 | break; 170 | case XKB_KEY_F1: 171 | /* Cycle to the next view */ 172 | if (wl_list_length(&server->views) < 2) { 173 | break; 174 | } 175 | struct tinywl_view *next_view = 176 | wl_container_of(server->views.prev, next_view, link); 177 | focus_view(next_view, next_view->xdg_toplevel->base->surface); 178 | break; 179 | default: 180 | return false; 181 | } 182 | return true; 183 | } 184 | 185 | static void keyboard_handle_key(struct wl_listener *listener, void *data) { 186 | /* This event is raised when a key is pressed or released. */ 187 | struct tinywl_keyboard *keyboard = wl_container_of(listener, keyboard, key); 188 | struct tinywl_server *server = keyboard->server; 189 | struct wlr_event_keyboard_key *event = data; 190 | struct wlr_seat *seat = server->seat; 191 | 192 | /* Translate libinput keycode -> xkbcommon */ 193 | uint32_t keycode = event->keycode + 8; 194 | /* Get a list of keysyms based on the keymap for this keyboard */ 195 | const xkb_keysym_t *syms; 196 | int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, 197 | keycode, &syms); 198 | 199 | bool handled = false; 200 | uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); 201 | if ((modifiers & WLR_MODIFIER_ALT) && 202 | event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { 203 | /* If alt is held down and this button was _pressed_, we attempt to 204 | * process it as a compositor keybinding. */ 205 | for (int i = 0; i < nsyms; i++) { 206 | handled = handle_keybinding(server, syms[i]); 207 | } 208 | } 209 | 210 | if (!handled) { 211 | /* Otherwise, we pass it along to the client. */ 212 | wlr_seat_set_keyboard(seat, keyboard->device); 213 | wlr_seat_keyboard_notify_key(seat, event->time_msec, event->keycode, 214 | event->state); 215 | } 216 | } 217 | 218 | static void server_new_keyboard(struct tinywl_server *server, 219 | struct wlr_input_device *device) { 220 | struct tinywl_keyboard *keyboard = calloc(1, sizeof(struct tinywl_keyboard)); 221 | keyboard->server = server; 222 | keyboard->device = device; 223 | 224 | /* We need to prepare an XKB keymap and assign it to the keyboard. This 225 | * assumes the defaults (e.g. layout = "us"). */ 226 | struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); 227 | struct xkb_keymap *keymap = 228 | xkb_keymap_new_from_names(context, NULL, XKB_KEYMAP_COMPILE_NO_FLAGS); 229 | 230 | wlr_keyboard_set_keymap(device->keyboard, keymap); 231 | xkb_keymap_unref(keymap); 232 | xkb_context_unref(context); 233 | wlr_keyboard_set_repeat_info(device->keyboard, 25, 600); 234 | 235 | /* Here we set up listeners for keyboard events. */ 236 | keyboard->modifiers.notify = keyboard_handle_modifiers; 237 | wl_signal_add(&device->keyboard->events.modifiers, &keyboard->modifiers); 238 | keyboard->key.notify = keyboard_handle_key; 239 | wl_signal_add(&device->keyboard->events.key, &keyboard->key); 240 | 241 | wlr_seat_set_keyboard(server->seat, device); 242 | 243 | /* And add the keyboard to our list of keyboards */ 244 | wl_list_insert(&server->keyboards, &keyboard->link); 245 | } 246 | 247 | static void server_new_pointer(struct tinywl_server *server, 248 | struct wlr_input_device *device) { 249 | /* We don't do anything special with pointers. All of our pointer handling 250 | * is proxied through wlr_cursor. On another compositor, you might take this 251 | * opportunity to do libinput configuration on the device to set 252 | * acceleration, etc. */ 253 | wlr_cursor_attach_input_device(server->cursor, device); 254 | } 255 | 256 | static void server_new_input(struct wl_listener *listener, void *data) { 257 | /* This event is raised by the backend when a new input device becomes 258 | * available. */ 259 | struct tinywl_server *server = wl_container_of(listener, server, new_input); 260 | struct wlr_input_device *device = data; 261 | switch (device->type) { 262 | case WLR_INPUT_DEVICE_KEYBOARD: 263 | server_new_keyboard(server, device); 264 | break; 265 | case WLR_INPUT_DEVICE_POINTER: 266 | server_new_pointer(server, device); 267 | break; 268 | default: 269 | break; 270 | } 271 | /* We need to let the wlr_seat know what our capabilities are, which is 272 | * communiciated to the client. In TinyWL we always have a cursor, even if 273 | * there are no pointer devices, so we always include that capability. */ 274 | uint32_t caps = WL_SEAT_CAPABILITY_POINTER; 275 | if (!wl_list_empty(&server->keyboards)) { 276 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; 277 | } 278 | wlr_seat_set_capabilities(server->seat, caps); 279 | } 280 | 281 | static void seat_request_cursor(struct wl_listener *listener, void *data) { 282 | struct tinywl_server *server = 283 | wl_container_of(listener, server, request_cursor); 284 | /* This event is raised by the seat when a client provides a cursor image */ 285 | struct wlr_seat_pointer_request_set_cursor_event *event = data; 286 | struct wlr_seat_client *focused_client = 287 | server->seat->pointer_state.focused_client; 288 | /* This can be sent by any client, so we check to make sure this one is 289 | * actually has pointer focus first. */ 290 | if (focused_client == event->seat_client) { 291 | /* Once we've vetted the client, we can tell the cursor to use the 292 | * provided surface as the cursor image. It will set the hardware cursor 293 | * on the output that it's currently on and continue to do so as the 294 | * cursor moves between outputs. */ 295 | wlr_cursor_set_surface(server->cursor, event->surface, event->hotspot_x, 296 | event->hotspot_y); 297 | } 298 | } 299 | 300 | static void seat_request_set_selection(struct wl_listener *listener, 301 | void *data) { 302 | /* This event is raised by the seat when a client wants to set the selection, 303 | * usually when the user copies something. wlroots allows compositors to 304 | * ignore such requests if they so choose, but in tinywl we always honor 305 | */ 306 | struct tinywl_server *server = 307 | wl_container_of(listener, server, request_set_selection); 308 | struct wlr_seat_request_set_selection_event *event = data; 309 | wlr_seat_set_selection(server->seat, event->source, event->serial); 310 | } 311 | 312 | static struct tinywl_view *desktop_view_at(struct tinywl_server *server, 313 | double lx, double ly, 314 | struct wlr_surface **surface, 315 | double *sx, double *sy) { 316 | /* This returns the topmost node in the scene at the given layout coords. 317 | * we only care about surface nodes as we are specifically looking for a 318 | * surface in the surface tree of a tinywl_view. */ 319 | struct wlr_scene_node *node = 320 | wlr_scene_node_at(&server->scene->node, lx, ly, sx, sy); 321 | if (node == NULL || node->type != WLR_SCENE_NODE_SURFACE) { 322 | return NULL; 323 | } 324 | *surface = wlr_scene_surface_from_node(node)->surface; 325 | /* Find the node corresponding to the tinywl_view at the root of this 326 | * surface tree, it is the only one for which we set the data field. */ 327 | while (node != NULL && node->data == NULL) { 328 | node = node->parent; 329 | } 330 | return node->data; 331 | } 332 | 333 | static void process_cursor_move(struct tinywl_server *server, uint32_t time) { 334 | /* Move the grabbed view to the new position. */ 335 | struct tinywl_view *view = server->grabbed_view; 336 | view->x = server->cursor->x - server->grab_x; 337 | view->y = server->cursor->y - server->grab_y; 338 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 339 | } 340 | 341 | static void process_cursor_resize(struct tinywl_server *server, uint32_t time) { 342 | /* 343 | * Resizing the grabbed view can be a little bit complicated, because we 344 | * could be resizing from any corner or edge. This not only resizes the view 345 | * on one or two axes, but can also move the view if you resize from the top 346 | * or left edges (or top-left corner). 347 | * 348 | * Note that I took some shortcuts here. In a more fleshed-out compositor, 349 | * you'd wait for the client to prepare a buffer at the new size, then 350 | * commit any movement that was prepared. 351 | */ 352 | struct tinywl_view *view = server->grabbed_view; 353 | double border_x = server->cursor->x - server->grab_x; 354 | double border_y = server->cursor->y - server->grab_y; 355 | int new_left = server->grab_geobox.x; 356 | int new_right = server->grab_geobox.x + server->grab_geobox.width; 357 | int new_top = server->grab_geobox.y; 358 | int new_bottom = server->grab_geobox.y + server->grab_geobox.height; 359 | 360 | if (server->resize_edges & WLR_EDGE_TOP) { 361 | new_top = border_y; 362 | if (new_top >= new_bottom) { 363 | new_top = new_bottom - 1; 364 | } 365 | } else if (server->resize_edges & WLR_EDGE_BOTTOM) { 366 | new_bottom = border_y; 367 | if (new_bottom <= new_top) { 368 | new_bottom = new_top + 1; 369 | } 370 | } 371 | if (server->resize_edges & WLR_EDGE_LEFT) { 372 | new_left = border_x; 373 | if (new_left >= new_right) { 374 | new_left = new_right - 1; 375 | } 376 | } else if (server->resize_edges & WLR_EDGE_RIGHT) { 377 | new_right = border_x; 378 | if (new_right <= new_left) { 379 | new_right = new_left + 1; 380 | } 381 | } 382 | 383 | struct wlr_box geo_box; 384 | wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box); 385 | view->x = new_left - geo_box.x; 386 | view->y = new_top - geo_box.y; 387 | wlr_scene_node_set_position(view->scene_node, view->x, view->y); 388 | 389 | int new_width = new_right - new_left; 390 | int new_height = new_bottom - new_top; 391 | wlr_xdg_toplevel_set_size(view->xdg_toplevel, new_width, new_height); 392 | } 393 | 394 | static void process_cursor_motion(struct tinywl_server *server, uint32_t time) { 395 | /* If the mode is non-passthrough, delegate to those functions. */ 396 | if (server->cursor_mode == TINYWL_CURSOR_MOVE) { 397 | process_cursor_move(server, time); 398 | return; 399 | } else if (server->cursor_mode == TINYWL_CURSOR_RESIZE) { 400 | process_cursor_resize(server, time); 401 | return; 402 | } 403 | 404 | /* Otherwise, find the view under the pointer and send the event along. */ 405 | double sx, sy; 406 | struct wlr_seat *seat = server->seat; 407 | struct wlr_surface *surface = NULL; 408 | struct tinywl_view *view = desktop_view_at( 409 | server, server->cursor->x, server->cursor->y, &surface, &sx, &sy); 410 | if (!view) { 411 | /* If there's no view under the cursor, set the cursor image to a 412 | * default. This is what makes the cursor image appear when you move it 413 | * around the screen, not over any views. */ 414 | wlr_xcursor_manager_set_cursor_image(server->cursor_mgr, "left_ptr", 415 | server->cursor); 416 | } 417 | if (surface) { 418 | /* 419 | * Send pointer enter and motion events. 420 | * 421 | * The enter event gives the surface "pointer focus", which is distinct 422 | * from keyboard focus. You get pointer focus by moving the pointer over 423 | * a window. 424 | * 425 | * Note that wlroots will avoid sending duplicate enter/motion events if 426 | * the surface has already has pointer focus or if the client is already 427 | * aware of the coordinates passed. 428 | */ 429 | wlr_seat_pointer_notify_enter(seat, surface, sx, sy); 430 | wlr_seat_pointer_notify_motion(seat, time, sx, sy); 431 | } else { 432 | /* Clear pointer focus so future button events and such are not sent to 433 | * the last client to have the cursor over it. */ 434 | wlr_seat_pointer_clear_focus(seat); 435 | } 436 | } 437 | 438 | static void server_cursor_motion(struct wl_listener *listener, void *data) { 439 | /* This event is forwarded by the cursor when a pointer emits a _relative_ 440 | * pointer motion event (i.e. a delta) */ 441 | struct tinywl_server *server = 442 | wl_container_of(listener, server, cursor_motion); 443 | struct wlr_event_pointer_motion *event = data; 444 | /* The cursor doesn't move unless we tell it to. The cursor automatically 445 | * handles constraining the motion to the output layout, as well as any 446 | * special configuration applied for the specific input device which 447 | * generated the event. You can pass NULL for the device if you want to move 448 | * the cursor around without any input. */ 449 | wlr_cursor_move(server->cursor, event->device, event->delta_x, 450 | event->delta_y); 451 | process_cursor_motion(server, event->time_msec); 452 | } 453 | 454 | static void server_cursor_motion_absolute(struct wl_listener *listener, 455 | void *data) { 456 | /* This event is forwarded by the cursor when a pointer emits an _absolute_ 457 | * motion event, from 0..1 on each axis. This happens, for example, when 458 | * wlroots is running under a Wayland window rather than KMS+DRM, and you 459 | * move the mouse over the window. You could enter the window from any edge, 460 | * so we have to warp the mouse there. There is also some hardware which 461 | * emits these events. */ 462 | struct tinywl_server *server = 463 | wl_container_of(listener, server, cursor_motion_absolute); 464 | struct wlr_event_pointer_motion_absolute *event = data; 465 | wlr_cursor_warp_absolute(server->cursor, event->device, event->x, event->y); 466 | process_cursor_motion(server, event->time_msec); 467 | } 468 | 469 | static void server_cursor_button(struct wl_listener *listener, void *data) { 470 | /* This event is forwarded by the cursor when a pointer emits a button 471 | * event. */ 472 | struct tinywl_server *server = 473 | wl_container_of(listener, server, cursor_button); 474 | struct wlr_event_pointer_button *event = data; 475 | /* Notify the client with pointer focus that a button press has occurred */ 476 | wlr_seat_pointer_notify_button(server->seat, event->time_msec, event->button, 477 | event->state); 478 | double sx, sy; 479 | struct wlr_surface *surface = NULL; 480 | struct tinywl_view *view = desktop_view_at( 481 | server, server->cursor->x, server->cursor->y, &surface, &sx, &sy); 482 | if (event->state == WLR_BUTTON_RELEASED) { 483 | /* If you released any buttons, we exit interactive move/resize mode. */ 484 | server->cursor_mode = TINYWL_CURSOR_PASSTHROUGH; 485 | } else { 486 | /* Focus that client if the button was _pressed_ */ 487 | focus_view(view, surface); 488 | } 489 | } 490 | 491 | static void server_cursor_axis(struct wl_listener *listener, void *data) { 492 | /* This event is forwarded by the cursor when a pointer emits an axis event, 493 | * for example when you move the scroll wheel. */ 494 | struct tinywl_server *server = wl_container_of(listener, server, cursor_axis); 495 | struct wlr_event_pointer_axis *event = data; 496 | /* Notify the client with pointer focus of the axis event. */ 497 | wlr_seat_pointer_notify_axis(server->seat, event->time_msec, 498 | event->orientation, event->delta, 499 | event->delta_discrete, event->source); 500 | } 501 | 502 | static void server_cursor_frame(struct wl_listener *listener, void *data) { 503 | /* This event is forwarded by the cursor when a pointer emits an frame 504 | * event. Frame events are sent after regular pointer events to group 505 | * multiple events together. For instance, two axis events may happen at the 506 | * same time, in which case a frame event won't be sent in between. */ 507 | struct tinywl_server *server = 508 | wl_container_of(listener, server, cursor_frame); 509 | /* Notify the client with pointer focus of the frame event. */ 510 | wlr_seat_pointer_notify_frame(server->seat); 511 | } 512 | 513 | static void output_frame(struct wl_listener *listener, void *data) { 514 | /* This function is called every time an output is ready to display a frame, 515 | * generally at the output's refresh rate (e.g. 60Hz). */ 516 | struct tinywl_output *output = wl_container_of(listener, output, frame); 517 | struct wlr_scene *scene = output->server->scene; 518 | 519 | struct wlr_scene_output *scene_output = 520 | wlr_scene_get_scene_output(scene, output->wlr_output); 521 | 522 | /* Render the scene if needed and commit the output */ 523 | wlr_scene_output_commit(scene_output); 524 | 525 | struct timespec now; 526 | clock_gettime(CLOCK_MONOTONIC, &now); 527 | wlr_scene_output_send_frame_done(scene_output, &now); 528 | } 529 | 530 | static void server_new_output(struct wl_listener *listener, void *data) { 531 | /* This event is raised by the backend when a new output (aka a display or 532 | * monitor) becomes available. */ 533 | struct tinywl_server *server = wl_container_of(listener, server, new_output); 534 | struct wlr_output *wlr_output = data; 535 | 536 | wlr_output->width = 1920; 537 | wlr_output->height = 1080; 538 | 539 | /* Configures the output created by the backend to use our allocator 540 | * and our renderer. Must be done once, before commiting the output */ 541 | wlr_output_init_render(wlr_output, server->allocator, server->renderer); 542 | 543 | /* Some backends don't have modes. DRM+KMS does, and we need to set a mode 544 | * before we can use the output. The mode is a tuple of (width, height, 545 | * refresh rate), and each monitor supports only a specific set of modes. We 546 | * just pick the monitor's preferred mode, a more sophisticated compositor 547 | * would let the user configure it. */ 548 | wlr_log(WLR_INFO, "%d output modes", wl_list_length(&wlr_output->modes)); 549 | if (!wl_list_empty(&wlr_output->modes)) { 550 | struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output); 551 | wlr_log(WLR_INFO, "mode: %ux%u:%umHZ, %s", mode->width, mode->height, 552 | mode->refresh, mode->preferred ? "preferred" : "not preferred"); 553 | wlr_output_set_mode(wlr_output, mode); 554 | wlr_output_enable(wlr_output, true); 555 | if (!wlr_output_commit(wlr_output)) { 556 | return; 557 | } 558 | } 559 | 560 | wlr_log(WLR_INFO, "Output is %ux%u:%umHZ", wlr_output->width, 561 | wlr_output->height, wlr_output->refresh); 562 | 563 | /* Allocates and configures our state for this output */ 564 | struct tinywl_output *output = calloc(1, sizeof(struct tinywl_output)); 565 | output->wlr_output = wlr_output; 566 | output->server = server; 567 | /* Sets up a listener for the frame notify event. */ 568 | output->frame.notify = output_frame; 569 | wl_signal_add(&wlr_output->events.frame, &output->frame); 570 | wl_list_insert(&server->outputs, &output->link); 571 | 572 | /* Adds this to the output layout. The add_auto function arranges outputs 573 | * from left-to-right in the order they appear. A more sophisticated 574 | * compositor would let the user configure the arrangement of outputs in the 575 | * layout. 576 | * 577 | * The output layout utility automatically adds a wl_output global to the 578 | * display, which Wayland clients can see to find out information about the 579 | * output (such as DPI, scale factor, manufacturer, etc). 580 | */ 581 | wlr_output_layout_add_auto(server->output_layout, wlr_output); 582 | } 583 | 584 | static void xdg_toplevel_map(struct wl_listener *listener, void *data) { 585 | /* Called when the surface is mapped, or ready to display on-screen. */ 586 | struct tinywl_view *view = wl_container_of(listener, view, map); 587 | 588 | wl_list_insert(&view->server->views, &view->link); 589 | 590 | focus_view(view, view->xdg_toplevel->base->surface); 591 | } 592 | 593 | static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) { 594 | /* Called when the surface is unmapped, and should no longer be shown. */ 595 | struct tinywl_view *view = wl_container_of(listener, view, unmap); 596 | 597 | wl_list_remove(&view->link); 598 | } 599 | 600 | static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) { 601 | /* Called when the surface is destroyed and should never be shown again. */ 602 | struct tinywl_view *view = wl_container_of(listener, view, destroy); 603 | 604 | wl_list_remove(&view->map.link); 605 | wl_list_remove(&view->unmap.link); 606 | wl_list_remove(&view->destroy.link); 607 | wl_list_remove(&view->request_move.link); 608 | wl_list_remove(&view->request_resize.link); 609 | 610 | free(view); 611 | } 612 | 613 | static void begin_interactive(struct tinywl_view *view, 614 | enum tinywl_cursor_mode mode, uint32_t edges) { 615 | /* This function sets up an interactive move or resize operation, where the 616 | * compositor stops propegating pointer events to clients and instead 617 | * consumes them itself, to move or resize windows. */ 618 | struct tinywl_server *server = view->server; 619 | struct wlr_surface *focused_surface = 620 | server->seat->pointer_state.focused_surface; 621 | if (view->xdg_toplevel->base->surface != 622 | wlr_surface_get_root_surface(focused_surface)) { 623 | /* Deny move/resize requests from unfocused clients. */ 624 | return; 625 | } 626 | server->grabbed_view = view; 627 | server->cursor_mode = mode; 628 | 629 | if (mode == TINYWL_CURSOR_MOVE) { 630 | server->grab_x = server->cursor->x - view->x; 631 | server->grab_y = server->cursor->y - view->y; 632 | } else { 633 | struct wlr_box geo_box; 634 | wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box); 635 | 636 | double border_x = 637 | (view->x + geo_box.x) + ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0); 638 | double border_y = (view->y + geo_box.y) + 639 | ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0); 640 | server->grab_x = server->cursor->x - border_x; 641 | server->grab_y = server->cursor->y - border_y; 642 | 643 | server->grab_geobox = geo_box; 644 | server->grab_geobox.x += view->x; 645 | server->grab_geobox.y += view->y; 646 | 647 | server->resize_edges = edges; 648 | } 649 | } 650 | 651 | static void xdg_toplevel_request_move(struct wl_listener *listener, 652 | void *data) { 653 | /* This event is raised when a client would like to begin an interactive 654 | * move, typically because the user clicked on their client-side 655 | * decorations. Note that a more sophisticated compositor should check the 656 | * provided serial against a list of button press serials sent to this 657 | * client, to prevent the client from requesting this whenever they want. */ 658 | struct tinywl_view *view = wl_container_of(listener, view, request_move); 659 | begin_interactive(view, TINYWL_CURSOR_MOVE, 0); 660 | } 661 | 662 | static void xdg_toplevel_request_resize(struct wl_listener *listener, 663 | void *data) { 664 | /* This event is raised when a client would like to begin an interactive 665 | * resize, typically because the user clicked on their client-side 666 | * decorations. Note that a more sophisticated compositor should check the 667 | * provided serial against a list of button press serials sent to this 668 | * client, to prevent the client from requesting this whenever they want. */ 669 | struct wlr_xdg_toplevel_resize_event *event = data; 670 | struct tinywl_view *view = wl_container_of(listener, view, request_resize); 671 | begin_interactive(view, TINYWL_CURSOR_RESIZE, event->edges); 672 | } 673 | 674 | static void server_new_xdg_surface(struct wl_listener *listener, void *data) { 675 | /* This event is raised when wlr_xdg_shell receives a new xdg surface from a 676 | * client, either a toplevel (application window) or popup. */ 677 | struct tinywl_server *server = 678 | wl_container_of(listener, server, new_xdg_surface); 679 | struct wlr_xdg_surface *xdg_surface = data; 680 | 681 | /* We must add xdg popups to the scene graph so they get rendered. The 682 | * wlroots scene graph provides a helper for this, but to use it we must 683 | * provide the proper parent scene node of the xdg popup. To enable this, 684 | * we always set the user data field of xdg_surfaces to the corresponding 685 | * scene node. */ 686 | if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { 687 | struct wlr_xdg_surface *parent = 688 | wlr_xdg_surface_from_wlr_surface(xdg_surface->popup->parent); 689 | struct wlr_scene_node *parent_node = parent->data; 690 | xdg_surface->data = wlr_scene_xdg_surface_create(parent_node, xdg_surface); 691 | return; 692 | } 693 | assert(xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); 694 | 695 | /* Allocate a tinywl_view for this surface */ 696 | struct tinywl_view *view = calloc(1, sizeof(struct tinywl_view)); 697 | view->server = server; 698 | view->xdg_toplevel = xdg_surface->toplevel; 699 | view->scene_node = wlr_scene_xdg_surface_create(&view->server->scene->node, 700 | view->xdg_toplevel->base); 701 | view->scene_node->data = view; 702 | xdg_surface->data = view->scene_node; 703 | 704 | /* Listen to the various events it can emit */ 705 | view->map.notify = xdg_toplevel_map; 706 | wl_signal_add(&xdg_surface->events.map, &view->map); 707 | view->unmap.notify = xdg_toplevel_unmap; 708 | wl_signal_add(&xdg_surface->events.unmap, &view->unmap); 709 | view->destroy.notify = xdg_toplevel_destroy; 710 | wl_signal_add(&xdg_surface->events.destroy, &view->destroy); 711 | 712 | /* cotd */ 713 | struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel; 714 | view->request_move.notify = xdg_toplevel_request_move; 715 | wl_signal_add(&toplevel->events.request_move, &view->request_move); 716 | view->request_resize.notify = xdg_toplevel_request_resize; 717 | wl_signal_add(&toplevel->events.request_resize, &view->request_resize); 718 | } 719 | 720 | int main(int argc, char *argv[]) { 721 | wlr_log_init(WLR_DEBUG, NULL); 722 | char *startup_cmd = NULL; 723 | 724 | int c; 725 | while ((c = getopt(argc, argv, "s:h")) != -1) { 726 | switch (c) { 727 | case 's': 728 | startup_cmd = optarg; 729 | break; 730 | default: 731 | printf("Usage: %s [-s startup command]\n", argv[0]); 732 | return 0; 733 | } 734 | } 735 | if (optind < argc) { 736 | printf("Usage: %s [-s startup command]\n", argv[0]); 737 | return 0; 738 | } 739 | 740 | struct tinywl_server server; 741 | /* The Wayland display is managed by libwayland. It handles accepting 742 | * clients from the Unix socket, manging Wayland globals, and so on. */ 743 | server.wl_display = wl_display_create(); 744 | /* The backend is a wlroots feature which abstracts the underlying input and 745 | * output hardware. The autocreate option will choose the most suitable 746 | * backend based on the current environment, such as opening an X11 window 747 | * if an X11 server is running. */ 748 | server.backend = wlr_backend_autocreate(server.wl_display); 749 | 750 | /* Autocreates a renderer, either Pixman, GLES2 or Vulkan for us. The user 751 | * can also specify a renderer using the WLR_RENDERER env var. 752 | * The renderer is responsible for defining the various pixel formats it 753 | * supports for shared memory, this configures that for clients. */ 754 | server.renderer = wlr_renderer_autocreate(server.backend); 755 | wlr_renderer_init_wl_display(server.renderer, server.wl_display); 756 | 757 | /* Autocreates an allocator for us. 758 | * The allocator is the bridge between the renderer and the backend. It 759 | * handles the buffer creation, allowing wlroots to render onto the 760 | * screen */ 761 | server.allocator = wlr_allocator_autocreate(server.backend, server.renderer); 762 | 763 | /* This creates some hands-off wlroots interfaces. The compositor is 764 | * necessary for clients to allocate surfaces, the subcompositor allows to 765 | * assign the role of subsurfaces to surfaces and the data device manager 766 | * handles the clipboard. Each of these wlroots interfaces has room for you 767 | * to dig your fingers in and play with their behavior if you want. Note that 768 | * the clients cannot set the selection directly without compositor approval, 769 | * see the handling of the request_set_selection event below.*/ 770 | wlr_compositor_create(server.wl_display, server.renderer); 771 | wlr_subcompositor_create(server.wl_display); 772 | wlr_data_device_manager_create(server.wl_display); 773 | 774 | /* Creates an output layout, which a wlroots utility for working with an 775 | * arrangement of screens in a physical layout. */ 776 | server.output_layout = wlr_output_layout_create(); 777 | 778 | /* Configure a listener to be notified when new outputs are available on the 779 | * backend. */ 780 | wl_list_init(&server.outputs); 781 | server.new_output.notify = server_new_output; 782 | wl_signal_add(&server.backend->events.new_output, &server.new_output); 783 | 784 | /* Create a scene graph. This is a wlroots abstraction that handles all 785 | * rendering and damage tracking. All the compositor author needs to do 786 | * is add things that should be rendered to the scene graph at the proper 787 | * positions and then call wlr_scene_output_commit() to render a frame if 788 | * necessary. 789 | */ 790 | server.scene = wlr_scene_create(); 791 | wlr_scene_attach_output_layout(server.scene, server.output_layout); 792 | 793 | /* Set up the xdg-shell. The xdg-shell is a Wayland protocol which is used 794 | * for application windows. For more detail on shells, refer to my article: 795 | * 796 | * https://drewdevault.com/2018/07/29/Wayland-shells.html 797 | */ 798 | wl_list_init(&server.views); 799 | server.xdg_shell = wlr_xdg_shell_create(server.wl_display); 800 | server.new_xdg_surface.notify = server_new_xdg_surface; 801 | wl_signal_add(&server.xdg_shell->events.new_surface, &server.new_xdg_surface); 802 | 803 | /* 804 | * Creates a cursor, which is a wlroots utility for tracking the cursor 805 | * image shown on screen. 806 | */ 807 | server.cursor = wlr_cursor_create(); 808 | wlr_cursor_attach_output_layout(server.cursor, server.output_layout); 809 | 810 | /* Creates an xcursor manager, another wlroots utility which loads up 811 | * Xcursor themes to source cursor images from and makes sure that cursor 812 | * images are available at all scale factors on the screen (necessary for 813 | * HiDPI support). We add a cursor theme at scale factor 1 to begin with. */ 814 | server.cursor_mgr = wlr_xcursor_manager_create(NULL, 24); 815 | wlr_xcursor_manager_load(server.cursor_mgr, 1); 816 | 817 | /* 818 | * wlr_cursor *only* displays an image on screen. It does not move around 819 | * when the pointer moves. However, we can attach input devices to it, and 820 | * it will generate aggregate events for all of them. In these events, we 821 | * can choose how we want to process them, forwarding them to clients and 822 | * moving the cursor around. More detail on this process is described in my 823 | * input handling blog post: 824 | * 825 | * https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html 826 | * 827 | * And more comments are sprinkled throughout the notify functions above. 828 | */ 829 | server.cursor_motion.notify = server_cursor_motion; 830 | wl_signal_add(&server.cursor->events.motion, &server.cursor_motion); 831 | server.cursor_motion_absolute.notify = server_cursor_motion_absolute; 832 | wl_signal_add(&server.cursor->events.motion_absolute, 833 | &server.cursor_motion_absolute); 834 | server.cursor_button.notify = server_cursor_button; 835 | wl_signal_add(&server.cursor->events.button, &server.cursor_button); 836 | server.cursor_axis.notify = server_cursor_axis; 837 | wl_signal_add(&server.cursor->events.axis, &server.cursor_axis); 838 | server.cursor_frame.notify = server_cursor_frame; 839 | wl_signal_add(&server.cursor->events.frame, &server.cursor_frame); 840 | 841 | /* 842 | * Configures a seat, which is a single "seat" at which a user sits and 843 | * operates the computer. This conceptually includes up to one keyboard, 844 | * pointer, touch, and drawing tablet device. We also rig up a listener to 845 | * let us know when new input devices are available on the backend. 846 | */ 847 | wl_list_init(&server.keyboards); 848 | server.new_input.notify = server_new_input; 849 | wl_signal_add(&server.backend->events.new_input, &server.new_input); 850 | server.seat = wlr_seat_create(server.wl_display, "seat0"); 851 | server.request_cursor.notify = seat_request_cursor; 852 | wl_signal_add(&server.seat->events.request_set_cursor, 853 | &server.request_cursor); 854 | server.request_set_selection.notify = seat_request_set_selection; 855 | wl_signal_add(&server.seat->events.request_set_selection, 856 | &server.request_set_selection); 857 | 858 | /* Add a Unix socket to the Wayland display. */ 859 | const char *socket = wl_display_add_socket_auto(server.wl_display); 860 | if (!socket) { 861 | wlr_backend_destroy(server.backend); 862 | return 1; 863 | } 864 | 865 | /* Start the backend. This will enumerate outputs and inputs, become the DRM 866 | * master, etc */ 867 | if (!wlr_backend_start(server.backend)) { 868 | wlr_backend_destroy(server.backend); 869 | wl_display_destroy(server.wl_display); 870 | return 1; 871 | } 872 | 873 | /* Set the WAYLAND_DISPLAY environment variable to our socket and run the 874 | * startup command if requested. */ 875 | setenv("WAYLAND_DISPLAY", socket, true); 876 | if (startup_cmd) { 877 | if (fork() == 0) { 878 | execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL); 879 | } 880 | } 881 | /* Run the Wayland event loop. This does not return until you exit the 882 | * compositor. Starting the backend rigged up all of the necessary event 883 | * loop configuration to listen to libinput events, DRM events, generate 884 | * frame events at the refresh rate, and so on. */ 885 | wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s", socket); 886 | wl_display_run(server.wl_display); 887 | 888 | /* Once wl_display_run returns, we shut down the server. */ 889 | wl_display_destroy_clients(server.wl_display); 890 | wl_display_destroy(server.wl_display); 891 | return 0; 892 | } 893 | --------------------------------------------------------------------------------