xvmain.c 25.9 KB
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 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 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 774 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 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 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 899 900 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 942 943 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 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 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 1075 1076 1077 1078 1079 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 1133 1134 1135 1136 1137 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 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
/* $XdotOrg: xc/programs/Xserver/Xext/xvmain.c,v 1.6 2005/07/03 08:53:36 daniels Exp $ */
/***********************************************************
Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $XFree86: xc/programs/Xserver/Xext/xvmain.c,v 1.15tsi Exp $ */

/*
** File: 
**
**   xvmain.c --- Xv server extension main device independent module.
**   
** Author: 
**
**   David Carver (Digital Workstation Engineering/Project Athena)
**
** Revisions:
**
**   04.09.91 Carver
**     - change: stop video always generates an event even when video
**       wasn't active
**
**   29.08.91 Carver
**     - change: unrealizing windows no longer preempts video
**
**   11.06.91 Carver
**     - changed SetPortControl to SetPortAttribute
**     - changed GetPortControl to GetPortAttribute
**     - changed QueryBestSize
**
**   28.05.91 Carver
**     - fixed Put and Get requests to not preempt operations to same drawable
**
**   15.05.91 Carver
**     - version 2.0 upgrade
**
**   19.03.91 Carver
**     - fixed Put and Get requests to honor grabbed ports.
**     - fixed Video requests to update di structure with new drawable, and
**       client after calling ddx.
**
**   24.01.91 Carver
**     - version 1.4 upgrade
**       
** Notes:
**
**   Port structures reference client structures in a two different
**   ways: when grabs, or video is active.  Each reference is encoded
**   as fake client resources and thus when the client is goes away so
**   does the reference (it is zeroed).  No other action is taken, so
**   video doesn't necessarily stop.  It probably will as a result of
**   other resources going away, but if a client starts video using
**   none of its own resources, then the video will continue to play
**   after the client disappears.
**
**
*/

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/X.h>
#include <X11/Xproto.h>
#include "misc.h"
#include "os.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "gc.h"
#include "extnsionst.h"
#include "dixstruct.h"
#include "resource.h"
#include "opaque.h"
#include "input.h"

#define GLOBAL

#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvproto.h>
#include "xvdix.h"

#ifdef EXTMODULE
#include "xf86_ansic.h"
#endif

#ifdef PANORAMIX
#include "panoramiX.h"
#include "panoramiXsrv.h"
#include "xvdisp.h"
#endif

int  XvScreenIndex = -1;
unsigned long XvExtensionGeneration = 0;
unsigned long XvScreenGeneration = 0;
unsigned long XvResourceGeneration = 0;

int XvReqCode;
int XvEventBase;
int XvErrorBase;

unsigned long XvRTPort;
unsigned long XvRTEncoding;
unsigned long XvRTGrab;
unsigned long XvRTVideoNotify;
unsigned long XvRTVideoNotifyList;
unsigned long XvRTPortNotify;



/* EXTERNAL */

extern XID clientErrorValue;

static void WriteSwappedVideoNotifyEvent(xvEvent *, xvEvent *);
static void WriteSwappedPortNotifyEvent(xvEvent *, xvEvent *);
static Bool CreateResourceTypes(void);

static Bool XvCloseScreen(int, ScreenPtr);
static Bool XvDestroyPixmap(PixmapPtr);
static Bool XvDestroyWindow(WindowPtr);
static void XvResetProc(ExtensionEntry*);
static int XvdiDestroyGrab(pointer, XID);
static int XvdiDestroyEncoding(pointer, XID);
static int XvdiDestroyVideoNotify(pointer, XID);
static int XvdiDestroyPortNotify(pointer, XID);
static int XvdiDestroyVideoNotifyList(pointer, XID);
static int XvdiDestroyPort(pointer, XID);
static int XvdiSendVideoNotify(XvPortPtr, DrawablePtr, int);




/*
** XvExtensionInit
**
**
*/

