File: x11Event.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 20,368 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (351 lines) | stat: -rw-r--r-- 10,259 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*********************************************************
 * Copyright (C) 2009 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @file x11Event.c
 *
 * Provides code relating the Glib main loop and Xlib/X11 event sources.
 */


#include <stdlib.h>
#include <stdio.h>

#include "vmware.h"
#include "unityX11.h"


/*
 * File scope prototypes.
 */


static void ConnectionWatch(Display *display, XPointer client_data, int fd,
                            int opening, XPointer *watch_data);
static gboolean TeardownHashRemove(gpointer key, gpointer value,
                                   gpointer user_data);
static gboolean USourcePrepare(GSource *source, gint *timeout);
static gboolean USourceCheck(GSource *source);
static gboolean USourceDispatch(GSource *source, GSourceFunc callback,
                                gpointer cbData);


/*
 * Library scope functions.
 */


/*
 ******************************************************************************
 * UnityX11EstablishSource --                                            */ /**
 *
 * @brief Creates Glib event source for X11 events.  Attaches to the Glib event
 * loop.
 *
 * @param[in] up        Our Unity platform context.
 *
 ******************************************************************************
 */

void
UnityX11EventEstablishSource(UnityPlatform *up) // IN
{
   static GSourceFuncs unitySourceFuncs = {
      .prepare = USourcePrepare,
      .check = USourceCheck,
      .dispatch = USourceDispatch,
   };

   UnityGSource *uSource;

   ASSERT(up);
   ASSERT(up->display);

   uSource = (UnityGSource *)g_source_new(&unitySourceFuncs, sizeof *uSource);
   uSource->up = up;
   uSource->fdTable = g_hash_table_new(g_direct_hash, g_direct_equal);

   up->glibSource = uSource;

   /* Hook our main X11 connection into our event source. */
   ConnectionWatch(up->display, (void*)up, ConnectionNumber(up->display), TRUE,
                   NULL);

   /* If Xlib opens an internal connection, bind it to the source, too. */
   XAddConnectionWatch(up->display, &ConnectionWatch, (void*)up);

   /* Attach the source to the event loop. */
   g_source_set_callback((GSource*)uSource, UnityX11HandleEvents, up, NULL);
   g_source_attach((GSource*)uSource, NULL);

   /* Transfer ownership to the event loop. */
   g_source_unref((GSource*)uSource);
}


/*
 ******************************************************************************
 * UnityX11TeardownSource --                                             */ /**
 *
 * @brief Detach Unity from our Glib event loop.
 *
 * @param[in] up        Our Unity platform context.
 *
 ******************************************************************************
 */

void
UnityX11EventTeardownSource(UnityPlatform *up) // IN
{
   UnityGSource *uSource = up->glibSource;

   ASSERT(up);
   ASSERT(uSource);

   /* Detach Xlib internal connection notification from the Glib event loop. */
   XRemoveConnectionWatch(up->display, &ConnectionWatch, (void*)up);

   /* Detach all Xlib file descriptors from our Glib event source. */
   g_hash_table_foreach_remove(uSource->fdTable, TeardownHashRemove, uSource);
   g_hash_table_unref(uSource->fdTable);
   uSource->fdTable = NULL;

   /* Destroy the Glib event source. */
   g_source_destroy((GSource*)uSource);
   up->glibSource = NULL;
}


/*
 * File scope functions.
 */


/*
 ******************************************************************************
 * ConnectionWatch --                                                    */ /**
 *
 * @brief Binds Xlib internal connections to Glib event sources.
 *
 * When Xlib or its extensions create new X11 connections, they're bound to
 * a @a Display as <em>"internal connections"</em>.  When Xlib processes the
 * incoming event queue, it pulls requests from @em all of these connections,
 * not just the main event connection.  As such, we should monitor all of
 * them.
 *
 * @param[in] display     X11 display context.
 * @param[in] client_data Our UnityPlatform context.
 * @param[in] fd          Relevant file descriptor.
 * @param[in] opening     TRUE when @a fd is opened, FALSE when @a fd is closed.
 * @param[in] watch_data  Unused.
 *
 * @sa XAddConnectionWatch
 *
 ******************************************************************************
 */

