/* Copyright (c), Niclas Meyer * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "server.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "luak/luak.h" bool server_init(struct kiwmi_server *server, char *config_path) { wlr_log(WLR_DEBUG, "Initializing Wayland server"); server->wl_display = wl_display_create(); if (!server->wl_display) { wlr_log(WLR_ERROR, "Failed to create display"); return false; } server->wl_event_loop = wl_display_get_event_loop(server->wl_display); server->backend = wlr_backend_autocreate(server->wl_display); if (!server->backend) { wlr_log(WLR_ERROR, "Failed to create backend"); wl_display_destroy(server->wl_display); return false; } server->renderer = wlr_renderer_autocreate(server->backend); wlr_renderer_init_wl_display(server->renderer, server->wl_display); server->allocator = wlr_allocator_autocreate(server->backend, server->renderer); wl_signal_init(&server->events.destroy); if (!desktop_init(&server->desktop)) { wlr_log(WLR_ERROR, "Failed to initialize desktop"); wl_display_destroy(server->wl_display); return false; } if (!input_init(&server->input)) { wlr_log(WLR_ERROR, "Failed to initialize input"); wl_display_destroy(server->wl_display); return false; } wlr_data_control_manager_v1_create(server->wl_display); wlr_gamma_control_manager_v1_create(server->wl_display); wlr_primary_selection_v1_device_manager_create(server->wl_display); server->socket = wl_display_add_socket_auto(server->wl_display); if (!server->socket) { wlr_log(WLR_ERROR, "Failed to open Wayland socket"); wl_display_destroy(server->wl_display); return false; } if (!config_path) { // default config path config_path = realloc(config_path, PATH_MAX); if (!config_path) { wlr_log(WLR_ERROR, "Falied to allocate memory"); wl_display_destroy(server->wl_display); return false; } const char *config_home = getenv("XDG_CONFIG_HOME"); if (config_home) { snprintf(config_path, PATH_MAX, "%s/kiwmi/init.lua", config_home); } else { snprintf( config_path, PATH_MAX, "%s/.config/kiwmi/init.lua", getenv("HOME")); } } wlr_screencopy_manager_v1_create(server->wl_display); server->config_path = config_path; if (!(server->lua = luaK_create(server))) { wlr_log(WLR_ERROR, "Failed to initialize Lua"); wl_display_destroy(server->wl_display); return false; } return true; } bool server_run(struct kiwmi_server *server) { wlr_log( WLR_DEBUG, "Running Wayland server on display '%s'", server->socket); setenv("WAYLAND_DISPLAY", server->socket, true); if (!luaK_dofile(server->lua, server->config_path)) { wl_display_destroy(server->wl_display); return false; } if (!wlr_backend_start(server->backend)) { wlr_log(WLR_ERROR, "Failed to start backend"); wl_display_destroy(server->wl_display); return false; } wl_display_run(server->wl_display); return true; } void server_fini(struct kiwmi_server *server) { wlr_log(WLR_DEBUG, "Shutting down Wayland server"); wl_signal_emit(&server->events.destroy, server); wl_display_destroy_clients(server->wl_display); desktop_fini(&server->desktop); input_fini(&server->input); wl_display_destroy(server->wl_display); luaK_destroy(server->lua); free(server->config_path); }