gdkwindow-directfb.c 89.5 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
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 1998-1999 Tor Lillqvist
 *
 * This library 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; 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.
 */

/*
 * GTK+ DirectFB backend
 * Copyright (C) 2001-2002  convergence integrated media GmbH
 * Copyright (C) 2002-2004  convergence GmbH
 * Written by Denis Oliver Kropp <dok@convergence.de> and
 *            Sven Neumann <sven@convergence.de>
 */

34
#include "config.h"
35 36 37 38 39
#include "gdk.h"
#include "gdkwindow.h"

#include "gdkdirectfb.h"
#include "gdkprivate-directfb.h"
40
#include "gdkdisplay-directfb.h"
41 42 43 44 45 46

#include "gdkregion-generic.h"

#include "gdkinternals.h"
#include "gdkalias.h"
#include "cairo.h"
47
#include <assert.h>
48

49 50
#include <direct/debug.h>

51 52
#include <directfb_util.h>

53 54 55 56 57 58 59 60 61 62




D_DEBUG_DOMAIN( GDKDFB_Crossing,  "GDKDFB/Crossing",  "GDK DirectFB Crossing Events" );
D_DEBUG_DOMAIN( GDKDFB_Updates,   "GDKDFB/Updates",   "GDK DirectFB Updates" );
D_DEBUG_DOMAIN( GDKDFB_Paintable, "GDKDFB/Paintable", "GDK DirectFB Paintable" );
D_DEBUG_DOMAIN( GDKDFB_Window,    "GDKDFB/Window",    "GDK DirectFB Window" );


63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static GdkRegion * gdk_window_impl_directfb_get_visible_region (GdkDrawable *drawable);
static void        gdk_window_impl_directfb_set_colormap       (GdkDrawable *drawable,
                                                                GdkColormap *colormap);
static void gdk_window_impl_directfb_init       (GdkWindowImplDirectFB      *window);
static void gdk_window_impl_directfb_class_init (GdkWindowImplDirectFBClass *klass);
static void gdk_window_impl_directfb_finalize   (GObject                    *object);

typedef struct
{
  GdkWindowChildChanged  changed;
  GdkWindowChildGetPos   get_pos;
  gpointer               user_data;
} GdkWindowChildHandlerData;


78 79 80 81 82 83 84 85
/* Code for dirty-region queueing
 */
static GSList *update_windows = NULL;
static guint update_idle = 0;

static void
gdk_window_directfb_process_all_updates (void)
{
86
  GSList *tmp_list;
87 88 89 90 91 92 93 94
  GSList *old_update_windows = update_windows;

  if (update_idle)
    g_source_remove (update_idle);
  
  update_windows = NULL;
  update_idle = 0;

95
  D_DEBUG_AT( GDKDFB_Updates, "%s()\n", __FUNCTION__ );
96
  
97 98
  g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
  tmp_list = old_update_windows;
99 100
  while (tmp_list)
    {
101 102
      GdkWindowObject       *private = GDK_WINDOW_OBJECT( tmp_list->data );
      GdkWindowImplDirectFB *wimpl   = GDK_WINDOW_IMPL_DIRECTFB( private->impl );
103 104
      
      if (private->update_freeze_count)
105 106 107 108 109 110
        {
          D_DEBUG_AT( GDKDFB_Updates, "  -> %p frozen [%4d,%4d-%4dx%4d] (%d boxes)\n",
                      private, DFB_RECTANGLE_VALS_FROM_REGION( &wimpl->flips.bounding ),
                      wimpl->flips.num_regions );
          update_windows = g_slist_prepend (update_windows, private);
        }
111
      else
112 113 114 115 116 117
        {
          D_DEBUG_AT( GDKDFB_Updates, "  -> %p update [%4d,%4d-%4dx%4d] (%d boxes)\n",
                      private, DFB_RECTANGLE_VALS_FROM_REGION( &wimpl->flips.bounding ),
                      wimpl->flips.num_regions );
          gdk_window_process_updates(tmp_list->data,TRUE);
        }
118 119 120 121 122
      
      g_object_unref (tmp_list->data);
      tmp_list = tmp_list->next;
    }

123 124
#ifndef GDK_DIRECTFB_NO_EXPERIMENTS
  g_slist_foreach (old_update_windows, (GFunc)g_object_ref, NULL);
125 126 127 128
  tmp_list = old_update_windows;
  while (tmp_list)
    {
      GdkWindowObject *top = GDK_WINDOW_OBJECT( gdk_window_get_toplevel( tmp_list->data ) );
129

130 131 132
      if (top)
        {
          GdkWindowImplDirectFB *wimpl = GDK_WINDOW_IMPL_DIRECTFB (top->impl);
133

134 135 136 137 138
          if (wimpl->flips.num_regions)
            {
              D_DEBUG_AT( GDKDFB_Updates, "  -> %p flip   [%4d,%4d-%4dx%4d] (%d boxes)\n",
                          top, DFB_RECTANGLE_VALS_FROM_REGION( &wimpl->flips.bounding ),
                          wimpl->flips.num_regions );
139

140
              wimpl->drawable.surface->Flip( wimpl->drawable.surface, &wimpl->flips.bounding, DSFLIP_NONE );
141

142
              dfb_updates_reset( &wimpl->flips );
143
            }
144 145 146 147 148
          else
            D_DEBUG_AT( GDKDFB_Updates, "  -> %p has no flips!\n", top );
        }
      else
        D_DEBUG_AT( GDKDFB_Updates, "  -> %p has no top level window!\n", tmp_list->data );
149

150 151 152
      g_object_unref (tmp_list->data);
      tmp_list = tmp_list->next;
    }
153 154
#endif

155 156 157 158 159 160 161 162 163 164 165 166 167 168
  g_slist_free (old_update_windows);
}

static gboolean
gdk_window_update_idle (gpointer data)
{
  gdk_window_directfb_process_all_updates ();
  
  return FALSE;
}

static void
gdk_window_schedule_update (GdkWindow *window)
{
169 170 171
  D_DEBUG_AT( GDKDFB_Updates, "%s( %p ) <- freeze count %d\n", __FUNCTION__, window,
              window ? GDK_WINDOW_OBJECT (window)->update_freeze_count : -1 );
  
172 173 174 175 176
  if (window && GDK_WINDOW_OBJECT (window)->update_freeze_count)
    return;

  if (!update_idle)
    {
177 178
      D_DEBUG_AT( GDKDFB_Updates, "  -> adding idle callback\n" );

179
      update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
180
                                               gdk_window_update_idle, NULL, NULL);
181 182 183
    }
}