void 
XvExtensionInit()
{
  ExtensionEntry *extEntry;

  /* LOOK TO SEE IF ANY SCREENS WERE INITIALIZED; IF NOT THEN
     INIT GLOBAL VARIABLES SO THE EXTENSION CAN FUNCTION */
  if (XvScreenGeneration != serverGeneration)
    {
      if (!CreateResourceTypes())
	{
	  ErrorF("XvExtensionInit: Unable to allocate resource types\n");
	  return;
	}
      XvScreenIndex = AllocateScreenPrivateIndex ();
      if (XvScreenIndex < 0)
	{
	  ErrorF("XvExtensionInit: Unable to allocate screen private index\n");
	  return;
	}
#ifdef PANORAMIX
        XineramaRegisterConnectionBlockCallback(XineramifyXv);
#endif
      XvScreenGeneration = serverGeneration;
    }

  if (XvExtensionGeneration != serverGeneration)
    {
      XvExtensionGeneration = serverGeneration;

      extEntry = AddExtension(XvName, XvNumEvents, XvNumErrors, 
			      ProcXvDispatch, SProcXvDispatch,
			      XvResetProc, StandardMinorOpcode);
      if (!extEntry) 
	{
	  FatalError("XvExtensionInit: AddExtensions failed\n");
	}

      XvReqCode = extEntry->base;
      XvEventBase = extEntry->eventBase;
      XvErrorBase = extEntry->errorBase;

      EventSwapVector[XvEventBase+XvVideoNotify] = 
	(EventSwapPtr)WriteSwappedVideoNotifyEvent;
      EventSwapVector[XvEventBase+XvPortNotify] = 
	(EventSwapPtr)WriteSwappedPortNotifyEvent;

      (void)MakeAtom(XvName, strlen(XvName), xTrue);

    }
}

static Bool
CreateResourceTypes()

{
  
  if (XvResourceGeneration == serverGeneration) return TRUE;

  XvResourceGeneration = serverGeneration;

  if (!(XvRTPort = CreateNewResourceType(XvdiDestroyPort)))
    {
      ErrorF("CreateResourceTypes: failed to allocate port resource.\n");
      return FALSE;
    }
  
  if (!(XvRTGrab = CreateNewResourceType(XvdiDestroyGrab)))
    {
      ErrorF("CreateResourceTypes: failed to allocate grab resource.\n");
      return FALSE;
    }
  
  if (!(XvRTEncoding = CreateNewResourceType(XvdiDestroyEncoding)))
    {
      ErrorF("CreateResourceTypes: failed to allocate encoding resource.\n");
      return FALSE;
    }
  
  if (!(XvRTVideoNotify = CreateNewResourceType(XvdiDestroyVideoNotify)))
    {
      ErrorF("CreateResourceTypes: failed to allocate video notify resource.\n");
      return FALSE;
    }
  
  if (!(XvRTVideoNotifyList = CreateNewResourceType(XvdiDestroyVideoNotifyList)))
    {
      ErrorF("CreateResourceTypes: failed to allocate video notify list resource.\n");
      return FALSE;
    }

  if (!(XvRTPortNotify = CreateNewResourceType(XvdiDestroyPortNotify)))
    {
      ErrorF("CreateResourceTypes: failed to allocate port notify resource.\n");
      return FALSE;
    }

  return TRUE;

}

int
XvScreenInit(ScreenPtr pScreen)
{
  XvScreenPtr pxvs;

  if (XvScreenGeneration != serverGeneration)
    {
      if (!CreateResourceTypes())
	{
	  ErrorF("XvScreenInit: Unable to allocate resource types\n");
	  return BadAlloc;
	}
      XvScreenIndex = AllocateScreenPrivateIndex ();
      if (XvScreenIndex < 0)
	{
	  ErrorF("XvScreenInit: Unable to allocate screen private index\n");
	  return BadAlloc;
	}
#ifdef PANORAMIX
        XineramaRegisterConnectionBlockCallback(XineramifyXv);
#endif
      XvScreenGeneration = serverGeneration; 
    }

  if (pScreen->devPrivates[XvScreenIndex].ptr)
    {
      ErrorF("XvScreenInit: screen devPrivates ptr non-NULL before init\n");
    }

  /* ALLOCATE SCREEN PRIVATE RECORD */
  
  pxvs = (XvScreenPtr) xalloc (sizeof (XvScreenRec));
  if (!pxvs)
    {
      ErrorF("XvScreenInit: Unable to allocate screen private structure\n");
      return BadAlloc;
    }

  pScreen->devPrivates[XvScreenIndex].ptr = (pointer)pxvs;

  
  pxvs->DestroyPixmap = pScreen->DestroyPixmap;
  pxvs->DestroyWindow = pScreen->DestroyWindow;
  pxvs->CloseScreen = pScreen->CloseScreen;
  
  pScreen->DestroyPixmap = XvDestroyPixmap;
  pScreen->DestroyWindow = XvDestroyWindow;
  pScreen->CloseScreen = XvCloseScreen;

  return Success;
}

