neuswc

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

panel.c (8122B)


      1 /* swc: libswc/panel.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 "panel.h"
     25 #include "compositor.h"
     26 #include "internal.h"
     27 #include "keyboard.h"
     28 #include "output.h"
     29 #include "screen.h"
     30 #include "seat.h"
     31 #include "surface.h"
     32 #include "util.h"
     33 #include "view.h"
     34 
     35 #include "swc-server-protocol.h"
     36 #include <assert.h>
     37 #include <stdlib.h>
     38 
     39 struct panel {
     40 	struct wl_resource *resource;
     41 
     42 	struct wl_listener surface_destroy_listener;
     43 	struct compositor_view *view;
     44 	struct view_handler view_handler;
     45 	struct screen *screen;
     46 	struct screen_modifier modifier;
     47 	uint32_t edge;
     48 	uint32_t offset, strut_size;
     49 	uint32_t y_offset;
     50 	/* length last sent */
     51 	uint32_t length;
     52 	bool docked;
     53 };
     54 
     55 static uint32_t
     56 edge_length(struct screen *screen, uint32_t edge)
     57 {
     58 	switch (edge) {
     59 	case SWC_PANEL_EDGE_TOP:
     60 	case SWC_PANEL_EDGE_BOTTOM:
     61 		return screen->base.geometry.width;
     62 	case SWC_PANEL_EDGE_LEFT:
     63 	case SWC_PANEL_EDGE_RIGHT:
     64 		return screen->base.geometry.height;
     65 	default:
     66 		return 0;
     67 	}
     68 }
     69 
     70 static void
     71 update_position(struct panel *panel)
     72 {
     73 	int32_t x, y;
     74 	struct swc_rectangle *screen = &panel->screen->base.geometry,
     75 	                     *view = &panel->view->base.geometry;
     76 
     77 	switch (panel->edge) {
     78 	case SWC_PANEL_EDGE_TOP:
     79 		x = screen->x + panel->offset;
     80 		y = screen->y + panel->y_offset;
     81 		break;
     82 	case SWC_PANEL_EDGE_BOTTOM:
     83 		x = screen->x + panel->offset;
     84 		y = screen->y + screen->height - view->height - panel->y_offset;
     85 		break;
     86 	case SWC_PANEL_EDGE_LEFT:
     87 		x = screen->x;
     88 		y = screen->y + screen->height - view->height - panel->offset;
     89 		break;
     90 	case SWC_PANEL_EDGE_RIGHT:
     91 		x = screen->x + screen->width - view->width;
     92 		y = screen->y + panel->offset;
     93 		break;
     94 	default:
     95 		return;
     96 	}
     97 
     98 	view_move(&panel->view->base, x, y);
     99 }
    100 
    101 static void
    102 dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge,
    103      struct wl_resource *screen_resource, uint32_t focus)
    104 {
    105 	struct panel *panel = wl_resource_get_user_data(resource);
    106 	struct screen *screen;
    107 	uint32_t length;
    108 
    109 	if (screen_resource) {
    110 		screen = wl_resource_get_user_data(screen_resource);
    111 	} else {
    112 		screen = wl_container_of(swc.screens.next, screen, link);
    113 	}
    114 
    115 	if (!(length = edge_length(screen, edge))) {
    116 		return;
    117 	}
    118 
    119 	if (panel->screen && screen != panel->screen) {
    120 		wl_list_remove(&panel->modifier.link);
    121 		screen_update_usable_geometry(panel->screen);
    122 	}
    123 
    124 	panel->screen = screen;
    125 	panel->edge = edge;
    126 	panel->length = length;
    127 	panel->docked = true;
    128 
    129 	update_position(panel);
    130 	compositor_view_show(panel->view);
    131 	raise_window_top(panel->view);
    132 	wl_list_insert(&screen->modifiers, &panel->modifier.link);
    133 
    134 	if (focus) {
    135 		keyboard_set_focus(swc.seat->keyboard, panel->view);
    136 	}
    137 
    138 	swc_panel_send_docked(resource, length);
    139 }
    140 
    141 static void
    142 set_offset(struct wl_client *client, struct wl_resource *resource,
    143            uint32_t offset)
    144 {
    145 	struct panel *panel = wl_resource_get_user_data(resource);
    146 
    147 	panel->offset = offset;
    148 	if (panel->docked) {
    149 		update_position(panel);
    150 	}
    151 }
    152 
    153 static void
    154 set_y_offset(struct wl_client *client, struct wl_resource *resource,
    155              uint32_t offset)
    156 {
    157 	struct panel *panel = wl_resource_get_user_data(resource);
    158 
    159 	panel->y_offset = offset;
    160 	if (panel->docked) {
    161 		update_position(panel);
    162 	}
    163 }
    164 
    165 static void
    166 set_strut(struct wl_client *client, struct wl_resource *resource, uint32_t size,
    167           uint32_t begin, uint32_t end)
    168 {
    169 	struct panel *panel = wl_resource_get_user_data(resource);
    170 
    171 	panel->strut_size = size;
    172 	if (panel->docked) {
    173 		screen_update_usable_geometry(panel->screen);
    174 	}
    175 }
    176 
    177 static const struct swc_panel_interface panel_impl = {
    178     .dock = dock,
    179     .set_offset = set_offset,
    180     .set_y_offset = set_y_offset,
    181     .set_strut = set_strut,
    182 };
    183 
    184 static void
    185 handle_resize(struct view_handler *handler, uint32_t old_width,
    186               uint32_t old_height)
    187 {
    188 	struct panel *panel = wl_container_of(handler, panel, view_handler);
    189 	update_position(panel);
    190 }
    191 
    192 static const struct view_handler_impl view_handler_impl = {
    193     .resize = handle_resize,
    194 };
    195 
    196 static void
    197 modify(struct screen_modifier *modifier, const struct swc_rectangle *geom,
    198        pixman_region32_t *usable)
    199 {
    200 	struct panel *panel = wl_container_of(modifier, panel, modifier);
    201 	pixman_box32_t box = {.x1 = geom->x,
    202 	                      .y1 = geom->y,
    203 	                      .x2 = geom->x + geom->width,
    204 	                      .y2 = geom->y + geom->height};
    205 
    206 	assert(panel->docked);
    207 
    208 	/* every usable-area pass visits us, so notice a size change here */
    209 	if (edge_length(panel->screen, panel->edge) != panel->length) {
    210 		panel->length = edge_length(panel->screen, panel->edge);
    211 		update_position(panel);
    212 		swc_panel_send_docked(panel->resource, panel->length);
    213 	}
    214 
    215 	DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", box.x1,
    216 	      box.y1, box.x2, box.y2);
    217 
    218 	switch (panel->edge) {
    219 	case SWC_PANEL_EDGE_TOP:
    220 		box.y1 = MAX(box.y1, geom->y + panel->strut_size);
    221 		break;
    222 	case SWC_PANEL_EDGE_BOTTOM:
    223 		box.y2 = MIN(box.y2, geom->y + geom->height - panel->strut_size);
    224 		break;
    225 	case SWC_PANEL_EDGE_LEFT:
    226 		box.x1 = MAX(box.x1, geom->x + panel->strut_size);
    227 		break;
    228 	case SWC_PANEL_EDGE_RIGHT:
    229 		box.x2 = MIN(box.x2, geom->x + geom->width - panel->strut_size);
    230 		break;
    231 	}
    232 
    233 	DEBUG("Usable region { x1: %d, y1: %d, x2: %d, y2: %d }\n", box.x1, box.y1,
    234 	      box.x2, box.y2);
    235 
    236 	pixman_region32_reset(usable, &box);
    237 }
    238 
    239 static void
    240 destroy_panel(struct wl_resource *resource)
    241 {
    242 	struct panel *panel = wl_resource_get_user_data(resource);
    243 
    244 	if (panel->docked) {
    245 		wl_list_remove(&panel->modifier.link);
    246 		screen_update_usable_geometry(panel->screen);
    247 	}
    248 
    249 	compositor_view_destroy(panel->view);
    250 	free(panel);
    251 }
    252 
    253 static void
    254 handle_surface_destroy(struct wl_listener *listener, void *data)
    255 {
    256 	struct panel *panel =
    257 	    wl_container_of(listener, panel, surface_destroy_listener);
    258 	wl_resource_destroy(panel->resource);
    259 }
    260 
    261 struct panel *
    262 panel_new(struct wl_client *client, uint32_t version, uint32_t id,
    263           struct surface *surface)
    264 {
    265 	struct panel *panel;
    266 
    267 	panel = malloc(sizeof(*panel));
    268 
    269 	if (!panel) {
    270 		goto error0;
    271 	}
    272 
    273 	panel->resource =
    274 	    wl_resource_create(client, &swc_panel_interface, version, id);
    275 
    276 	if (!panel->resource) {
    277 		goto error0;
    278 	}
    279 
    280 	if (!surface_set_role(surface, panel->resource)) {
    281 		goto error2;
    282 	}
    283 
    284 	if (!(panel->view = compositor_create_view(surface))) {
    285 		goto error3;
    286 	}
    287 
    288 	wl_resource_set_implementation(panel->resource, &panel_impl, panel,
    289 	                               &destroy_panel);
    290 	panel->surface_destroy_listener.notify = &handle_surface_destroy;
    291 	panel->view_handler.impl = &view_handler_impl;
    292 	panel->view->always_top = true;
    293 	panel->modifier.modify = &modify;
    294 	panel->screen = NULL;
    295 	panel->offset = 0;
    296 	panel->y_offset = 0;
    297 	panel->strut_size = 0;
    298 	panel->docked = false;
    299 	wl_list_insert(&panel->view->base.handlers, &panel->view_handler.link);
    300 	wl_resource_add_destroy_listener(surface->resource,
    301 	                                 &panel->surface_destroy_listener);
    302 
    303 	return panel;
    304 
    305 error3:
    306 	wl_resource_destroy(panel->resource);
    307 error2:
    308 	free(panel);
    309 error0:
    310 	return NULL;
    311 }