XpNotifyPdm.c 23.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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
/* $Xorg: XpNotifyPdm.c,v 1.4 2000/08/17 19:46:07 cpqbld Exp $ */
/******************************************************************************
 ******************************************************************************
 **
 ** Description:  XpNotifyPdm is used to engage a Page Dialog Manager
 **               (PDM).  Though the PDM is not a formal part of the
 **               Print Extension, the concept and likely usage of
 **               PDM's is strong enough that this routine is being
 **               provided as a sample standard mechanism for engaging
 **               PDM's from the Print Extension.
 **
 ** (c) Copyright 1996 Hewlett-Packard Company
 ** (c) Copyright 1996 International Business Machines Corp.
 ** (c) Copyright 1996 Sun Microsystems, Inc.
 ** (c) Copyright 1996 Novell, Inc.
 ** (c) Copyright 1996 Digital Equipment Corp.
 ** (c) Copyright 1996 Fujitsu Limited
 ** (c) Copyright 1996 Hitachi, Ltd.
 ** 
 ** 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
 ** COPYRIGHT HOLDERS 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 names of the copyright holders shall
 ** not be used in advertising or otherwise to promote the sale, use or other
 ** dealings in this Software without prior written authorization from said
 ** copyright holders.
 **
 ******************************************************************************
 *****************************************************************************/
/* $XFree86: xc/lib/Xp/XpNotifyPdm.c,v 1.7tsi Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/extensions/Print.h>
#include <X11/Xlibint.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xauth.h>
#include <stdlib.h>
#include <X11/Xos.h>

/*
 * Alternate selection environment variables.
 */
#define ALT_PDM_SELECTION_ENV_VAR  "XPDMSELECTION"
#define ALT_PMD_DISPLAY_ENV_VAR    "XPDMDISPLAY"

/*
 * X-Authority file for authorization tickets.
 */
#define PDM_XAUTHORITY_ENV_VAR     "XPDMXAUTHORITY"

/*
 * str_dup using Xmalloc
 */
char *_xpstrdup(char * str)
{
    int len;
    char *newstr;

    if (!str)
	return( (char *) NULL );

    len = strlen(str) + 1;
    newstr = (char *) Xmalloc( len );
    memcpy( newstr, str, len );
    return( newstr );
}

/******************************************************************************
 *
 * _XpGetSelectionServer
 *
 * Routine that considers print_display, video_display, and alt
 * environment variables, and figures out the selection_display
 * and selection_atom.
 *
 * selection_display can be one of print_display, video_display,
 * or a brand new display connection that the caller will later
 * have to close.
 */
static Display *
_XpGetSelectionServer (
    Display         *print_display,
    Display         *video_display,
    Atom            *sel_atom			/* return value */
)
{
    char          *tstr1, *tstr2, *tstr3, *tstrptr;
    char          *sel_displaystr;
    Display       *sel_display;
    char          *selectionstr;


    /*
     * Possibly tickle a selection on an alternate display.
     */
    if ( (sel_displaystr = getenv( ALT_PMD_DISPLAY_ENV_VAR )) != NULL ) {
	if ( !strcmp(sel_displaystr, "print") ) {
	    /*
	     * Explicitly use the Print display.
	     */
	    sel_display = print_display;
	}
	else if ( !strcmp(sel_displaystr, "video") ) {
	    /*
	     * Explicitly use the Video display.
	     */
	    sel_display = video_display;
	}
	else {
	    /*
	     * Do more work to determine the selection server connection.
	     * The following is not clever enough to handle host aliases.
	     */
	    tstr1 = _xpstrdup( sel_displaystr );
	    tstr2 = _xpstrdup( XDisplayString(print_display) );
	    tstr3 = _xpstrdup( XDisplayString(video_display) );

	    /*
	     * remove ".scr" portion from "host:disp.scr" strings.
	     * Use strrchr to find : separating host from display
	     * to allow IPv6 numeric addresses with embedded colons.
	     */
	    if (( tstrptr = strrchr( tstr1, ':' ) ))
		if (( tstrptr = strchr( tstrptr, '.' ) ))
		    *tstrptr = '\0';

	    if (( tstrptr = strrchr( tstr2, ':' ) ))
		if (( tstrptr = strchr( tstrptr, '.' ) ))
		    *tstrptr = '\0';

	    if (( tstrptr = strrchr( tstr3, ':' ) ))
		if (( tstrptr = strchr( tstrptr, '.' ) ))
		    *tstrptr = '\0';

	    if ( !strcmp( tstr1, tstr2 ) ) {
		/*
		 * Explicitly use the Print display.
		 */
		sel_display = print_display;
	    }
	    else if ( !strcmp( tstr1, tstr3 ) ) {
		/*
		 * Explicitly use the Print display.
		 */
		sel_display = video_display;
	    }
	    else {
		/*
		 * Create a new display connection to a server
		 * never previously contacted. The caller will
		 * have to realize this is a new display handle
		 * (ie, its not equal to print_display or
		 * video_display) when done.
		 */
		sel_display = XOpenDisplay(sel_displaystr);
	    }

	    XFree( tstr3 );
	    XFree( tstr2 );
	    XFree( tstr1 );
	}
    }
    else {
	/*
	 * If no alt selection server, use the print server.
	 */
	sel_display = print_display;
    }

    if (sel_display) {
	/*
	 * Tickle (possibly alternate) PDM_MANAGER selection
	 */
	if ( (selectionstr = getenv( ALT_PDM_SELECTION_ENV_VAR )) == NULL )
	    selectionstr = "PDM_MANAGER";

	    *sel_atom = XInternAtom( sel_display, selectionstr, False );
    }

    return( sel_display );
}