static Bool
XvCloseScreen(
  int ii,
  ScreenPtr pScreen
){

  XvScreenPtr pxvs;

  pxvs = (XvScreenPtr) pScreen->devPrivates[XvScreenIndex].ptr;

  pScreen->DestroyPixmap = pxvs->DestroyPixmap;
  pScreen->DestroyWindow = pxvs->DestroyWindow;
  pScreen->CloseScreen = pxvs->CloseScreen;

  (* pxvs->ddCloseScreen)(ii, pScreen); 

  xfree(pxvs);

  pScreen->devPrivates[XvScreenIndex].ptr = (pointer)NULL;

  return (*pScreen->CloseScreen)(ii, pScreen);

}

static void
XvResetProc(ExtensionEntry* extEntry)
{
}

int
XvGetScreenIndex()
{
  return XvScreenIndex;
}

unsigned long
XvGetRTPort()
{
  return XvRTPort;
}

static Bool
XvDestroyPixmap(PixmapPtr pPix)
{
  Bool status;
  ScreenPtr pScreen;
  XvScreenPtr pxvs;
  XvAdaptorPtr pa;
  int na;
  XvPortPtr pp;
  int np;

  pScreen = pPix->drawable.pScreen;

  SCREEN_PROLOGUE(pScreen, DestroyPixmap);

  pxvs = (XvScreenPtr)pScreen->devPrivates[XvScreenIndex].ptr;

  /* CHECK TO SEE IF THIS PORT IS IN USE */

  pa = pxvs->pAdaptors;
  na = pxvs->nAdaptors;
  while (na--)
    {
      np = pa->nPorts;
      pp = pa->pPorts;

      while (np--)
	{
	  if (pp->pDraw == (DrawablePtr)pPix)
	    {
	      XvdiSendVideoNotify(pp, pp->pDraw, XvPreempted);

	      (void)(* pp->pAdaptor->ddStopVideo)((ClientPtr)NULL, pp, 
						  pp->pDraw);

	      pp->pDraw = (DrawablePtr)NULL;
	      pp->client = (ClientPtr)NULL;
	      pp->time = currentTime;
	    }
	  pp++;
	}
      pa++;
    }
  
  status = (* pScreen->DestroyPixmap)(pPix);

  SCREEN_EPILOGUE(pScreen, DestroyPixmap, XvDestroyPixmap);

  return status;

}

static Bool
XvDestroyWindow(WindowPtr pWin)
{
  Bool status;
  ScreenPtr pScreen;
  XvScreenPtr pxvs;
  XvAdaptorPtr pa;
  int na;
  XvPortPtr pp;
  int np;

  pScreen = pWin->drawable.pScreen;

  SCREEN_PROLOGUE(pScreen, DestroyWindow);

  pxvs = (XvScreenPtr)pScreen->devPrivates[XvScreenIndex].ptr;

  /* CHECK TO SEE IF THIS PORT IS IN USE */

  pa = pxvs->pAdaptors;
  na = pxvs->nAdaptors;
  while (na--)
    {
      np = pa->nPorts;
      pp = pa->pPorts;

      while (np--)
	{
	  if (pp->pDraw == (DrawablePtr)pWin)
	    {
	      XvdiSendVideoNotify(pp, pp->pDraw, XvPreempted);

	      (void)(* pp->pAdaptor->ddStopVideo)((ClientPtr)NULL, pp, 
						  pp->pDraw);

	      pp->pDraw = (DrawablePtr)NULL;
	      pp->client = (ClientPtr)NULL;
	      pp->time = currentTime;
	    }
	  pp++;
	}
      pa++;
    }

  
  status = (* pScreen->DestroyWindow)(pWin);

  SCREEN_EPILOGUE(pScreen, DestroyWindow, XvDestroyWindow);

  return status;

}