184 185 186 187 188

static GdkWindow *gdk_directfb_window_containing_pointer = NULL;
static GdkWindow *gdk_directfb_focused_window            = NULL;
static gpointer   parent_class                           = NULL;
GdkWindow * _gdk_parent_root = NULL;
189 190 191 192 193
static void
gdk_window_impl_directfb_paintable_init (GdkPaintableIface *iface);



194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215


GType
gdk_window_impl_directfb_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
        {
          sizeof (GdkWindowImplDirectFBClass),
          (GBaseInitFunc) NULL,
          (GBaseFinalizeFunc) NULL,
          (GClassInitFunc) gdk_window_impl_directfb_class_init,
          NULL,           /* class_finalize */
          NULL,           /* class_data */
          sizeof (GdkWindowImplDirectFB),
          0,              /* n_preallocs */
          (GInstanceInitFunc) gdk_window_impl_directfb_init,
        };

216 217 218 219 220 221 222
    static const GInterfaceInfo paintable_info =
      {
    (GInterfaceInitFunc) gdk_window_impl_directfb_paintable_init,
    NULL,
    NULL
      };

223 224 225
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_DIRECTFB,
                                            "GdkWindowImplDirectFB",
                                            &object_info, 0);
226 227 228 229
       g_type_add_interface_static (object_type,
                   GDK_TYPE_PAINTABLE,
                   &paintable_info);

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
    }

  return object_type;
}

GType
_gdk_window_impl_get_type (void)
{
  return gdk_window_impl_directfb_get_type ();
}

static void
gdk_window_impl_directfb_init (GdkWindowImplDirectFB *impl)
{
  impl->drawable.width  = 1;
  impl->drawable.height = 1;
  //cannot use gdk_cursor_new here since gdk_display_get_default
  //does not work yet.
  impl->cursor          = gdk_cursor_new_for_display (GDK_DISPLAY_OBJECT(_gdk_display),GDK_LEFT_PTR);
  impl->opacity         = 255;
}

static void
gdk_window_impl_directfb_class_init (GdkWindowImplDirectFBClass *klass)
{
  GObjectClass     *object_class   = G_OBJECT_CLASS (klass);
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_window_impl_directfb_finalize;

  drawable_class->set_colormap = gdk_window_impl_directfb_set_colormap;

  /* Visible and clip regions are the same */

  drawable_class->get_clip_region =
    gdk_window_impl_directfb_get_visible_region;

  drawable_class->get_visible_region =
    gdk_window_impl_directfb_get_visible_region;
}

static void
g_free_2nd (gpointer a,
            gpointer b,
            gpointer data)
{
  g_free (b);
}

static void
gdk_window_impl_directfb_finalize (GObject *object)
{
  GdkWindowImplDirectFB *impl = GDK_WINDOW_IMPL_DIRECTFB (object);

286 287
  D_DEBUG_AT( GDKDFB_Window, "%s( %p ) <- %dx%d\n", __FUNCTION__, impl, impl->drawable.width, impl->drawable.height );

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
  if (GDK_WINDOW_IS_MAPPED (impl->drawable.wrapper))
    gdk_window_hide (impl->drawable.wrapper);

  if (impl->cursor)
    gdk_cursor_unref (impl->cursor);

  if (impl->properties)
    {
      g_hash_table_foreach (impl->properties, g_free_2nd, NULL);
      g_hash_table_destroy (impl->properties);
    }
  if (impl->window)
    {
      gdk_directfb_window_id_table_remove (impl->dfb_id);
	  /* native window resource must be release before we can finalize !*/
      impl->window = NULL;
    }

  if (G_OBJECT_CLASS (parent_class)->finalize)
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static GdkRegion*
gdk_window_impl_directfb_get_visible_region (GdkDrawable *drawable)
{
  GdkDrawableImplDirectFB *priv = GDK_DRAWABLE_IMPL_DIRECTFB (drawable);
  GdkRectangle             rect = { 0, 0, 0, 0 };
  DFBRectangle             drect = { 0, 0, 0, 0 };

317 318
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, drawable );

319
  if (priv->surface)
320
  priv->surface->GetVisibleRectangle (priv->surface, &drect);
321 322 323 324
  rect.x= drect.x;
  rect.y= drect.y;
  rect.width=drect.w;
  rect.height=drect.h;
325 326 327

  D_DEBUG_AT( GDKDFB_Window, "  -> returning %4d,%4d-%4dx%4d\n", drect.x, drect.y, drect.w, drect.h );

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  return gdk_region_rectangle (&rect);
}

static void
gdk_window_impl_directfb_set_colormap (GdkDrawable *drawable,
                                       GdkColormap *colormap)
{
  GDK_DRAWABLE_CLASS (parent_class)->set_colormap (drawable, colormap);

  if (colormap)
    {
       GdkDrawableImplDirectFB *priv = GDK_DRAWABLE_IMPL_DIRECTFB (drawable);

       if (priv->surface)
	 {
	   IDirectFBPalette *palette = gdk_directfb_colormap_get_palette (colormap);

           if (palette)
             priv->surface->SetPalette (priv->surface, palette);
         }
    }
}


static gboolean
create_directfb_window (GdkWindowImplDirectFB *impl,
                        DFBWindowDescription  *desc,
                        DFBWindowOptions       window_options)
{
  DFBResult        ret;
  IDirectFBWindow *window;

360 361
  D_DEBUG_AT( GDKDFB_Window, "%s( %4dx%4d, caps 0x%08x )\n", __FUNCTION__, desc->width, desc->height, desc->caps );

362 363 364 365 366
  ret = _gdk_display->layer->CreateWindow (_gdk_display->layer, desc, &window);

  if (ret != DFB_OK)
    {
      DirectFBError ("gdk_window_new: Layer->CreateWindow failed", ret);
367
      g_assert (0);
368 369 370 371
      return FALSE;
    }

  if ((desc->flags & DWDESC_CAPS) && (desc->caps & DWCAPS_INPUTONLY))
372
  {
373
    impl->drawable.surface = NULL;
374
  } else 
375 376 377 378 379 380 381 382 383 384 385
    window->GetSurface (window, &impl->drawable.surface);

  if (window_options)
    {
      DFBWindowOptions options;
      window->GetOptions (window, &options);
      window->SetOptions (window,  options | window_options);
    }

  impl->window = window;

386 387 388 389 390 391
#ifndef GDK_DIRECTFB_NO_EXPERIMENTS
  //direct_log_printf( NULL, "Initializing (window %p, wimpl %p)\n", win, impl );

  dfb_updates_init( &impl->flips, impl->flip_regions, G_N_ELEMENTS(impl->flip_regions) );
#endif

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  return TRUE;
}

