├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── include └── vimway.h ├── meson.build └── src ├── main.c └── vimway.c /.gitignore: -------------------------------------------------------------------------------- 1 | obj/* 2 | build/* 3 | tags 4 | include/libnvim.a 5 | .vscode/ 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third-party/neovim"] 2 | path = third-party/neovim 3 | url = https://github.com/joalon/neovim 4 | [submodule "third-party/wlroots"] 5 | path = third-party/wlroots 6 | url = https://github.com/swaywm/wlroots 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Joakim Lönnegren 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Mostly a formality for building libnvim. TODO: Remove or make it useful 2 | CC = gcc 3 | 4 | .PHONY: libnvim 5 | 6 | clean: 7 | rm include/libnvim.a 8 | 9 | libnvim: 10 | cd third-party/neovim; make libnvim; cp build/lib/libnvim.a ../../include/ 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Just another wayland compositor 2 | 3 | Small experiment, trying to get a wayland compositor up and running with the neovim expression engine. 4 | 5 | Used lots of good tutorials: 6 | 7 | * https://drewdevault.com/2018/02/17/Writing-a-Wayland-compositor-1.html 8 | 9 | * https://gist.github.com/SirCmpwn/ae4d1cdcca97ffeb2c35f0878d75dc17 10 | 11 | * https://people.freedesktop.org/%7Ewhot/wayland-doxygen/wayland/Server/structwl__listener.html 12 | 13 | ## Dependencies 14 | 15 | * meson 16 | * ninja 17 | 18 | * lua 19 | * wayland-server 20 | * wlroots 21 | * xkbcommon 22 | * pthreads 23 | * libuv 24 | * msgpack-c 25 | * unibilium 26 | * libtermkey 27 | * vterm 28 | * libutil-linux 29 | 30 | ## Build 31 | Not really usable yet but to run it: 32 | 33 | ``` 34 | git clone https://github.com/joalon/vimway; \ 35 | cd vimway; \ 36 | git submodule init; \ 37 | git submodule update; \ 38 | make libnvim; \ 39 | mkdir build; \ 40 | meson build; \ 41 | cd build; \ 42 | ninja 43 | ``` 44 | 45 | Make sure to run under another wayland compositor! Doesn't handle input yet so you can't interrupt it/switch to another tty. 46 | -------------------------------------------------------------------------------- /include/vimway.h: -------------------------------------------------------------------------------- 1 | #ifndef _VIMWAY_H 2 | #define _VIMWAY_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | struct vw_server { 9 | struct wl_display *wl_display; 10 | struct wl_event_loop *wl_event_loop; 11 | 12 | struct wlr_backend *backend; 13 | struct wlr_compositor *compositor; 14 | 15 | struct wl_listener new_output; 16 | struct wl_list outputs; 17 | 18 | struct wl_listener new_input; 19 | struct wl_list keyboards; 20 | 21 | struct wlr_seat *seat; 22 | }; 23 | 24 | struct vw_input { 25 | struct wlr_input *wlr_input; 26 | }; 27 | 28 | struct vw_output { 29 | struct wlr_output *wlr_output; 30 | struct vw_server *server; 31 | struct timespec last_frame; 32 | 33 | struct wl_listener destroy; 34 | struct wl_listener frame; 35 | 36 | struct wl_list link; 37 | }; 38 | 39 | void keyboard_handle_key(struct wl_listener *listener, void *data); 40 | 41 | void keyboard_handle_modifiers(struct wl_listener *listener, void *data); 42 | 43 | void new_input_notify(struct wl_listener *listener, void *data); 44 | 45 | void output_destroy_notify(struct wl_listener *listener, void *data); 46 | 47 | void output_frame_notify(struct wl_listener *listener, void *data); 48 | 49 | void new_output_notify(struct wl_listener *listener, void *data); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('vimway', 'c') 2 | 3 | cc = meson.get_compiler('c') 4 | maths_dep = cc.find_library('m', required : true) 5 | util_dep = cc.find_library('util', required : true) 6 | 7 | deps = [ 8 | dependency('pixman-1'), 9 | dependency('wayland-server'), 10 | dependency('wlroots'), 11 | dependency('xkbcommon'), 12 | dependency('threads'), 13 | dependency('libuv'), 14 | dependency('msgpack'), 15 | dependency('unibilium'), 16 | dependency('termkey'), 17 | dependency('lua'), 18 | dependency('luajit'), 19 | dependency('vterm'), 20 | maths_dep, 21 | util_dep 22 | ] 23 | 24 | include_dirs= [ 25 | include_directories('include/'), 26 | include_directories('third-party/neovim/src/') 27 | ] 28 | 29 | executable('vimway', ['src/main.c', 'src/vimway.c', 'include/vimway.h'], dependencies: deps, objects: 'include/libnvim.a', c_args: '-DWLR_USE_UNSTABLE', include_directories: include_dirs) 30 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "vimway.h" 11 | 12 | int main(int argc, char* argv[]) { 13 | struct vw_server server; 14 | 15 | server.wl_display = wl_display_create(); 16 | assert(server.wl_display); 17 | server.wl_event_loop = wl_display_get_event_loop(server.wl_display); 18 | assert(server.wl_event_loop); 19 | 20 | server.backend = wlr_backend_autocreate(server.wl_display, NULL); 21 | assert(server.backend); 22 | 23 | // Create output callbacks 24 | wl_list_init(&server.outputs); 25 | server.new_output.notify = new_output_notify; 26 | wl_signal_add(&server.backend->events.new_output, &server.new_output); 27 | 28 | // Wire up new input callbacks 29 | wl_list_init(&server.keyboards); 30 | server.new_input.notify = new_input_notify; 31 | wl_signal_add(&server.backend->events.new_input, &server.new_input); 32 | 33 | server.seat = wlr_seat_create(server.wl_display, "seat0"); 34 | assert(server.seat); 35 | 36 | const char *socket = wl_display_add_socket_auto(server.wl_display); 37 | assert(socket); 38 | 39 | if (!wlr_backend_start(server.backend)) { 40 | fprintf(stderr, "Failed to start backend\n"); 41 | wl_display_destroy(server.wl_display); 42 | return 1; 43 | } 44 | 45 | printf("Running compositor on wayland display '%s'\n", socket); 46 | setenv("WAYLAND_DISPLAY", socket, true); 47 | wl_display_init_shm(server.wl_display); 48 | server.compositor = wlr_compositor_create(server.wl_display, wlr_backend_get_renderer(server.backend)); 49 | 50 | printf("Creating xdg_shell\n"); 51 | wlr_xdg_shell_v6_create(server.wl_display); 52 | 53 | printf("Starting display\n"); 54 | wl_display_run(server.wl_display); 55 | 56 | printf("Stopping vimway\n"); 57 | wl_display_destroy_clients(server.wl_display); 58 | wl_display_destroy(server.wl_display); 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/vimway.c: -------------------------------------------------------------------------------- 1 | #include 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 | 17 | #include "vimway.h" 18 | 19 | extern Array nvim_get_api_info(uint64_t channel_id); 20 | 21 | 22 | void keyboard_handle_key(struct wl_listener *listener, void *data) { 23 | struct wlr_event_keyboard_key *event = data; 24 | struct wlr_input_device *device = wl_container_of(listener, device, keyboard); 25 | struct vw_server *server = wl_container_of(listener, server, new_input); 26 | 27 | uint32_t modifiers = wlr_keyboard_get_modifiers(device->keyboard); 28 | if ((modifiers & WLR_MODIFIER_CTRL) && event->state == WLR_BUTTON_PRESSED) { 29 | printf("Ctrl was pressed. Modifers are: %d, wlr_modifier_alt is: %d\n", modifiers, WLR_MODIFIER_ALT); 30 | wl_display_terminate(server->wl_display); 31 | } 32 | 33 | //Array result = nvim_get_api_info(1); 34 | //printf("nvim_get_api_info result size is: %d\n", result.size); 35 | } 36 | 37 | void keyboard_handle_modifiers(struct wl_listener *listener, void *data) { 38 | printf("Handling keyboard modifiers\n"); 39 | } 40 | 41 | void new_input_notify(struct wl_listener *listener, void *data) { 42 | struct vw_server *server = wl_container_of(listener, server, new_input); 43 | struct wlr_input_device *wlr_input_device = data; 44 | printf("In new_input_notify!\n"); 45 | 46 | struct wlr_keyboard *keyboard; 47 | // struct wlr_pointer *pointer; 48 | 49 | if (wlr_input_device->type == WLR_INPUT_DEVICE_KEYBOARD) { 50 | printf("Got a keyboard!\n"); 51 | keyboard = wlr_input_device->keyboard; 52 | printf("num_keycodes: %d\n", keyboard->num_keycodes); 53 | 54 | // Create a new xkb keymap 55 | struct xkb_rule_names rules = { 0 }; 56 | struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); 57 | struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); 58 | 59 | // Wire up keyboard 60 | wlr_keyboard_set_keymap(keyboard, keymap); 61 | xkb_keymap_unref(keymap); 62 | xkb_context_unref(context); 63 | wlr_keyboard_set_repeat_info(keyboard, 25, 600); 64 | 65 | 66 | // Set callbacks for keyboard events 67 | // keyboard->modifiers.notify = keyboard_handle_modifiers; 68 | // wl_signal_add(&keyboard->events.modifiers, NULL); 69 | // keyboard->key.notify = keyboard_handle_key; 70 | 71 | struct wl_listener *new_listener = calloc(1, sizeof(struct wl_listener)); 72 | new_listener->notify = keyboard_handle_key; 73 | wl_signal_add(&wlr_input_device->keyboard->events.key, new_listener); 74 | 75 | wlr_seat_set_keyboard(server->seat, wlr_input_device); 76 | 77 | wlr_seat_set_capabilities(server->seat, server->seat->capabilities |= WL_SEAT_CAPABILITY_KEYBOARD); 78 | 79 | 80 | } else if (wlr_input_device->type == WLR_INPUT_DEVICE_POINTER) { 81 | printf("Got a pointer!\n"); 82 | // pointer = wlr_input_device->pointer; 83 | } 84 | 85 | return; 86 | } 87 | 88 | void output_destroy_notify(struct wl_listener *listener, void *data) { 89 | struct vw_output *output = wl_container_of(listener, output, destroy); 90 | wl_list_remove(&output->link); 91 | wl_list_remove(&output->destroy.link); 92 | wl_list_remove(&output->frame.link); 93 | free(output); 94 | } 95 | 96 | void output_frame_notify(struct wl_listener *listener, void *data) { 97 | struct vw_output *output = wl_container_of(listener, output, frame); 98 | struct vw_server *server = output->server; 99 | struct wlr_output *wlr_output = data; 100 | struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); 101 | 102 | struct timespec now; 103 | clock_gettime(CLOCK_MONOTONIC, &now); 104 | 105 | wlr_output_attach_render(wlr_output, NULL); 106 | wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); 107 | 108 | float color[4] = { 1.0f, 0.2f, 0.2f, 1.0f }; 109 | wlr_renderer_clear(renderer, color); 110 | 111 | struct wl_resource *_surface; 112 | wl_resource_for_each(_surface, &server->compositor->surface_resources) { 113 | struct wlr_surface *surface = wlr_surface_from_resource(_surface); 114 | if (!wlr_surface_has_buffer(surface)) { 115 | continue; 116 | } 117 | 118 | struct wlr_box render_box = { 119 | .x = 20, .y = 20, 120 | .width = surface->current.width, 121 | .height = surface->current.height 122 | }; 123 | float matrix[16]; 124 | 125 | wlr_render_rect(renderer, &render_box, color, wlr_output->transform_matrix); 126 | wlr_surface_send_frame_done(surface, &now); 127 | } 128 | 129 | //wlr_output_swap_buffers(wlr_output, NULL, NULL); 130 | //wlr_output_set_damage(wlr_output, NULL); 131 | wlr_output_commit(wlr_output); 132 | 133 | wlr_renderer_end(renderer); 134 | } 135 | 136 | void new_output_notify(struct wl_listener *listener, void *data) { 137 | struct vw_server *server = wl_container_of(listener, server, new_output); 138 | struct wlr_output *wlr_output = data; 139 | 140 | printf("In new_output_notify\n"); 141 | 142 | if (!wl_list_empty(&wlr_output->modes)) { 143 | struct wlr_output_mode *mode = wl_container_of(wlr_output->modes.prev, mode, link); 144 | wlr_output_set_mode(wlr_output, mode); 145 | } 146 | 147 | struct vw_output *output = calloc(1, sizeof(struct vw_output)); 148 | clock_gettime(CLOCK_MONOTONIC, &output->last_frame); 149 | output->server = server; 150 | output-> wlr_output = wlr_output; 151 | wl_list_insert(&server->outputs, &output->link); 152 | 153 | output->destroy.notify = output_destroy_notify; 154 | wl_signal_add(&wlr_output->events.destroy, &output->destroy); 155 | 156 | output->frame.notify = output_frame_notify; 157 | wl_signal_add(&wlr_output->events.frame, &output->frame); 158 | 159 | wlr_output_create_global(wlr_output); 160 | } 161 | 162 | --------------------------------------------------------------------------------