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

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

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

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

39 40 41 42 43 44 45 46 47
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 */
    struct thread *owner_thread;     /* thread id that owns the clipboard */
    user_handle_t  owner_win;        /* window that owns the clipboard data */
    user_handle_t  viewer;           /* first window in clipboard viewer list */
    unsigned int   seqno;            /* clipboard change sequence number */
48
    timeout_t      seqno_timestamp;  /* time stamp of last seqno increment */
49 50 51 52 53 54 55 56
};

static void clipboard_dump( struct object *obj, int verbose );

static const struct object_ops clipboard_ops =
{
    sizeof(struct clipboard),     /* size */
    clipboard_dump,               /* dump */
57
    no_get_type,                  /* get_type */
58 59 60 61 62 63
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
    no_get_fd,                    /* get_fd */
64
    no_map_access,                /* map_access */
65 66
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
67
    no_lookup_name,               /* lookup_name */
68
    no_open_file,                 /* open_file */
69 70 71
    no_close_handle,              /* close_handle */
    no_destroy                    /* destroy */
};
72 73


74
#define MINUPDATELAPSE (2 * TICKS_PER_SEC)
75

76 77 78 79 80
/* dump a clipboard object */
static void clipboard_dump( struct object *obj, int verbose )
{
    struct clipboard *clipboard = (struct clipboard *)obj;

81
    fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner_thread=%p owner_win=%08x viewer=%08x seq=%u\n",
82 83 84 85 86 87
             clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
             clipboard->owner_win, clipboard->viewer, clipboard->seqno );
}

/* retrieve the clipboard info for the current process, allocating it if needed */
static struct clipboard *get_process_clipboard(void)
88
{
89 90 91 92 93
    struct clipboard *clipboard;
    struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );

    if (!winstation) return NULL;

94
    if (!(clipboard = winstation->clipboard))
95
    {
96 97 98 99 100 101 102 103 104
        if ((clipboard = alloc_object( &clipboard_ops )))
        {
            clipboard->open_thread = NULL;
            clipboard->open_win = 0;
            clipboard->owner_thread = NULL;
            clipboard->owner_win = 0;
            clipboard->viewer = 0;
            clipboard->seqno = 0;
            clipboard->seqno_timestamp = 0;
105
            winstation->clipboard = clipboard;
106
        }
107
    }
108 109 110 111 112 113 114 115 116
    release_object( winstation );
    return clipboard;
}


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

119 120
    if (!thread->process->winstation) return;
    if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
121

122
    if ((clipboard = winstation->clipboard))
123
    {
124 125 126 127 128 129 130 131 132 133
        if (thread == clipboard->open_thread)
        {
            clipboard->open_win = 0;
            clipboard->open_thread = NULL;
        }
        if (thread == clipboard->owner_thread)
        {
            clipboard->owner_win = 0;
            clipboard->owner_thread = NULL;
        }
134
    }
135
    release_object( winstation );
136 137
}

138
static int open_clipboard( struct clipboard *clipboard, user_handle_t win )
139
{
140
    win = get_user_full_handle( win );
141
    if (clipboard->open_thread && clipboard->open_win != win)
142 143 144 145
    {
        set_error(STATUS_WAS_LOCKED);
        return 0;
    }
146 147 148 149 150 151 152 153
    clipboard->open_win = win;
    clipboard->open_thread = current;
    return 1;
}

static int close_clipboard( struct clipboard *clipboard )
{
    if (clipboard->open_thread != current)
154
    {
155 156
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
        return 0;
157
    }
158 159
    clipboard->open_thread = NULL;
    clipboard->open_win = 0;
160 161 162
    return 1;
}

163
static int release_clipboard_owner( struct clipboard *clipboard, user_handle_t win )
164
{
165 166
    if ((clipboard->open_thread && clipboard->open_thread->process != current->process) ||
        (win && clipboard->owner_win != get_user_full_handle( win )))
167
    {
168
        set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
169
        return 0;
170
    }
171 172
    clipboard->owner_win = 0;
    clipboard->owner_thread = NULL;
173
    return 1;
174 175 176
}


177
static int get_seqno( struct clipboard *clipboard )
178
{
179
    if (!clipboard->owner_thread && (current_time - clipboard->seqno_timestamp > MINUPDATELAPSE))
180
    {
181
        clipboard->seqno_timestamp = current_time;
182
        clipboard->seqno++;
183
    }
184
    return clipboard->seqno;
185 186 187 188 189
}


DECL_HANDLER(set_clipboard_info)
{
190 191 192 193 194 195 196
    struct clipboard *clipboard = get_process_clipboard();

    if (!clipboard) return;

    reply->old_clipboard = clipboard->open_win;
    reply->old_owner     = clipboard->owner_win;
    reply->old_viewer    = clipboard->viewer;
197 198 199

    if (req->flags & SET_CB_OPEN)
    {
200
        if (!open_clipboard( clipboard, req->clipboard )) return;
201 202 203
    }
    else if (req->flags & SET_CB_CLOSE)
    {
204
        if (!close_clipboard( clipboard )) return;
205 206
    }

207
    if (req->flags & SET_CB_RELOWNER)
208
    {
209
        if (!release_clipboard_owner( clipboard, req->owner )) return;
210 211
    }

212
    if (req->flags & SET_CB_VIEWER) clipboard->viewer = get_user_full_handle( req->viewer );
213

214
    if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
215

216
    reply->seqno = get_seqno( clipboard );
217

218 219 220
    if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
    if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
    if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
221
        reply->flags |= CB_PROCESS;
222
}
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240


/* 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;
    }
    clipboard->owner_win = clipboard->open_win;
    clipboard->owner_thread = clipboard->open_thread;
    clipboard->seqno++;
}