/******************************************************************************
 *
 * XpGetPdmStartParams
 */
Status
XpGetPdmStartParams (
    Display         *print_display,
    Window          print_window,
    XPContext       print_context,
    Display         *video_display,
    Window          video_window,
    Display         **selection_display,	/* return value */
    Atom            *selection,			/* return value */
    Atom            *type,			/* return value */
    int             *format,			/* return value */
    unsigned char   **data,			/* return value */
    int             *nelements			/* return value */
)
{
    XTextProperty text_prop;
    int           status;
    char          str1[128], str2[128], str3[128];
    char          *list[6];


    *selection_display = _XpGetSelectionServer( print_display, video_display,
						selection );

    if (*selection_display == NULL) {
	/*
	 * Error - cannot determine or establish a selection_display.
	 */
	return( (Status) NULL );
    }

    /*
     * Create a property that can be included in the PDM_MANAGER
     * selection to communicate information.
     *
     *    video_display    host:display[.anyScreen]
     *    video_window
     *    print_display    host:display[.anyScreen]
     *    print_window
     *    print_context    use to derive host:display.properScreen and other
     *    locale_hint
     */
    list[0] = XDisplayString( video_display );
    sprintf( str1, "0x%lx", (long)video_window );
    list[1] = str1;

    list[2] = XDisplayString( print_display );
    sprintf( str2, "0x%lx", (long)print_window );
    list[3] = str2;

    sprintf( str3, "0x%lx", (long)print_context );
    list[4] = str3;

    list[5] = XpGetLocaleNetString();

    status = XSupportsLocale();
    if ( ! status ) {
	/*
	 * Error.
	 */
	if ( (*selection_display != print_display) &&
	     (*selection_display != video_display)    ) {
	    XCloseDisplay( *selection_display );
	    *selection_display = (Display *) NULL;
	}
	return( (Status) NULL );
    }

    status = XmbTextListToTextProperty( *selection_display, list, 6,
					XStdICCTextStyle, &text_prop );

    if ( status < 0 ) {
	/*
	 * Error.
	 */
	if ( (*selection_display != print_display) &&
	     (*selection_display != video_display)    ) {
	    XCloseDisplay( *selection_display );
	    *selection_display = (Display *) NULL;
	}
	return( (Status) NULL );
    }

    *type              = text_prop.encoding;
    *format            = text_prop.format;
    *data              = text_prop.value;
    *nelements         = text_prop.nitems;

    XFree(list[5]);

    return( (Status) 1 );
}