/* The XvdiVideoStopped procedure is a hook for the device dependent layer.
   It provides a way for the dd layer to inform the di layer that video has
   stopped in a port for reasons that the di layer had no control over; note
   that it doesn't call back into the dd layer */

int
XvdiVideoStopped(XvPortPtr pPort, int reason)
{
  
  /* IF PORT ISN'T ACTIVE THEN WE'RE DONE */

  if (!pPort->pDraw) return Success;

  XvdiSendVideoNotify(pPort, pPort->pDraw, reason);

  pPort->pDraw = (DrawablePtr)NULL;
  pPort->client = (ClientPtr)NULL;
  pPort->time = currentTime;

  return Success;

}

static int 
XvdiDestroyPort(pointer pPort, XID id)
{
  return (* ((XvPortPtr)pPort)->pAdaptor->ddFreePort)(pPort);
}

static int
XvdiDestroyGrab(pointer pGrab, XID id)
{
  ((XvGrabPtr)pGrab)->client = (ClientPtr)NULL;
  return Success;
}

static int
XvdiDestroyVideoNotify(pointer pn, XID id)
{
  /* JUST CLEAR OUT THE client POINTER FIELD */

  ((XvVideoNotifyPtr)pn)->client = (ClientPtr)NULL;
  return Success;
}

static int
XvdiDestroyPortNotify(pointer pn, XID id)
{
  /* JUST CLEAR OUT THE client POINTER FIELD */

  ((XvPortNotifyPtr)pn)->client = (ClientPtr)NULL;
  return Success;
}

static int
XvdiDestroyVideoNotifyList(pointer pn, XID id)
{
  XvVideoNotifyPtr npn,cpn;

  /* ACTUALLY DESTROY THE NOTITY LIST */

  cpn = (XvVideoNotifyPtr)pn;

  while (cpn)
    {
      npn = cpn->next;
      if (cpn->client) FreeResource(cpn->id, XvRTVideoNotify);
      xfree(cpn);
      cpn = npn;
    }
  return Success;
}

static int
XvdiDestroyEncoding(pointer value, XID id)
{
  return Success;
}

static int
XvdiSendVideoNotify(pPort, pDraw, reason)

XvPortPtr pPort;
DrawablePtr pDraw;
int reason;

{
  xvEvent event;
  XvVideoNotifyPtr pn;

  pn = (XvVideoNotifyPtr)LookupIDByType(pDraw->id, XvRTVideoNotifyList);

  while (pn) 
    {
      if (pn->client)
	{
	  event.u.u.type = XvEventBase + XvVideoNotify;
	  event.u.u.sequenceNumber = pn->client->sequence;
	  event.u.videoNotify.time = currentTime.milliseconds;
	  event.u.videoNotify.drawable = pDraw->id;
	  event.u.videoNotify.port = pPort->id;
	  event.u.videoNotify.reason = reason;
	  (void) TryClientEvents(pn->client, (xEventPtr)&event, 1, NoEventMask,
				 NoEventMask, NullGrab);
	}
      pn = pn->next;
    }

  return Success;

}


int
XvdiSendPortNotify(
  XvPortPtr pPort,
  Atom attribute,
  INT32 value
){
  xvEvent event;
  XvPortNotifyPtr pn;

  pn = pPort->pNotify;

  while (pn) 
    {
      if (pn->client)
	{
	  event.u.u.type = XvEventBase + XvPortNotify;
	  event.u.u.sequenceNumber = pn->client->sequence;
	  event.u.portNotify.time = currentTime.milliseconds;
	  event.u.portNotify.port = pPort->id;
	  event.u.portNotify.attribute = attribute;
	  event.u.portNotify.value = value;
	  (void) TryClientEvents(pn->client, (xEventPtr)&event, 1, NoEventMask,
				 NoEventMask, NullGrab);
	}
      pn = pn->next;
    }

  return Success;

}


