Added a separate set_handle_cached_fd request instead of abusing
[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_close_handle,              /* close_handle */
59     no_destroy                    /* destroy */
60 };
61
62
63 #define MINUPDATELAPSE 2
64
65 /* dump a clipboard object */
66 static void clipboard_dump( struct object *obj, int verbose )
67 {
68     struct clipboard *clipboard = (struct clipboard *)obj;
69
70     fprintf( stderr, "Clipboard open_thread=%p open_win=%p owner_thread=%p owner_win=%p viewer=%p seq=%u\n",
71              clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
72              clipboard->owner_win, clipboard->viewer, clipboard->seqno );
73 }
74
75 /* retrieve the clipboard info for the current process, allocating it if needed */
76 static struct clipboard *get_process_clipboard(void)
77 {
78     struct clipboard *clipboard;
79     struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
80
81     if (!winstation) return NULL;
82
83     if (!(clipboard = winstation->clipboard))
84     {
85         if ((clipboard = alloc_object( &clipboard_ops )))
86         {
87             clipboard->open_thread = NULL;
88             clipboard->open_win = 0;
89             clipboard->owner_thread = NULL;
90             clipboard->owner_win = 0;
91             clipboard->viewer = 0;
92             clipboard->seqno = 0;
93             clipboard->seqno_timestamp = 0;
94             winstation->clipboard = clipboard;
95         }
96     }
97     release_object( winstation );
98     return clipboard;
99 }
100
101
102 /* Called when thread terminates to allow release of clipboard */
103 void cleanup_clipboard_thread(struct thread *thread)
104 {
105     struct clipboard *clipboard;
106     struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
107
108     if (!winstation) return;
109
110     if ((clipboard = winstation->clipboard))
111     {
112         if (thread == clipboard->open_thread)
113         {
114             clipboard->open_win = 0;
115             clipboard->open_thread = NULL;
116         }
117         if (thread == clipboard->owner_thread)
118         {
119             clipboard->owner_win = 0;
120             clipboard->owner_thread = NULL;
121         }
122     }
123     release_object( winstation );
124 }
125
126 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
127 {
128     if (clipboard->open_thread && clipboard->open_thread != current)
129     {
130         set_error(STATUS_WAS_LOCKED);
131         return 0;
132     }
133     else if (!clear)
134     {
135         clipboard->open_win = win;
136         clipboard->open_thread = current;
137     }
138     else
139     {
140         clipboard->open_thread = NULL;
141         clipboard->open_win = 0;
142     }
143     return 1;
144 }
145
146
147 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
148 {
149     if (clipboard->open_thread && clipboard->open_thread->process != current->process)
150     {
151         set_error(STATUS_WAS_LOCKED);
152         return 0;
153     }
154     else if (!clear)
155     {
156         clipboard->owner_win = win;
157         clipboard->owner_thread = current;
158     }
159     else
160     {
161         clipboard->owner_win = 0;
162         clipboard->owner_thread = NULL;
163     }
164     return 1;
165 }
166
167
168 static int get_seqno( struct clipboard *clipboard )
169 {
170     time_t tm = time(NULL);
171
172     if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
173     {
174         clipboard->seqno_timestamp = tm;
175         clipboard->seqno++;
176     }
177     return clipboard->seqno;
178 }
179
180
181 DECL_HANDLER(set_clipboard_info)
182 {
183     struct clipboard *clipboard = get_process_clipboard();
184
185     if (!clipboard) return;
186
187     reply->old_clipboard = clipboard->open_win;
188     reply->old_owner     = clipboard->owner_win;
189     reply->old_viewer    = clipboard->viewer;
190
191     if (req->flags & SET_CB_OPEN)
192     {
193         if (clipboard->open_thread)
194         {
195             /* clipboard already opened */
196             set_error(STATUS_WAS_LOCKED);
197             return;
198         }
199
200         if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
201     }
202     else if (req->flags & SET_CB_CLOSE)
203     {
204         if (clipboard->open_thread != current)
205         {
206             set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
207             return;
208         }
209
210         if (!set_clipboard_window( clipboard, 0, 1 )) return;
211     }
212
213     if (req->flags & SET_CB_OWNER)
214     {
215         if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
216     }
217     else if (req->flags & SET_CB_RELOWNER)
218     {
219         if (!set_clipboard_owner( clipboard, 0, 1 )) return;
220     }
221
222     if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
223
224     if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
225
226     reply->seqno = get_seqno( clipboard );
227
228     if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
229     if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
230     if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
231         reply->flags |= CB_PROCESS;
232 }