Move new lxsession with built-in settings daemon to trunk.
[lxde/lxsession.git] / lxsession / xutils.c
1 /*
2 * Copyright © 2004 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Red Hat not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. Red Hat makes no representations about the
11 * suitability of this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
13 *
14 * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
16 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * Author: Matthias Clasen, Red Hat, Inc.
22 */
23
24 #include <stdlib.h>
25
26 #include "xutils.h"
27
28 Atom XA_ATOM_PAIR;
29 Atom XA_CLIPBOARD_MANAGER;
30 Atom XA_CLIPBOARD;
31 Atom XA_DELETE;
32 Atom XA_INCR;
33 Atom XA_INSERT_PROPERTY;
34 Atom XA_INSERT_SELECTION;
35 Atom XA_MANAGER;
36 Atom XA_MULTIPLE;
37 Atom XA_NULL;
38 Atom XA_SAVE_TARGETS;
39 Atom XA_TARGETS;
40 Atom XA_TIMESTAMP;
41
42 unsigned long SELECTION_MAX_SIZE = 0;
43
44
45 void
46 init_atoms (Display *display)
47 {
48 unsigned long max_request_size;
49
50 if (SELECTION_MAX_SIZE > 0)
51 return;
52
53 XA_ATOM_PAIR = XInternAtom (display, "ATOM_PAIR", False);
54 XA_CLIPBOARD_MANAGER = XInternAtom (display, "CLIPBOARD_MANAGER", False);
55 XA_CLIPBOARD = XInternAtom (display, "CLIPBOARD", False);
56 XA_DELETE = XInternAtom (display, "DELETE", False);
57 XA_INCR = XInternAtom (display, "INCR", False);
58 XA_INSERT_PROPERTY = XInternAtom (display, "INSERT_PROPERTY", False);
59 XA_INSERT_SELECTION = XInternAtom (display, "INSERT_SELECTION", False);
60 XA_MANAGER = XInternAtom (display, "MANAGER", False);
61 XA_MULTIPLE = XInternAtom (display, "MULTIPLE", False);
62 XA_NULL = XInternAtom (display, "NULL", False);
63 XA_SAVE_TARGETS = XInternAtom (display, "SAVE_TARGETS", False);
64 XA_TARGETS = XInternAtom (display, "TARGETS", False);
65 XA_TIMESTAMP = XInternAtom (display, "TIMESTAMP", False);
66
67 max_request_size = XExtendedMaxRequestSize (display);
68 if (max_request_size == 0)
69 max_request_size = XMaxRequestSize (display);
70
71 SELECTION_MAX_SIZE = max_request_size - 100;
72 if (SELECTION_MAX_SIZE > 262144)
73 SELECTION_MAX_SIZE = 262144;
74 }
75
76 typedef struct
77 {
78 Window window;
79 Atom timestamp_prop_atom;
80 } TimeStampInfo;
81
82 static Bool
83 timestamp_predicate (Display *display,
84 XEvent *xevent,
85 XPointer arg)
86 {
87 TimeStampInfo *info = (TimeStampInfo *)arg;
88
89 if (xevent->type == PropertyNotify &&
90 xevent->xproperty.window == info->window &&
91 xevent->xproperty.atom == info->timestamp_prop_atom)
92 return True;
93
94 return False;
95 }
96
97 Time
98 get_server_time (Display *display,
99 Window window)
100 {
101 unsigned char c = 'a';
102 XEvent xevent;
103 TimeStampInfo info;
104
105 info.timestamp_prop_atom = XInternAtom (display, "_TIMESTAMP_PROP", False);
106 info.window = window;
107
108 XChangeProperty (display, window,
109 info.timestamp_prop_atom, info.timestamp_prop_atom,
110 8, PropModeReplace, &c, 1);
111
112 XIfEvent (display, &xevent,
113 timestamp_predicate, (XPointer)&info);
114
115 return xevent.xproperty.time;
116 }
117