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