gdkdisplay-x11.c 14.9 KB
Newer Older
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
/* GDK - The GIMP Drawing Kit
 * gdkdisplay-x11.c
 * 
 * Copyright 2001 Sun Microsystems Inc. 
 *
 * Erwann Chenede <erwann.chenede@sun.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <unistd.h>

#include <glib.h>
#include "gdkx.h"
#include "gdkdisplay.h"
#include "gdkdisplay-x11.h"
#include "gdkscreen.h"
#include "gdkscreen-x11.h"
#include "gdkinternals.h"
#include "gdkinputprivate.h"
#include "xsettings-client.h"

#include <X11/Xatom.h>

#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

static void                 gdk_display_x11_class_init         (GdkDisplayX11Class *class);
static void                 gdk_display_x11_finalize           (GObject            *object);

47 48 49 50 51 52
static void gdk_internal_connection_watch (Display  *display,
					   XPointer  arg,
					   gint      fd,
					   gboolean  opening,
					   XPointer *watch_data);

53 54 55
static gpointer parent_class = NULL;

GType
56
_gdk_display_x11_get_type (void)
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
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
	{
	  sizeof (GdkDisplayX11Class),
	  (GBaseInitFunc) NULL,
	  (GBaseFinalizeFunc) gdk_display_x11_finalize,
	  (GClassInitFunc) gdk_display_x11_class_init,
	  NULL,			/* class_finalize */
	  NULL,			/* class_data */
	  sizeof (GdkDisplayX11),
	  0,			/* n_preallocs */
	  (GInstanceInitFunc) NULL,
	};
      
      object_type = g_type_register_static (GDK_TYPE_DISPLAY,
					    "GdkDisplayX11",
					    &object_info, 0);
    }
  
  return object_type;
}

static void
gdk_display_x11_class_init (GdkDisplayX11Class * class)
{
  G_OBJECT_CLASS (class)->finalize = gdk_display_x11_finalize;
  
  parent_class = g_type_class_peek_parent (class);
}

Matthias Clasen's avatar
Matthias Clasen committed
91 92 93 94 95 96 97 98
/**
 * gdk_open_display:
 * @display_name: the name of the display to open
 * @returns: a #GdkDisplay, or %NULL if the display
 *  could not be opened.
 *
 * Opens a display.
 */
99 100 101
GdkDisplay *
gdk_open_display (const gchar *display_name)
{
102
  Display *xdisplay;
103 104 105 106 107 108 109 110 111
  GdkDisplay *display;
  GdkDisplayX11 *display_x11;
  gint argc;
  gchar **argv;
  
  XClassHint *class_hint;
  XKeyboardState keyboard_state;
  gulong pid;
  gint i;
112 113 114 115

  xdisplay = XOpenDisplay (display_name);
  if (!xdisplay)
    return NULL;
116 117 118 119 120
  
  display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
  display_x11 = GDK_DISPLAY_X11 (display);

  display_x11->use_xft = -1;
121
  display_x11->xdisplay = xdisplay;
122 123 124

  /* Set up handlers for Xlib internal connections */
  XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
125
  
126 127
  /* initialize the display's screens */ 
  display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
128
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
129
    display_x11->screens[i] = _gdk_x11_screen_new (display, i);
130

131 132 133 134
  /*set the default screen */
  display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
  display_x11->leader_window = XCreateSimpleWindow (display_x11->xdisplay,
						    GDK_SCREEN_X11 (display_x11->default_screen)->xroot_window,
Owen Taylor's avatar
Owen Taylor committed
135 136 137
						    10, 10, 10, 10, 0, 0, 0);  
  display_x11->have_shape = GDK_UNKNOWN;
  display_x11->gravity_works = GDK_UNKNOWN;
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

  if (_gdk_synchronize)
    XSynchronize (display_x11->xdisplay, True);
  
  class_hint = XAllocClassHint();
  class_hint->res_name = g_get_prgname ();
  
  class_hint->res_class = (char *)gdk_get_program_class ();
  _gdk_get_command_line_args (&argc, &argv);
  XmbSetWMProperties (display_x11->xdisplay,
		      display_x11->leader_window,
		      NULL, NULL, argv, argc, NULL, NULL,
		      class_hint);
  XFree (class_hint);

  pid = getpid ();
  XChangeProperty (display_x11->xdisplay,
		   display_x11->leader_window,
		   gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
		   XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
  
  XGetKeyboardControl (display_x11->xdisplay, &keyboard_state);

#ifdef HAVE_XKB
  {
    gint xkb_major = XkbMajorVersion;
    gint xkb_minor = XkbMinorVersion;
    if (XkbLibraryVersion (&xkb_major, &xkb_minor))
      {
        xkb_major = XkbMajorVersion;
        xkb_minor = XkbMinorVersion;
	    
        if (XkbQueryExtension (display_x11->xdisplay, 
			       NULL, &display_x11->xkb_event_type, NULL,
                               &xkb_major, &xkb_minor))
          {
	    Bool detectable_autorepeat_supported;
	    
	    display_x11->use_xkb = TRUE;

            XkbSelectEvents (display_x11->xdisplay,
                             XkbUseCoreKbd,
                             XkbMapNotifyMask | XkbStateNotifyMask,
                             XkbMapNotifyMask | XkbStateNotifyMask);

	    XkbSetDetectableAutoRepeat (display_x11->xdisplay,
					True,
					&detectable_autorepeat_supported);

	    GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
				       detectable_autorepeat_supported ? 
				       "supported" : "not supported"));
	    
	    display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
          }
      }
  }
