Add some 64 bit definitions.
[wine/multimedia.git] / server / clipboard.c
blob26570dba4c49f900236ad42e9b274a21cfcfd943
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 "request.h"
30 #include "object.h"
31 #include "user.h"
32 #include "winuser.h"
34 struct clipboard
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 */
46 static void clipboard_dump( struct object *obj, int verbose );
48 static const struct object_ops clipboard_ops =
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 */
64 #define MINUPDATELAPSE 2
66 /* dump a clipboard object */
67 static void clipboard_dump( struct object *obj, int verbose )
69 struct clipboard *clipboard = (struct clipboard *)obj;
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 );
76 /* retrieve the clipboard info for the current process, allocating it if needed */
77 static struct clipboard *get_process_clipboard(void)
79 struct clipboard *clipboard;
80 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
82 if (!winstation) return NULL;
84 if (!(clipboard = winstation->clipboard))
86 if ((clipboard = alloc_object( &clipboard_ops )))
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;
98 release_object( winstation );
99 return clipboard;
103 /* Called when thread terminates to allow release of clipboard */
104 void cleanup_clipboard_thread(struct thread *thread)
106 struct clipboard *clipboard;
107 struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
109 if (!winstation) return;
111 if ((clipboard = winstation->clipboard))
113 if (thread == clipboard->open_thread)
115 clipboard->open_win = 0;
116 clipboard->open_thread = NULL;
118 if (thread == clipboard->owner_thread)
120 clipboard->owner_win = 0;
121 clipboard->owner_thread = NULL;
124 release_object( winstation );
127 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
129 if (clipboard->open_thread && clipboard->open_thread != current)
131 set_error(STATUS_WAS_LOCKED);
132 return 0;
134 else if (!clear)
136 clipboard->open_win = win;
137 clipboard->open_thread = current;
139 else
141 clipboard->open_thread = NULL;
142 clipboard->open_win = 0;
144 return 1;
148 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
150 if (clipboard->open_thread && clipboard->open_thread->process != current->process)
152 set_error(STATUS_WAS_LOCKED);
153 return 0;
155 else if (!clear)
157 clipboard->owner_win = win;
158 clipboard->owner_thread = current;
160 else
162 clipboard->owner_win = 0;
163 clipboard->owner_thread = NULL;
165 return 1;
169 static int get_seqno( struct clipboard *clipboard )
171 time_t tm = time(NULL);
173 if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
175 clipboard->seqno_timestamp = tm;
176 clipboard->seqno++;
178 return clipboard->seqno;
182 DECL_HANDLER(set_clipboard_info)
184 struct clipboard *clipboard = get_process_clipboard();
186 if (!clipboard) return;
188 reply->old_clipboard = clipboard->open_win;
189 reply->old_owner = clipboard->owner_win;
190 reply->old_viewer = clipboard->viewer;
192 if (req->flags & SET_CB_OPEN)
194 if (clipboard->open_thread)
196 /* clipboard already opened */
197 set_error(STATUS_WAS_LOCKED);
198 return;
201 if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
203 else if (req->flags & SET_CB_CLOSE)
205 if (clipboard->open_thread != current)
207 set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
208 return;
211 if (!set_clipboard_window( clipboard, 0, 1 )) return;
214 if (req->flags & SET_CB_OWNER)
216 if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
218 else if (req->flags & SET_CB_RELOWNER)
220 if (!set_clipboard_owner( clipboard, 0, 1 )) return;
223 if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
225 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
227 reply->seqno = get_seqno( clipboard );
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;