clipboard.c 19.1 KB
Newer Older
1 2 3
/*
 * Server-side clipboard management
 *
4 5
 * Copyright 2002 Ulrich Czekalla
 * Copyright 2016 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

30 31
#include "ntstatus.h"
#define WIN32_NO_STATUS
32 33
#include "request.h"
#include "object.h"
34
#include "file.h"
35
#include "process.h"
36
#include "user.h"
37
#include "winuser.h"
38
#include "winternl.h"
39

40 41 42 43
struct clip_format
{
    struct list    entry;            /* entry in format list */
    unsigned int   id;               /* format id */
44
    unsigned int   from;             /* for synthesized data, format to generate it from */
45
    unsigned int   seqno;            /* sequence number when the data was set */
46 47 48 49
    data_size_t    size;             /* size of the data block */
    void          *data;             /* data contents, or NULL for delay-rendered */
};

50 51 52 53 54
struct clipboard
{
    struct object  obj;              /* object header */
    struct thread *open_thread;      /* thread id that has clipboard open */
    user_handle_t  open_win;         /* window that has clipboard open */
55
    user_handle_t  owner;            /* window that owns the clipboard */
56
    user_handle_t  viewer;           /* first window in clipboard viewer list */
57
    unsigned int   lcid;             /* locale id to use for synthesizing text formats */
58
    unsigned int   seqno;            /* clipboard change sequence number */
59
    unsigned int   open_seqno;       /* sequence number at open time */
60
    unsigned int   rendering;        /* format rendering recursion counter */
61 62
    struct list    formats;          /* list of data formats */
    unsigned int   format_count;     /* count of data formats */
63
    unsigned int   format_map;       /* existence bitmap for formats < CF_MAX */
64 65 66
    unsigned int   listen_size;      /* size of listeners array */
    unsigned int   listen_count;     /* count of listeners */
    user_handle_t *listeners;        /* array of listener windows */
67 68 69
};

static void clipboard_dump( struct object *obj, int verbose );
70
static void clipboard_destroy( struct object *obj );
71 72 73 74 75

static const struct object_ops clipboard_ops =
{
    sizeof(struct clipboard),     /* size */
    clipboard_dump,               /* dump */
76
    no_get_type,                  /* get_type */
77 78 79 80 81 82
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
    no_get_fd,                    /* get_fd */
83
    no_map_access,                /* map_access */
84 85
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
86
    no_lookup_name,               /* lookup_name */
87 88
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
89
    no_open_file,                 /* open_file */
90
    no_kernel_obj_list,           /* get_kernel_obj_list */
91
    no_close_handle,              /* close_handle */
92
    clipboard_destroy             /* destroy */
93
};
94 95


96 97
#define HAS_FORMAT(map,id) ((map) & (1 << (id)))  /* only for formats < CF_MAX */

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* find a data format in the clipboard */
static struct clip_format *get_format( struct clipboard *clipboard, unsigned int id )
{
    struct clip_format *format;

    LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
        if (format->id == id) return format;

    return NULL;
}

/* add a data format to the clipboard */
static struct clip_format *add_format( struct clipboard *clipboard, unsigned int id )
{
    struct clip_format *format;

    if (!(format = mem_alloc( sizeof(*format )))) return NULL;
    format->id   = id;
116
    format->from = 0;
117 118 119 120
    format->size = 0;
    format->data = NULL;
    list_add_tail( &clipboard->formats, &format->entry );
    clipboard->format_count++;
121
    if (id < CF_MAX) clipboard->format_map |= 1 << id;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    return format;
}

/* free all clipboard formats */
static void free_clipboard_formats( struct clipboard *clipboard )
{
    struct clip_format *format, *next;

    LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
    {
        list_remove( &format->entry );
        free( format->data );
        free( format );
    }
    clipboard->format_count = 0;
137
    clipboard->format_map = 0;
138 139
}

140 141 142 143 144
/* dump a clipboard object */
static void clipboard_dump( struct object *obj, int verbose )
{
    struct clipboard *clipboard = (struct clipboard *)obj;

145 146 147
    fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner=%08x viewer=%08x seq=%u\n",
             clipboard->open_thread, clipboard->open_win,
             clipboard->owner, clipboard->viewer, clipboard->seqno );
148 149
}

150 151 152 153 154
static void clipboard_destroy( struct object *obj )
{
    struct clipboard *clipboard = (struct clipboard *)obj;

    free( clipboard->listeners );
155
    free_clipboard_formats( clipboard );
156 157
}