void
_gdk_windowing_window_init (void)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;
  DFBDisplayLayerConfig  dlc;

  g_assert (_gdk_parent_root == NULL);

  _gdk_display->layer->GetConfiguration( 
	_gdk_display->layer, &dlc );

  _gdk_parent_root = g_object_new (GDK_TYPE_WINDOW, NULL);
  private = GDK_WINDOW_OBJECT (_gdk_parent_root);
  impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  private->window_type = GDK_WINDOW_ROOT;
  private->state       = 0;
  private->children    = NULL;
414
//  impl->drawable.paint_region   = NULL;
415
  impl->gdkWindow      = _gdk_parent_root;
416 417 418 419 420 421 422 423 424 425 426
  impl->window           = NULL;
  impl->drawable.abs_x   = 0;
  impl->drawable.abs_y   = 0;
  impl->drawable.width   = dlc.width;
  impl->drawable.height  = dlc.height;
  impl->drawable.wrapper = GDK_DRAWABLE (private);
  /* custom root window init */
  {
    DFBWindowDescription   desc;
    desc.flags = 0;
	/*XXX I must do this now its a bug  ALPHA ROOT*/
427 428 429 430 431

    desc.flags = DWDESC_CAPS;
    desc.caps = 0;
    desc.caps  |= DWCAPS_NODECORATION;
    desc.caps  |= DWCAPS_ALPHACHANNEL;
432 433 434 435 436 437 438 439 440 441 442 443
    desc.flags |= ( DWDESC_WIDTH | DWDESC_HEIGHT |
                      DWDESC_POSX  | DWDESC_POSY );
    desc.posx   = 0;
    desc.posy   = 0;
    desc.width  = dlc.width;
    desc.height = dlc.height;
    create_directfb_window (impl,&desc,0);
	g_assert(impl->window != NULL);
    g_assert(impl->drawable.surface != NULL );
  }
  impl->drawable.surface->GetPixelFormat(impl->drawable.surface,&impl->drawable.format); 
  private->depth = DFB_BITS_PER_PIXEL(impl->drawable.format);
444 445 446 447
  /*
	Now we can set up the system colormap
  */
  gdk_drawable_set_colormap (GDK_DRAWABLE (_gdk_parent_root),gdk_colormap_get_system());
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}



GdkWindow *
gdk_directfb_window_new (GdkWindow              *parent,
                         GdkWindowAttr          *attributes,
                         gint                    attributes_mask,
                         DFBWindowCapabilities   window_caps,
                         DFBWindowOptions        window_options,
                         DFBSurfaceCapabilities  surface_caps)
{
  GdkWindow             *window;
  GdkWindowObject       *private;
  GdkWindowObject       *parent_private;
  GdkWindowImplDirectFB *impl;
  GdkWindowImplDirectFB *parent_impl;
  GdkVisual             *visual;
  DFBWindowDescription   desc;
  gint x, y;

  g_return_val_if_fail (attributes != NULL, NULL);

471 472
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, parent );

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  if (!parent || attributes->window_type != GDK_WINDOW_CHILD)
    parent = _gdk_parent_root;

  window = g_object_new (GDK_TYPE_WINDOW, NULL);
  private = GDK_WINDOW_OBJECT (window);

  parent_private = GDK_WINDOW_OBJECT (parent);
  parent_impl = GDK_WINDOW_IMPL_DIRECTFB (parent_private->impl);
  private->parent = parent_private;

  x = (attributes_mask & GDK_WA_X) ? attributes->x : 0;
  y = (attributes_mask & GDK_WA_Y) ? attributes->y : 0;

  gdk_window_set_events (window, attributes->event_mask | GDK_STRUCTURE_MASK);

  impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);
  impl->drawable.wrapper = GDK_DRAWABLE (window);
490
  impl->gdkWindow      = window;
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 517 518 519 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 573 574 575 576 577 578 579

  private->x = x;
  private->y = y;

  _gdk_directfb_calc_abs (window);

  impl->drawable.width  = MAX (1, attributes->width);
  impl->drawable.height = MAX (1, attributes->height);

  private->window_type = attributes->window_type;

  desc.flags = 0;

  if (attributes_mask & GDK_WA_VISUAL)
    visual = attributes->visual;
  else
    visual = gdk_drawable_get_visual (parent);

  switch (attributes->wclass)
    {
    case GDK_INPUT_OUTPUT:
      private->input_only = FALSE;

      desc.flags |= DWDESC_PIXELFORMAT;
      desc.pixelformat = ((GdkVisualDirectFB *) visual)->format;

      if (DFB_PIXELFORMAT_HAS_ALPHA (desc.pixelformat))
        {
          desc.flags |= DWDESC_CAPS;
          desc.caps = DWCAPS_ALPHACHANNEL;
        }
      break;

    case GDK_INPUT_ONLY:
      private->input_only = TRUE;
      desc.flags |= DWDESC_CAPS;
      desc.caps = DWCAPS_INPUTONLY;
      break;

    default:
      g_warning ("gdk_window_new: unsupported window class\n");
      _gdk_window_destroy (window, FALSE);
      return NULL;
    }

  switch (private->window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
      desc.flags |= ( DWDESC_WIDTH | DWDESC_HEIGHT |
                      DWDESC_POSX  | DWDESC_POSY );
      desc.posx   = x;
      desc.posy   = y;
      desc.width  = impl->drawable.width;
      desc.height = impl->drawable.height;
#if 0
      if (window_caps)
        {
          if (! (desc.flags & DWDESC_CAPS))
            {
              desc.flags |= DWDESC_CAPS;
              desc.caps   = DWCAPS_NONE;
            }

          desc.caps |= window_caps;
        }

      if (surface_caps)
        {
          desc.flags |= DWDESC_SURFACE_CAPS;
          desc.surface_caps = surface_caps;
        }
#endif

      if (!create_directfb_window (impl, &desc, window_options))
        {
		  g_assert(0);
          _gdk_window_destroy (window, FALSE);
          return NULL;
        }
      	if( desc.caps != DWCAPS_INPUTONLY )
			impl->window->SetOpacity(impl->window, 0x00 );
      break;

    case GDK_WINDOW_CHILD:
	   impl->window=NULL;
      if (!private->input_only && parent_impl->drawable.surface)
        {
580

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
          DFBRectangle rect =
          { x, y, impl->drawable.width, impl->drawable.height };
          parent_impl->drawable.surface->GetSubSurface (parent_impl->drawable.surface,
                                                        &rect,
                                                        &impl->drawable.surface);
        }
      break;

    default:
      g_warning ("gdk_window_new: unsupported window type: %d",
                 private->window_type);
      _gdk_window_destroy (window, FALSE);
      return NULL;
    }

  if (impl->drawable.surface)
    {
      GdkColormap *colormap;

      impl->drawable.surface->GetPixelFormat (impl->drawable.surface,
					      &impl->drawable.format);

  	  private->depth = DFB_BITS_PER_PIXEL(impl->drawable.format);

      if ((attributes_mask & GDK_WA_COLORMAP) && attributes->colormap)
	{
	  colormap = attributes->colormap;
	}
      else
	{
	  if (gdk_visual_get_system () == visual)
	    colormap = gdk_colormap_get_system ();
	  else
	    colormap =gdk_drawable_get_colormap (parent);
	}

      gdk_drawable_set_colormap (GDK_DRAWABLE (window), colormap);
    }
  else
    {
      impl->drawable.format = ((GdkVisualDirectFB *)visual)->format;
  	  private->depth = visual->depth;
    }

  gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
                                  (attributes->cursor) : NULL));

  if (parent_private)
    parent_private->children = g_list_prepend (parent_private->children,
                                               window);

  /* we hold a reference count on ourselves */
  g_object_ref (window);

  if (impl->window)
    {
      impl->window->GetID (impl->window, &impl->dfb_id);
      gdk_directfb_window_id_table_insert (impl->dfb_id, window);
      gdk_directfb_event_windows_add (window);
    }