#define CHECK_SIZE(dw, dh, sw, sh) {                                  \
  if(!dw || !dh || !sw || !sh)  return Success;                       \
  /* The region code will break these if they are too large */        \
  if((dw > 32767) || (dh > 32767) || (sw > 32767) || (sh > 32767))    \
        return BadValue;                                              \
}


int
XvdiPutVideo(   
   ClientPtr client,
   DrawablePtr pDraw,
   XvPortPtr pPort,
   GCPtr pGC,
   INT16 vid_x, INT16 vid_y, 
   CARD16 vid_w, CARD16 vid_h, 
   INT16 drw_x, INT16 drw_y,
   CARD16 drw_w, CARD16 drw_h
){
  DrawablePtr pOldDraw;

  CHECK_SIZE(drw_w, drw_h, vid_w, vid_h);

  /* UPDATE TIME VARIABLES FOR USE IN EVENTS */

  UpdateCurrentTime();

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if (pPort->grab.client && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  /* CHECK TO SEE IF PORT IS IN USE; IF SO THEN WE MUST DELIVER INTERRUPTED
     EVENTS TO ANY CLIENTS WHO WANT THEM */

  pOldDraw = pPort->pDraw;
  if ((pOldDraw) && (pOldDraw != pDraw))
    {
      XvdiSendVideoNotify(pPort, pPort->pDraw, XvPreempted);
    }

  (void) (* pPort->pAdaptor->ddPutVideo)(client, pDraw, pPort, pGC,
					   vid_x, vid_y, vid_w, vid_h, 
					   drw_x, drw_y, drw_w, drw_h);

  if ((pPort->pDraw) && (pOldDraw != pDraw))
    {
      pPort->client = client;
      XvdiSendVideoNotify(pPort, pPort->pDraw, XvStarted);
    }

  pPort->time = currentTime;

  return (Success);

}

int
XvdiPutStill(   
   ClientPtr client,
   DrawablePtr pDraw,
   XvPortPtr pPort,
   GCPtr pGC,
   INT16 vid_x, INT16 vid_y, 
   CARD16 vid_w, CARD16 vid_h, 
   INT16 drw_x, INT16 drw_y,
   CARD16 drw_w, CARD16 drw_h
){
  int status;

  CHECK_SIZE(drw_w, drw_h, vid_w, vid_h);

  /* UPDATE TIME VARIABLES FOR USE IN EVENTS */

  UpdateCurrentTime();

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if (pPort->grab.client && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  pPort->time = currentTime;

  status = (* pPort->pAdaptor->ddPutStill)(client, pDraw, pPort, pGC, 
					   vid_x, vid_y, vid_w, vid_h, 
					   drw_x, drw_y, drw_w, drw_h);

  return status;

}

int
XvdiPutImage(   
   ClientPtr client, 
   DrawablePtr pDraw, 
   XvPortPtr pPort, 
   GCPtr pGC,
   INT16 src_x, INT16 src_y, 
   CARD16 src_w, CARD16 src_h, 
   INT16 drw_x, INT16 drw_y,
   CARD16 drw_w, CARD16 drw_h,
   XvImagePtr image,
   unsigned char* data,
   Bool sync,
   CARD16 width, CARD16 height
){
  CHECK_SIZE(drw_w, drw_h, src_w, src_h);

  /* UPDATE TIME VARIABLES FOR USE IN EVENTS */

  UpdateCurrentTime();

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if (pPort->grab.client && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  pPort->time = currentTime;

  return (* pPort->pAdaptor->ddPutImage)(client, pDraw, pPort, pGC, 
					   src_x, src_y, src_w, src_h, 
					   drw_x, drw_y, drw_w, drw_h,
					   image, data, sync, width, height);
}


int
XvdiGetVideo(
   ClientPtr client,
   DrawablePtr pDraw,
   XvPortPtr pPort,
   GCPtr pGC,
   INT16 vid_x, INT16 vid_y, 
   CARD16 vid_w, CARD16 vid_h, 
   INT16 drw_x, INT16 drw_y,
   CARD16 drw_w, CARD16 drw_h
){
  DrawablePtr pOldDraw;

  CHECK_SIZE(drw_w, drw_h, vid_w, vid_h);

  /* UPDATE TIME VARIABLES FOR USE IN EVENTS */

  UpdateCurrentTime();

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if (pPort->grab.client && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  /* CHECK TO SEE IF PORT IS IN USE; IF SO THEN WE MUST DELIVER INTERRUPTED
     EVENTS TO ANY CLIENTS WHO WANT THEM */

  pOldDraw = pPort->pDraw;
  if ((pOldDraw) && (pOldDraw != pDraw))
    {
      XvdiSendVideoNotify(pPort, pPort->pDraw, XvPreempted);
    }

  (void) (* pPort->pAdaptor->ddGetVideo)(client, pDraw, pPort, pGC,
					   vid_x, vid_y, vid_w, vid_h, 
					   drw_x, drw_y, drw_w, drw_h);

  if ((pPort->pDraw) && (pOldDraw != pDraw))
    {
      pPort->client = client;
      XvdiSendVideoNotify(pPort, pPort->pDraw, XvStarted);
    }

  pPort->time = currentTime;

  return (Success);

}

int
XvdiGetStill(
   ClientPtr client,
   DrawablePtr pDraw,
   XvPortPtr pPort,
   GCPtr pGC,
   INT16 vid_x, INT16 vid_y, 
   CARD16 vid_w, CARD16 vid_h, 
   INT16 drw_x, INT16 drw_y,
   CARD16 drw_w, CARD16 drw_h
){
  int status;

  CHECK_SIZE(drw_w, drw_h, vid_w, vid_h);

  /* UPDATE TIME VARIABLES FOR USE IN EVENTS */

  UpdateCurrentTime();

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if (pPort->grab.client && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  status = (* pPort->pAdaptor->ddGetStill)(client, pDraw, pPort, pGC, 
					   vid_x, vid_y, vid_w, vid_h, 
					   drw_x, drw_y, drw_w, drw_h);

  pPort->time = currentTime;

  return status;

}

int
XvdiGrabPort(
   ClientPtr client,
   XvPortPtr pPort,
   Time ctime,
   int *p_result
){
  unsigned long id;
  TimeStamp time;

  UpdateCurrentTime();
  time = ClientTimeToServerTime(ctime);

  if (pPort->grab.client && (client != pPort->grab.client))
    {
      *p_result = XvAlreadyGrabbed;
      return Success;
    }

  if ((CompareTimeStamps(time, currentTime) == LATER) ||
      (CompareTimeStamps(time, pPort->time) == EARLIER))
    {
      *p_result = XvInvalidTime;
      return Success;
    }

  if (client == pPort->grab.client)
    {
      *p_result = Success;
      return Success;
    }

  id = FakeClientID(client->index);

  if (!AddResource(id, XvRTGrab, &pPort->grab))
    {
      return BadAlloc;
    }

  /* IF THERE IS ACTIVE VIDEO THEN STOP IT */

  if ((pPort->pDraw) && (client != pPort->client))
    {
      XVCALL(diStopVideo)((ClientPtr)NULL, pPort, pPort->pDraw);
    }

  pPort->grab.client = client;
  pPort->grab.id = id;

  pPort->time = currentTime;

  *p_result = Success;

  return Success;

}

int
XvdiUngrabPort(
  ClientPtr client,
  XvPortPtr pPort,
  Time ctime
){
  TimeStamp time;

  UpdateCurrentTime();
  time = ClientTimeToServerTime(ctime);

  if ((!pPort->grab.client) || (client != pPort->grab.client))
    {
      return Success;
    }

  if ((CompareTimeStamps(time, currentTime) == LATER) ||
      (CompareTimeStamps(time, pPort->time) == EARLIER))
    {
      return Success;
    }

  /* FREE THE GRAB RESOURCE; AND SET THE GRAB CLIENT TO NULL */

  FreeResource(pPort->grab.id, XvRTGrab);
  pPort->grab.client = (ClientPtr)NULL;

  pPort->time = currentTime;

  return Success;

}


int
XvdiSelectVideoNotify(
  ClientPtr client,
  DrawablePtr pDraw,
  BOOL onoff
){
  XvVideoNotifyPtr pn,tpn,fpn;

  /* FIND VideoNotify LIST */

  pn = (XvVideoNotifyPtr)LookupIDByType(pDraw->id, XvRTVideoNotifyList);

  /* IF ONE DONES'T EXIST AND NO MASK, THEN JUST RETURN */

  if (!onoff && !pn) return Success;

  /* IF ONE DOESN'T EXIST CREATE IT AND ADD A RESOURCE SO THAT THE LIST
     WILL BE DELETED WHEN THE DRAWABLE IS DESTROYED */

  if (!pn) 
    {
      if (!(tpn = (XvVideoNotifyPtr)xalloc(sizeof(XvVideoNotifyRec))))
	return BadAlloc;
      tpn->next = (XvVideoNotifyPtr)NULL;
      if (!AddResource(pDraw->id, XvRTVideoNotifyList, tpn))
	{
	  xfree(tpn);
	  return BadAlloc;
	}
    }
  else
    {
      /* LOOK TO SEE IF ENTRY ALREADY EXISTS */

      fpn = (XvVideoNotifyPtr)NULL;
      tpn = pn;
      while (tpn)
	{
	  if (tpn->client == client) 
	    {
	      if (!onoff) tpn->client = (ClientPtr)NULL;
	      return Success;
	    }
	  if (!tpn->client) fpn = tpn; /* TAKE NOTE OF FREE ENTRY */
	  tpn = tpn->next;
	}

      /* IF TUNNING OFF, THEN JUST RETURN */

      if (!onoff) return Success;

      /* IF ONE ISN'T FOUND THEN ALLOCATE ONE AND LINK IT INTO THE LIST */

      if (fpn)
	{
	  tpn = fpn;
	}
      else
	{
	  if (!(tpn = (XvVideoNotifyPtr)xalloc(sizeof(XvVideoNotifyRec))))
	    return BadAlloc;
	  tpn->next = pn->next;
	  pn->next = tpn;
	}
    }

  /* INIT CLIENT PTR IN CASE WE CAN'T ADD RESOURCE */
  /* ADD RESOURCE SO THAT IF CLIENT EXITS THE CLIENT PTR WILL BE CLEARED */

  tpn->client = (ClientPtr)NULL;
  tpn->id = FakeClientID(client->index);
  AddResource(tpn->id, XvRTVideoNotify, tpn);

  tpn->client = client;
  return Success;

}

int
XvdiSelectPortNotify(
   ClientPtr client,
   XvPortPtr pPort,
   BOOL onoff
){
  XvPortNotifyPtr pn,tpn;

  /* SEE IF CLIENT IS ALREADY IN LIST */

  tpn = (XvPortNotifyPtr)NULL;
  pn = pPort->pNotify;
  while (pn)
    {
      if (!pn->client) tpn = pn; /* TAKE NOTE OF FREE ENTRY */
      if (pn->client == client) break;
      pn = pn->next;
    }

  /* IS THE CLIENT ALREADY ON THE LIST? */

  if (pn)
    {
      /* REMOVE IT? */

      if (!onoff)
	{
	  pn->client = (ClientPtr)NULL;
	  FreeResource(pn->id, XvRTPortNotify);
	}

      return Success;
    }

  /* DIDN'T FIND IT; SO REUSE LIST ELEMENT IF ONE IS FREE OTHERWISE 
     CREATE A NEW ONE AND ADD IT TO THE BEGINNING OF THE LIST */

  if (!tpn)
    {
      if (!(tpn = (XvPortNotifyPtr)xalloc(sizeof(XvPortNotifyRec))))
	return BadAlloc;
      tpn->next = pPort->pNotify;
      pPort->pNotify = tpn;
    }

  tpn->client = client;
  tpn->id = FakeClientID(client->index);
  AddResource(tpn->id, XvRTPortNotify, tpn);

  return Success;

}

int
XvdiStopVideo(
  ClientPtr client,
  XvPortPtr pPort,
  DrawablePtr pDraw
){
  int status;

  /* IF PORT ISN'T ACTIVE THEN WE'RE DONE */

  if (!pPort->pDraw || (pPort->pDraw != pDraw)) 
    {
      XvdiSendVideoNotify(pPort, pDraw, XvStopped);
      return Success;
    }

  /* CHECK FOR GRAB; IF THIS CLIENT DOESN'T HAVE THE PORT GRABBED THEN
     INFORM CLIENT OF ITS FAILURE */

  if ((client) && (pPort->grab.client) && (pPort->grab.client != client))
    {
      XvdiSendVideoNotify(pPort, pDraw, XvBusy);
      return Success;
    }

  XvdiSendVideoNotify(pPort, pDraw, XvStopped);

  status = (* pPort->pAdaptor->ddStopVideo)(client, pPort, pDraw);

  pPort->pDraw = (DrawablePtr)NULL;
  pPort->client = (ClientPtr)client;
  pPort->time = currentTime;

  return status;

}

int
XvdiPreemptVideo(
  ClientPtr client,
  XvPortPtr pPort,
  DrawablePtr pDraw
){
  int status;

  /* IF PORT ISN'T ACTIVE THEN WE'RE DONE */

  if (!pPort->pDraw || (pPort->pDraw != pDraw)) return Success;

  XvdiSendVideoNotify(pPort, pPort->pDraw, XvPreempted);

  status = (* pPort->pAdaptor->ddStopVideo)(client, pPort, pPort->pDraw);

  pPort->pDraw = (DrawablePtr)NULL;
  pPort->client = (ClientPtr)client;
  pPort->time = currentTime;

  return status;

}

int
XvdiMatchPort(
  XvPortPtr pPort,
  DrawablePtr pDraw
){

  XvAdaptorPtr pa;
  XvFormatPtr pf;
  int nf;

  pa = pPort->pAdaptor;

  if (pa->pScreen != pDraw->pScreen) return BadMatch;

  nf = pa->nFormats;
  pf = pa->pFormats;

  while (nf--)
    {
      if ((pf->depth == pDraw->depth) 
#if 0
         && ((pDraw->type == DRAWABLE_PIXMAP) || 
	   (wVisual(((WindowPtr)pDraw)) == pf->visual))
#endif
	)
	return Success;
      pf++;
    }

  return BadMatch;

}

int
XvdiSetPortAttribute(
  ClientPtr client,
  XvPortPtr pPort,
  Atom attribute,
  INT32 value
){

    XvdiSendPortNotify(pPort, attribute, value);

  return 
    (* pPort->pAdaptor->ddSetPortAttribute)(client, pPort, attribute, value);

}

int
XvdiGetPortAttribute(
  ClientPtr client,
  XvPortPtr pPort,
  Atom attribute,
  INT32 *p_value
){

  return 
    (* pPort->pAdaptor->ddGetPortAttribute)(client, pPort, attribute, p_value);

}

static void
WriteSwappedVideoNotifyEvent(xvEvent *from, xvEvent *to)

{

  to->u.u.type = from->u.u.type;
  to->u.u.detail = from->u.u.detail;
  cpswaps(from->u.videoNotify.sequenceNumber, 
	  to->u.videoNotify.sequenceNumber);
  cpswapl(from->u.videoNotify.time, to->u.videoNotify.time);
  cpswapl(from->u.videoNotify.drawable, to->u.videoNotify.drawable);
  cpswapl(from->u.videoNotify.port, to->u.videoNotify.port);

}

static void
WriteSwappedPortNotifyEvent(xvEvent *from, xvEvent *to)

{

  to->u.u.type = from->u.u.type;
  to->u.u.detail = from->u.u.detail;
  cpswaps(from->u.portNotify.sequenceNumber, to->u.portNotify.sequenceNumber);
  cpswapl(from->u.portNotify.time, to->u.portNotify.time);
  cpswapl(from->u.portNotify.port, to->u.portNotify.port);
  cpswapl(from->u.portNotify.value, to->u.portNotify.value);

}