window.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Wayland window handling
 *
 * Copyright 2020 Alexandros Frantzis for Collabora Ltd
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#if 0
#pragma makedep unix
#endif

#include "config.h"

27 28 29
#include <assert.h>
#include <stdlib.h>

30 31 32 33 34 35
#include "waylanddrv.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(waylanddrv);

36 37 38 39 40 41
/* private window data */
struct wayland_win_data
{
    struct rb_entry entry;
    /* hwnd that this private data belongs to */
    HWND hwnd;
42 43
    /* wayland surface (if any) for this window */
    struct wayland_surface *wayland_surface;
44 45
    /* wine window_surface backing this window */
    struct window_surface *window_surface;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};

static int wayland_win_data_cmp_rb(const void *key,
                                   const struct rb_entry *entry)
{
    HWND key_hwnd = (HWND)key; /* cast to work around const */
    const struct wayland_win_data *entry_win_data =
        RB_ENTRY_VALUE(entry, const struct wayland_win_data, entry);

    if (key_hwnd < entry_win_data->hwnd) return -1;
    if (key_hwnd > entry_win_data->hwnd) return 1;
    return 0;
}

static pthread_mutex_t win_data_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct rb_tree win_data_rb = { wayland_win_data_cmp_rb };

/***********************************************************************
 *           wayland_win_data_create
 *
 * Create a data window structure for an existing window.
 */
static struct wayland_win_data *wayland_win_data_create(HWND hwnd)
{
    struct wayland_win_data *data;
    struct rb_entry *rb_entry;
    HWND parent;

    /* Don't create win data for desktop or HWND_MESSAGE windows. */
    if (!(parent = NtUserGetAncestor(hwnd, GA_PARENT))) return NULL;
    if (parent != NtUserGetDesktopWindow() && !NtUserGetAncestor(parent, GA_PARENT))
        return NULL;

    if (!(data = calloc(1, sizeof(*data)))) return NULL;

    data->hwnd = hwnd;

    pthread_mutex_lock(&win_data_mutex);

    /* Check that another thread hasn't already created the wayland_win_data. */
    if ((rb_entry = rb_get(&win_data_rb, hwnd)))
    {
        free(data);
        return RB_ENTRY_VALUE(rb_entry, struct wayland_win_data, entry);
    }

    rb_put(&win_data_rb, hwnd, &data->entry);

    TRACE("hwnd=%p\n", data->hwnd);

    return data;
}

/***********************************************************************
 *           wayland_win_data_destroy
 */
static void wayland_win_data_destroy(struct wayland_win_data *data)
{
    TRACE("hwnd=%p\n", data->hwnd);

    rb_remove(&win_data_rb, &data->entry);

    pthread_mutex_unlock(&win_data_mutex);

110 111 112 113 114
    if (data->window_surface)
    {
        wayland_window_surface_update_wayland_surface(data->window_surface, NULL);
        window_surface_release(data->window_surface);
    }
115
    if (data->wayland_surface) wayland_surface_destroy(data->wayland_surface);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    free(data);
}

/***********************************************************************
 *           wayland_win_data_get
 *
 * Lock and return the data structure associated with a window.
 */
static struct wayland_win_data *wayland_win_data_get(HWND hwnd)
{
    struct rb_entry *rb_entry;

    pthread_mutex_lock(&win_data_mutex);

    if ((rb_entry = rb_get(&win_data_rb, hwnd)))
        return RB_ENTRY_VALUE(rb_entry, struct wayland_win_data, entry);

    pthread_mutex_unlock(&win_data_mutex);

    return NULL;
}

/***********************************************************************
 *           wayland_win_data_release
 *
 * Release the data returned by wayland_win_data_get.
 */
static void wayland_win_data_release(struct wayland_win_data *data)
{
    assert(data);
    pthread_mutex_unlock(&win_data_mutex);
}

