1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3*4882a593Smuzhiyun *Copyright (C) Colin Harrison 2005-2008
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun *Permission is hereby granted, free of charge, to any person obtaining
6*4882a593Smuzhiyun * a copy of this software and associated documentation files (the
7*4882a593Smuzhiyun *"Software"), to deal in the Software without restriction, including
8*4882a593Smuzhiyun *without limitation the rights to use, copy, modify, merge, publish,
9*4882a593Smuzhiyun *distribute, sublicense, and/or sell copies of the Software, and to
10*4882a593Smuzhiyun *permit persons to whom the Software is furnished to do so, subject to
11*4882a593Smuzhiyun *the following conditions:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun *The above copyright notice and this permission notice shall be
14*4882a593Smuzhiyun *included in all copies or substantial portions of the Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*4882a593Smuzhiyun *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*4882a593Smuzhiyun *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*4882a593Smuzhiyun *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
20*4882a593Smuzhiyun *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21*4882a593Smuzhiyun *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22*4882a593Smuzhiyun *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun *Except as contained in this notice, the name of the copyright holder(s)
25*4882a593Smuzhiyun *and author(s) shall not be used in advertising or otherwise to promote
26*4882a593Smuzhiyun *the sale, use or other dealings in this Software without prior written
27*4882a593Smuzhiyun *authorization from the copyright holder(s) and author(s).
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * Authors: Harold L Hunt II
30*4882a593Smuzhiyun * Colin Harrison
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
34*4882a593Smuzhiyun #include <xwin-config.h>
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "win.h"
38*4882a593Smuzhiyun #include "dixstruct.h"
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * Local function prototypes
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun DISPATCH_PROC(winProcEstablishConnection);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Wrapper for internal EstablishConnection function.
48*4882a593Smuzhiyun * Initializes internal clients that must not be started until
49*4882a593Smuzhiyun * an external client has connected.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun int
winProcEstablishConnection(ClientPtr client)53*4882a593Smuzhiyun winProcEstablishConnection(ClientPtr client)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun int iReturn;
56*4882a593Smuzhiyun static int s_iCallCount = 0;
57*4882a593Smuzhiyun static unsigned long s_ulServerGeneration = 0;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (s_iCallCount == 0)
60*4882a593Smuzhiyun winDebug("winProcEstablishConnection - Hello\n");
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Do nothing if clipboard is not enabled */
63*4882a593Smuzhiyun if (!g_fClipboard) {
64*4882a593Smuzhiyun ErrorF("winProcEstablishConnection - Clipboard is not enabled, "
65*4882a593Smuzhiyun "returning.\n");
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* Unwrap the original function, call it, and return */
68*4882a593Smuzhiyun InitialVector[2] = winProcEstablishConnectionOrig;
69*4882a593Smuzhiyun iReturn = (*winProcEstablishConnectionOrig) (client);
70*4882a593Smuzhiyun winProcEstablishConnectionOrig = NULL;
71*4882a593Smuzhiyun return iReturn;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Watch for server reset */
75*4882a593Smuzhiyun if (s_ulServerGeneration != serverGeneration) {
76*4882a593Smuzhiyun /* Save new generation number */
77*4882a593Smuzhiyun s_ulServerGeneration = serverGeneration;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* Reset call count */
80*4882a593Smuzhiyun s_iCallCount = 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Increment call count */
84*4882a593Smuzhiyun ++s_iCallCount;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * This procedure is only used for initialization.
88*4882a593Smuzhiyun * We can unwrap the original procedure at this point
89*4882a593Smuzhiyun * so that this function is no longer called until the
90*4882a593Smuzhiyun * server resets and the function is wrapped again.
91*4882a593Smuzhiyun */
92*4882a593Smuzhiyun InitialVector[2] = winProcEstablishConnectionOrig;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Call original function and bail if it fails.
96*4882a593Smuzhiyun * NOTE: We must do this first, since we need XdmcpOpenDisplay
97*4882a593Smuzhiyun * to be called before we initialize our clipboard client.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun iReturn = (*winProcEstablishConnectionOrig) (client);
100*4882a593Smuzhiyun if (iReturn != 0) {
101*4882a593Smuzhiyun ErrorF("winProcEstablishConnection - ProcEstablishConnection "
102*4882a593Smuzhiyun "failed, bailing.\n");
103*4882a593Smuzhiyun return iReturn;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Clear original function pointer */
107*4882a593Smuzhiyun winProcEstablishConnectionOrig = NULL;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* Startup the clipboard client if clipboard mode is being used */
110*4882a593Smuzhiyun if (g_fClipboard) {
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * NOTE: The clipboard client is started here for a reason:
113*4882a593Smuzhiyun * 1) Assume you are using XDMCP (e.g. XWin -query %hostname%)
114*4882a593Smuzhiyun * 2) If the clipboard client attaches during X Server startup,
115*4882a593Smuzhiyun * then it becomes the "magic client" that causes the X Server
116*4882a593Smuzhiyun * to reset if it exits.
117*4882a593Smuzhiyun * 3) XDMCP calls KillAllClients when it starts up.
118*4882a593Smuzhiyun * 4) The clipboard client is a client, so it is killed.
119*4882a593Smuzhiyun * 5) The clipboard client is the "magic client", so the X Server
120*4882a593Smuzhiyun * resets itself.
121*4882a593Smuzhiyun * 6) This repeats ad infinitum.
122*4882a593Smuzhiyun * 7) We avoid this by waiting until at least one client (could
123*4882a593Smuzhiyun * be XDM, could be another client) connects, which makes it
124*4882a593Smuzhiyun * almost certain that the clipboard client will not connect
125*4882a593Smuzhiyun * until after XDM when using XDMCP.
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* Create the clipboard client thread */
129*4882a593Smuzhiyun if (!winInitClipboard()) {
130*4882a593Smuzhiyun ErrorF("winProcEstablishConnection - winClipboardInit "
131*4882a593Smuzhiyun "failed.\n");
132*4882a593Smuzhiyun return iReturn;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun ErrorF("winProcEstablishConnection - winInitClipboard returned.\n");
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return iReturn;
139*4882a593Smuzhiyun }
140