Be more strict and verbose while testing
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "request.h"
30 #include "object.h"
31 #include "user.h"
32 #include "winuser.h"
33
34 struct clipboard
35 {
36     struct object  obj;              /* object header */
37     struct thread *open_thread;      /* thread id that has clipboard open */
38     user_handle_t  open_win;         /* window that has clipboard open */
39     struct thread *owner_thread;     /* thread id that owns the clipboard */
40     user_handle_t  owner_win;        /* window that owns the clipboard data */
41     user_handle_t  viewer;           /* first window in clipboard viewer list */
42     unsigned int   seqno;            /* clipboard change sequence number */
43     time_t         seqno_timestamp;  /* time stamp of last seqno increment */
44 };
45
46 static void clipboard_dump( struct object *obj, int verbose );
47
48 static const struct object_ops clipboard_ops =
49 {
50     sizeof(struct clipboard),     /* size */
51     clipboard_dump,               /* dump */
52     no_add_queue,                 /* add_queue */
53     NULL,                         /* remove_queue */
54     NULL,                         /* signaled */
55     NULL,                         /* satisfied */
56     no_signal,                    /* signal */
57     no_get_fd,                    /* get_fd */
58     no_lookup_name,               /* lookup_name */
59     no_close_handle,              /* close_handle */
60     no_destroy                    /* destroy */
61 };
62
63
64 #define MINUPDATELAPSE 2
65
66 /* dump a clipboard object */
67 static void clipboard_dump( struct object *obj, int verbose )
68 {
69     struct clipboard *clipboard = (struct clipboard *)obj;
70
71     fprintf( stderr, "Clipboard open_thread=%p open_win=%p owner_thread=%p owner_win=%p viewer=%p seq=%u\n",
72              clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
73              clipboard->owner_win, clipboard->viewer, clipboard->seqno );
74 }
75
76 /* retrieve the clipboard info for the current process, allocating it if needed */
77 static struct clipboard *get_process_clipboard(void)
78 {
79     struct clipboard *clipboard;
80     struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
81
82     if (!winstation) return NULL;
83
84     if (!(clipboard = winstation->clipboard))
85     {
86         if ((clipboard = alloc_object( &clipboard_ops )))
87         {
88             clipboard->open_thread = NULL;
89             clipboard->open_win = 0;
90             clipboard->owner_thread = NULL;
91             clipboard->owner_win = 0;
92             clipboard->viewer = 0;
93             clipboard->seqno = 0;
94             clipboard->seqno_timestamp = 0;
95             winstation->clipboard = clipboard;
96         }
97     }
98     release_object( winstation );
99     return clipboard;
100 }
101
102
103 /* Called when thread terminates to allow release of clipboard */
104 void cleanup_clipboard_thread(struct thread *thread)
105 {
106     struct clipboard *clipboard;
107     struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
108
109     if (!winstation) return;
110
111     if ((clipboard = winstation->clipboard))
112     {
113         if (thread == clipboard->open_thread)
114         {
115             clipboard->open_win = 0;
116             clipboard->open_thread = NULL;
117         }
118         if (thread == clipboard->owner_thread)
119         {
120             clipboard->owner_win = 0;
121             clipboard->owner_thread = NULL;
122         }
123     }
124     release_object( winstation );
125 }
126
127 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
128 {
129     if (clipboard->open_thread && clipboard->open_thread != current)
130     {
131         set_error(STATUS_WAS_LOCKED);
132         return 0;
133     }
134     else if (!clear)
135     {
136         clipboard->open_win = win;
137         clipboard->open_thread = current;
138     }
139     else
140     {
141         clipboard->open_thread = NULL;
142         clipboard->open_win = 0;
143     }
144     return 1;
145 }
146
147
148 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
149 {
150     if (clipboard->open_thread && clipboard->open_thread->process != current->process)
151     {
152         set_error(STATUS_WAS_LOCKED);
153         return 0;
154     }
155     else if (!clear)
156     {
157         clipboard->owner_win = win;
158         clipboard->owner_thread = current;
159     }
160     else
161     {
162         clipboard->owner_win = 0;
163         clipboard->owner_thread = NULL;
164     }
165     return 1;
166 }
167
168
169 static int get_seqno( struct clipboard *clipboard )
170 {
171     time_t tm = time(NULL);
172
173     if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
174     {
175         clipboard->seqno_timestamp = tm;
176         clipboard->seqno++;
177     }
178     return clipboard->seqno;
179 }
180
181
182 DECL_HANDLER(set_clipboard_info)
183 {
184     struct clipboard *clipboard = get_process_clipboard();
185
186     if (!clipboard) return;
187
188     reply->old_clipboard = clipboard->open_win;
189     reply->old_owner     = clipboard->owner_win;
190     reply->old_viewer    = clipboard->viewer;
191
192     if (req->flags & SET_CB_OPEN)
193     {
194         if (clipboard->open_thread)
195         {
196             /* clipboard already opened */
197             set_error(STATUS_WAS_LOCKED);
198             return;
199         }
200
201         if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
202     }
203     else if (req->flags & SET_CB_CLOSE)
204     {
205         if (clipboard->open_thread != current)
206         {
207             set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
208             return;
209         }
210
211         if (!set_clipboard_window( clipboard, 0, 1 )) return;
212     }
213
214     if (req->flags & SET_CB_OWNER)
215     {
216         if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
217     }
218     else if (req->flags & SET_CB_RELOWNER)
219     {
220         if (!set_clipboard_owner( clipboard, 0, 1 )) return;
221     }
222
223     if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
224
225     if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
226
227     reply->seqno = get_seqno( clipboard );
228
229     if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
230     if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
231     if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
232         reply->flags |= CB_PROCESS;
233 }