NULL and empty strings are the same in conditions.
[wine/multimedia.git] / server / clipboard.c
blobefa5b7b7e63bed6678a4427e26527f05a9ab80fd
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_close_handle, /* close_handle */
59 no_destroy /* destroy */
63 #define MINUPDATELAPSE 2
65 /* dump a clipboard object */
66 static void clipboard_dump( struct object *obj, int verbose )
68 struct clipboard *clipboard = (struct clipboard *)obj;
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 );
75 /* retrieve the clipboard info for the current process, allocating it if needed */
76 static struct clipboard *get_process_clipboard(void)
78 struct clipboard *clipboard;
79 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
81 if (!winstation) return NULL;
83 if (!(clipboard = winstation->clipboard))
85 if ((clipboard = alloc_object( &clipboard_ops )))
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;
97 release_object( winstation );
98 return clipboard;
102 /* Called when thread terminates to allow release of clipboard */
103 void cleanup_clipboard_thread(struct thread *thread)
105 struct clipboard *clipboard;
106 struct winstation *winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD );
108 if (!winstation) return;
110 if ((clipboard = winstation->clipboard))
112 if (thread == clipboard->open_thread)
114 clipboard->open_win = 0;
115 clipboard->open_thread = NULL;
117 if (thread == clipboard->owner_thread)
119 clipboard->owner_win = 0;
120 clipboard->owner_thread = NULL;
123 release_object( winstation );
126 static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear )
128 if (clipboard->open_thread && clipboard->open_thread != current)
130 set_error(STATUS_WAS_LOCKED);
131 return 0;
133 else if (!clear)
135 clipboard->open_win = win;
136 clipboard->open_thread = current;
138 else
140 clipboard->open_thread = NULL;
141 clipboard->open_win = 0;
143 return 1;
147 static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear )
149 if (clipboard->open_thread && clipboard->open_thread->process != current->process)
151 set_error(STATUS_WAS_LOCKED);
152 return 0;
154 else if (!clear)
156 clipboard->owner_win = win;
157 clipboard->owner_thread = current;
159 else
161 clipboard->owner_win = 0;
162 clipboard->owner_thread = NULL;
164 return 1;
168 static int get_seqno( struct clipboard *clipboard )
170 time_t tm = time(NULL);
172 if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE)))
174 clipboard->seqno_timestamp = tm;
175 clipboard->seqno++;
177 return clipboard->seqno;
181 DECL_HANDLER(set_clipboard_info)
183 struct clipboard *clipboard = get_process_clipboard();
185 if (!clipboard) return;
187 reply->old_clipboard = clipboard->open_win;
188 reply->old_owner = clipboard->owner_win;
189 reply->old_viewer = clipboard->viewer;
191 if (req->flags & SET_CB_OPEN)
193 if (clipboard->open_thread)
195 /* clipboard already opened */
196 set_error(STATUS_WAS_LOCKED);
197 return;
200 if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return;
202 else if (req->flags & SET_CB_CLOSE)
204 if (clipboard->open_thread != current)
206 set_win32_error(ERROR_CLIPBOARD_NOT_OPEN);
207 return;
210 if (!set_clipboard_window( clipboard, 0, 1 )) return;
213 if (req->flags & SET_CB_OWNER)
215 if (!set_clipboard_owner( clipboard, req->owner, 0 )) return;
217 else if (req->flags & SET_CB_RELOWNER)
219 if (!set_clipboard_owner( clipboard, 0, 1 )) return;
222 if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer;
224 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
226 reply->seqno = get_seqno( clipboard );
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;