Cody Russell's avatar
Cody Russell committed
642 643 644
  if (attributes_mask & GDK_WA_TYPE_HINT)
    gdk_window_set_type_hint (window, attributes->type_hint);

645 646 647 648
  return window;
}

GdkWindow *
649
_gdk_window_new (GdkWindow     *parent,
650 651 652 653 654
                GdkWindowAttr *attributes,
                gint           attributes_mask)
{
  g_return_val_if_fail (attributes != NULL, NULL);

655 656
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, parent );

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  return gdk_directfb_window_new (parent, attributes, attributes_mask,
                                  DWCAPS_NONE, DWOP_NONE, DSCAPS_NONE);
}
void
_gdk_windowing_window_destroy_foreign (GdkWindow *window)
{
  /* It's somebody else's window, but in our heirarchy,
   * so reparent it to the root window, and then send
   * it a delete event, as if we were a WM
   */
	_gdk_windowing_window_destroy (window,TRUE,TRUE);
}


void
_gdk_windowing_window_destroy (GdkWindow *window,
                               gboolean   recursing,
                               gboolean   foreign_destroy)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;

  g_return_if_fail (GDK_IS_WINDOW (window));

681 682 683
  D_DEBUG_AT( GDKDFB_Window, "%s( %p, %srecursing, %sforeign )\n", __FUNCTION__, window,
              recursing ? "" : "not ", foreign_destroy ? "" : "no " );

684 685 686 687
  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  _gdk_selection_window_destroyed (window);
688 689 690
#if (DIRECTFB_MAJOR_VERSION >= 1)
  gdk_directfb_event_windows_remove (window);
#endif
691 692 693 694 695 696 697 698
  if (window == _gdk_directfb_pointer_grab_window)
    gdk_pointer_ungrab (GDK_CURRENT_TIME);
  if (window == _gdk_directfb_keyboard_grab_window)
    gdk_keyboard_ungrab (GDK_CURRENT_TIME);

  if (window == gdk_directfb_focused_window)
    gdk_directfb_change_focus (NULL);

699 700 701 702 703 704 705 706 707 708 709

  if (impl->drawable.surface) {
    GdkDrawableImplDirectFB *dimpl = GDK_DRAWABLE_IMPL_DIRECTFB (private->impl);
    if(dimpl->cairo_surface) {
      cairo_surface_destroy(dimpl->cairo_surface);
      dimpl->cairo_surface= NULL;
    }
    impl->drawable.surface->Release (impl->drawable.surface);
    impl->drawable.surface = NULL;
  }

710 711 712 713
  if (!recursing && !foreign_destroy && impl->window ) {
    	impl->window->SetOpacity (impl->window,0);
   		impl->window->Close(impl->window);
      	impl->window->Release(impl->window);
714
        impl->window = NULL;
715 716 717 718 719 720 721 722 723 724
  }
}

/* This function is called when the window is really gone.
 */
void
gdk_window_destroy_notify (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

725 726
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
  if (!GDK_WINDOW_DESTROYED (window))
    {
      if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_FOREIGN)
	g_warning ("GdkWindow %p unexpectedly destroyed", window);

      _gdk_window_destroy (window, TRUE);
    }
   g_object_unref (window);
}

/* Focus follows pointer */
GdkWindow *
gdk_directfb_window_find_toplevel (GdkWindow *window)
{
  while (window && window != _gdk_parent_root)
    {
      GdkWindow *parent = (GdkWindow *) (GDK_WINDOW_OBJECT (window))->parent;

      if ((parent == _gdk_parent_root) && GDK_WINDOW_IS_MAPPED (window))
        return window;

      window = parent;
    }

  return _gdk_parent_root;
}

GdkWindow *
gdk_directfb_window_find_focus (void)
{
  if (_gdk_directfb_keyboard_grab_window)
    return _gdk_directfb_keyboard_grab_window;

  if (!gdk_directfb_focused_window)
    gdk_directfb_focused_window = g_object_ref (_gdk_parent_root);

  return gdk_directfb_focused_window;
}

