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 #include "winauth.h"
42 
43 #define WIN_CLIPBOARD_RETRIES			40
44 #define WIN_CLIPBOARD_DELAY			1
45 
46 /*
47  * Local variables
48  */
49 
50 static pthread_t g_ptClipboardProc;
51 
52 /*
53  *
54  */
55 static void *
winClipboardThreadProc(void * arg)56 winClipboardThreadProc(void *arg)
57 {
58   char szDisplay[512];
59   xcb_auth_info_t *auth_info;
60   int clipboardRestarts = 0;
61 
62   while (1)
63     {
64       Bool fShutdown;
65 
66       ++clipboardRestarts;
67 
68       /* Setup the display connection string */
69       /*
70        * NOTE: Always connect to screen 0 since we require that screen
71        * numbers start at 0 and increase without gaps.  We only need
72        * to connect to one screen on the display to get events
73        * for all screens on the display.  That is why there is only
74        * one clipboard client thread.
75       */
76       winGetDisplayName(szDisplay, 0);
77 
78       /* Print the display connection string */
79       ErrorF("winClipboardThreadProc - DISPLAY=%s\n", szDisplay);
80 
81       /* Flag that clipboard client has been launched */
82       g_fClipboardStarted = TRUE;
83 
84       /* Use our generated cookie for authentication */
85       auth_info = winGetXcbAuthInfo();
86 
87       fShutdown = winClipboardProc(szDisplay, auth_info);
88 
89       /* Flag that clipboard client has stopped */
90       g_fClipboardStarted = FALSE;
91 
92       if (fShutdown)
93         break;
94 
95       /* checking if we need to restart */
96       if (clipboardRestarts >= WIN_CLIPBOARD_RETRIES) {
97         /* terminates clipboard thread but the main server still lives */
98         ErrorF("winClipboardProc - the clipboard thread has restarted %d times and seems to be unstable, disabling clipboard integration\n", clipboardRestarts);
99         g_fClipboard = FALSE;
100         break;
101       }
102 
103       sleep(WIN_CLIPBOARD_DELAY);
104       ErrorF("winClipboardProc - trying to restart clipboard thread \n");
105     }
106 
107   return NULL;
108 }
109 
110 /*
111  * Initialize the Clipboard module
112  */
113 
114 Bool
winInitClipboard(void)115 winInitClipboard(void)
116 {
117     winDebug("winInitClipboard ()\n");
118 
119     /* Spawn a thread for the Clipboard module */
120     if (pthread_create(&g_ptClipboardProc, NULL, winClipboardThreadProc, NULL)) {
121         /* Bail if thread creation failed */
122         ErrorF("winInitClipboard - pthread_create failed.\n");
123         return FALSE;
124     }
125 
126     return TRUE;
127 }
128 
129 void
winClipboardShutdown(void)130 winClipboardShutdown(void)
131 {
132   /* Close down clipboard resources */
133   if (g_fClipboard && g_fClipboardStarted) {
134     /* Synchronously destroy the clipboard window */
135     winClipboardWindowDestroy();
136 
137     /* Wait for the clipboard thread to exit */
138     pthread_join(g_ptClipboardProc, NULL);
139 
140     g_fClipboardStarted = FALSE;
141 
142     winDebug("winClipboardShutdown - Clipboard thread has exited.\n");
143   }
144 }
145