spoolss: Build the import library.
[wine/dibdrv.git] / server / clipboard.c
blob764d89c9a3c32ebda30d1ee6da8dfa55c39ec56a
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 "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_map_access, /* map_access */
62 no_lookup_name, /* lookup_name */
63 no_open_file, /* open_file */
64 no_close_handle, /* close_handle */
65 no_destroy /* destroy */
69 #define MINUPDATELAPSE 2
71 /* dump a clipboard object */
72 static void clipboard_dump( struct object *obj, int verbose )
74 struct clipboard *clipboard = (struct clipboard *)obj;
76 fprintf( stderr, "Clipboard open_thread=%p open_win=%p owner_thread=%p owner_win=%p viewer=%p seq=%u\n",
77 clipboard->open_thread, clipboard->open_win, clipboard->owner_thread,
78 clipboard->owner_win, clipboard->viewer, clipboard->seqno );
81 /* retrieve the clipboard info for the current process, allocating it if needed */
82 static struct clipboard *get_process_clipboard(void)
84 struct clipboard *clipboard;
85 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
87 if (!winstation) return NULL;
89 if (!(clipboard = winstation->clipboard))
91 if ((clipboard = alloc_object( &clipboard_ops )))
93 clipboard->open_thread = NULL;
94 clipboard->open_win = 0;
95 clipboard->owner_thread = NULL;
96 clipboard->owner_win = 0;
97 clipboard->viewer = 0;
98 clipboard->seqno = 0;
99 clipboard->seqno_timestamp = 0;
100 winstation->clipboard = clipboard;
103 release_object( winstation );
104 return clipboard;
108 /* Called when thread terminates to allow release of clipboard */
109 void cleanup_clipboard_thread(struct thread *thread)
111 struct clipboard *clipboard;
112 struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
114 if (!winstation) return;
116 if ((clipboard = winstation->clipboard))
118 if (thread == clipboard->open_thread)
120 clipboard->open_win = 0;
121 clipboard->open_thread = NULL;
123 if (thread == clipboard->owner_thread)
125 clipboard->owner_win = 0;
126 clipboard->owner_thread = NULL;
129 release_object( winstation );
132 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
134 if (clipboard->open_thread && clipboard->open_thread != current)
136 set_error(STATUS_WAS_LOCKED);
137 return 0;
139 else if (!clear)
141 clipboard->open_win = win;
142 clipboard->open_thread = current;
144 else
146 clipboard->open_thread = NULL;
147 clipboard->open_win = 0;
149 return 1;
153 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
155 if (clipboard->open_thread && clipboard->open_thread->process != current->process)
157 set_error(STATUS_WAS_LOCKED);
158 return 0;
160 else if (!clear)
162 clipboard->owner_win = win;
163 clipboard->owner_thread = current;
165 else
167 clipboard->owner_win = 0;
168 clipboard->owner_thread = NULL;
170 return 1;
174 static int get_seqno( struct clipboard *clipboard )
176 time_t tm = time(NULL);
178 if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
180 clipboard->seqno_timestamp = tm;
181 clipboard->seqno++;
183 return clipboard->seqno;
187 DECL_HANDLER(set_clipboard_info)
189 struct clipboard *clipboard = get_process_clipboard();
191 if (!clipboard) return;
193 reply->old_clipboard = clipboard->open_win;
194 reply->old_owner = clipboard->owner_win;
195 reply->old_viewer = clipboard->viewer;
197 if (req->flags & SET_CB_OPEN)
199 if (clipboard->open_thread)
201 /* clipboard already opened */
202 set_error(STATUS_WAS_LOCKED);
203 return;
206 if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
208 else if (req->flags & SET_CB_CLOSE)
210 if (clipboard->open_thread != current)
212 set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
213 return;
216 if (!set_clipboard_window( clipboard, 0, 1 )) return;
219 if (req->flags & SET_CB_OWNER)
221 if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
223 else if (req->flags & SET_CB_RELOWNER)
225 if (!set_clipboard_owner( clipboard, 0, 1 )) return;
228 if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
230 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
232 reply->seqno = get_seqno( clipboard );
234 if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
235 if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
236 if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
237 reply->flags |= CB_PROCESS;