riched32: Skeleton of richedit 1.0 test plus WM_SETTEXT test.
[wine] / server / clipboard.c
1 /*
2  * Server-side clipboard management
3  *
4  * Copyright (C) 2002 Ulrich Czekalla
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "request.h"
32 #include "object.h"
33 #include "user.h"
34 #include "winuser.h"
35 #include "winternl.h"
36
37 struct clipboard
38 {
39     struct object  obj;              /* object header */
40     struct thread *open_thread;      /* thread id that has clipboard open */
41     user_handle_t  open_win;         /* window that has clipboard open */
42     struct thread *owner_thread;     /* thread id that owns the clipboard */
43     user_handle_t  owner_win;        /* window that owns the clipboard data */
44     user_handle_t  viewer;           /* first window in clipboard viewer list */
45     unsigned int   seqno;            /* clipboard change sequence number */
46     time_t         seqno_timestamp;  /* time stamp of last seqno increment */
47 };
48
49 static void clipboard_dump( struct object *obj, int verbose );
50
51 static const struct object_ops clipboard_ops =
52 {
53     sizeof(struct clipboard),     /* size */
54     clipboard_dump,               /* dump */
55     no_add_queue,                 /* add_queue */
56     NULL,                         /* remove_queue */
57     NULL,                         /* signaled */
58     NULL,                         /* satisfied */
59     no_signal,                    /* signal */
60     no_get_fd,                    /* get_fd */
61     no_map_access,                /* map_access */
62     default_get_sd,               /* get_sd */
63     default_set_sd,               /* set_sd */
64     no_lookup_name,               /* lookup_name */
65     no_open_file,                 /* open_file */
66     no_close_handle,              /* close_handle */
67     no_destroy                    /* destroy */
68 };
69
70
71 #define MINUPDATELAPSE 2
72
73 /* dump a clipboard object */
74 static void clipboard_dump( struct object *obj, int verbose )
75 {
76     struct clipboard *clipboard = (struct clipboard *)obj;
77
78     fprintf( stderr, "Clipboard open_thread=%p open_win=%p owner_thread=%p owner_win=%p viewer=%p seq=%u\n",
79              clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
80              clipboard->owner_win, clipboard->viewer, clipboard->seqno );
81 }
82
83 /* retrieve the clipboard info for the current process, allocating it if needed */
84 static struct clipboard *get_process_clipboard(void)
85 {
86     struct clipboard *clipboard;
87     struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
88
89     if (!winstation) return NULL;
90
91     if (!(clipboard = winstation->clipboard))
92     {
93         if ((clipboard = alloc_object( &clipboard_ops )))
94         {
95             clipboard->open_thread = NULL;
96             clipboard->open_win = 0;
97             clipboard->owner_thread = NULL;
98             clipboard->owner_win = 0;
99             clipboard->viewer = 0;
100             clipboard->seqno = 0;
101             clipboard->seqno_timestamp = 0;
102             winstation->clipboard = clipboard;
103         }
104     }
105     release_object( winstation );
106     return clipboard;
107 }
108
109
110 /* Called when thread terminates to allow release of clipboard */
111 void cleanup_clipboard_thread(struct thread *thread)
112 {
113     struct clipboard *clipboard;
114     struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
115
116     if (!winstation) return;
117
118     if ((clipboard = winstation->clipboard))
119     {
120         if (thread == clipboard->open_thread)
121         {
122             clipboard->open_win = 0;
123             clipboard->open_thread = NULL;
124         }
125         if (thread == clipboard->owner_thread)
126         {
127             clipboard->owner_win = 0;
128             clipboard->owner_thread = NULL;
129         }
130     }
131     release_object( winstation );
132 }
133
134 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
135 {
136     if (clipboard->open_thread && clipboard->open_thread != current)
137     {
138         set_error(STATUS_WAS_LOCKED);
139         return 0;
140     }
141     else if (!clear)
142     {
143         clipboard->open_win = win;
144         clipboard->open_thread = current;
145     }
146     else
147     {
148         clipboard->open_thread = NULL;
149         clipboard->open_win = 0;
150     }
151     return 1;
152 }
153
154
155 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
156 {
157     if (clipboard->open_thread && clipboard->open_thread->process != current->process)
158     {
159         set_error(STATUS_WAS_LOCKED);
160         return 0;
161     }
162     else if (!clear)
163     {
164         clipboard->owner_win = win;
165         clipboard->owner_thread = current;
166     }
167     else
168     {
169         clipboard->owner_win = 0;
170         clipboard->owner_thread = NULL;
171     }
172     return 1;
173 }
174
175
176 static int get_seqno( struct clipboard *clipboard )
177 {
178     time_t tm = time(NULL);
179
180     if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
181     {
182         clipboard->seqno_timestamp = tm;
183         clipboard->seqno++;
184     }
185     return clipboard->seqno;
186 }
187
188
189 DECL_HANDLER(set_clipboard_info)
190 {
191     struct clipboard *clipboard = get_process_clipboard();
192
193     if (!clipboard) return;
194
195     reply->old_clipboard = clipboard->open_win;
196     reply->old_owner     = clipboard->owner_win;
197     reply->old_viewer    = clipboard->viewer;
198
199     if (req->flags & SET_CB_OPEN)
200     {
201         if (clipboard->open_thread)
202         {
203             /* clipboard already opened */
204             set_error(STATUS_WAS_LOCKED);
205             return;
206         }
207
208         if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
209     }
210     else if (req->flags & SET_CB_CLOSE)
211     {
212         if (clipboard->open_thread != current)
213         {
214             set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
215             return;
216         }
217
218         if (!set_clipboard_window( clipboard, 0, 1 )) return;
219     }
220
221     if (req->flags & SET_CB_OWNER)
222     {
223         if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
224     }
225     else if (req->flags & SET_CB_RELOWNER)
226     {
227         if (!set_clipboard_owner( clipboard, 0, 1 )) return;
228     }
229
230     if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
231
232     if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
233
234     reply->seqno = get_seqno( clipboard );
235
236     if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
237     if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
238     if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
239         reply->flags |= CB_PROCESS;
240 }