neuswc

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

screen.c (14023B)


      1 /* swc: libswc/screen.c
      2  *
      3  * Copyright (c) 2013, 2014 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 "screen.h"
     25 #include "backend.h"
     26 #include "compositor.h"
     27 #ifdef ENABLE_DRM
     28 #include "drm.h"
     29 #endif
     30 #ifdef ENABLE_FB
     31 #include "fb.h"
     32 #endif
     33 #include "event.h"
     34 #include "internal.h"
     35 #include "layer_shell.h"
     36 #include "mode.h"
     37 #include "output.h"
     38 #ifdef ENABLE_DRM
     39 #include "plane.h"
     40 #endif
     41 #include "pointer.h"
     42 #include "seat.h"
     43 #include "util.h"
     44 
     45 #include "swc-server-protocol.h"
     46 #include <stdlib.h>
     47 #include <wayland-server.h>
     48 
     49 #define INTERNAL(s) ((struct screen *)(s))
     50 
     51 static struct screen *active_screen;
     52 static const struct swc_screen_handler null_handler;
     53 
     54 static bool
     55 handle_motion(struct pointer_handler *handler,
     56               uint32_t time,
     57               wl_fixed_t x,
     58               wl_fixed_t y);
     59 
     60 struct pointer_handler screens_pointer_handler = {
     61     .motion = handle_motion,
     62 };
     63 
     64 EXPORT void
     65 swc_screen_set_handler(struct swc_screen *base,
     66                        const struct swc_screen_handler *handler,
     67                        void *data)
     68 {
     69 	struct screen *screen = INTERNAL(base);
     70 
     71 	screen->handler = handler;
     72 	screen->handler_data = data;
     73 }
     74 
     75 /* the screens of the video backend swc started with */
     76 static bool
     77 create_default_screens(void)
     78 {
     79 #ifdef ENABLE_DRM
     80 	if (swc.video == SWC_VIDEO_DRM) {
     81 		return drm_create_screens(&swc.screens);
     82 	}
     83 #endif
     84 #ifdef ENABLE_FB
     85 	if (swc.video == SWC_VIDEO_FB) {
     86 		return fb_create_screens(&swc.screens);
     87 	}
     88 #endif
     89 	return false;
     90 }
     91 
     92 bool
     93 screens_initialize(void)
     94 {
     95 	wl_list_init(&swc.screens);
     96 
     97 	if (!(swc.backend->create_screens ? swc.backend->create_screens(&swc.screens)
     98 	                                  : create_default_screens())) {
     99 		return false;
    100 	}
    101 
    102 	if (wl_list_empty(&swc.screens)) {
    103 		return false;
    104 	}
    105 
    106 	return true;
    107 }
    108 
    109 void
    110 screens_finalize(void)
    111 {
    112 	struct screen *screen, *tmp;
    113 
    114 	wl_list_for_each_safe(screen, tmp, &swc.screens, link)
    115 	    screen_destroy(screen);
    116 }
    117 
    118 static void
    119 bind_screen(struct wl_client *client, void *data, uint32_t version, uint32_t id)
    120 {
    121 	struct screen *screen = data;
    122 	struct wl_resource *resource;
    123 
    124 	resource = wl_resource_create(client, &swc_screen_interface, version, id);
    125 
    126 	if (!resource) {
    127 		wl_client_post_no_memory(client);
    128 		return;
    129 	}
    130 
    131 	wl_resource_set_implementation(resource, NULL, screen, &remove_resource);
    132 	wl_list_insert(&screen->resources, wl_resource_get_link(resource));
    133 }
    134 
    135 struct screen *
    136 screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane)
    137 {
    138 	struct screen *screen;
    139 	int32_t x = 0;
    140 
    141 	/* Simple heuristic for initial screen positioning. */
    142 	wl_list_for_each(screen, &swc.screens, link) x =
    143 	    MAX(x, screen->base.geometry.x + screen->base.geometry.width);
    144 
    145 	if (!(screen = malloc(sizeof(*screen)))) {
    146 		goto error0;
    147 	}
    148 
    149 	screen->global = wl_global_create(
    150 	    swc.display, &swc_screen_interface, 1, screen, &bind_screen);
    151 
    152 	if (!screen->global) {
    153 		ERROR("Failed to create screen global\n");
    154 		goto error1;
    155 	}
    156 
    157 #ifdef ENABLE_DRM
    158 	if (swc.video == SWC_VIDEO_DRM) {
    159 		screen->crtc = crtc;
    160 		if (!primary_plane_initialize_drm(&screen->planes.primary,
    161 		                                  crtc,
    162 		                                  output->preferred_mode,
    163 		                                  &output->connector,
    164 		                                  1)) {
    165 			ERROR("Failed to initialize primary plane\n");
    166 			goto error2;
    167 		}
    168 
    169 		cursor_plane->screen = screen;
    170 		screen->planes.cursor = cursor_plane;
    171 	} else
    172 #endif
    173 	{
    174 #ifdef ENABLE_FB
    175 		if (!primary_plane_initialize_fb(&screen->planes.primary,
    176 		                                 output->preferred_mode)) {
    177 			ERROR("Failed to initialize primary plane\n");
    178 			goto error2;
    179 		}
    180 #endif
    181 		(void)crtc;
    182 		(void)cursor_plane;
    183 		screen->planes.cursor = NULL;
    184 	}
    185 
    186 	screen->handler = &null_handler;
    187 	wl_signal_init(&screen->destroy_signal);
    188 	wl_list_init(&screen->resources);
    189 	wl_list_init(&screen->outputs);
    190 	wl_list_insert(&screen->outputs, &output->link);
    191 	wl_list_init(&screen->modifiers);
    192 
    193 	view_move(&screen->planes.primary.view, x, 0);
    194 	screen->base.geometry = screen->planes.primary.view.geometry;
    195 	screen->base.usable_geometry = screen->base.geometry;
    196 
    197 	swc.manager->new_screen(&screen->base);
    198 
    199 	return screen;
    200 
    201 error2:
    202 	wl_global_destroy(screen->global);
    203 error1:
    204 	free(screen);
    205 error0:
    206 	return NULL;
    207 }
    208 
    209 void
    210 screen_destroy(struct screen *screen)
    211 {
    212 	struct output *output, *next;
    213 
    214 	if (active_screen == screen) {
    215 		active_screen = NULL;
    216 	}
    217 	if (screen->handler->destroy) {
    218 		screen->handler->destroy(screen->handler_data);
    219 	}
    220 	wl_signal_emit(&screen->destroy_signal, NULL);
    221 	wl_list_for_each_safe(output, next, &screen->outputs, link)
    222 	    output_destroy(output);
    223 	if (screen->virtual) {
    224 		view_finalize(&screen->planes.primary.view);
    225 	} else {
    226 		primary_plane_finalize(&screen->planes.primary);
    227 #ifdef ENABLE_DRM
    228 		if (screen->planes.cursor) {
    229 			plane_destroy(screen->planes.cursor);
    230 		}
    231 #endif
    232 	}
    233 	free(screen);
    234 }
    235 
    236 struct screen *
    237 screen_new_virtual(struct output *output, const struct view_impl *impl)
    238 {
    239 	struct screen *screen;
    240 	int32_t x = 0;
    241 
    242 	wl_list_for_each(screen, &swc.screens, link) x =
    243 	    MAX(x, screen->base.geometry.x + screen->base.geometry.width);
    244 
    245 	if (!(screen = calloc(1, sizeof(*screen)))) {
    246 		return NULL;
    247 	}
    248 
    249 	screen->global = wl_global_create(
    250 	    swc.display, &swc_screen_interface, 1, screen, &bind_screen);
    251 	if (!screen->global) {
    252 		ERROR("Failed to create screen global\n");
    253 		free(screen);
    254 		return NULL;
    255 	}
    256 
    257 	screen->virtual = true;
    258 	view_initialize(&screen->planes.primary.view, impl);
    259 	screen->planes.primary.view.geometry.width = output->preferred_mode->width;
    260 	screen->planes.primary.view.geometry.height =
    261 	    output->preferred_mode->height;
    262 	screen->planes.primary.mode = *output->preferred_mode;
    263 
    264 	screen->handler = &null_handler;
    265 	wl_signal_init(&screen->destroy_signal);
    266 	wl_list_init(&screen->resources);
    267 	wl_list_init(&screen->outputs);
    268 	wl_list_insert(&screen->outputs, &output->link);
    269 	wl_list_init(&screen->modifiers);
    270 
    271 	view_move(&screen->planes.primary.view, x, 0);
    272 	screen->base.geometry = screen->planes.primary.view.geometry;
    273 	screen->base.usable_geometry = screen->base.geometry;
    274 
    275 	swc.manager->new_screen(&screen->base);
    276 
    277 	return screen;
    278 }
    279 
    280 /* the pointer may go anywhere on a screen and nowhere else */
    281 static void
    282 pointer_region_update(void)
    283 {
    284 	struct screen *screen;
    285 	pixman_region32_t region;
    286 
    287 	if (!swc.seat) {
    288 		return;
    289 	}
    290 	pixman_region32_init(&region);
    291 	wl_list_for_each(screen, &swc.screens, link)
    292 	    pixman_region32_union_rect(&region, &region,
    293 	                               screen->base.geometry.x,
    294 	                               screen->base.geometry.y,
    295 	                               screen->base.geometry.width,
    296 	                               screen->base.geometry.height);
    297 	pointer_set_region(swc.seat->pointer, &region);
    298 	pixman_region32_fini(&region);
    299 }
    300 
    301 /* the screen's area changed: what depends on it follows, and the window
    302  * manager is told */
    303 static void
    304 screen_changed(struct screen *screen, bool resized)
    305 {
    306 	if (resized) {
    307 		compositor_screen_resize(screen);
    308 	} else {
    309 		compositor_damage_all();
    310 	}
    311 	pointer_region_update();
    312 	/* a change can leave origin and edges of the usable area as they
    313 	 * were, so it is told anyway */
    314 	screen->base.usable_geometry.width = 0;
    315 	screen_update_usable_geometry(screen);
    316 	if (screen->handler->geometry_changed) {
    317 		screen->handler->geometry_changed(screen->handler_data);
    318 	}
    319 }
    320 
    321 void
    322 screen_resize(struct screen *screen, uint32_t width, uint32_t height)
    323 {
    324 	struct output *output;
    325 
    326 	screen->planes.primary.view.geometry.width = width;
    327 	screen->planes.primary.view.geometry.height = height;
    328 	screen->planes.primary.mode.width = width;
    329 	screen->planes.primary.mode.height = height;
    330 	screen->base.geometry.width = width;
    331 	screen->base.geometry.height = height;
    332 
    333 	wl_list_for_each(output, &screen->outputs, link)
    334 	    output_set_size(output, width, height);
    335 
    336 	screen_changed(screen, true);
    337 }
    338 
    339 static struct output *
    340 screen_output(struct screen *screen)
    341 {
    342 	struct output *output;
    343 
    344 	if (wl_list_empty(&screen->outputs)) {
    345 		return NULL;
    346 	}
    347 	return wl_container_of(screen->outputs.next, output, link);
    348 }
    349 
    350 EXPORT const char *
    351 swc_screen_name(struct swc_screen *base)
    352 {
    353 	struct output *output = screen_output(INTERNAL(base));
    354 
    355 	return output ? output->name : "";
    356 }
    357 
    358 EXPORT enum swc_screen_kind
    359 swc_screen_kind(struct swc_screen *base)
    360 {
    361 	struct screen *screen = INTERNAL(base);
    362 
    363 	if (screen->virtual) {
    364 		return SWC_SCREEN_WINDOW;
    365 	}
    366 #ifdef ENABLE_DRM
    367 	if (swc.video == SWC_VIDEO_DRM) {
    368 		return SWC_SCREEN_DISPLAY;
    369 	}
    370 #endif
    371 	return SWC_SCREEN_FRAMEBUFFER;
    372 }
    373 
    374 EXPORT int
    375 swc_screen_modes(struct swc_screen *base, struct swc_mode *out, int max)
    376 {
    377 	struct screen *screen = INTERNAL(base);
    378 	struct output *output = screen_output(screen);
    379 	struct mode *mode;
    380 	int n = 0;
    381 
    382 	if (!output) {
    383 		return 0;
    384 	}
    385 	wl_array_for_each(mode, &output->modes)
    386 	{
    387 		if (n < max) {
    388 			out[n] = (struct swc_mode){
    389 			    .width = mode->width,
    390 			    .height = mode->height,
    391 			    .refresh = mode->refresh,
    392 			    .preferred = mode->preferred,
    393 			    .current = mode_equal(&screen->planes.primary.mode, mode),
    394 			};
    395 		}
    396 		n++;
    397 	}
    398 	return n;
    399 }
    400 
    401 EXPORT bool
    402 swc_screen_set_mode(struct swc_screen *base, uint32_t width, uint32_t height,
    403                     uint32_t refresh)
    404 {
    405 	struct screen *screen = INTERNAL(base);
    406 	struct output *output = screen_output(screen);
    407 	struct mode *mode, *best = NULL;
    408 	struct wl_resource *resource;
    409 
    410 	if (!output || swc_screen_kind(base) != SWC_SCREEN_DISPLAY) {
    411 		return false;
    412 	}
    413 	wl_array_for_each(mode, &output->modes)
    414 	{
    415 		if (mode->width != width || mode->height != height) {
    416 			continue;
    417 		}
    418 		if (refresh ? mode->refresh == refresh
    419 		            : !best || mode->refresh > best->refresh) {
    420 			best = mode;
    421 		}
    422 	}
    423 	if (!best) {
    424 		return false;
    425 	}
    426 #ifdef ENABLE_DRM
    427 	screen->planes.primary.mode = *best;
    428 	screen->planes.primary.need_modeset = true;
    429 #endif
    430 	screen->planes.primary.view.geometry.width = width;
    431 	screen->planes.primary.view.geometry.height = height;
    432 	screen->base.geometry.width = width;
    433 	screen->base.geometry.height = height;
    434 	wl_resource_for_each(resource, &output->resources)
    435 	{
    436 		wl_output_send_mode(resource,
    437 		                    WL_OUTPUT_MODE_CURRENT |
    438 		                        (best->preferred ? WL_OUTPUT_MODE_PREFERRED : 0),
    439 		                    best->width, best->height, best->refresh);
    440 		if (wl_resource_get_version(resource) >= 2) {
    441 			wl_output_send_done(resource);
    442 		}
    443 	}
    444 	screen_changed(screen, true);
    445 	return true;
    446 }
    447 
    448 EXPORT void
    449 swc_screen_move(struct swc_screen *base, int32_t x, int32_t y)
    450 {
    451 	struct screen *screen = INTERNAL(base);
    452 	struct output *output = screen_output(screen);
    453 	struct wl_resource *resource;
    454 
    455 	view_move(&screen->planes.primary.view, x, y);
    456 	screen->base.geometry.x = x;
    457 	screen->base.geometry.y = y;
    458 	if (output) {
    459 		wl_resource_for_each(resource, &output->resources)
    460 		{
    461 			wl_output_send_geometry(resource, x, y, output->physical_width,
    462 			                        output->physical_height, 0, "unknown",
    463 			                        "unknown", WL_OUTPUT_TRANSFORM_NORMAL);
    464 			if (wl_resource_get_version(resource) >= 2) {
    465 				wl_output_send_done(resource);
    466 			}
    467 		}
    468 	}
    469 	screen_changed(screen, false);
    470 }
    471 
    472 void
    473 screen_update_usable_geometry(struct screen *screen)
    474 {
    475 	pixman_region32_t total_usable, usable;
    476 	pixman_box32_t *extents;
    477 	struct screen_modifier *modifier;
    478 	struct swc_rectangle *geom = &screen->base.geometry;
    479 
    480 	DEBUG("Updating usable geometry\n");
    481 
    482 	pixman_region32_init_rect(
    483 	    &total_usable, geom->x, geom->y, geom->width, geom->height);
    484 	pixman_region32_init(&usable);
    485 
    486 	wl_list_for_each(modifier, &screen->modifiers, link)
    487 	{
    488 		modifier->modify(modifier, geom, &usable);
    489 		pixman_region32_intersect(&total_usable, &total_usable, &usable);
    490 	}
    491 
    492 	extents = pixman_region32_extents(&total_usable);
    493 
    494 	if (extents->x1 != screen->base.usable_geometry.x ||
    495 	    extents->y1 != screen->base.usable_geometry.y ||
    496 	    (extents->x2 - extents->x1) != screen->base.usable_geometry.width ||
    497 	    (extents->y2 - extents->y1) != screen->base.usable_geometry.height) {
    498 		screen->base.usable_geometry.x = extents->x1;
    499 		screen->base.usable_geometry.y = extents->y1;
    500 		screen->base.usable_geometry.width = extents->x2 - extents->x1;
    501 		screen->base.usable_geometry.height = extents->y2 - extents->y1;
    502 
    503 		layer_shell_usable_changed(screen);
    504 		if (screen->handler->usable_geometry_changed) {
    505 			screen->handler->usable_geometry_changed(screen->handler_data);
    506 		}
    507 	}
    508 }
    509 
    510 bool
    511 handle_motion(struct pointer_handler *handler,
    512               uint32_t time,
    513               wl_fixed_t fx,
    514               wl_fixed_t fy)
    515 {
    516 	struct screen *screen;
    517 	int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy);
    518 
    519 	wl_list_for_each(screen, &swc.screens, link)
    520 	{
    521 		if (rectangle_contains_point(&screen->base.geometry, x, y)) {
    522 			if (screen != active_screen) {
    523 				active_screen = screen;
    524 
    525 				if (screen->handler->entered) {
    526 					screen->handler->entered(screen->handler_data);
    527 				}
    528 			}
    529 			break;
    530 		}
    531 	}
    532 
    533 	return false;
    534 }