#endif

  _gdk_windowing_image_init (display);
  _gdk_events_init (display);
  _gdk_input_init (display);
  _gdk_dnd_init (display);

202 203
  g_signal_emit_by_name (gdk_display_manager_get(),
			 "display_opened", display);
Owen Taylor's avatar
Owen Taylor committed
204

205 206 207
  return display;
}

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
/*
 * XLib internal connection handling
 */
typedef struct _GdkInternalConnection GdkInternalConnection;

struct _GdkInternalConnection
{
  gint	         fd;
  GSource	*source;
  Display	*display;
};

static gboolean
process_internal_connection (GIOChannel  *gioc,
			     GIOCondition cond,
			     gpointer     data)
{
  GdkInternalConnection *connection = (GdkInternalConnection *)data;

  GDK_THREADS_ENTER ();

  XProcessInternalConnection ((Display*)connection->display, connection->fd);

  GDK_THREADS_LEAVE ();

  return TRUE;
}

static GdkInternalConnection *
gdk_add_connection_handler (Display *display,
			    guint    fd)
{
  GIOChannel *io_channel;
  GdkInternalConnection *connection;

  connection = g_new (GdkInternalConnection, 1);

  connection->fd = fd;
  connection->display = display;
  
  io_channel = g_io_channel_unix_new (fd);
  
  connection->source = g_io_create_watch (io_channel, G_IO_IN);
  g_source_set_callback (connection->source,
			 (GSourceFunc)process_internal_connection, connection, NULL);
  g_source_attach (connection->source, NULL);
  
  g_io_channel_unref (io_channel);
  
  return connection;
}

static void
gdk_remove_connection_handler (GdkInternalConnection *connection)
{
  g_source_destroy (connection->source);
  g_free (connection);
}

static void
gdk_internal_connection_watch (Display  *display,
			       XPointer  arg,
			       gint      fd,
			       gboolean  opening,
			       XPointer *watch_data)
{
  if (opening)
    *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
  else
277
    gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
278 279
}

280 281 282 283 284 285 286 287 288 289
/**
 * gdk_display_get_name:
 * @display: a #GdkDisplay
 *
 * Gets the name of the display.
 * 
 * Returns: a string representing the display name.
 */
G_CONST_RETURN gchar *
gdk_display_get_name (GdkDisplay * display)
290
{
291
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
292
  
293
  return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
294 295
}

296 297 298 299 300 301 302 303 304 305
/**
 * gdk_display_get_n_screens:
 * @display: a #GdkDisplay
 *
 * Gets the number of screen managed by the @display.
 * 
 * Returns: number of screens.
 */
gint
gdk_display_get_n_screens (GdkDisplay * display)
306
{
307
  g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
308
  
309
  return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
310 311
}

312 313 314 315 316 317 318 319 320 321 322
/**
 * gdk_display_get_screen:
 * @display: a #GdkDisplay
 * @screen_num: the screen number
 *
 * Returns a screen object for one of the screens of the display.
 * 
 * Returns: the #GdkScreen object
 */
GdkScreen *
gdk_display_get_screen (GdkDisplay * display, gint screen_num)
323
{
324 325
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
  g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
326
  
327
  return GDK_DISPLAY_X11 (display)->screens[screen_num];
328 329
}

330 331 332 333 334 335 336 337 338 339
/**
 * gdk_display_get_default_screen:
 * @display: a #GdkDisplay
 *
 * Get the default #GdkScreen for @display.
 * 
 * Returns: the default #GdkScreen object for @display
 */
GdkScreen *
gdk_display_get_default_screen (GdkDisplay * display)
340
{
341
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
342
  
343
  return GDK_DISPLAY_X11 (display)->default_screen;
344 345 346 347 348 349 350
}

gboolean
_gdk_x11_display_is_root_window (GdkDisplay *display,
				 Window      xroot_window)
{
  GdkDisplayX11 *display_x11;
351
  gint i;
352 353 354 355 356
  
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
  
  display_x11 = GDK_DISPLAY_X11 (display);
  
357
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
358
    {
359 360
      if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
	return TRUE;
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    }
  return FALSE;
}

/**
 * gdk_display_pointer_ungrab:
 * @display : a #GdkDisplay.
 * @time: a timestap (e.g. GDK_CURRENT_TIME).
 *
 * Release any pointer grab.
 */
void
gdk_display_pointer_ungrab (GdkDisplay *display,
			    guint32     time)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  _gdk_input_ungrab_pointer (display, time);
  XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time);
  GDK_DISPLAY_X11 (display)->pointer_xgrab_window = NULL;
}