void
gdk_directfb_change_focus (GdkWindow *new_focus_window)
{
  GdkEventFocus *event;
  GdkWindow     *old_win;
  GdkWindow     *new_win;
  GdkWindow     *event_win;

774
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, new_focus_window );
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

  /* No focus changes while the pointer is grabbed */
  if (_gdk_directfb_pointer_grab_window)
    return;

  old_win = gdk_directfb_focused_window;
  new_win = gdk_directfb_window_find_toplevel (new_focus_window);

  if (old_win == new_win)
    return;

  if (old_win)
    {
      event_win = gdk_directfb_keyboard_event_window (old_win,
                                                      GDK_FOCUS_CHANGE);
      if (event_win)
        {
          event = (GdkEventFocus *) gdk_directfb_event_make (event_win,
                                                             GDK_FOCUS_CHANGE);
          event->in = FALSE;
        }
    }

  event_win = gdk_directfb_keyboard_event_window (new_win,
                                                  GDK_FOCUS_CHANGE);
  if (event_win)
    {
      event = (GdkEventFocus *) gdk_directfb_event_make (event_win,
                                                         GDK_FOCUS_CHANGE);
      event->in = TRUE;
    }

  if (gdk_directfb_focused_window)
    g_object_unref (gdk_directfb_focused_window);
  gdk_directfb_focused_window = g_object_ref (new_win);
}

void
gdk_window_set_accept_focus (GdkWindow *window,
                             gboolean accept_focus)
{
  GdkWindowObject *private;
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
                                                                                                                      
  private = (GdkWindowObject *)window;
                                                                                                                      
  accept_focus = accept_focus != FALSE;
                                                                                                                      
  if (private->accept_focus != accept_focus)
    private->accept_focus = accept_focus;

}

void
gdk_window_set_focus_on_map (GdkWindow *window,
                             gboolean focus_on_map)
{
  GdkWindowObject *private;
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
                                                                                                                      
  private = (GdkWindowObject *)window;
                                                                                                                      
  focus_on_map = focus_on_map != FALSE;
                                                                                                                      
  if (private->focus_on_map != focus_on_map)
    private->focus_on_map = focus_on_map;
}

static gboolean
gdk_directfb_window_raise (GdkWindow *window)
{
  GdkWindowObject *parent;

850 851
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  parent = GDK_WINDOW_OBJECT (window)->parent;

  if (parent->children->data == window)
    return FALSE;

  parent->children = g_list_remove (parent->children, window);
  parent->children = g_list_prepend (parent->children, window);

  return TRUE;
}

static void
gdk_directfb_window_lower (GdkWindow *window)
{
  GdkWindowObject *parent;

868 869
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
  parent = GDK_WINDOW_OBJECT (window)->parent;

  parent->children = g_list_remove (parent->children, window);
  parent->children = g_list_append (parent->children, window);
}

static gboolean
all_parents_shown (GdkWindowObject *private)
{
  while (GDK_WINDOW_IS_MAPPED (private))
    {
      if (private->parent)
        private = GDK_WINDOW_OBJECT (private)->parent;
      else
        return TRUE;
    }

  return FALSE;
}

static void
send_map_events (GdkWindowObject *private)
{
  GList     *list;
  GdkWindow *event_win;

  if (!GDK_WINDOW_IS_MAPPED (private))
    return;

899 900
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, private );

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
  event_win = gdk_directfb_other_event_window ((GdkWindow *) private, GDK_MAP);
  if (event_win)
    gdk_directfb_event_make (event_win, GDK_MAP);

  for (list = private->children; list; list = list->next)
    send_map_events (list->data);
}

static GdkWindow *
gdk_directfb_find_common_ancestor (GdkWindow *win1,
                                   GdkWindow *win2)
{
  GdkWindowObject *a;
  GdkWindowObject *b;

  for (a = GDK_WINDOW_OBJECT (win1); a; a = a->parent)
    for (b = GDK_WINDOW_OBJECT (win2); b; b = b->parent)
      {
        if (a == b)
          return GDK_WINDOW (a);
      }

  return NULL;
}