/******************************************************************************
 *
 * XpSendOneTicket
 *
 * The ticket transfer protocol is as follows:
 *    unsigned short ticket_state;
 *       - 0 means NULL ticket, terminating ticket
 *       - 1 means non-NULL ticket, terminating ticket
 *       - 2 means non-NULL ticket, and more tickets to send
 *    unsigned short address_length;
 *    unsigned short number_length;
 *    unsigned short name_length;
 *    unsigned short data_length;
 *    unsigned short family;
 *
 *    char *address;
 *    char *number;
 *    char *name;
 *    char *data;
 *
 */

Status XpSendOneTicket(
    Display *display,
    Window  window,
    Xauth   *ticket,
    Bool    more )
{
    XClientMessageEvent ev;
    int                 bigstrlen, left, gofor;
    char                *bigstr, *tptr;
    Status              status;

    /*
     * Fixed portion of event.
     */
    ev.type = ClientMessage;
    ev.display = display;
    ev.window  = window;
    ev.message_type = XInternAtom( display, "PDM_MAIL", False );

    /*
     * Special build the first ClientMessage to carry the
     * ticket transfer header.
     */
    ev.format = 16;

    if (!ticket) {
	ev.data.s[0] = 0;
    }
    else {
	if (more)
	    ev.data.s[0] = 2;
	else
	    ev.data.s[0] = 1;
	ev.data.s[1] = (short) ticket->address_length;
	ev.data.s[2] = (short) ticket->number_length;
	ev.data.s[3] = (short) ticket->name_length;
	ev.data.s[4] = (short) ticket->data_length;
	ev.data.s[5] = (short) ticket->family;
    }

    status = XSendEvent( display, window, False, 0L, (XEvent *) &ev );
    if (!status)
	return( (Status) 0 );

    if (!ticket)
	return( (Status) 1 );;

    /*
     * Break down the remaining ticket data and build the
     * second thru N ClientMessages.
     */
    ev.format = 8;
    bigstrlen = (int) ticket->address_length +
		(int) ticket->number_length +
		(int) ticket->name_length +
		(int) ticket->data_length;

    bigstr = Xmalloc( bigstrlen );

    tptr = bigstr;
    memcpy( tptr, ticket->address, ticket->address_length );
    tptr += ticket->address_length;
    memcpy( tptr, ticket->number, ticket->number_length );
    tptr += ticket->number_length;
    memcpy( tptr, ticket->name, ticket->name_length );
    tptr += ticket->name_length;
    memcpy( tptr, ticket->data, ticket->data_length );

    left = bigstrlen;
    tptr = bigstr;

    while ( left ) {
	if (left > 20)
	    gofor = 20;
	else
	    gofor = left;

	memcpy( ev.data.b, tptr, gofor );

	tptr += gofor;
	left -= gofor;

	status = XSendEvent( display, window, False, 0L, (XEvent *) &ev );
	if (!status) {
	    Xfree( bigstr );
	    return( (Status) 0 );
	}
    }

    Xfree( bigstr );

    return( (Status) 1 );
}

Status XpSendAuth( Display *display, Window window )
{

    FILE    *auth_file;
    char    *auth_name;
    Xauth   *entry;

    if ( !(auth_name = getenv( PDM_XAUTHORITY_ENV_VAR )) ) {
        return( (Status) 0 );
	/* auth_name = XauFileName (); */
    }

    if (access (auth_name, R_OK) != 0)          /* checks REAL id */
        return( (Status) 0 );

    auth_file = fopen (auth_name, "r");
    if (!auth_file)
        return( (Status) 0 );

    for (;;) {
        entry = XauReadAuth (auth_file);
        if (!entry)
            break;

	/*
	 * NOTE: in and around this area, an optimization can
	 * be made.  Rather than sending all the tickets in
	 * .Xauthority, just pull out the *one* that is needed
	 * by the PDM.
	 */
	XpSendOneTicket( display, window, entry, True );

	XauDisposeAuth (entry);
    }

    XpSendOneTicket( display, window, (Xauth *) NULL, False );

    (void) fclose (auth_file);

    return( (Status) 1 );
}

/******************************************************************************
 *
 *
 */