158 159
/* retrieve the clipboard info for the current process, allocating it if needed */
static struct clipboard *get_process_clipboard(void)
160
{
161 162 163 164 165
    struct clipboard *clipboard;
    struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );

    if (!winstation) return NULL;

166
    if (!(clipboard = winstation->clipboard))
167
    {
168 169 170 171
        if ((clipboard = alloc_object( &clipboard_ops )))
        {
            clipboard->open_thread = NULL;
            clipboard->open_win = 0;
172
            clipboard->owner = 0;
173 174
            clipboard->viewer = 0;
            clipboard->seqno = 0;
175
            clipboard->format_count = 0;
176
            clipboard->format_map = 0;
177 178 179
            clipboard->listen_size = 0;
            clipboard->listen_count = 0;
            clipboard->listeners = NULL;
180
            list_init( &clipboard->formats );
181
            winstation->clipboard = clipboard;
182
        }
183
    }
184 185 186 187
    release_object( winstation );
    return clipboard;
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/* add synthesized formats upon clipboard close */
static int synthesize_formats( struct clipboard *clipboard )
{
    static const unsigned int formats[][3] =
    {
        { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT },
        { CF_OEMTEXT, CF_UNICODETEXT, CF_TEXT },
        { CF_UNICODETEXT, CF_TEXT, CF_OEMTEXT },
        { CF_METAFILEPICT, CF_ENHMETAFILE },
        { CF_ENHMETAFILE, CF_METAFILEPICT },
        { CF_BITMAP, CF_DIB, CF_DIBV5 },
        { CF_DIB, CF_BITMAP, CF_DIBV5 },
        { CF_DIBV5, CF_BITMAP, CF_DIB }
    };
    unsigned int i, from, total = 0, map = clipboard->format_map;
    struct clip_format *format;

    if (!HAS_FORMAT( map, CF_LOCALE ) &&
        (HAS_FORMAT( map, CF_TEXT ) || HAS_FORMAT( map, CF_OEMTEXT ) || HAS_FORMAT( map, CF_UNICODETEXT )))
    {
        void *data = memdup( &clipboard->lcid, sizeof(clipboard->lcid) );
209
        if (data && (format = add_format( clipboard, CF_LOCALE )))
210
        {
211
            format->seqno = clipboard->seqno++;
212 213 214 215 216 217
            format->data  = data;
            format->size  = sizeof(clipboard->lcid);
        }
        else free( data );
    }

218
    for (i = 0; i < ARRAY_SIZE( formats ); i++)
219 220 221 222 223 224 225
    {
        if (HAS_FORMAT( map, formats[i][0] )) continue;
        if (HAS_FORMAT( map, formats[i][1] )) from = formats[i][1];
        else if (HAS_FORMAT( map, formats[i][2] )) from = formats[i][2];
        else continue;
        if (!(format = add_format( clipboard, formats[i][0] ))) continue;
        format->from = from;
226
        format->seqno = clipboard->seqno;
227 228 229 230 231
        total++;
    }
    return total;
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/* add a clipboard listener */
static void add_listener( struct clipboard *clipboard, user_handle_t window )
{
    unsigned int i;

    for (i = 0; i < clipboard->listen_count; i++)
    {
        if (clipboard->listeners[i] != window) continue;
        set_error( STATUS_INVALID_PARAMETER );  /* already set */
        return;
    }
    if (clipboard->listen_size == clipboard->listen_count)
    {
        unsigned int new_size = max( 8, clipboard->listen_size * 2 );
        user_handle_t *new = realloc( clipboard->listeners, new_size * sizeof(*new) );
        if (!new)
        {
            set_error( STATUS_NO_MEMORY );
            return;
        }
        clipboard->listeners = new;
        clipboard->listen_size = new_size;
    }
    clipboard->listeners[clipboard->listen_count++] = window;
}

/* remove a clipboard listener */
static int remove_listener( struct clipboard *clipboard, user_handle_t window )
{
    unsigned int i;

    for (i = 0; i < clipboard->listen_count; i++)
    {
        if (clipboard->listeners[i] != window) continue;
        memmove( clipboard->listeners + i, clipboard->listeners + i + 1,
                 (clipboard->listen_count - i - 1) * sizeof(*clipboard->listeners) );
        clipboard->listen_count--;
        return 1;
    }
    return 0;
}

274 275
/* notify all listeners, and return the viewer window that should be notified if any */
static user_handle_t notify_listeners( struct clipboard *clipboard )
276 277 278 279 280 281 282 283
{
    unsigned int i;

    for (i = 0; i < clipboard->listen_count; i++)
        post_message( clipboard->listeners[i], WM_CLIPBOARDUPDATE, 0, 0 );
    return clipboard->viewer;
}

284 285 286 287 288 289
/* close the clipboard, and return the viewer window that should be notified if any */
static user_handle_t close_clipboard( struct clipboard *clipboard )
{
    clipboard->open_win = 0;
    clipboard->open_thread = NULL;
    if (clipboard->seqno == clipboard->open_seqno) return 0;  /* unchanged */
290
    if (synthesize_formats( clipboard )) clipboard->seqno++;
291 292 293
    return notify_listeners( clipboard );
}

294 295 296
/* release the clipboard owner, and return the viewer window that should be notified if any */
static user_handle_t release_clipboard( struct clipboard *clipboard )
{
297 298 299
    struct clip_format *format, *next;
    int changed = 0;

300
    clipboard->owner = 0;
301 302 303 304

    /* free the delayed-rendered formats, since we no longer have an owner to render them */
    LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
    {
305
        if (format->data || format->from) continue;
306
        list_remove( &format->entry );
307
        if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
308 309 310 311 312 313 314 315
        clipboard->format_count--;
        free( format );
        changed = 1;
    }

    if (!changed) return 0;
    clipboard->seqno++;
    return notify_listeners( clipboard );
316 317
}

318 319 320 321 322 323 324
/* cleanup clipboard information upon window destruction */
void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
{
    struct clipboard *clipboard = desktop->winstation->clipboard;

    if (!clipboard) return;

325 326
    remove_listener( clipboard, window );
    if (clipboard->viewer == window) clipboard->viewer = 0;
327
    if (clipboard->owner == window) release_clipboard( clipboard );
328 329 330
    if (clipboard->open_win == window)
    {
        user_handle_t viewer = close_clipboard( clipboard );
331
        if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
332
    }
333
}
334 335 336 337 338

/* Called when thread terminates to allow release of clipboard */
void cleanup_clipboard_thread(struct thread *thread)
{
    struct clipboard *clipboard;
339
    struct winstation *winstation;
340

341 342
    if (!thread->process->winstation) return;
    if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
343

344
    if ((clipboard = winstation->clipboard))
345
    {
346 347 348
        if (thread == clipboard->open_thread)
        {
            user_handle_t viewer = close_clipboard( clipboard );
349
            if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
350
        }
351
    }
352
    release_object( winstation );
353 354
}

355 356
/* open the clipboard */
DECL_HANDLER(open_clipboard)
357
{
358
    struct clipboard *clipboard = get_process_clipboard();
359
    user_handle_t win = 0;
360 361

    if (!clipboard) return;
362
    if (req->window && !(win = get_valid_window_handle( req->window ))) return;
363

364
    if (clipboard->open_thread && clipboard->open_win != win)
365
    {
366
        set_error( STATUS_INVALID_LOCK_SEQUENCE );
367
        return;
368
    }
369 370

    if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
371
    if (clipboard->open_thread != current) clipboard->rendering = 0;
372 373 374
    clipboard->open_win = win;
    clipboard->open_thread = current;

375
    reply->owner = clipboard->owner;
376 377 378 379 380 381 382 383 384 385 386
}


/* close the clipboard */
DECL_HANDLER(close_clipboard)
{
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    if (clipboard->open_thread != current)
387
    {
388 389
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return;
390
    }
391
    reply->viewer = close_clipboard( clipboard );
392
    reply->owner  = clipboard->owner;
393 394 395
}


396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/* add a data format to the clipboard */
DECL_HANDLER(set_clipboard_data)
{
    struct clip_format *format;
    struct clipboard *clipboard = get_process_clipboard();
    void *data = NULL;

    if (!clipboard) return;

    if (!req->format || !clipboard->open_thread)
    {
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return;
    }

    if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;

    if (!(format = get_format( clipboard, req->format )))
    {
        if (!(format = add_format( clipboard, req->format )))
        {
            free( data );
            return;
        }
    }

422
    free( format->data );
423
    format->from   = 0;
424
    format->seqno  = clipboard->seqno;
425 426
    format->size   = get_req_data_size();
    format->data   = data;
427
    if (!clipboard->rendering) clipboard->seqno++;
428 429 430

    if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
        clipboard->lcid = req->lcid;
431 432

    reply->seqno = format->seqno;
433 434 435
}


436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
/* fetch a data format from the clipboard */
DECL_HANDLER(get_clipboard_data)
{
    struct clip_format *format;
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    if (clipboard->open_thread != current)
    {
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return;
    }
    if (!(format = get_format( clipboard, req->format )))
    {
        set_error( STATUS_OBJECT_NAME_NOT_FOUND );
452
        goto done;
453
    }
454
    reply->from   = format->from;
455
    reply->total  = format->size;
456
    reply->seqno  = format->seqno;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    reply->owner  = clipboard->owner;

    if (!format->data && req->render)  /* try rendering it client-side */
    {
        if (format->from || clipboard->owner) clipboard->rendering++;
        return;
    }

    if (req->cached && req->seqno == format->seqno) goto done;  /* client-side cache still valid */

    if (format->size > get_reply_max_size())
    {
        set_error( STATUS_BUFFER_OVERFLOW );
        return;
    }
    set_reply_data( format->data, format->size );

done:
    if (!req->render) clipboard->rendering--;
476 477 478
}


479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/* retrieve a list of available formats */
DECL_HANDLER(get_clipboard_formats)
{
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    if (!req->format)
    {
        struct clip_format *format;
        unsigned int i = 0, *ptr;
        data_size_t size = clipboard->format_count * sizeof(unsigned int);

        reply->count = clipboard->format_count;
        if (size <= get_reply_max_size())
        {
            if ((ptr = mem_alloc( size )))
            {
                LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
                    ptr[i++] = format->id;
                assert( i == clipboard->format_count );
                set_reply_data_ptr( ptr, size );
            }
        }
        else set_error( STATUS_BUFFER_TOO_SMALL );
    }
    else reply->count = (get_format( clipboard, req->format ) != NULL);  /* query a single format */
}


/* retrieve the next available format */
DECL_HANDLER(enum_clipboard_formats)
{
    struct list *ptr;
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    if (clipboard->open_thread != current)
    {
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return;
    }

    ptr = list_head( &clipboard->formats );
    if (req->previous)
    {
        while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
            ptr = list_next( &clipboard->formats, ptr );
        if (ptr) ptr = list_next( &clipboard->formats, ptr );
    }
    if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
}


534 535 536 537 538 539 540 541 542 543 544 545
/* empty the clipboard and grab ownership */
DECL_HANDLER(empty_clipboard)
{
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    if (clipboard->open_thread != current)
    {
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return;
    }
546 547

    free_clipboard_formats( clipboard );
548
    clipboard->owner = clipboard->open_win;
549 550
    clipboard->seqno++;
}
551 552


553 554 555 556 557 558 559 560 561 562
/* release ownership of the clipboard */
DECL_HANDLER(release_clipboard)
{
    struct clipboard *clipboard = get_process_clipboard();
    user_handle_t owner;

    if (!clipboard) return;

    if (!(owner = get_valid_window_handle( req->owner ))) return;

563
    if (clipboard->owner == owner)
564
    {
565
        reply->viewer = release_clipboard( clipboard );
566
        reply->owner = clipboard->owner;
567 568
    }
    else set_error( STATUS_INVALID_OWNER );
569 570 571 572
}


/* get clipboard information */
573 574 575 576 577 578 579
DECL_HANDLER(get_clipboard_info)
{
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    reply->window = clipboard->open_win;
580
    reply->owner  = clipboard->owner;
581
    reply->viewer = clipboard->viewer;
582
    reply->seqno  = clipboard->seqno;
583 584 585
}


586 587 588 589
/* set the clipboard viewer window */
DECL_HANDLER(set_clipboard_viewer)
{
    struct clipboard *clipboard = get_process_clipboard();
590
    user_handle_t viewer = 0, previous = 0;
591 592

    if (!clipboard) return;
593 594
    if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
    if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
595

596
    reply->old_viewer = clipboard->viewer;
597
    reply->owner      = clipboard->owner;
598

599 600
    if (!previous || clipboard->viewer == previous)
        clipboard->viewer = viewer;
601 602 603
    else
        set_error( STATUS_PENDING );  /* need to send message instead */
}
604 605 606 607 608 609


/* add a clipboard listener window */
DECL_HANDLER(add_clipboard_listener)
{
    struct clipboard *clipboard = get_process_clipboard();
610
    user_handle_t win;
611 612

    if (!clipboard) return;
613
    if (!(win = get_valid_window_handle( req->window ))) return;
614 615 616 617 618 619 620 621 622

    add_listener( clipboard, win );
}


/* remove a clipboard listener window */
DECL_HANDLER(remove_clipboard_listener)
{
    struct clipboard *clipboard = get_process_clipboard();
623
    user_handle_t win;
624 625

    if (!clipboard) return;
626
    if (!(win = get_valid_window_handle( req->window ))) return;
627 628 629

    if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );
}