void
gdk_directfb_window_send_crossing_events (GdkWindow       *src,
                                          GdkWindow       *dest,
                                          GdkCrossingMode  mode)
{
  GdkWindow       *c;
  GdkWindow       *win, *last, *next;
  GdkEvent        *event;
  gint             x, y, x_int, y_int;
  GdkModifierType  modifiers;
  GSList          *path, *list;
  gboolean         non_linear;
  GdkWindow       *a;
  GdkWindow       *b;
  GdkWindow       *event_win;

942 943
  D_DEBUG_AT( GDKDFB_Crossing, "%s( %p -> %p, %d )\n", __FUNCTION__, src, dest, mode );

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
  /* Do a possible cursor change before checking if we need to
     generate crossing events so cursor changes due to pointer
     grabs work correctly. */
  {
    static GdkCursorDirectFB *last_cursor = NULL;

    GdkWindowObject       *private = GDK_WINDOW_OBJECT (dest);
    GdkWindowImplDirectFB *impl    = GDK_WINDOW_IMPL_DIRECTFB (private->impl);
    GdkCursorDirectFB     *cursor;

    if (_gdk_directfb_pointer_grab_cursor)
      cursor = (GdkCursorDirectFB*) _gdk_directfb_pointer_grab_cursor;
    else
      cursor = (GdkCursorDirectFB*) impl->cursor;

    if (cursor != last_cursor)
      {
        win     = gdk_directfb_window_find_toplevel (dest);
        private = GDK_WINDOW_OBJECT (win);
        impl    = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

        if (impl->window)
          impl->window->SetCursorShape (impl->window,
                                        cursor->shape,
                                        cursor->hot_x, cursor->hot_y);
        last_cursor = cursor;
      }
  }

973 974
  if (dest == gdk_directfb_window_containing_pointer) {
    D_DEBUG_AT( GDKDFB_Crossing, "  -> already containing the pointer\n" );
975
    return;
976
  }
977 978 979 980 981 982 983 984 985 986 987

  if (gdk_directfb_window_containing_pointer == NULL)
    gdk_directfb_window_containing_pointer = g_object_ref (_gdk_parent_root);

  if (src)
    a = src;
  else
    a = gdk_directfb_window_containing_pointer;

  b = dest;

988 989
  if (a == b) {
    D_DEBUG_AT( GDKDFB_Crossing, "  -> src == dest\n" );
990
    return;
991
  }
992 993 994 995 996

  /* gdk_directfb_window_containing_pointer might have been destroyed.
   * The refcount we hold on it should keep it, but it's parents
   * might have died.
   */
997 998
  if (GDK_WINDOW_DESTROYED (a)) {
    D_DEBUG_AT( GDKDFB_Crossing, "  -> src is destroyed!\n" );
999
    a = _gdk_parent_root;
1000
  }
1001 1002 1003 1004 1005

  gdk_directfb_mouse_get_info (&x, &y, &modifiers);

  c = gdk_directfb_find_common_ancestor (a, b);

1006 1007
  D_DEBUG_AT( GDKDFB_Crossing, "  -> common ancestor %p\n", c );

1008 1009
  non_linear = (c != a) && (c != b);

1010 1011
  D_DEBUG_AT( GDKDFB_Crossing, "  -> non_linear: %s\n", non_linear ? "YES" : "NO" );

1012 1013 1014
  event_win = gdk_directfb_pointer_event_window (a, GDK_LEAVE_NOTIFY);
  if (event_win)
    {
1015 1016
      D_DEBUG_AT( GDKDFB_Crossing, "  -> sending LEAVE to src\n" );

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
      event = gdk_directfb_event_make (event_win, GDK_LEAVE_NOTIFY);
      event->crossing.subwindow = NULL;

      gdk_window_get_origin (a, &x_int, &y_int);

      event->crossing.x      = x - x_int;
      event->crossing.y      = y - y_int;
      event->crossing.x_root = x;
      event->crossing.y_root = y;
      event->crossing.mode   = mode;

      if (non_linear)
        event->crossing.detail = GDK_NOTIFY_NONLINEAR;
      else if (c == a)
        event->crossing.detail = GDK_NOTIFY_INFERIOR;
      else
        event->crossing.detail = GDK_NOTIFY_ANCESTOR;

      event->crossing.focus = FALSE;
      event->crossing.state = modifiers;
1037 1038 1039 1040 1041

      D_DEBUG_AT( GDKDFB_Crossing, "  => LEAVE (%p/%p) at %4f,%4f (%4f,%4f) mode %d, detail %d\n",
                  event_win, a,
                  event->crossing.x, event->crossing.y, event->crossing.x_root, event->crossing.y_root,
                  event->crossing.mode, event->crossing.detail );
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    }

   /* Traverse up from a to (excluding) c */
  if (c != a)
    {
      last = a;
      win = GDK_WINDOW (GDK_WINDOW_OBJECT (a)->parent);
      while (win != c)
        {
          event_win =
            gdk_directfb_pointer_event_window (win, GDK_LEAVE_NOTIFY);

          if (event_win)
            {
              event = gdk_directfb_event_make (event_win, GDK_LEAVE_NOTIFY);

              event->crossing.subwindow = g_object_ref (last);

              gdk_window_get_origin (win, &x_int, &y_int);

              event->crossing.x      = x - x_int;
              event->crossing.y      = y - y_int;
              event->crossing.x_root = x;
              event->crossing.y_root = y;
              event->crossing.mode   = mode;

              if (non_linear)
                event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
              else
                event->crossing.detail = GDK_NOTIFY_VIRTUAL;

              event->crossing.focus = FALSE;
              event->crossing.state = modifiers;
1075 1076 1077 1078 1079

              D_DEBUG_AT( GDKDFB_Crossing, "  -> LEAVE (%p/%p) at %4f,%4f (%4f,%4f) mode %d, detail %d\n",
                          event_win, win,
                          event->crossing.x, event->crossing.y, event->crossing.x_root, event->crossing.y_root,
                          event->crossing.mode, event->crossing.detail );
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
            }

          last = win;
          win = GDK_WINDOW (GDK_WINDOW_OBJECT (win)->parent);
        }
    }

  /* Traverse down from c to b */
  if (c != b)
    {
      path = NULL;
      win = GDK_WINDOW (GDK_WINDOW_OBJECT (b)->parent);
      while (win != c)
        {
          path = g_slist_prepend (path, win);
          win = GDK_WINDOW (GDK_WINDOW_OBJECT (win)->parent);
        }

      list = path;
      while (list)
        {
          win = GDK_WINDOW (list->data);
          list = g_slist_next (list);

          if (list)
            next = GDK_WINDOW (list->data);
          else
            next = b;

          event_win =
            gdk_directfb_pointer_event_window (win, GDK_ENTER_NOTIFY);

          if (event_win)
            {
              event = gdk_directfb_event_make (event_win, GDK_ENTER_NOTIFY);

              event->crossing.subwindow = g_object_ref (next);

              gdk_window_get_origin (win, &x_int, &y_int);

              event->crossing.x      = x - x_int;
              event->crossing.y      = y - y_int;
              event->crossing.x_root = x;
              event->crossing.y_root = y;
              event->crossing.mode   = mode;

              if (non_linear)
                event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
              else
                event->crossing.detail = GDK_NOTIFY_VIRTUAL;

              event->crossing.focus = FALSE;
              event->crossing.state = modifiers;
1133 1134 1135 1136 1137

              D_DEBUG_AT( GDKDFB_Crossing, "  -> ENTER (%p/%p) at %4f,%4f (%4f,%4f) mode %d, detail %d\n",
                          event_win, win,
                          event->crossing.x, event->crossing.y, event->crossing.x_root, event->crossing.y_root,
                          event->crossing.mode, event->crossing.detail );
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
            }
        }

      g_slist_free (path);
    }

  event_win = gdk_directfb_pointer_event_window (b, GDK_ENTER_NOTIFY);
  if (event_win)
    {
      event = gdk_directfb_event_make (event_win, GDK_ENTER_NOTIFY);

      event->crossing.subwindow = NULL;

      gdk_window_get_origin (b, &x_int, &y_int);

      event->crossing.x      = x - x_int;
      event->crossing.y      = y - y_int;
      event->crossing.x_root = x;
      event->crossing.y_root = y;
      event->crossing.mode   = mode;

      if (non_linear)
        event->crossing.detail = GDK_NOTIFY_NONLINEAR;
      else if (c==a)
        event->crossing.detail = GDK_NOTIFY_ANCESTOR;
      else
        event->crossing.detail = GDK_NOTIFY_INFERIOR;

      event->crossing.focus = FALSE;
      event->crossing.state = modifiers;
1168 1169 1170 1171 1172

      D_DEBUG_AT( GDKDFB_Crossing, "  => ENTER (%p/%p) at %4f,%4f (%4f,%4f) mode %d, detail %d\n",
                  event_win, b,
                  event->crossing.x, event->crossing.y, event->crossing.x_root, event->crossing.y_root,
                  event->crossing.mode, event->crossing.detail );
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    }

  if (mode != GDK_CROSSING_GRAB)
    {
      //this seems to cause focus to change as the pointer moves yuck
      //gdk_directfb_change_focus (b);
      if (b != gdk_directfb_window_containing_pointer)
        {
          g_object_unref (gdk_directfb_window_containing_pointer);
          gdk_directfb_window_containing_pointer = g_object_ref (b);
        }
    }
}