Status
XpGetAuthParams (
    Display         *print_display,
    Display         *video_display,
    Display         **sel_display,		/* return value */
    Atom            *sel_atom,			/* return value */
    Atom            *sel_target_atom		/* return value */
)
{
    *sel_display = _XpGetSelectionServer( print_display, video_display,
					  sel_atom );

    if (*sel_display == NULL) {
	/*
	 * Error - cannot determine or establish a selection_display.
	 */
	return (Status)0;
    }

    /*
     * Create property and transfer data to.
     */
    *sel_target_atom = XInternAtom( *sel_display, "PDM_MBOX", False );
    return (Status)1;
}


/******************************************************************************
 ******************************************************************************
 **
 ** UNSUPPORTED ROUTINES used for testing and debugging.
 **
 **/


/******************************************************************************
 *
 * Predicate routines to dig out events from the users event queue.
 */

/*
 * client_data structures to use with XIfEvent()
 */
typedef struct {
    Window	requestor;
    Atom	selection;
    Atom	target;
} Dosnrec;

typedef struct {
    Window	window;
    Atom	atom;
} Dopnrec, Docmrec;


/*
 * Dig out a selection notify from the users event
 * queue.
 */
static Bool digOutSelectionNotify(
    Display *display,
    XEvent *event,
    char *arg
)

{
    Dosnrec *t;

    if (event->type == SelectionNotify) {
	t = (Dosnrec *) arg;

	/*
	 * Selection complete because of good reply.
	 */
	if ( ( t->requestor == event->xselection.requestor ) &&
	     ( t->selection == event->xselection.selection ) &&
	     ( t->target    == event->xselection.target    )    ) {
	    return( True );
	}

	/*
	 * Selection complete because of problem, and X-Server
	 * had to generate the event.
	 */
	if ( ( t->requestor == event->xselection.requestor ) &&
	     ( t->selection == event->xselection.selection ) &&
	     ( None         == event->xselection.property  )    ) {
	    return( True );
	}
    }
    return( False );
}

/*
 * Dig out a property notify event for the XpNotifyPdm
 * selection mechanism from the users event queue.
 */
static Bool digOutPropertyNotify(
    Display *display,
    XEvent *event,
    char *arg
)
{
    Dopnrec *t;

    if (event->type == PropertyNotify) {
	t = (Dopnrec *) arg;
	if ( ( t->window == event->xproperty.window ) &&
	     ( t->atom   == event->xproperty.atom   )    ) {
	    return( True );
	}
    }
    return( False );
}

#ifdef UNUSED
/*
 * Dig out a ClientMessage of type PDM_REPLY.
 */
static Bool digOutClientMessage(
    Display *display,
    XEvent *event,
    char *arg
)
{
    Docmrec *t;

    if (event->type == ClientMessage) {
	t = (Docmrec *) arg;
	if ( ( t->window  == event->xclient.window       ) &&
	     ( t->atom    == event->xclient.message_type )    ) {
	    return( True );
	}
    }
    return( False );
}
#endif


/******************************************************************************
 *
 * XpCookieToPdm
 */
