server: Use attributes instead of inherit flag in console requests.
[wine/multimedia.git] / server / clipboard.c
blob3aa7f01bf1ecc608cb5e07d96862aeb957fc80f4
1 /*
2 * Server-side clipboard management
4 * Copyright (C) 2002 Ulrich Czekalla
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
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"
37 struct clipboard
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 */
49 static void clipboard_dump( struct object *obj, int verbose );
51 static const struct object_ops clipboard_ops =
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_lookup_name, /* lookup_name */
62 no_close_handle, /* close_handle */
63 no_destroy /* destroy */
67 #define MINUPDATELAPSE 2
69 /* dump a clipboard object */
70 static void clipboard_dump( struct object *obj, int verbose )
72 struct clipboard *clipboard = (struct clipboard *)obj;
74 fprintf( stderr, "Clipboard open_thread=%p open_win=%p owner_thread=%p owner_win=%p viewer=%p seq=%u\n",
75 clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
76 clipboard->owner_win, clipboard->viewer, clipboard->seqno );
79 /* retrieve the clipboard info for the current process, allocating it if needed */
80 static struct clipboard *get_process_clipboard(void)
82 struct clipboard *clipboard;
83 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
85 if (!winstation) return NULL;
87 if (!(clipboard = winstation->clipboard))
89 if ((clipboard = alloc_object( &clipboard_ops )))
91 clipboard->open_thread = NULL;
92 clipboard->open_win = 0;
93 clipboard->owner_thread = NULL;
94 clipboard->owner_win = 0;
95 clipboard->viewer = 0;
96 clipboard->seqno = 0;
97 clipboard->seqno_timestamp = 0;
98 winstation->clipboard = clipboard;
101 release_object( winstation );
102 return clipboard;
106 /* Called when thread terminates to allow release of clipboard */
107 void cleanup_clipboard_thread(struct thread *thread)
109 struct clipboard *clipboard;
110 struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
112 if (!winstation) return;
114 if ((clipboard = winstation->clipboard))
116 if (thread == clipboard->open_thread)
118 clipboard->open_win = 0;
119 clipboard->open_thread = NULL;
121 if (thread == clipboard->owner_thread)
123 clipboard->owner_win = 0;
124 clipboard->owner_thread = NULL;
127 release_object( winstation );
130 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
132 if (clipboard->open_thread && clipboard->open_thread != current)
134 set_error(STATUS_WAS_LOCKED);
135 return 0;
137 else if (!clear)
139 clipboard->open_win = win;
140 clipboard->open_thread = current;
142 else
144 clipboard->open_thread = NULL;
145 clipboard->open_win = 0;
147 return 1;
151 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
153 if (clipboard->open_thread && clipboard->open_thread->process != current->process)
155 set_error(STATUS_WAS_LOCKED);
156 return 0;
158 else if (!clear)
160 clipboard->owner_win = win;
161 clipboard->owner_thread = current;
163 else
165 clipboard->owner_win = 0;
166 clipboard->owner_thread = NULL;
168 return 1;
172 static int get_seqno( struct clipboard *clipboard )
174 time_t tm = time(NULL);
176 if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
178 clipboard->seqno_timestamp = tm;
179 clipboard->seqno++;
181 return clipboard->seqno;
185 DECL_HANDLER(set_clipboard_info)
187 struct clipboard *clipboard = get_process_clipboard();
189 if (!clipboard) return;
191 reply->old_clipboard = clipboard->open_win;
192 reply->old_owner = clipboard->owner_win;
193 reply->old_viewer = clipboard->viewer;
195 if (req->flags & SET_CB_OPEN)
197 if (clipboard->open_thread)
199 /* clipboard already opened */
200 set_error(STATUS_WAS_LOCKED);
201 return;
204 if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
206 else if (req->flags & SET_CB_CLOSE)
208 if (clipboard->open_thread != current)
210 set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
211 return;
214 if (!set_clipboard_window( clipboard, 0, 1 )) return;
217 if (req->flags & SET_CB_OWNER)
219 if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
221 else if (req->flags & SET_CB_RELOWNER)
223 if (!set_clipboard_owner( clipboard, 0, 1 )) return;
226 if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
228 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
230 reply->seqno = get_seqno( clipboard );
232 if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
233 if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
234 if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
235 reply->flags |= CB_PROCESS;