xref: /OK3568_Linux_fs/external/xserver/hw/xwin/winclipboardinit.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *Permission is hereby granted, free of charge, to any person obtaining
5*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
6*4882a593Smuzhiyun  *"Software"), to deal in the Software without restriction, including
7*4882a593Smuzhiyun  *without limitation the rights to use, copy, modify, merge, publish,
8*4882a593Smuzhiyun  *distribute, sublicense, and/or sell copies of the Software, and to
9*4882a593Smuzhiyun  *permit persons to whom the Software is furnished to do so, subject to
10*4882a593Smuzhiyun  *the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun  *included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun  *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun  *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun  *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19*4882a593Smuzhiyun  *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20*4882a593Smuzhiyun  *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21*4882a593Smuzhiyun  *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  *Except as contained in this notice, the name of Harold L Hunt II
24*4882a593Smuzhiyun  *shall not be used in advertising or otherwise to promote the sale, use
25*4882a593Smuzhiyun  *or other dealings in this Software without prior written authorization
26*4882a593Smuzhiyun  *from Harold L Hunt II.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Authors:	Harold L Hunt II
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
32*4882a593Smuzhiyun #include <xwin-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <unistd.h>
36*4882a593Smuzhiyun #include <pthread.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #include "win.h"
39*4882a593Smuzhiyun #include "winclipboard/winclipboard.h"
40*4882a593Smuzhiyun #include "windisplay.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define WIN_CLIPBOARD_RETRIES			40
43*4882a593Smuzhiyun #define WIN_CLIPBOARD_DELAY			1
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Local variables
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static pthread_t g_ptClipboardProc;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun static void *
winClipboardThreadProc(void * arg)55*4882a593Smuzhiyun winClipboardThreadProc(void *arg)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun   char szDisplay[512];
58*4882a593Smuzhiyun   int clipboardRestarts = 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun   while (1)
61*4882a593Smuzhiyun     {
62*4882a593Smuzhiyun       Bool fShutdown;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun       ++clipboardRestarts;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun       /* Use our generated cookie for authentication */
67*4882a593Smuzhiyun       winSetAuthorization();
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun       /* Setup the display connection string */
70*4882a593Smuzhiyun       /*
71*4882a593Smuzhiyun        * NOTE: Always connect to screen 0 since we require that screen
72*4882a593Smuzhiyun        * numbers start at 0 and increase without gaps.  We only need
73*4882a593Smuzhiyun        * to connect to one screen on the display to get events
74*4882a593Smuzhiyun        * for all screens on the display.  That is why there is only
75*4882a593Smuzhiyun        * one clipboard client thread.
76*4882a593Smuzhiyun       */
77*4882a593Smuzhiyun       winGetDisplayName(szDisplay, 0);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun       /* Print the display connection string */
80*4882a593Smuzhiyun       ErrorF("winClipboardThreadProc - DISPLAY=%s\n", szDisplay);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun       /* Flag that clipboard client has been launched */
83*4882a593Smuzhiyun       g_fClipboardStarted = TRUE;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun       fShutdown = winClipboardProc(g_fUnicodeClipboard, szDisplay);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun       /* Flag that clipboard client has stopped */
88*4882a593Smuzhiyun       g_fClipboardStarted = FALSE;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun       if (fShutdown)
91*4882a593Smuzhiyun         break;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun       /* checking if we need to restart */
94*4882a593Smuzhiyun       if (clipboardRestarts >= WIN_CLIPBOARD_RETRIES) {
95*4882a593Smuzhiyun         /* terminates clipboard thread but the main server still lives */
96*4882a593Smuzhiyun         ErrorF("winClipboardProc - the clipboard thread has restarted %d times and seems to be unstable, disabling clipboard integration\n", clipboardRestarts);
97*4882a593Smuzhiyun         g_fClipboard = FALSE;
98*4882a593Smuzhiyun         break;
99*4882a593Smuzhiyun       }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun       sleep(WIN_CLIPBOARD_DELAY);
102*4882a593Smuzhiyun       ErrorF("winClipboardProc - trying to restart clipboard thread \n");
103*4882a593Smuzhiyun     }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun   return NULL;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * Intialize the Clipboard module
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun Bool
winInitClipboard(void)113*4882a593Smuzhiyun winInitClipboard(void)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun     winDebug("winInitClipboard ()\n");
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun     /* Spawn a thread for the Clipboard module */
118*4882a593Smuzhiyun     if (pthread_create(&g_ptClipboardProc, NULL, winClipboardThreadProc, NULL)) {
119*4882a593Smuzhiyun         /* Bail if thread creation failed */
120*4882a593Smuzhiyun         ErrorF("winInitClipboard - pthread_create failed.\n");
121*4882a593Smuzhiyun         return FALSE;
122*4882a593Smuzhiyun     }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun     return TRUE;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun void
winClipboardShutdown(void)128*4882a593Smuzhiyun winClipboardShutdown(void)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun   /* Close down clipboard resources */
131*4882a593Smuzhiyun   if (g_fClipboard && g_fClipboardStarted) {
132*4882a593Smuzhiyun     /* Synchronously destroy the clipboard window */
133*4882a593Smuzhiyun     winClipboardWindowDestroy();
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun     /* Wait for the clipboard thread to exit */
136*4882a593Smuzhiyun     pthread_join(g_ptClipboardProc, NULL);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun     g_fClipboardStarted = FALSE;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun     winDebug("winClipboardShutdown - Clipboard thread has exited.\n");
141*4882a593Smuzhiyun   }
142*4882a593Smuzhiyun }
143