static void
show_window_internal (GdkWindow *window,
                      gboolean   raise)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;
  GdkWindow             *mousewin;

1195 1196
  D_DEBUG_AT( GDKDFB_Window, "%s( %p, %sraise )\n", __FUNCTION__, window, raise ? "" : "no " );

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  if (!private->destroyed && !GDK_WINDOW_IS_MAPPED (private))
    {
      private->state &= ~GDK_WINDOW_STATE_WITHDRAWN;

      if (raise)
        gdk_window_raise (window);

      if (all_parents_shown (GDK_WINDOW_OBJECT (private)->parent))
        {
          send_map_events (private);

          mousewin = gdk_window_at_pointer (NULL, NULL);
          gdk_directfb_window_send_crossing_events (NULL, mousewin,
                                                    GDK_CROSSING_NORMAL);

          if (private->input_only)
            return;

          gdk_window_invalidate_rect (window, NULL, TRUE);
        }
    }

  if (impl->window)
    {
      if (gdk_directfb_apply_focus_opacity)
	impl->window->SetOpacity (impl->window,
				  (impl->opacity >> 1) + (impl->opacity >> 2));
      else
	impl->window->SetOpacity (impl->window, impl->opacity);
	  /* if its the first window focus it */
    }
}

void
gdk_window_show_unraised (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

1238 1239
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

1240 1241 1242 1243 1244 1245 1246 1247
  show_window_internal (window, FALSE);
}

void
gdk_window_show (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

1248 1249
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  show_window_internal (window, TRUE);
}

void
gdk_window_hide (GdkWindow *window)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;
  GdkWindow             *mousewin;
  GdkWindow             *event_win;

  g_return_if_fail (GDK_IS_WINDOW (window));

1263 1264
  D_DEBUG_AT( GDKDFB_Window, "%s( %p )\n", __FUNCTION__, window );

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
  private = GDK_WINDOW_OBJECT (window);
  impl    = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  if (impl->window)
    impl->window->SetOpacity (impl->window, 0);

  if (!private->destroyed && GDK_WINDOW_IS_MAPPED (private))
    {
      GdkEvent *event;

      private->state |= GDK_WINDOW_STATE_WITHDRAWN;

      if (!private->input_only && private->parent)
        {
          _gdk_windowing_window_clear_area (GDK_WINDOW (private->parent),
                                            private->x,
                                            private->y,
                                            impl->drawable.width,
                                            impl->drawable.height);
        }

      event_win = gdk_directfb_other_event_window (window, GDK_UNMAP);
      if (event_win)
        event = gdk_directfb_event_make (event_win, GDK_UNMAP);

      mousewin = gdk_window_at_pointer (NULL, NULL);
      gdk_directfb_window_send_crossing_events (NULL,
                                                mousewin,
                                                GDK_CROSSING_NORMAL);

      if (window == _gdk_directfb_pointer_grab_window)
        gdk_pointer_ungrab (GDK_CURRENT_TIME);
      if (window == _gdk_directfb_keyboard_grab_window)
        gdk_keyboard_ungrab (GDK_CURRENT_TIME);
    }
}

void
gdk_window_withdraw (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

  /* for now this should be enough */
  gdk_window_hide (window);
}

void
gdk_window_move (GdkWindow *window,
                 gint       x,
                 gint       y)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;

  g_return_if_fail (GDK_IS_WINDOW (window));

  private = GDK_WINDOW_OBJECT (window);
  impl    = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  if (impl->window)
    {
  	  private->x = x;
  	  private->y = y;
      impl->window->MoveTo (impl->window, x, y);
    }
  else
    {
	  gint width=impl->drawable.width;
	  gint height=impl->drawable.height;
      GdkRectangle  old =
      { private->x, private->y,width,height };

      _gdk_directfb_move_resize_child (window, x, y, width, height);
      _gdk_directfb_calc_abs (window);

      if (GDK_WINDOW_IS_MAPPED (private))
        {
          GdkWindow    *mousewin;
          GdkRectangle  new = { x, y, width, height };

          gdk_rectangle_union (&new, &old, &new);
          gdk_window_invalidate_rect (GDK_WINDOW (private->parent), &new,TRUE);

          /* The window the pointer is in might have changed */
          mousewin = gdk_window_at_pointer (NULL, NULL);
          gdk_directfb_window_send_crossing_events (NULL, mousewin,
                                                    GDK_CROSSING_NORMAL);
        }
    }
}

void
gdk_window_resize (GdkWindow *window,
                   gint       width,
                   gint       height)
{
  GdkWindowObject *private;
  gint             x, y;

  g_return_if_fail (GDK_IS_WINDOW (window));

  private = GDK_WINDOW_OBJECT (window);

  x = private->x;
  y = private->y;

  if (private->parent && (private->parent->window_type != GDK_WINDOW_CHILD))
    {
      GdkWindowChildHandlerData *data;

      data = g_object_get_data (G_OBJECT (private->parent),
				"gdk-window-child-handler");

      if (data)
	(*data->get_pos) (window, &x, &y, data->user_data);
    }

  gdk_window_move_resize (window, x, y, width, height);
}

void
_gdk_directfb_move_resize_child (GdkWindow *window,
                                 gint       x,
                                 gint       y,
                                 gint       width,
                                 gint       height)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;
  GdkWindowImplDirectFB *parent_impl;
  GList                 *list;

  g_return_if_fail (GDK_IS_WINDOW (window));

  private = GDK_WINDOW_OBJECT (window);
  impl    = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  private->x = x;
  private->y = y;

  impl->drawable.width  = width;
  impl->drawable.height = height;

  if (!private->input_only)
    {
1410 1411 1412 1413 1414 1415 1416 1417 1418
    if (impl->drawable.surface) {
      GdkDrawableImplDirectFB *dimpl = GDK_DRAWABLE_IMPL_DIRECTFB (private->impl);
      if(dimpl->cairo_surface) {
        cairo_surface_destroy(dimpl->cairo_surface);
        dimpl->cairo_surface= NULL;
      }
    impl->drawable.surface->Release (impl->drawable.surface);
    impl->drawable.surface = NULL;
  }
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511

      parent_impl = GDK_WINDOW_IMPL_DIRECTFB (GDK_WINDOW_OBJECT (private->parent)->impl);

      if (parent_impl->drawable.surface)
        {
          DFBRectangle rect = { x, y, width, height };

          parent_impl->drawable.surface->GetSubSurface (parent_impl->drawable.surface,
                                                        &rect,
                                                        &impl->drawable.surface);
        }
    }

  for (list = private->children; list; list = list->next)
    {
      private = GDK_WINDOW_OBJECT (list->data);
  	  impl  = GDK_WINDOW_IMPL_DIRECTFB (private->impl);
      _gdk_directfb_move_resize_child (list->data,
                                       private->x, private->y,
                                       impl->drawable.width, impl->drawable.height);
    }
}

