msi: Avoid using dialog record after free.
[wine/multimedia.git] / server / clipboard.c
blob2f56c72f676f538958e86647d3100be75cc9a99b
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "file.h"
34 #include "process.h"
35 #include "user.h"
36 #include "winuser.h"
37 #include "winternl.h"
39 struct clipboard
41 struct object obj; /* object header */
42 struct thread *open_thread; /* thread id that has clipboard open */
43 user_handle_t open_win; /* window that has clipboard open */
44 struct thread *owner_thread; /* thread id that owns the clipboard */
45 user_handle_t owner_win; /* window that owns the clipboard data */
46 user_handle_t viewer; /* first window in clipboard viewer list */
47 unsigned int seqno; /* clipboard change sequence number */
48 timeout_t seqno_timestamp; /* time stamp of last seqno increment */
51 static void clipboard_dump( struct object *obj, int verbose );
53 static const struct object_ops clipboard_ops =
55 sizeof(struct clipboard), /* size */
56 clipboard_dump, /* dump */
57 no_get_type, /* get_type */
58 no_add_queue, /* add_queue */
59 NULL, /* remove_queue */
60 NULL, /* signaled */
61 NULL, /* satisfied */
62 no_signal, /* signal */
63 no_get_fd, /* get_fd */
64 no_map_access, /* map_access */
65 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
67 no_lookup_name, /* lookup_name */
68 no_open_file, /* open_file */
69 no_close_handle, /* close_handle */
70 no_destroy /* destroy */
74 #define MINUPDATELAPSE (2 * TICKS_PER_SEC)
76 /* dump a clipboard object */
77 static void clipboard_dump( struct object *obj, int verbose )
79 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 clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
83 clipboard->owner_win, clipboard->viewer, clipboard->seqno );
86 /* retrieve the clipboard info for the current process, allocating it if needed */
87 static struct clipboard *get_process_clipboard(void)
89 struct clipboard *clipboard;
90 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
92 if (!winstation) return NULL;
94 if (!(clipboard = winstation->clipboard))
96 if ((clipboard = alloc_object( &clipboard_ops )))
98 clipboard->open_thread = NULL;
99 clipboard->open_win = 0;
100 clipboard->owner_thread = NULL;
101 clipboard->owner_win = 0;
102 clipboard->viewer = 0;
103 clipboard->seqno = 0;
104 clipboard->seqno_timestamp = 0;
105 winstation->clipboard = clipboard;
108 release_object( winstation );
109 return clipboard;
113 /* Called when thread terminates to allow release of clipboard */
114 void cleanup_clipboard_thread(struct thread *thread)
116 struct clipboard *clipboard;
117 struct winstation *winstation;
119 if (!thread->process->winstation) return;
120 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
122 if ((clipboard = winstation->clipboard))
124 if (thread == clipboard->open_thread)
126 clipboard->open_win = 0;
127 clipboard->open_thread = NULL;
129 if (thread == clipboard->owner_thread)
131 clipboard->owner_win = 0;
132 clipboard->owner_thread = NULL;
135 release_object( winstation );
138 static int open_clipboard( struct clipboard *clipboard, user_handle_t win )
140 win = get_user_full_handle( win );
141 if (clipboard->open_thread && clipboard->open_win != win)
143 set_error(STATUS_WAS_LOCKED);
144 return 0;
146 clipboard->open_win = win;
147 clipboard->open_thread = current;
148 return 1;
151 static int close_clipboard( struct clipboard *clipboard )
153 if (clipboard->open_thread != current)
155 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
156 return 0;
158 clipboard->open_thread = NULL;
159 clipboard->open_win = 0;
160 return 1;
163 static int release_clipboard_owner( struct clipboard *clipboard, user_handle_t win )
165 if ((clipboard->open_thread && clipboard->open_thread->process != current->process) ||
166 (win && clipboard->owner_win != get_user_full_handle( win )))
168 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
169 return 0;
171 clipboard->owner_win = 0;
172 clipboard->owner_thread = NULL;
173 return 1;
177 static int get_seqno( struct clipboard *clipboard )
179 if (!clipboard->owner_thread && (current_time - clipboard->seqno_timestamp > MINUPDATELAPSE))
181 clipboard->seqno_timestamp = current_time;
182 clipboard->seqno++;
184 return clipboard->seqno;
188 DECL_HANDLER(set_clipboard_info)
190 struct clipboard *clipboard = get_process_clipboard();
192 if (!clipboard) return;
194 reply->old_clipboard = clipboard->open_win;
195 reply->old_owner = clipboard->owner_win;
196 reply->old_viewer = clipboard->viewer;
198 if (req->flags & SET_CB_OPEN)
200 if (!open_clipboard( clipboard, req->clipboard )) return;
202 else if (req->flags & SET_CB_CLOSE)
204 if (!close_clipboard( clipboard )) return;
207 if (req->flags & SET_CB_RELOWNER)
209 if (!release_clipboard_owner( clipboard, req->owner )) return;
212 if (req->flags & SET_CB_VIEWER) clipboard->viewer = get_user_full_handle( req->viewer );
214 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
216 reply->seqno = get_seqno( clipboard );
218 if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
219 if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
220 if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
221 reply->flags |= CB_PROCESS;
225 /* empty the clipboard and grab ownership */
226 DECL_HANDLER(empty_clipboard)
228 struct clipboard *clipboard = get_process_clipboard();
230 if (!clipboard) return;
232 if (clipboard->open_thread != current)
234 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
235 return;
237 clipboard->owner_win = clipboard->open_win;
238 clipboard->owner_thread = clipboard->open_thread;
239 clipboard->seqno++;