static char *
XpCookieToPdm (
    Display         *print_display,
    Display         *video_display,
    Window          sel_window
)
{
    Display *sel_display;
    Atom    prop_atom, sel_atom, pdm_mbox_atom, ttype;
    Dosnrec dosnrec;
    Dopnrec dopnrec;
    XEvent  tevent;
    int           tformat;
    unsigned long nitems, bytes_after;
    unsigned char *return_data;
    char *sel_str;
    char    cdata[2048];
    Window tmpw;


    if ( !getenv(PDM_XAUTHORITY_ENV_VAR) ) {
	/*
	 * short cut for lack of cookie file.
	 */
        return( (char *) NULL );
    }

    if (! XpGetAuthParams( print_display, video_display,
			     &sel_display, &sel_atom, &pdm_mbox_atom ) ) {
	sprintf(cdata,
		 "XpCookieToPdm: XpGetAuthParams failed");
	return( _xpstrdup( cdata ) );
    }

    prop_atom = XInternAtom( sel_display, "PDM_MBOX_PROP", False );

    XConvertSelection( sel_display, sel_atom, pdm_mbox_atom,
		       prop_atom, sel_window, CurrentTime );

    /*
     * Hang out waiting for a SelectionNotify.  Dig out from
     * event queue when it arrives.
     */
    dosnrec.requestor = sel_window;
    dosnrec.selection = sel_atom;
    dosnrec.target    = pdm_mbox_atom;

    XIfEvent( sel_display, &tevent, digOutSelectionNotify, (char *) &dosnrec );

    /*
     * See if selection was successful.
     */
    if ( tevent.xselection.property == None ) {
	/*
	 * The selection failed.
	 */
	sel_str = XGetAtomName( sel_display, sel_atom );
	sprintf(cdata,
		 "XpCookieToPdm: Unable to make selection on %s", sel_str);
	XFree(sel_str);

	XDeleteProperty( sel_display, sel_window, prop_atom );
	if ((sel_display != print_display) && (sel_display != video_display))
	    XCloseDisplay( sel_display );
	return( _xpstrdup( cdata ) );
    }

    /*
     * Read property content for status.
     */
    XGetWindowProperty( sel_display, sel_window,
			prop_atom, 0, 100000, True, AnyPropertyType,
			&ttype, &tformat, &nitems, &bytes_after,
			&return_data );

    /*
     * So that the client won't see how XpCookieToPdm is implemented,
     * run through the event queue, dig out, and destroy all
     * PropertyNotify events related to this call.
     */
    dopnrec.window = sel_window;
    dopnrec.atom   = prop_atom;
    while ( XCheckIfEvent( sel_display, &tevent, digOutPropertyNotify,
		 (char *) &dopnrec ) );

    if ((sel_display != print_display) && (sel_display != video_display))
	XCloseDisplay( sel_display );

    if ((ttype != XA_WINDOW) && (tformat != 32) && (nitems != 1)) {
	/*
	 * Unable to read SelectionNotify property.
	 */ 
	sprintf(cdata,
	     "XpCookieToPdm: Unable to read SelectionNotify property" );
	return( _xpstrdup( cdata ) );
    }

    tmpw = *((Window *) return_data);
    Xfree( return_data );

    /*
     * Now send cookie information.
     */
    XpSendAuth( sel_display, tmpw );

    return( (char *) NULL );
}


/******************************************************************************
 *
 * XpNotifyPdm
 */
