1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Copyright © 2002 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Red Hat not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Red Hat makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Owen Taylor, Red Hat, Inc.
*/
#include <config.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include "drw-selection.h"
struct _DrwSelection
{
GdkWindow *owner_window;
GtkWidget *invisible;
};
#define SELECTION_NAME "_CODEFACTORY_DRWRIGHT"
static GdkFilterReturn drw_selection_filter (GdkXEvent *xevent,
GdkEvent *event,
gpointer data);
static void drw_selection_negotiate (DrwSelection *drw_selection);
static void
drw_selection_reset (DrwSelection *drw_selection)
{
if (drw_selection->owner_window) {
gdk_window_remove_filter (drw_selection->owner_window,
drw_selection_filter, drw_selection);
g_object_unref (drw_selection->owner_window);
drw_selection->owner_window = NULL;
}
if (drw_selection->invisible) {
gtk_widget_destroy (drw_selection->invisible);
drw_selection->invisible = NULL;
}
}
static void
drw_selection_clear (GtkWidget *widget,
GdkEventSelection *event,
gpointer user_data)
{
DrwSelection *drw_selection = user_data;
drw_selection_reset (drw_selection);
drw_selection_negotiate (drw_selection);
}
static gboolean
drw_selection_find_existing (DrwSelection *drw_selection)
{
Display *xdisplay = GDK_DISPLAY ();
Window old;
gdk_error_trap_push ();
old = XGetSelectionOwner (xdisplay,
gdk_x11_get_xatom_by_name (SELECTION_NAME));
if (old) {
XSelectInput (xdisplay, old, StructureNotifyMask);
drw_selection->owner_window = gdk_window_foreign_new (old);
}
XSync (xdisplay, False);
if (gdk_error_trap_pop () == 0 && drw_selection->owner_window) {
gdk_window_add_filter (drw_selection->owner_window,
drw_selection_filter, drw_selection);
XUngrabServer (xdisplay);
return TRUE;
} else {
if (drw_selection->owner_window) {
g_object_unref (drw_selection->owner_window);
drw_selection->owner_window = NULL;
}
return FALSE;
}
}
static gboolean
drw_selection_claim (DrwSelection *drw_selection)
{
drw_selection->invisible = gtk_invisible_new ();
g_signal_connect (drw_selection->invisible, "selection-clear-event",
G_CALLBACK (drw_selection_clear), drw_selection);
if (gtk_selection_owner_set (drw_selection->invisible,
gdk_atom_intern (SELECTION_NAME, FALSE),
GDK_CURRENT_TIME)) {
return TRUE;
} else {
drw_selection_reset (drw_selection);
return FALSE;
}
}
static void
drw_selection_negotiate (DrwSelection *drw_selection)
{
Display *xdisplay = GDK_DISPLAY ();
gboolean found = FALSE;
/* We don't need both the XGrabServer() and the loop here;
* the XGrabServer() should make sure that we only go through
* the loop once. It also works if you remove the XGrabServer()
* and just have the loop, but then the selection ownership
* can get transfered a bunch of times before things
* settle down.
*/
while (!found)
{
XGrabServer (xdisplay);
if (drw_selection_find_existing (drw_selection))
found = TRUE;
else if (drw_selection_claim (drw_selection))
found = TRUE;
XUngrabServer (xdisplay);
}
}
static GdkFilterReturn
drw_selection_filter (GdkXEvent *xevent,
GdkEvent *event,
gpointer data)
{
DrwSelection *drw_selection = data;
XEvent *xev = (XEvent *)xevent;
if (xev->xany.type == DestroyNotify &&
xev->xdestroywindow.window == xev->xdestroywindow.event)
{
drw_selection_reset (drw_selection);
drw_selection_negotiate (drw_selection);
return GDK_FILTER_REMOVE;
}
return GDK_FILTER_CONTINUE;
}
DrwSelection *
drw_selection_start (void)
{
DrwSelection *drw_selection = g_new (DrwSelection, 1);
drw_selection->owner_window = NULL;
drw_selection->invisible = NULL;
drw_selection_negotiate (drw_selection);
return drw_selection;
}
void
drw_selection_stop (DrwSelection *drw_selection)
{
drw_selection_reset (drw_selection);
g_free (drw_selection);
}
gboolean
drw_selection_is_master (DrwSelection *drw_selection)
{
return drw_selection->invisible != NULL;
}
|