/**
 * gdk_display_pointer_is_grabbed :
 * @display : a #GdkDisplay
 *
 * Test if the pointer is grabbed.
 *
 * Returns : TRUE if an active X pointer grab is in effect
 */
gboolean
gdk_display_pointer_is_grabbed (GdkDisplay * display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
  
  return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL);
}

/**
 * gdk_display_keyboard_ungrab:
 * @display : a #GdkDisplay.
 * @time : a timestap (e.g #GDK_CURRENT_TIME).
 *
 * Release any keyboard grab
 */
void
gdk_display_keyboard_ungrab (GdkDisplay *display,
			     guint32     time)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time);
  GDK_DISPLAY_X11 (display)->keyboard_xgrab_window = NULL;
}

/**
 * gdk_display_beep:
 * @display: a #GdkDisplay
 *
 * Emits a short beep on @display
 *
 */
void
gdk_display_beep (GdkDisplay * display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  XBell (GDK_DISPLAY_XDISPLAY (display), 0);
}

/**
 * gdk_display_sync :
 * @display : a #GdkDisplay
 *
 * Flushes any requests queued for the windowing system and waits until all
 * requests have been handled. This is often used for making sure that the
 * display is synchronized with the current state of the program. Calling
 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
 * generated from earlier requests are handled before the error trap is removed.
 *
 * This is most useful for X11. On windowing systems where requests handled
 * synchronously, this function will do nothing.
 */
void
gdk_display_sync (GdkDisplay * display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  XSync (GDK_DISPLAY_XDISPLAY (display), False);
}

void
gdk_x11_display_grab (GdkDisplay * display)
{
  GdkDisplayX11 *display_x11;
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  display_x11 = GDK_DISPLAY_X11 (display);
  
  if (display_x11->grab_count == 0)
    XGrabServer (display_x11->xdisplay);
  display_x11->grab_count++;
}

void
gdk_x11_display_ungrab (GdkDisplay * display)
{
  GdkDisplayX11 *display_x11;
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  display_x11 = GDK_DISPLAY_X11 (display);;
  g_return_if_fail (display_x11->grab_count > 0);
  
  display_x11->grab_count--;
  if (display_x11->grab_count == 0)
    XUngrabServer (display_x11->xdisplay);
}

static void
gdk_display_x11_finalize (GObject *object)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
  int i;
  GList *tmp;
  /* FIXME need to write GdkKeymap finalize fct 
     g_object_unref (display_x11->keymap);
   */
  /* Free motif Dnd */
  if (display_x11->motif_target_lists)
    {
      for (i = 0; i < display_x11->motif_n_target_lists; i++)
        g_list_free (display_x11->motif_target_lists[i]);
      g_free (display_x11->motif_target_lists);
    }

  /* Atom Hashtable */
  g_hash_table_destroy (display_x11->atom_from_virtual);
  g_hash_table_destroy (display_x11->atom_to_virtual);
  /* Leader Window */
  XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
  /* list of filters for client messages */
  g_list_free (display_x11->client_filters);
  /* X ID hashtable */
  g_hash_table_destroy (display_x11->xid_ht);
  /* input GdkDevice list */
  /* FIXME need to write finalize fct */
  for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
    g_object_unref (G_OBJECT (tmp->data));
  g_list_free (display_x11->input_devices);
  /* input GdkWindow list */
  for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
    g_object_unref (G_OBJECT (tmp->data));
  g_list_free (display_x11->input_windows);
  /* Free all GdkScreens */
517 518 519
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
    g_object_unref (G_OBJECT (display_x11->screens[i]));
  g_free (display_x11->screens);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  XCloseDisplay (display_x11->xdisplay);
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * gdk_x11_lookup_xdisplay:
 * @xdisplay: a pointer to an X Display
 * 
 * Find the #GdkDisplay corresponding to @display, if any exists.
 * 
 * Return value: the #GdkDisplay, if found, otherwise %NULL.
 **/
GdkDisplay *
gdk_x11_lookup_xdisplay (Display *xdisplay)
{
  GSList *tmp_list;

  for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
    {
      if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
	return tmp_list->data;
    }
  
  return NULL;
}

/**
 * _gdk_x11_display_screen_for_xrootwin:
 * @display: a #Display
 * @xrootwin: window ID for one of of the screen's of the display.
 * 
 * Given the root window ID of one of the screen's of a #GdkDisplay,
 * finds the screen.
 * 
 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
 **/
GdkScreen *
_gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
				      Window      xrootwin)
{
  gint n_screens, i;

  n_screens = gdk_display_get_n_screens (display);
  for (i = 0; i < n_screens; i++)
    {
      GdkScreen *screen = gdk_display_get_screen (display, i);
      if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
	return screen;
    }

  return NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
573 574 575 576 577 578 579
/**
 * gdk_x11_display_get_xdisplay:
 * @display: a #GdkDisplay
 * @returns: an X display.
 *
 * Returns the X display of a #GdkDisplay.
 */
580 581 582 583 584
Display *
gdk_x11_display_get_xdisplay (GdkDisplay  *display)
{
  return GDK_DISPLAY_X11 (display)->xdisplay;
}
Matthias Clasen's avatar
Matthias Clasen committed
585 586