static void
ConnectionWatch(Display *display,       // IN
                XPointer client_data,   // IN
                int fd,                 // IN
                int opening,            // IN
                XPointer *watch_data)   // UNUSED
{
   UnityPlatform *up;
   UnityGSource *uSource;

   up = (UnityPlatform *)client_data;

   ASSERT(up);             // Make sure we're correctly registered.
   ASSERT(up->isRunning);  // This cb should be stripped before we exit Unity.
   ASSERT(up->glibSource); // This function is useless w/o an established source.
   ASSERT(display == up->display);

   uSource = up->glibSource;

   if (opening) {
      /*
       * Add new a new file descriptor to the poll array.
       */
      GPollFD *newFd = g_new0(GPollFD, 1);

      ASSERT(g_hash_table_lookup(uSource->fdTable, GINT_TO_POINTER(fd)) == NULL);

      newFd->fd = fd;
      newFd->events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;

      g_hash_table_insert(uSource->fdTable, GINT_TO_POINTER(fd), newFd);
      g_source_add_poll((GSource*)uSource, newFd);
   } else {
      /*
       * Remove a file descriptor from the poll array.
       */
      GPollFD *oldFd = g_hash_table_lookup(uSource->fdTable, GINT_TO_POINTER(fd));

      if (oldFd) {
         g_source_remove_poll((GSource *)uSource, oldFd);
         g_hash_table_remove(uSource->fdTable, GINT_TO_POINTER(fd));
         g_free(oldFd);
      }
   }
}


/*
 ******************************************************************************
 * TeardownHashRemove --                                                 */ /**
 *
 * @brief Assists TeardownSource with destroying its GPollFD hash table.
 *
 * @param[in] key       GINT_TO_POINTER-converted file descriptor.
 * @param[in] value     Corresponding GPollFD *.
 * @param[in] user_data Our UnityGSource.
 *
 * @see g_hash_table_foreach_remove
 *
 ******************************************************************************
 */

static gboolean
TeardownHashRemove(gpointer key,        // IN
                   gpointer value,      // IN
                   gpointer user_data)  // IN
{
   GPollFD *oldFd = value;
   UnityGSource *uSource = user_data;

   ASSERT(value);
   ASSERT(user_data);

   g_source_remove_poll((GSource *)uSource, oldFd);
   g_free(oldFd);

   return TRUE;
}


/*
 ******************************************************************************
 * USourcePrepare --                                                     */ /**
 *
 * @brief See GSourceFuncs::prepare.
 *
 * @param[in]  source   Points to our UnityGSource.
 * @param[out] timeout  May specify a maximum timeout value for poll(2).
 *
 * @retval TRUE  Source is ready.
 * @retval FALSE Source not yet ready.
 *
 ******************************************************************************
 */

static gboolean
USourcePrepare(GSource *source, // IN
               gint *timeout)   // OUT
{
   UnityGSource *uSource = (UnityGSource *)source;

   /*
    * "It sets the returned timeout to -1 to indicate that it doesn't mind how
    * long the poll() call blocks."
    *  - http://library.gnome.org/devel/glib/unstable/glib-The-Main-Event-Loop.html#GSourceFuncs
    */
   *timeout = -1;

   return XQLength(uSource->up->display) > 0;
}


/*
 ******************************************************************************
 * USourceCheck --                                                       */ /**
 *
 * @brief See GSourceFuncs::check.
 *
 * @param[in]  source   Points to our UnityGSource.
 *
 * @retval TRUE  Source is ready.
 * @retval FALSE Source not yet ready.
 *
 * @todo Should exit Unity upon file descriptor error.
 *
 ******************************************************************************
 */

static gboolean
USourceCheck(GSource *source)   // IN
{
   UnityGSource *uSource = (UnityGSource *)source;
   gboolean haveData = FALSE;

   /*
    * XXX Could/should test for FD errors here.
    */
   if (XQLength(uSource->up->display)) {
      haveData = TRUE;
   } else {
      GList *pollFds;
      GList *listIter;

      pollFds = g_hash_table_get_values(uSource->fdTable);

      for (listIter = pollFds; listIter; listIter = listIter->next) {
         GPollFD *pollFd = listIter->data;

         if (pollFd->revents & G_IO_IN) {
            haveData = TRUE;
            break;
         }
      }

      g_list_free(pollFds);
   }

   return haveData;
}


/*
 ******************************************************************************
 * USourceDispatch --                                                    */ /**
 *
 * @brief See GSourceFuncs::dispatch.
 *
 * @param[in] source    Points to our UnityGSource.
 * @param[in] callback  Points to the user's callback.
 * @param[in] cbData    Points to user's callback data.
 *
 * @retval TRUE  Glib should continue monitoring the source.
 * @retval FALSE Glib should stop monitoring the source.
 *
 ******************************************************************************
 */

static gboolean
USourceDispatch(GSource *source,      // IN
                GSourceFunc callback, // IN
                gpointer cbData)      // IN
{
   /*
    * Remind callers to attach a callback.
    */
   ASSERT(callback);
   ASSERT(cbData);

   return callback(cbData);
}