summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xwin/winwndproc.c
blob: 35fb5369c2044a58ec8ad55a8d56fea00d9c7c2f (plain)
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
352
353
354
355
356
357
358
359
360
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
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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
 *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
 *
 *Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 *"Software"), to deal in the Software without restriction, including
 *without limitation the rights to use, copy, modify, merge, publish,
 *distribute, sublicense, and/or sell copies of the Software, and to
 *permit persons to whom the Software is furnished to do so, subject to
 *the following conditions:
 *
 *The above copyright notice and this permission notice shall be
 *included in all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *Except as contained in this notice, the name of the XFree86 Project
 *shall not be used in advertising or otherwise to promote the sale, use
 *or other dealings in this Software without prior written authorization
 *from the XFree86 Project.
 *
 * Authors:	Dakshinamurthy Karra
 *		Suhaib M Siddiqi
 *		Peter Busch
 *		Harold L Hunt II
 */
/* $XFree86: xc/programs/Xserver/hw/xwin/winwndproc.c,v 1.2 2001/04/18 17:14:07 dawes Exp $ */

#include "win.h"

/* Called by the WakeupHandler
 * Processes and/or dispatches Windows messages
 */
LRESULT CALLBACK
winWindowProc (HWND hWnd, UINT message, 
	       WPARAM wParam, LPARAM lParam)
{
  winPrivScreenPtr      pScreenPriv = NULL;
  winScreenInfo		*pScreenInfo = NULL;
  ScreenPtr		pScreen = NULL;
  xEvent		xCurrentEvent;
  int			iDeltaZ;
  HDC			hdcUpdate;
  PAINTSTRUCT		ps;
  LPCREATESTRUCT	pcs;
  HRESULT		ddrval;
  RECT			rcClient, rcSrc;
  int			iScanCode;

  /* Initialize our event structure */
  ZeroMemory (&xCurrentEvent, sizeof (xCurrentEvent));

  /* Retrieve screen privates pointers for this window */
  pScreenPriv = GetProp (hWnd, WIN_SCR_PROP);
  if (pScreenPriv != NULL)
    {
      pScreenInfo = pScreenPriv->pScreenInfo;
      pScreen = pScreenInfo->pScreen;
    }

  /* Branch on message type */
  switch (message)
    {
    case WM_CREATE:
      /* Add a property to our display window that references
	 this screens' privates.
	 
	 This allows the window procedure to refer to the
	 appropriate window DC and shadow DC for the window that
	 it is processing.  We use this to repaint exposed
	 areas of our display window.
      */
      pcs = (LPCREATESTRUCT) lParam;
      pScreenPriv = pcs->lpCreateParams;
      pScreen = pScreenPriv->pScreenInfo->pScreen;
      SetProp (hWnd,
	       WIN_SCR_PROP,
	       pScreenPriv);

      /* Store the mode key states so restore doesn't try to restore them */
      winStoreModeKeyStates (pScreen);
      return 0;

    case WM_PAINT:
      /* Only paint if we have privates and the server is enabled */
      if (pScreenPriv == NULL
	  || !pScreenPriv->fEnabled
	  || (pScreenInfo->fFullScreen && !pScreenPriv->fActive))
	{
	  /* We don't want to paint */
	  break;
	}
      
      /* BeginPaint gives us an hdc that clips to the invalidated region */
      hdcUpdate = BeginPaint (hWnd, &ps);

      /* Branch on server style */
      switch (pScreenInfo->dwEngine)
	{
	case WIN_SERVER_SHADOW_GDI:
	  /* Our BitBlt will be clipped to the invalidated region */
	  BitBlt (hdcUpdate,
		  0, 0,
		  pScreenInfo->dwWidth, pScreenInfo->dwHeight,
		  pScreenPriv->hdcShadow,
		  0, 0,
		  SRCCOPY);
	  break;

	case WIN_SERVER_SHADOW_DD:
	  /* Unlock the shadow surface, so we can blit */
	  ddrval = IDirectDrawSurface_Unlock (pScreenPriv->pddsShadow, NULL);
	  if (FAILED (ddrval))
	    FatalError ("winWindowProc () - DD unlock failed\n");
	  
	  /* Get client area in screen coords */
	  GetClientRect (pScreenPriv->hwndScreen, &rcClient);
	  MapWindowPoints (pScreenPriv->hwndScreen,
			   HWND_DESKTOP,
			   (LPPOINT)&rcClient, 2);
	  
	  /* Source can be enter shadow surface, as Blt should clip */
	  rcSrc.left = 0;
	  rcSrc.top = 0;
	  rcSrc.right = pScreenInfo->dwWidth;
	  rcSrc.bottom = pScreenInfo->dwHeight;

	  /* Our Blt should be clipped to the invalidated region */
	  ddrval = IDirectDrawSurface_Blt (pScreenPriv->pddsPrimary,
					   &rcClient,
					   pScreenPriv->pddsShadow,
					   &rcSrc,
					   DDBLT_WAIT,
					   NULL);

	  /* Relock the shadow surface */
	  ddrval = IDirectDrawSurface_Lock (pScreenPriv->pddsShadow,
					    NULL,
					    pScreenPriv->pddsdShadow,
					    DDLOCK_WAIT,
					    NULL);
	  if (FAILED (ddrval))
	    FatalError ("winWindowProc () - DD lock failed\n");

	  /* Has our memory pointer changed? */
	  if (pScreenInfo->pfb != pScreenPriv->pddsdShadow->lpSurface)
	    winUpdateFBPointer (pScreen,
				pScreenPriv->pddsdShadow->lpSurface);
	  break;

	case WIN_SERVER_SHADOW_DDNL:
	  /* Get client area in screen coords */
	  GetClientRect (pScreenPriv->hwndScreen, &rcClient);
	  MapWindowPoints (pScreenPriv->hwndScreen,
			   HWND_DESKTOP,
			   (LPPOINT)&rcClient, 2);
	  
	  /* Source can be enter shadow surface, as Blt should clip */
	  rcSrc.left = 0;
	  rcSrc.top = 0;
	  rcSrc.right = pScreenInfo->dwWidth;
	  rcSrc.bottom = pScreenInfo->dwHeight;

	  /* Our Blt should be clipped to the invalidated region */
	  ddrval = IDirectDrawSurface_Blt (pScreenPriv->pddsPrimary4,
					   &rcClient,
					   pScreenPriv->pddsShadow4,
					   &rcSrc,
					   DDBLT_WAIT,
					   NULL);
	  break;

	case WIN_SERVER_PRIMARY_DD:
	  /* FIXME: We only run in fullscreen mode with primary fb
	     DirectDraw server.

	     We'll have to hand roll the clipping for windowed mode;
	     the performance of the primary fb server is so bad
	     that it probably wouldn't be worth the effort to write
	     the clipping code.
	  */
	  break;
	default:
	  FatalError ("winWindowProc () - WM_PAINT - Unknown engine type\n");
	}

      /* EndPaint frees the DC */
      EndPaint (hWnd, &ps);
      return 0;

    case WM_MOUSEMOVE:
      /* We can't do anything without privates */
      if (pScreenPriv == NULL)
	break;

      /* Sometimes we hide, sometimes we show */
      if (pScreenPriv->fActive
	  && pScreenPriv->fCursor)
	{
	  /* Hide Windows cursor */
	  pScreenPriv->fCursor = FALSE;
	  ShowCursor (FALSE);
	}
      else if (!pScreenPriv->fActive
	       && !pScreenPriv->fCursor)
	{
	  /* Show Windows cursor */
	  pScreenPriv->fCursor = TRUE;
	  ShowCursor (TRUE);
	}

      /* Deliver absolute cursor position to X Server */
      miPointerAbsoluteCursor (GET_X_LPARAM(lParam),
			       GET_Y_LPARAM(lParam),
			       GetTickCount ());
      return 0;

    case WM_NCMOUSEMOVE:
      /* Non-client mouse movement, show Windows cursor */
      if (!pScreenPriv->fCursor)
	{
	  pScreenPriv->fCursor = TRUE;
	  ShowCursor (TRUE);
	}
      return 0;

    case WM_LBUTTONDBLCLK:
    case WM_LBUTTONDOWN:
      xCurrentEvent.u.u.type = ButtonPress;
      xCurrentEvent.u.u.detail = Button1;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_LBUTTONUP:
      xCurrentEvent.u.u.type = ButtonRelease;
      xCurrentEvent.u.u.detail = Button1;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_MBUTTONDBLCLK:
    case WM_MBUTTONDOWN:
      xCurrentEvent.u.u.type = ButtonPress;
      xCurrentEvent.u.u.detail = Button2;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_MBUTTONUP:
      xCurrentEvent.u.u.type = ButtonRelease;
      xCurrentEvent.u.u.detail = Button2;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_RBUTTONDBLCLK:
    case WM_RBUTTONDOWN:
      xCurrentEvent.u.u.type = ButtonPress;
      xCurrentEvent.u.u.detail = Button3;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_RBUTTONUP:
      xCurrentEvent.u.u.type = ButtonRelease;
      xCurrentEvent.u.u.detail = Button3;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_MOUSEWHEEL:
      /* Button4 = WheelUp */
      /* Button5 = WheelDown */

      /* Find out how far the wheel has moved */
      iDeltaZ = GET_WHEEL_DELTA_WPARAM(wParam);

      /* Do we have any previous delta stored? */
      if ((pScreenPriv->iDeltaZ > 0
	  && iDeltaZ > 0)
	  || (pScreenPriv->iDeltaZ < 0
	       && iDeltaZ < 0))
	{
	  /* Previous delta and of same sign as current delta */
	  iDeltaZ += pScreenPriv->iDeltaZ;
	  pScreenPriv->iDeltaZ = 0;
	}
      else
	{
	  /* Previous delta of different sign, or zero.
	     We will set it to zero for either case,
	     as blindly setting takes just as much time
	     as checking, then setting if necessary :) */
	  pScreenPriv->iDeltaZ = 0;
	}

      /*
	Only process this message if the wheel has moved further than
	WHEEL_DELTA
      */
      if (iDeltaZ >= WHEEL_DELTA || (-1 * iDeltaZ) >= WHEEL_DELTA)
	{
	  pScreenPriv->iDeltaZ = 0;
	  
	  /* Figure out how many whole deltas of the wheel we have */
	  iDeltaZ /= WHEEL_DELTA;
	}
      else
	{
	  /*
	    Wheel has not moved past WHEEL_DELTA threshold;
	    we will store the wheel delta until the threshold
	    has been reached.
	  */
	  pScreenPriv->iDeltaZ = iDeltaZ;
	  return 0;
	}

      /* Set the button to indicate up or down wheel delta */
      if (iDeltaZ > 0)
	{
	  xCurrentEvent.u.u.detail = Button4;
	}
      else
	{
	  xCurrentEvent.u.u.detail = Button5;
	}

      /*
	Flip iDeltaZ to positive, if negative,
	because always need to generate a *positive* number of
	button clicks for the Z axis.
      */
      if (iDeltaZ < 0)
	{
	  iDeltaZ *= -1;
	}

      /* Generate X input messages for each wheel delta we have seen */
      while (iDeltaZ--)
	{
	  /* Push the wheel button */
	  xCurrentEvent.u.u.type = ButtonPress;
	  xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
	  mieqEnqueue (&xCurrentEvent);

	  /* Release the wheel button */
	  xCurrentEvent.u.u.type = ButtonRelease;
	  xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
	  mieqEnqueue (&xCurrentEvent);
	}
      return 0;

    case WM_SYSKEYDOWN:
    case WM_KEYDOWN:
      winTranslateKey (wParam, lParam, &iScanCode);
      xCurrentEvent.u.u.type = KeyPress;
      xCurrentEvent.u.u.detail = iScanCode;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_SYSKEYUP:
    case WM_KEYUP:
      winTranslateKey (wParam, lParam, &iScanCode);
      xCurrentEvent.u.u.type = KeyRelease;
      xCurrentEvent.u.u.detail = iScanCode;
      xCurrentEvent.u.keyButtonPointer.time = GetTickCount ();
      mieqEnqueue (&xCurrentEvent);
      return 0;

    case WM_HOTKEY:
      if (pScreenPriv == NULL)
	break;

      /* Handle each engine type */
      switch (pScreenInfo->dwEngine)
	{
	case WIN_SERVER_PRIMARY_DD:
	  /* Alt+Tab was pressed, we will lose focus very soon */
	  pScreenPriv->fActive = FALSE;

	  /* We need to save the primary fb to an offscreen fb when
	     we get deactivated, and point the fb code at the offscreen
	     fb for the duration of the deactivation.
	  */
	  if (pScreenPriv != NULL
	      && pScreenPriv->pddsPrimary != NULL
	      && pScreenPriv->pddsPrimary != NULL)
	    {
	      /* We are deactivating */
		      
	      /* Get client area in screen coords */
	      GetClientRect (pScreenPriv->hwndScreen, &rcClient);
	      MapWindowPoints (pScreenPriv->hwndScreen,
			       HWND_DESKTOP,
			       (LPPOINT)&rcClient, 2);

	      /* Did we loose the primary surface? */
	      ddrval = IDirectDrawSurface_IsLost (pScreenPriv->pddsPrimary);
	      if (ddrval == DD_OK)
		{
		  ddrval = IDirectDrawSurface_Unlock (pScreenPriv->pddsPrimary,
						      NULL);
		  if (FAILED (ddrval))
		    FatalError ("winWindowProc () - Failed unlocking primary "\
				"surface\n");
		}
	      
	      /* Setup a source rectangle */
	      rcSrc.left = 0;
	      rcSrc.top = 0;
	      rcSrc.right = pScreenInfo->dwWidth;
	      rcSrc.bottom = pScreenInfo->dwHeight;

	      /* Blit the primary surface to the offscreen surface */
	      ddrval = IDirectDrawSurface_Blt (pScreenPriv->pddsOffscreen,
					       NULL, /* should be rcDest */
					       pScreenPriv->pddsPrimary,
					       NULL,
					       DDBLT_WAIT,
					       NULL);
	      if (ddrval == DDERR_SURFACELOST)
		{
		  IDirectDrawSurface_Restore (pScreenPriv->pddsOffscreen);  
		  IDirectDrawSurface_Restore (pScreenPriv->pddsPrimary);
		  		  
		  /* Blit the primary surface to the offscreen surface */
		  ddrval = IDirectDrawSurface_Blt (pScreenPriv->pddsOffscreen,
						   NULL, /* should be rcDest */
						   pScreenPriv->pddsPrimary,
						   NULL,
						   DDBLT_WAIT,
						   NULL);
		  if (FAILED (ddrval))
		    FatalError ("winWindowProc () - Failed blitting primary "\
				"surface to offscreen surface: %08x\n",
				ddrval);
		}
	      else
		{
		  FatalError ("winWindowProc() - Unknown error from "\
			      "Blt: %08dx\n", ddrval);
		}

	      /* Lock the offscreen surface */
	      ddrval = IDirectDrawSurface_Lock (pScreenPriv->pddsOffscreen,
						NULL,
						pScreenPriv->pddsdOffscreen,
						DDLOCK_WAIT,
						NULL);
	      if (ddrval != DD_OK
		  || pScreenPriv->pddsdPrimary->lpSurface == NULL)
		FatalError ("winWindowProc () - Could not lock "\
			    "offscreen surface\n");

	      /* Notify FB of the new memory pointer */
	      winUpdateFBPointer (pScreen,
				  pScreenPriv->pddsdOffscreen->lpSurface);

	      /* Unregister our hotkey */
	      UnregisterHotKey (hWnd, 1);
	      return 0;
	    }
	}
      break;

    case WM_ACTIVATE:
      /* Focus is being changed to another window.
	 The other window may or may not belong to
	 our process.
      */
      
      /* We can't do anything if we don't have screen privates */
      if (pScreenPriv == NULL)
	break;

      if (LOWORD (wParam) == WA_ACTIVE || LOWORD (wParam) == WA_CLICKACTIVE)
	{
	  /* Restore the state of all mode keys */
	  winRestoreModeKeyStates (pScreen);

	  /* Have we changed input screens? */
	  if (pScreenPriv->fEnabled
	      && pScreen != miPointerCurrentScreen ())
	    {
	      /*
		Tell mi that we are changing the screen that receives
		mouse input events.
	      */
	      miPointerSetNewScreen (pScreenInfo->dwScreen,
				     0, 0);
	    }
	}
      else
	{
	  /* Store the state of all mode keys */
	  winStoreModeKeyStates (pScreen);
	}

      /* Are we activating or deactivating? */
      if ((LOWORD(wParam) == WA_ACTIVE
	  || LOWORD(wParam) == WA_CLICKACTIVE)
	  && pScreenPriv->fCursor)
	{
	  /* Hide Windows cursor */
	  pScreenPriv->fCursor = FALSE;
	  ShowCursor (FALSE);
	}
      else if (LOWORD(wParam) == WA_INACTIVE
	       && !pScreenPriv->fCursor)
	{
	  /* Show Windows cursor */
	  pScreenPriv->fCursor = TRUE;
	  ShowCursor (TRUE);
	}
      return 0;

    case WM_ACTIVATEAPP:
      /* We can't do anything if we don't have screen privates */
      if (pScreenPriv == NULL)
	break;

      /* Activate or deactivate */
      pScreenPriv->fActive = wParam;

      /* Are we activating or deactivating? */
      if (pScreenPriv->fActive
	  && pScreenPriv->fCursor)
	{
	  /* Hide Windows cursor */
	  pScreenPriv->fCursor = FALSE;
	  ShowCursor (FALSE);
	}
      else if (!pScreenPriv->fActive
	       && !pScreenPriv->fCursor)
	{
	  /* Show Windows cursor */
	  pScreenPriv->fCursor = TRUE;
	  ShowCursor (TRUE);
	}

      /* Handle activation/deactivation for each engine */
      switch (pScreenInfo->dwEngine)
	{
	case WIN_SERVER_SHADOW_GDI:
	  /*
	    Are we active?
	    Are we fullscreen?
	  */
	  if (pScreenPriv != NULL
	      && pScreenPriv->fActive
	      && pScreenInfo->fFullScreen)
	    {
	      /*
		Activating, attempt to bring our window 
		to the top of the display
	      */
	      ShowWindow (hWnd, SW_RESTORE);
	    }
	  
	  /*
	    Are we inactive?
	    Are we fullscreen?
	  */
	  if (pScreenPriv != NULL
	      && !pScreenPriv->fActive
	      && pScreenInfo->fFullScreen)
	    {
	      /* Deactivating, stuff our window onto the
		 task bar.
	      */
	      ShowWindow (hWnd, SW_MINIMIZE);
	    }
	  break;

	case WIN_SERVER_SHADOW_DD:
	  /*
	    Do we have a surface?
	    Are we active?
	    Are we fullscreen?
	  */
	  if (pScreenPriv != NULL
	      && pScreenPriv->pddsPrimary != NULL
	      && pScreenPriv->fActive
	      //&& pScreenInfo->fFullScreen
	      )
	    {
	      /* Primary surface was lost, restore it */
	      IDirectDrawSurface_Restore (pScreenPriv->pddsPrimary);
	    }
	  break;

	case WIN_SERVER_SHADOW_DDNL:
	  /*
	    Do we have a surface?
	    Are we active?
	    Are we full screen?
	  */
	  if (pScreenPriv != NULL
	      && pScreenPriv->pddsPrimary4 != NULL
	      && pScreenPriv->fActive
	      //&& pScreenInfo->fFullScreen
	      )
	    {
	      /* Primary surface was lost, restore it */
	      IDirectDrawSurface_Restore (pScreenPriv->pddsPrimary4);
	    }
	  break;

	case WIN_SERVER_PRIMARY_DD:
	  /* We need to blit our offscreen fb to
	     the screen when we are activated, and we need to point
	     the fb code back to the primary surface memory.
	  */
	  if (pScreenPriv != NULL
	      && pScreenPriv->pddsPrimary != NULL
	      && pScreenPriv->pddsOffscreen != NULL
	      && pScreenPriv->fActive)
	    {
	      /* We are activating */
	      ddrval = IDirectDrawSurface_IsLost (pScreenPriv->pddsOffscreen);
	      if (ddrval == DD_OK)
		{
		  ddrval = IDirectDrawSurface_Unlock (pScreenPriv->pddsOffscreen,
						      NULL);
#if 0		 
		  if (FAILED (ddrval))
		    FatalError ("winWindowProc () - Failed unlocking "\
				"offscreen surface %08x\n", ddrval);
#endif
		}
	      
	      /* Restore both surfaces, just cause I like it that way */
	      IDirectDrawSurface_Restore (pScreenPriv->pddsOffscreen);
	      IDirectDrawSurface_Restore (pScreenPriv->pddsPrimary);
			      
	      /* Get client area in screen coords */
	      GetClientRect (pScreenPriv->hwndScreen, &rcClient);
	      MapWindowPoints (pScreenPriv->hwndScreen,
			       HWND_DESKTOP,
			       (LPPOINT)&rcClient, 2);
	      
	      /* Setup a source rectangle */
	      rcSrc.left = 0;
	      rcSrc.top = 0;
	      rcSrc.right = pScreenInfo->dwWidth;
	      rcSrc.bottom = pScreenInfo->dwHeight;

	      ddrval = IDirectDrawSurface_Blt (pScreenPriv->pddsPrimary,
					       &rcClient,
					       pScreenPriv->pddsOffscreen,
					       &rcSrc,
					       DDBLT_WAIT,
					       NULL);
	      if (FAILED (ddrval))
		FatalError ("winWindowProc () - Failed blitting offscreen "\
			    "surface to primary surface %08x\n", ddrval);

	      /* Lock the primary surface */
	      ddrval = IDirectDrawSurface_Lock (pScreenPriv->pddsPrimary,
						&rcClient,
						pScreenPriv->pddsdPrimary,
						DDLOCK_WAIT,
						NULL);
	      if (ddrval != DD_OK
		  || pScreenPriv->pddsdPrimary->lpSurface == NULL)
		FatalError ("winWindowProc () - Could not lock "\
			    "primary surface\n");

	      /* Notify FB of the new memory pointer */
	      winUpdateFBPointer (pScreen,
				  pScreenPriv->pddsdPrimary->lpSurface);

	      /* Register the Alt-Tab combo as a hotkey so we can copy
		 the primary framebuffer before the display mode changes
	      */
	      RegisterHotKey (hWnd, 1, MOD_ALT, 9);
	    }
	  break;
	default:
	  FatalError ("winWindowProc () - WM_ACTIVATEAPP - Unknown engine\n");
	}
      return 0;

    case WM_CLOSE:
      /* Tell X that we are giving up */
      GiveUp (0);
      return 0;
    }

  return DefWindowProc (hWnd, message, wParam, lParam);
}