void
gdk_window_move_resize (GdkWindow *window,
                        gint       x,
                        gint       y,
                        gint       width,
                        gint       height)
{
  GdkWindowObject       *private;
  GdkWindowImplDirectFB *impl;

  g_return_if_fail (GDK_IS_WINDOW (window));

  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);

  if (width < 1)
     width = 1;
  if (height < 1)
    height = 1;

  if (private->destroyed ||
      (private->x == x  &&  private->y == y  &&
       impl->drawable.width == width  &&  impl->drawable.height == height))
    return;

  if (private->parent && (private->parent->window_type != GDK_WINDOW_CHILD))
    {
      GdkWindowChildHandlerData *data;

      data = g_object_get_data (G_OBJECT (private->parent),
                                "gdk-window-child-handler");

      if (data &&
          (*data->changed) (window, x, y, width, height, data->user_data))
        return;
    }

  if (impl->drawable.width == width  &&  impl->drawable.height == height)
    {
      gdk_window_move (window, x, y);
    }
  else if (impl->window)
    {
  	  private->x = x;
  	  private->y = y;
      impl->window->MoveTo (impl->window, x, y);
      impl->window->Resize (impl->window, width, height);
    }
  else
    {
      GdkRectangle  old =
      { private->x, private->y, impl->drawable.width, impl->drawable.height };
      _gdk_directfb_move_resize_child (window, x, y, width, height);
      _gdk_directfb_calc_abs (window);

      if (GDK_WINDOW_IS_MAPPED (private))
        {
          GdkWindow    *mousewin;
          GdkRectangle  new = { x, y, width, height };

          gdk_rectangle_union (&new, &old, &new);
          gdk_window_invalidate_rect (GDK_WINDOW (private->parent), &new,TRUE);

          /* The window the pointer is in might have changed */
          mousewin = gdk_window_at_pointer (NULL, NULL);
          gdk_directfb_window_send_crossing_events (NULL, mousewin,
                                                    GDK_CROSSING_NORMAL);
        }
    }
}
1512

1513
void
1514
_gdk_window_reparent (GdkWindow *window,
1515 1516 1517 1518 1519 1520 1521
                     GdkWindow *new_parent,
                     gint       x,
                     gint       y)
{
  GdkWindowObject *window_private;
  GdkWindowObject *parent_private;
  GdkWindowObject *old_parent_private;
1522 1523 1524
  GdkWindowImplDirectFB *impl;
  GdkWindowImplDirectFB *parent_impl;
  GdkVisual             *visual;
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

  g_return_if_fail (GDK_IS_WINDOW (window));

  if (GDK_WINDOW_DESTROYED (window))
    return;

  if (!new_parent)
    new_parent = _gdk_parent_root;

  window_private = (GdkWindowObject *) window;
  old_parent_private = (GdkWindowObject *) window_private->parent;
  parent_private = (GdkWindowObject *) new_parent;
1537 1538 1539 1540 1541 1542
  parent_impl = GDK_WINDOW_IMPL_DIRECTFB (parent_private->impl);
  visual = gdk_drawable_get_visual (window);

  /* already parented */
  if( window_private->parent == (GdkWindowObject *)new_parent )
          return;
1543 1544 1545 1546 1547 1548 1549 1550 1551

  window_private->parent = (GdkWindowObject *) new_parent;

  if (old_parent_private)
    {
      old_parent_private->children =
        g_list_remove (old_parent_private->children, window);
    }

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    parent_private->children = g_list_prepend (parent_private->children, window);

    impl = GDK_WINDOW_IMPL_DIRECTFB (window_private->impl);

    if( impl->drawable.surface ) {
        impl->drawable.surface->Release (impl->drawable.surface);
        impl->drawable.surface = NULL;
    }

    if( impl->window != NULL ) { 
        gdk_directfb_window_id_table_remove (impl->dfb_id);
    	impl->window->SetOpacity (impl->window,0);
   		impl->window->Close(impl->window);
      	impl->window->Release(impl->window);
        impl->window = NULL;
    }
1568

1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
    //create window were a child of the root now
    if( window_private->parent == (GdkWindowObject *)_gdk_parent_root)  {
        DFBWindowDescription  desc;
        DFBWindowOptions  window_options = DWOP_NONE;
        desc.flags = DWDESC_CAPS;
        if( window_private->input_only ) {
            desc.caps = DWCAPS_INPUTONLY;
        } else {
            desc.flags |= DWDESC_PIXELFORMAT;
            desc.pixelformat = ((GdkVisualDirectFB *) visual)->format;
            if (DFB_PIXELFORMAT_HAS_ALPHA (desc.pixelformat)) {
                desc.flags |= DWDESC_CAPS;
                desc.caps = DWCAPS_ALPHACHANNEL;
            }
       }
       if( window_private->window_type ==  GDK_WINDOW_CHILD )
           window_private->window_type = GDK_WINDOW_TOPLEVEL;
        desc.flags |= ( DWDESC_WIDTH | DWDESC_HEIGHT |
                      DWDESC_POSX  | DWDESC_POSY );
        desc.posx   = x;
        desc.posy   = y;
        desc.width  = impl->drawable.width;
        desc.height = impl->drawable.height;
        if (!create_directfb_window (impl, &desc, window_options))
        {
		  g_assert(0);
          _gdk_window_destroy (window, FALSE);
          return;
        }
        /* we hold a reference count on ourselves */
        g_object_ref (window);
        impl->window->GetID (impl->window, &impl->dfb_id);
        gdk_directfb_window_id_table_insert (impl->dfb_id, window);
        gdk_directfb_event_windows_add (window);
   } else {
          DFBRectangle rect = { x, y, impl->drawable.width,
                                     impl->drawable.height};
          impl->window = NULL;
          parent_impl->drawable.surface->GetSubSurface (
                          parent_impl->drawable.surface,
                          &rect,
                          &impl->drawable.surface);
   }