149 150 151 152 153 154 155 156 157 158 159
static void wayland_win_data_update_wayland_surface(struct wayland_win_data *data)
{
    struct wayland_surface *surface = data->wayland_surface;
    HWND parent = NtUserGetAncestor(data->hwnd, GA_PARENT);
    BOOL visible, xdg_visible;

    TRACE("hwnd=%p\n", data->hwnd);

    /* We don't want wayland surfaces for child windows. */
    if (parent != NtUserGetDesktopWindow() && parent != 0)
    {
160 161
        if (data->window_surface)
            wayland_window_surface_update_wayland_surface(data->window_surface, NULL);
162 163 164 165 166 167
        if (surface) wayland_surface_destroy(surface);
        surface = NULL;
        goto out;
    }

    /* Otherwise ensure that we have a wayland surface. */
168
    if (!surface && !(surface = wayland_surface_create(data->hwnd))) return;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    visible = (NtUserGetWindowLongW(data->hwnd, GWL_STYLE) & WS_VISIBLE) == WS_VISIBLE;
    xdg_visible = surface->xdg_toplevel != NULL;

    if (visible != xdg_visible)
    {
        pthread_mutex_lock(&surface->mutex);

        /* If we have a pre-existing surface ensure it has no role. */
        if (data->wayland_surface) wayland_surface_clear_role(surface);
        /* If the window is a visible toplevel make it a wayland
         * xdg_toplevel. Otherwise keep it role-less to avoid polluting the
         * compositor with empty xdg_toplevels. */
        if (visible) wayland_surface_make_toplevel(surface);

        pthread_mutex_unlock(&surface->mutex);
    }

187 188 189
    if (data->window_surface)
        wayland_window_surface_update_wayland_surface(data->window_surface, surface);

190 191 192 193 194
out:
    TRACE("hwnd=%p surface=%p=>%p\n", data->hwnd, data->wayland_surface, surface);
    data->wayland_surface = surface;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/***********************************************************************
 *           WAYLAND_DestroyWindow
 */
void WAYLAND_DestroyWindow(HWND hwnd)
{
    struct wayland_win_data *data;

    TRACE("%p\n", hwnd);

    if (!(data = wayland_win_data_get(hwnd))) return;
    wayland_win_data_destroy(data);
}

/***********************************************************************
 *           WAYLAND_WindowPosChanging
 */
BOOL WAYLAND_WindowPosChanging(HWND hwnd, HWND insert_after, UINT swp_flags,
                               const RECT *window_rect, const RECT *client_rect,
                               RECT *visible_rect, struct window_surface **surface)
{
    struct wayland_win_data *data = wayland_win_data_get(hwnd);
216 217 218
    HWND parent;
    BOOL visible;
    RECT surface_rect;
219 220 221 222 223 224 225

    TRACE("hwnd %p window %s client %s visible %s after %p flags %08x\n",
          hwnd, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
          wine_dbgstr_rect(visible_rect), insert_after, swp_flags);

    if (!data && !(data = wayland_win_data_create(hwnd))) return TRUE;

226 227 228 229 230 231 232 233
    /* Release the dummy surface wine provides for toplevels. */
    if (*surface) window_surface_release(*surface);
    *surface = NULL;

    parent = NtUserGetAncestor(hwnd, GA_PARENT);
    visible = ((NtUserGetWindowLongW(hwnd, GWL_STYLE) & WS_VISIBLE) ||
               (swp_flags & SWP_SHOWWINDOW)) &&
              !(swp_flags & SWP_HIDEWINDOW);
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    /* Check if we don't want a dedicated window surface. */
    if ((parent && parent != NtUserGetDesktopWindow()) || !visible) goto done;

    surface_rect = *window_rect;
    OffsetRect(&surface_rect, -surface_rect.left, -surface_rect.top);

    /* Check if we can reuse our current window surface. */
    if (data->window_surface &&
        EqualRect(&data->window_surface->rect, &surface_rect))
    {
        window_surface_add_ref(data->window_surface);
        *surface = data->window_surface;
        TRACE("reusing surface %p\n", *surface);
        goto done;
    }

    *surface = wayland_window_surface_create(data->hwnd, &surface_rect);

done:
    wayland_win_data_release(data);
255 256 257
    return TRUE;
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/***********************************************************************
 *           WAYLAND_WindowPosChanged
 */
void WAYLAND_WindowPosChanged(HWND hwnd, HWND insert_after, UINT swp_flags,
                              const RECT *window_rect, const RECT *client_rect,
                              const RECT *visible_rect, const RECT *valid_rects,
                              struct window_surface *surface)
{
    struct wayland_win_data *data = wayland_win_data_get(hwnd);

    TRACE("hwnd %p window %s client %s visible %s after %p flags %08x\n",
          hwnd, wine_dbgstr_rect(window_rect), wine_dbgstr_rect(client_rect),
          wine_dbgstr_rect(visible_rect), insert_after, swp_flags);

    if (!data) return;

274 275 276 277
    if (surface) window_surface_add_ref(surface);
    if (data->window_surface) window_surface_release(data->window_surface);
    data->window_surface = surface;

278 279 280 281 282
    wayland_win_data_update_wayland_surface(data);

    wayland_win_data_release(data);
}

283 284 285 286 287 288 289 290 291 292
static void wayland_resize_desktop(void)
{
    RECT virtual_rect = NtUserGetVirtualScreenRect();
    NtUserSetWindowPos(NtUserGetDesktopWindow(), 0,
                       virtual_rect.left, virtual_rect.top,
                       virtual_rect.right - virtual_rect.left,
                       virtual_rect.bottom - virtual_rect.top,
                       SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE);
}

293 294 295 296 297 298 299 300 301
/**********************************************************************
 *           WAYLAND_WindowMessage
 */
LRESULT WAYLAND_WindowMessage(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_WAYLAND_INIT_DISPLAY_DEVICES:
        wayland_init_display_devices(TRUE);
302
        wayland_resize_desktop();
303 304 305 306 307 308
        return 0;
    default:
        FIXME("got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, (long)wp, lp);
        return 0;
    }
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

/**********************************************************************
 *           WAYLAND_DesktopWindowProc
 */
LRESULT WAYLAND_DesktopWindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_DISPLAYCHANGE:
        wayland_resize_desktop();
        break;
    }

    return NtUserMessageCall(hwnd, msg, wp, lp, 0, NtUserDefWindowProc, FALSE);
}
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

/**********************************************************************
 *           wayland_window_flush
 *
 *  Flush the window_surface associated with a HWND.
 */
void wayland_window_flush(HWND hwnd)
{
    struct wayland_win_data *data = wayland_win_data_get(hwnd);

    if (!data) return;

    if (data->window_surface)
        data->window_surface->funcs->flush(data->window_surface);

    wayland_win_data_release(data);
}