char *
XpNotifyPdm (
    Display         *print_display,
    Window          print_window,
    XPContext       print_context,
    Display         *video_display,
    Window          video_window,
    Bool            auth_flag
)
{
    enum { XA_PDM_CLIENT_PROP, XA_PDM_START, XA_PDM_START_OK,
	   XA_PDM_START_VXAUTH, XA_PDM_START_PXAUTH, XA_PDM_START_ERROR, 
	   NUM_ATOMS };
    static char *atom_names[] = { 
      "PDM_CLIENT_PROP", "PDM_START", "PDM_START_OK",
      "PDM_START_VXAUTH", "PDM_START_PXAUTH", "PDM_START_ERROR" };

    char          cdata[2048];
    char          *tptr;
    Dosnrec       dosnrec;
    Dopnrec       dopnrec;
    XEvent        tevent;

    Display       *sel_display;
    int           sel_screen;
    Atom          sel_atom;
    char          *sel_str;

    Window        sel_window;
    Atom          prop_type;
    int           prop_format;
    unsigned char *prop_data;
    int           prop_nelements;

    Atom          ttype;
    int           tformat;
    unsigned long nitems, bytes_after;
    unsigned char *return_data;

    int           tmpi;
    Atom	  atoms[NUM_ATOMS];



    if ( ! XpGetPdmStartParams( print_display, print_window, print_context,
			 video_display, video_window,
			 &sel_display,
			 &sel_atom,
			 &prop_type,
			 &prop_format,
			 &prop_data,
			 &prop_nelements ) ) {

	sprintf(cdata, "XpNotifyPdm: XpGetPdmStartParams failed" );
	return( _xpstrdup( cdata ) );
    }


    sel_screen = DefaultScreen( sel_display );
    sel_window = XCreateSimpleWindow( sel_display,
					DefaultRootWindow( sel_display ),
					0, 0, 1, 1, 1,
					BlackPixel(sel_display, sel_screen),
					WhitePixel(sel_display, sel_screen) );

    /*
     * Possibly send over authorization cookies first.
     */
    if (auth_flag) {
	tptr = XpCookieToPdm ( print_display, video_display, sel_window );
	if (tptr)
	    return( tptr );
    }

    /*
     * Create property and transfer data to.
     */
    XInternAtoms( sel_display, atom_names, NUM_ATOMS, False, atoms );

    XChangeProperty( sel_display,
		     sel_window, atoms[XA_PDM_CLIENT_PROP],
		     prop_type,
		     prop_format,
		     PropModeReplace,
		     prop_data,
		     prop_nelements );

    XFree( prop_data );

    /*
     * Tickle PDM_MANAGER selection with PDM_START target
     */
    XConvertSelection( sel_display, sel_atom, atoms[XA_PDM_START],
		       atoms[XA_PDM_CLIENT_PROP], sel_window, CurrentTime );

    /*
     * Hang out waiting for a SelectionNotify.  Dig out from
     * event queue when it arrives.
     */
    dosnrec.requestor = sel_window;
    dosnrec.selection = sel_atom;
    dosnrec.target    = atoms[XA_PDM_START];

    XIfEvent( sel_display, &tevent, digOutSelectionNotify, (char *) &dosnrec );

    /*
     * See if selection was successful.
     */
    if ( tevent.xselection.property == None ) {
	/*
	 * The selection failed.
	 */
	sel_str = XGetAtomName( sel_display, sel_atom );
	sprintf(cdata,
		 "XpNotifyPdm: Unable to make selection on %s", sel_str);
	XFree(sel_str);

	XDeleteProperty( sel_display, sel_window, atoms[XA_PDM_CLIENT_PROP] );
	XDestroyWindow( sel_display, sel_window );
	if ((sel_display != print_display) && (sel_display != video_display))
	    XCloseDisplay( sel_display );
	return( _xpstrdup( cdata ) );
    }

    /*
     * Read property content for status.
     */
    XGetWindowProperty( sel_display, sel_window,
			atoms[XA_PDM_CLIENT_PROP], 
			0, 100000, True, AnyPropertyType,
			&ttype, &tformat, &nitems, &bytes_after,
			&return_data );

    /*
     * So that the client won't see how XpNotifyPdm is implemented,
     * run through the event queue, dig out, and destroy all
     * PropertyNotify events related to this call.
     */
    dopnrec.window = sel_window;
    dopnrec.atom   = atoms[XA_PDM_CLIENT_PROP];
    while ( XCheckIfEvent( sel_display, &tevent, digOutPropertyNotify,
		 (char *) &dopnrec ) );

    XDestroyWindow( sel_display, sel_window );
    if ((sel_display != print_display) && (sel_display != video_display))
	XCloseDisplay( sel_display );

    if ((ttype != XA_ATOM) && (tformat != 32) && (nitems != 1)) {
	/*
	 * Unable to read SelectionNotify property.
	 */ 
	sprintf(cdata,
	     "XpNotifyPdm: Unable to read SelectionNotify property" );
	return( _xpstrdup( cdata ) );
    }

    tmpi = *((Atom *) return_data);
    Xfree( return_data );

    if ( tmpi == atoms[XA_PDM_START_OK] ) {
	return( (char *) NULL );
    }
    else if ( tmpi == atoms[XA_PDM_START_VXAUTH] ) {
	sprintf(cdata,
	     "XpNotifyPdm: PDM not authorized to connect to video display." );
	return( _xpstrdup( cdata ) );
    }
    else if ( tmpi == atoms[XA_PDM_START_PXAUTH] ) {
	sprintf(cdata,
	     "XpNotifyPdm: PDM not authorized to connect to print display." );
	return( _xpstrdup( cdata ) );
    }
    else if ( tmpi == atoms[XA_PDM_START_ERROR] ) {
	sprintf(cdata,
	     "XpNotifyPdm: PDM encountered an error. See PDM log file." );
	return( _xpstrdup( cdata ) );
    }
    else {
	sprintf(cdata,
	     "XpNotifyPdm: unknown PDM error." );
	return( _xpstrdup( cdata ) );
    }
}