neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

swc.c (12067B)


      1 /* swc: libswc/swc.c
      2  *
      3  * Copyright (c) 2013-2020 Michael Forney
      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
     13  * all 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  */
     23 
     24 #include "swc.h"
     25 #include "bindings.h"
     26 #include "compositor.h"
     27 #include "data_device_manager.h"
     28 #ifdef ENABLE_DRM
     29 #include "drm.h"
     30 #endif
     31 #ifdef ENABLE_FB
     32 #include "fb.h"
     33 #endif
     34 #include "event.h"
     35 #include "internal.h"
     36 #include "kde_decoration.h"
     37 #include "keyboard.h"
     38 #include "launch.h"
     39 #include "layer_shell.h"
     40 #ifdef ENABLE_PEER
     41 #include "peer.h"
     42 #endif
     43 #ifdef ENABLE_NESTED
     44 #include "nested.h"
     45 #endif
     46 #include "panel_manager.h"
     47 #include "pointer.h"
     48 #include "screen.h"
     49 #include "seat.h"
     50 #include "select.h"
     51 #include "shell.h"
     52 #include "shm.h"
     53 #include "snap.h"
     54 #include "subcompositor.h"
     55 #include "util.h"
     56 #include "window.h"
     57 #include "xdg_decoration.h"
     58 #include "xdg_dialog.h"
     59 #include "cursor_shape.h"
     60 #include "xdg_output.h"
     61 #include "xdg_shell.h"
     62 #ifdef ENABLE_XWAYLAND
     63 #include "xserver.h"
     64 #endif
     65 
     66 extern struct swc_launch swc_launch;
     67 extern const struct swc_bindings swc_bindings;
     68 extern struct swc_compositor swc_compositor;
     69 #ifdef ENABLE_DRM
     70 extern struct swc_drm swc_drm;
     71 #endif
     72 #ifdef ENABLE_XWAYLAND
     73 extern struct swc_xserver swc_xserver;
     74 #endif
     75 
     76 extern struct pointer_handler screens_pointer_handler;
     77 
     78 struct swc swc = {
     79     .bindings = &swc_bindings,
     80     .compositor = &swc_compositor,
     81 #ifdef ENABLE_DRM
     82     .drm = &swc_drm,
     83 #endif
     84 #ifdef ENABLE_XWAYLAND
     85     .xserver = &swc_xserver,
     86 #endif
     87 };
     88 
     89 static void
     90 setup_compositor(void)
     91 {
     92 	pixman_region32_t pointer_region;
     93 	struct screen *screen;
     94 	struct swc_rectangle *geom;
     95 
     96 	wl_list_insert(&swc.seat->keyboard->handlers,
     97 	               &swc.bindings->keyboard_handler->link);
     98 	wl_list_insert(&swc.seat->pointer->handlers,
     99 	               &swc.bindings->pointer_handler->link);
    100 	wl_list_insert(&swc.seat->pointer->handlers,
    101 	               &swc.compositor->pointer_handler->link);
    102 	wl_list_insert(&swc.seat->pointer->handlers, &screens_pointer_handler.link);
    103 	wl_signal_add(&swc.seat->pointer->focus.event_signal,
    104 	              &window_enter_listener);
    105 
    106 	/* Calculate pointer region */
    107 	pixman_region32_init(&pointer_region);
    108 
    109 	wl_list_for_each(screen, &swc.screens, link)
    110 	{
    111 		geom = &screen->base.geometry;
    112 		pixman_region32_union_rect(&pointer_region, &pointer_region, geom->x,
    113 		                           geom->y, geom->width, geom->height);
    114 	}
    115 
    116 	pointer_set_region(swc.seat->pointer, &pointer_region);
    117 	pixman_region32_fini(&pointer_region);
    118 }
    119 
    120 void
    121 swc_activate(void)
    122 {
    123 	swc.active = true;
    124 	send_event(&swc.event_signal, SWC_EVENT_ACTIVATED, NULL);
    125 	if (swc.manager->activate) {
    126 		swc.manager->activate();
    127 	}
    128 }
    129 
    130 void
    131 swc_deactivate(void)
    132 {
    133 	swc.active = false;
    134 	send_event(&swc.event_signal, SWC_EVENT_DEACTIVATED, NULL);
    135 	if (swc.manager->deactivate) {
    136 		swc.manager->deactivate();
    137 	}
    138 }
    139 
    140 EXPORT bool
    141 swc_cursor_position(int32_t *x, int32_t *y)
    142 {
    143 	if (x) {
    144 		*x = 0;
    145 	}
    146 	if (y) {
    147 		*y = 0;
    148 	}
    149 
    150 	if (!swc.seat || !swc.seat->pointer) {
    151 		return false;
    152 	}
    153 
    154 	if (x) {
    155 		*x = swc.seat->pointer->x;
    156 	}
    157 	if (y) {
    158 		*y = swc.seat->pointer->y;
    159 	}
    160 
    161 	return true;
    162 }
    163 
    164 EXPORT bool
    165 swc_cursor_set_position(int32_t x, int32_t y)
    166 {
    167 	if (!swc.seat || !swc.seat->pointer) {
    168 		return false;
    169 	}
    170 
    171 	pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
    172 	                               wl_fixed_from_int(x), wl_fixed_from_int(y));
    173 	pointer_handle_frame(swc.seat->pointer);
    174 
    175 	return true;
    176 }
    177 
    178 EXPORT bool
    179 swc_button(uint32_t button, uint32_t state)
    180 {
    181 	if (!swc.seat || !swc.seat->pointer) {
    182 		return false;
    183 	}
    184 
    185 	pointer_handle_button(swc.seat->pointer, get_time(), button, state);
    186 	pointer_handle_frame(swc.seat->pointer);
    187 
    188 	return true;
    189 }
    190 
    191 EXPORT bool
    192 swc_key(uint32_t key, uint32_t state)
    193 {
    194 	if (!swc.seat || !swc.seat->keyboard) {
    195 		return false;
    196 	}
    197 
    198 	keyboard_handle_key(swc.seat->keyboard, get_time(), key, state);
    199 
    200 	return true;
    201 }
    202 
    203 EXPORT bool
    204 swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level)
    205 {
    206 	struct xkb_keymap *map;
    207 	xkb_keycode_t code, max;
    208 	const xkb_keysym_t *syms;
    209 	uint32_t l;
    210 	int i, n;
    211 
    212 	if (!swc.seat || !swc.seat->keyboard) {
    213 		return false;
    214 	}
    215 
    216 	map = swc.seat->keyboard->xkb.keymap.map;
    217 	max = xkb_keymap_max_keycode(map);
    218 	/* bare keys first */
    219 	for (l = 0; l < 2; l++) {
    220 		for (code = xkb_keymap_min_keycode(map); code <= max; code++) {
    221 			n = xkb_keymap_key_get_syms_by_level(map, code, 0, l, &syms);
    222 			for (i = 0; i < n; i++) {
    223 				if (syms[i] != keysym) {
    224 					continue;
    225 				}
    226 				*key = code - 8;
    227 				*level = l;
    228 				return true;
    229 			}
    230 		}
    231 	}
    232 
    233 	return false;
    234 }
    235 
    236 /* whether SWC_BACKEND leaves the video backend called name open: drm or fb
    237  * name one, anything else, or nothing, lets swc try each that was built */
    238 static bool
    239 video_wanted(const char *name)
    240 {
    241 	const char *backend = getenv("SWC_BACKEND");
    242 
    243 	if (!backend || (strcmp(backend, "drm") != 0 && strcmp(backend, "fb") != 0)) {
    244 		return true;
    245 	}
    246 	return strcmp(backend, name) == 0;
    247 }
    248 
    249 /* the first video backend that was built, was asked for and comes up */
    250 static bool
    251 video_initialize(void)
    252 {
    253 #ifdef ENABLE_DRM
    254 	if (video_wanted("drm") && drm_initialize()) {
    255 		swc.video = SWC_VIDEO_DRM;
    256 		return true;
    257 	}
    258 #endif
    259 #ifdef ENABLE_FB
    260 	if (video_wanted("fb") && fb_initialize()) {
    261 		swc.video = SWC_VIDEO_FB;
    262 		return true;
    263 	}
    264 #endif
    265 	return false;
    266 }
    267 
    268 static bool
    269 backend_initialize(void)
    270 {
    271 #ifdef ENABLE_PEER
    272 	if (peer_requested()) {
    273 		return peer_initialize();
    274 	}
    275 #endif
    276 #ifdef ENABLE_NESTED
    277 	if (nested_requested()) {
    278 		return nested_initialize();
    279 	}
    280 #endif
    281 
    282 	if (!launch_initialize()) {
    283 		ERROR("Could not connect to swc-launch\n");
    284 		return false;
    285 	}
    286 
    287 	if (!video_initialize()) {
    288 		ERROR("Could not initialize video backend\n");
    289 		launch_finalize();
    290 		return false;
    291 	}
    292 
    293 	return true;
    294 }
    295 
    296 static void
    297 backend_finalize(void)
    298 {
    299 	if (swc.backend->finalize) {
    300 		swc.backend->finalize();
    301 		return;
    302 	}
    303 
    304 #ifdef ENABLE_DRM
    305 	if (swc.video == SWC_VIDEO_DRM) {
    306 		drm_finalize();
    307 	}
    308 #endif
    309 #ifdef ENABLE_FB
    310 	if (swc.video == SWC_VIDEO_FB) {
    311 		fb_finalize();
    312 	}
    313 #endif
    314 	launch_finalize();
    315 }
    316 
    317 EXPORT bool
    318 swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
    319                const struct swc_manager *manager)
    320 {
    321 	swc.display = display;
    322 	swc.event_loop =
    323 	    event_loop ? event_loop : wl_display_get_event_loop(display);
    324 	swc.manager = manager;
    325 	const char *default_seat = "seat0";
    326 	wl_signal_init(&swc.event_signal);
    327 
    328 	if (!backend_initialize()) {
    329 		goto error0;
    330 	}
    331 
    332 	swc.shm = shm_create(display);
    333 	if (!swc.shm) {
    334 		ERROR("Could not initialize SHM\n");
    335 		goto error2;
    336 	}
    337 
    338 	if (!bindings_initialize()) {
    339 		ERROR("Could not initialize bindings\n");
    340 		goto error3;
    341 	}
    342 
    343 	swc.subcompositor = subcompositor_create(display);
    344 	if (!swc.subcompositor) {
    345 		ERROR("Could not initialize subcompositor\n");
    346 		goto error4;
    347 	}
    348 
    349 	if (!screens_initialize()) {
    350 		ERROR("Could not initialize screens\n");
    351 		goto error5;
    352 	}
    353 
    354 	if (!compositor_initialize()) {
    355 		ERROR("Could not initialize compositor\n");
    356 		goto error6;
    357 	}
    358 
    359 	swc.data_device_manager = data_device_manager_create(display);
    360 	if (!swc.data_device_manager) {
    361 		ERROR("Could not initialize data device manager\n");
    362 		goto error7;
    363 	}
    364 
    365 	swc.seat = seat_create(display, default_seat);
    366 	if (!swc.seat) {
    367 		ERROR("Could not initialize seat\n");
    368 		goto error8;
    369 	}
    370 
    371 	swc.shell = shell_create(display);
    372 	if (!swc.shell) {
    373 		ERROR("Could not initialize shell\n");
    374 		goto error9;
    375 	}
    376 
    377 	swc.xdg_shell = xdg_shell_create(display);
    378 	if (!swc.xdg_shell) {
    379 		ERROR("Could not initialize XDG shell\n");
    380 		goto error10;
    381 	}
    382 
    383 	/* optional */
    384 	swc.xdg_dialog_manager = xdg_dialog_manager_create(display);
    385 
    386 	/* optional */
    387 	swc.cursor_shape_manager = cursor_shape_manager_create(display);
    388 
    389 	swc.xdg_decoration_manager = xdg_decoration_manager_create(display);
    390 	if (!swc.xdg_decoration_manager) {
    391 		ERROR("Could not initialize XDG decoration manager\n");
    392 		goto error11;
    393 	}
    394 
    395 	swc.kde_decoration_manager = kde_decoration_manager_create(display);
    396 	if (!swc.kde_decoration_manager) {
    397 		ERROR("Could not initialize KDE decoration manager\n");
    398 		goto error12;
    399 	}
    400 
    401 	swc.layer_shell = layer_shell_create(display);
    402 	if (!swc.layer_shell) {
    403 		ERROR("Could not initialize layer shell\n");
    404 		goto error13;
    405 	}
    406 
    407 	swc.panel_manager = panel_manager_create(display);
    408 	if (!swc.panel_manager) {
    409 		ERROR("Could not initialize panel manager\n");
    410 		goto error14;
    411 	}
    412 
    413 	swc.snap_manager = snap_manager_create(display);
    414 	if (!swc.snap_manager) {
    415 		ERROR("Could not initialize snap manager\n");
    416 		goto error15;
    417 	}
    418 
    419 #ifdef ENABLE_XWAYLAND
    420 	if (!xserver_initialize()) {
    421 		ERROR("Could not initialize xwayland\n");
    422 		goto error16;
    423 	}
    424 #endif
    425 
    426 	swc.select_manager = select_manager_create(display);
    427 	if (!swc.select_manager) {
    428 		ERROR("Could not initialize select manager\n");
    429 		goto error17;
    430 	}
    431 
    432 	swc.xdg_output_manager = xdg_output_manager_create(display);
    433 	if (!swc.xdg_output_manager) {
    434 		ERROR("Could not initialize XDG output manager\n");
    435 		goto error17;
    436 	}
    437 
    438 	setup_compositor();
    439 
    440 	if (swc.backend->start) {
    441 		swc.backend->start();
    442 	}
    443 
    444 	return true;
    445 
    446 error17:
    447 	wl_global_destroy(swc.select_manager);
    448 #ifdef ENABLE_XWAYLAND
    449 error16:
    450 #endif
    451 	wl_global_destroy(swc.snap_manager);
    452 error15:
    453 	wl_global_destroy(swc.panel_manager);
    454 error14:
    455 	wl_global_destroy(swc.layer_shell);
    456 error13:
    457 	wl_global_destroy(swc.kde_decoration_manager);
    458 error12:
    459 	wl_global_destroy(swc.xdg_decoration_manager);
    460 error11:
    461 	wl_global_destroy(swc.xdg_shell);
    462 error10:
    463 	wl_global_destroy(swc.shell);
    464 error9:
    465 	seat_destroy(swc.seat);
    466 error8:
    467 	wl_global_destroy(swc.data_device_manager);
    468 error7:
    469 	compositor_finalize();
    470 error6:
    471 	screens_finalize();
    472 error5:
    473 	wl_global_destroy(swc.subcompositor);
    474 error4:
    475 	bindings_finalize();
    476 error3:
    477 	shm_destroy(swc.shm);
    478 error2:
    479 	backend_finalize();
    480 error0:
    481 	return false;
    482 }
    483 
    484 EXPORT void
    485 swc_finalize(void)
    486 {
    487 #ifdef ENABLE_XWAYLAND
    488 	xserver_finalize();
    489 #endif
    490 	wl_global_destroy(swc.xdg_output_manager);
    491 	wl_global_destroy(swc.snap_manager);
    492 	wl_global_destroy(swc.select_manager);
    493 	wl_global_destroy(swc.panel_manager);
    494 	wl_global_destroy(swc.layer_shell);
    495 	wl_global_destroy(swc.xdg_decoration_manager);
    496 	wl_global_destroy(swc.xdg_shell);
    497 	wl_global_destroy(swc.shell);
    498 	seat_destroy(swc.seat);
    499 	wl_global_destroy(swc.data_device_manager);
    500 	compositor_finalize();
    501 	screens_finalize();
    502 	bindings_finalize();
    503 	shm_destroy(swc.shm);
    504 	backend_finalize();
    505 }
    506 
    507 EXPORT bool
    508 swc_set_keyboard_layout(const char *layout, const char *variant,
    509                         const char *options)
    510 {
    511 	struct xkb_rule_names names = {
    512 	    .layout = layout, .variant = variant, .options = options};
    513 
    514 	return keyboard_set_names(swc.seat->keyboard, &names);
    515 }
    516 
    517 EXPORT bool
    518 swc_set_keyboard_keymap(const char *keymap)
    519 {
    520 	struct xkb_keymap *map;
    521 	bool ok;
    522 
    523 	if (!(map = xkb_keymap_new_from_string(swc.seat->keyboard->xkb.context,
    524 	                                       keymap, XKB_KEYMAP_FORMAT_TEXT_V1,
    525 	                                       0)))
    526 		return false;
    527 	ok = keyboard_set_keymap(swc.seat->keyboard, map);
    528 	xkb_keymap_unref(map);
    529 	return ok;
    530 }
    531 
    532 EXPORT void
    533 swc_set_keyboard_repeat(int32_t rate, int32_t delay)
    534 {
    535 	keyboard_set_repeat(swc.seat->keyboard, rate, delay);
    536 }