-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckoclip.c
354 lines (292 loc) · 11.5 KB
/
ckoclip.c
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
/*
Authors: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
Columbia University Academic Information Systems, New York City.
Jeffrey E Altman (jaltman@secure-endpoints.com)
Secure Endpoints Inc., New York City
Copyright (C) 1985, 2004, Trustees of Columbia University in the City of New
York.
*/
#define INCL_WIN
#define INCL_WINMLE
#define INCL_GPICONTROL
#define INCL_WINCLIPBOARD
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#define INCL_DOSMEMMGR
#include <os2.h>
#undef COMMENT
#include <stdlib.h>
#include <string.h>
#include "ckoclip.h"
/* definitions & macros */
#define PAPA( x) WinQueryWindow( x, QW_PARENT)
#define MENU( x) WinWindowFromID( x, FID_MENU)
#define HAB( x) WinQueryAnchorBlock( x)
#define FCF_WPS FCF_HIDEMAX | FCF_STANDARD & ~FCF_MINMAX
#define CT_MLE 1000
#define MAXCHARS 65535
#define WM_PASSPROC WM_USER + 0
#define WM_GETDATA WM_USER + 1
#define WM_PUTDATA WM_USER + 2
/* function prototypes */
int main( void) ;
MRESULT EXPENTRY ClientWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) ;
/* globals */
HAB hab ; /* Handle to the Anchor Block */
HWND hwndFrame, /* Handle to the Frame Window */
hwndClient ; /* Handle to the Client Window ! */
int killthread = 0 ; /* Signal to Server thread to die */
HMUX hmuxClipbrdSrv = 0 ; /* Only allow one client at a time */
HEV hevClipbrdGet = 0 ; /* Signal to get clipboard data */
HEV hevClipbrdPut = 0 ; /* Signal to put clipboard data */
HEV hevClipbrdReady = 0 ; /* The data is ready for client */
HEV hevClipbrdData = 0 ; /* Let the client know its ready */
HEV hevClipbrdDone = 0 ; /* The Client is done; reset */
PSZ pSharedMem =0; /* The shared memory buffer */
/* This function is the Clipboard Server thread. It handles all
communication with our clients via Named Shared Semaphores.
We can't access the clipboard from here, so we post a message
to our message queue thread when we need the clipboard data.
The Window Procedure then gets the data and signals us via a
private semaphore that it the data is ready. At that time
we signal our client, and wait for the client to complete.
If the client, doesn't release us we will wait forever. But better
that then allow two clients to possibly crash into each other
*/
void
clipsrv( void * param )
{
ULONG clipsize =0;
PBYTE * clipdata =0;
ULONG postcount =0;
APIRET rc = 0 ;
HMUX hmuxWaitForWork = 0 ;
SEMRECORD work_rec[2] ;
ULONG jobtype ;
#define GETTEXT 0L
#define PUTTEXT 1L
rc = DosAllocSharedMem( (PPVOID) &pSharedMem,
"\\SHAREMEM\\CKERMIT\\CLIPBRD\\DATA",
MAXCHARS, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_TILE ) ;
work_rec[0].hsemCur = (HSEM) hevClipbrdGet ;
work_rec[0].ulUser = GETTEXT ;
work_rec[1].hsemCur = (HSEM) hevClipbrdPut ;
work_rec[1].ulUser = PUTTEXT ;
rc = DosCreateMuxWaitSem( NULL, &hmuxWaitForWork, 2, work_rec,
DCMW_WAIT_ANY | DC_SEM_SHARED ) ;
while ( !killthread ) {
pSharedMem[0] = '\0' ;
/* Wait for something to do */
rc = DosWaitMuxWaitSem( hmuxWaitForWork, SEM_INDEFINITE_WAIT, &jobtype ) ;
switch ( jobtype ) {
case GETTEXT:
rc = DosResetEventSem( hevClipbrdGet, &postcount ) ;
WinPostMsg( hwndClient, WM_GETDATA, 0, 0 );
rc = DosWaitEventSem( hevClipbrdReady, SEM_INDEFINITE_WAIT );
rc = DosResetEventSem( hevClipbrdReady, &postcount );
/* Now let the requester know its ready */
rc = DosPostEventSem( hevClipbrdData ) ;
/* Now wait for the requester to finish using the memory */
rc = DosWaitEventSem( hevClipbrdDone, SEM_INDEFINITE_WAIT ) ;
rc = DosResetEventSem( hevClipbrdDone, &postcount ) ;
break;
case PUTTEXT:
rc = DosResetEventSem( hevClipbrdPut, &postcount ) ;
/* There is text in the shared memory to be sent to the Clipboard */
/* so let the Window Message thread know */
WinPostMsg( hwndClient, WM_PUTDATA, 0 , 0 ) ;
/* Wait for the Window Message thread to finish the Copy */
rc = DosWaitEventSem( hevClipbrdReady, SEM_INDEFINITE_WAIT );
rc = DosResetEventSem( hevClipbrdReady, &postcount );
/* Let the client know we are Done */
rc = DosPostEventSem( hevClipbrdDone ) ;
break;
}
}
rc = DosCloseMuxWaitSem( hmuxWaitForWork ) ;
rc = DosFreeMem( pSharedMem ) ;
}
int
initserver( void )
{
APIRET rc ;
if ( rc = DosCreateMutexSem( "\\SEM32\\CKERMIT\\CLIPBRD\\MUX",
&hmuxClipbrdSrv, DC_SEM_SHARED, TRUE ) )
return 1 ;
if ( rc = DosCreateEventSem( "\\SEM32\\CKERMIT\\CLIPBRD\\GET", &hevClipbrdGet,
DC_SEM_SHARED, FALSE ) ) {
DosCloseMutexSem( hmuxClipbrdSrv ) ;
return 1 ;
}
if ( rc = DosCreateEventSem( "\\SEM32\\CKERMIT\\CLIPBRD\\DATA", &hevClipbrdData,
DC_SEM_SHARED, FALSE ) ) {
DosCloseMutexSem( hmuxClipbrdSrv ) ;
DosCloseEventSem( hevClipbrdGet ) ;
return 1 ;
}
if ( rc = DosCreateEventSem( "\\SEM32\\CKERMIT\\CLIPBRD\\DONE", &hevClipbrdDone,
DC_SEM_SHARED, FALSE ) ) {
DosCloseMutexSem( hmuxClipbrdSrv ) ;
DosCloseEventSem( hevClipbrdGet ) ;
DosCloseEventSem( hevClipbrdData ) ;
return 1 ;
}
if ( rc = DosCreateEventSem( NULL, &hevClipbrdReady, 0, FALSE ) ) {
DosCloseMutexSem( hmuxClipbrdSrv ) ;
DosCloseEventSem( hevClipbrdGet ) ;
DosCloseEventSem( hevClipbrdData ) ;
DosCloseEventSem( hevClipbrdDone ) ;
return 1 ;
}
if ( rc = DosCreateEventSem( "\\SEM32\\CKERMIT\\CLIPBRD\\PUT", &hevClipbrdPut,
DC_SEM_SHARED, FALSE ) ) {
DosCloseMutexSem( hmuxClipbrdSrv ) ;
DosCloseEventSem( hevClipbrdGet ) ;
DosCloseEventSem( hevClipbrdData ) ;
DosCloseEventSem( hevClipbrdDone ) ;
DosCloseEventSem( hevClipbrdReady ) ;
return 1 ;
}
return 0 ;
}
void
killserver( void )
{
DosRequestMutexSem( hmuxClipbrdSrv, SEM_INDEFINITE_WAIT ) ;
killthread = 1 ;
DosCloseMutexSem( hmuxClipbrdSrv ) ;
DosCloseEventSem( hevClipbrdGet ) ;
DosCloseEventSem( hevClipbrdData ) ;
DosCloseEventSem( hevClipbrdDone ) ;
DosCloseEventSem( hevClipbrdPut ) ;
}
/* Main thread. Allocate all semaphores. If we can't it probably
means that another copy of is already running. So just die.
*/
int main( void)
{
CHAR szWindowTitle[255] ;
HMQ hmq ;
QMSG qmsg ;
ULONG flFrameFlags = FCF_WPS & ~FCF_ACCELTABLE ;
APIRET rc ;
hab = WinInitialize( 0) ;
hmq = WinCreateMsgQueue( hab, 0L) ;
if ( initserver() ) { /* couldn't create semaphores */
WinLoadString( hab, NULLHANDLE, ST_CREATION_ERROR,
sizeof( szWindowTitle), szWindowTitle) ;
WinMessageBox( HWND_DESKTOP, HWND_DESKTOP, szWindowTitle,
(PSZ) NULL, 1, MB_ERROR ) ;
return 1 ;
}
else {
/* load window title from resource file */
WinLoadString( hab, NULLHANDLE, ST_WINDOWTITLE,
sizeof( szWindowTitle), szWindowTitle) ;
/* create main window */
hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
0,
&flFrameFlags,
WC_MLE,
szWindowTitle,
WS_CLIPCHILDREN,
NULLHANDLE,
RS_ALL,
&hwndClient) ;
WinSetWindowPos( hwndFrame, HWND_BOTTOM, 0L, 0L, 450L, 100L,
SWP_SIZE | SWP_SHOW | SWP_ZORDER) ;
WinShowWindow( hwndFrame, FALSE ) ;
/* subclass the MLE */
WinSendMsg( hwndClient, WM_PASSPROC,
MPFROMP( WinSubclassWindow( hwndClient, ClientWndProc)), 0L) ;
/* Start processing client requests */
_beginthread( clipsrv, NULL, 65535, NULL ) ;
rc = DosReleaseMutexSem( hmuxClipbrdSrv ) ;
while( WinGetMsg( hab, &qmsg, NULLHANDLE, 0L, 0L))
WinDispatchMsg( hab, &qmsg) ;
/* destroy resources */
killserver() ;
WinDestroyWindow (hwndFrame) ;
WinDestroyMsgQueue (hmq) ;
WinTerminate (hab) ;
}
return 0L ;
}
/* The Clients Window Procedure. It is here that we actually get the
Clipboard Data and place it into the shared memory.
*/
MRESULT EXPENTRY ClientWndProc( HWND hwnd,
ULONG msg,
MPARAM mp1,
MPARAM mp2)
{
static PFNWP pfnwp ;
static HWND hmenu ;
APIRET rc ;
PSZ pClipboard ;
switch( msg)
{
case WM_PASSPROC:
pfnwp = (PFNWP)mp1 ;
/* limit text to MAXCHARS characters and make it readonly*/
WinSendMsg( hwnd, MLM_SETTEXTLIMIT, MPFROMLONG(MAXCHARS), 0L) ;
WinSendMsg( hwnd, MLM_SETREADONLY, MPFROMLONG(1L), 0L) ;
/* get the menu handle */
hmenu = MENU(PAPA(hwnd)) ;
return 0L ;
case WM_GETDATA:
/* Get the data from the clipboard */
rc = WinOpenClipbrd(hab) ;
if ( rc ) {
pClipboard = (PSZ) WinQueryClipbrdData( hab, CF_TEXT ) ;
/* Copy it to our Shared Memory */
if ( pClipboard )
strcpy( pSharedMem, pClipboard ) ;
WinCloseClipbrd(hab) ;
}
/* set text in the MLE window */
WinSetWindowText( hwnd, pSharedMem) ;
DosPostEventSem( hevClipbrdReady );
return 0L;
case WM_PUTDATA:
/* The data is in our named shared memory */
/* we must allocate shared memory to give to the clipboard */
/* copy the text into it */
/* and pass it off to the clipboard */
rc = DosAllocSharedMem( (PPVOID) &pClipboard,
NULL, strlen(pSharedMem)+1, PAG_COMMIT | PAG_WRITE | OBJ_GIVEABLE );
if ( pClipboard ) {
strcpy( pClipboard, pSharedMem ) ;
rc = WinOpenClipbrd(hab);
if ( rc ) {
rc = WinSetClipbrdData( hab, (ULONG) pClipboard, CF_TEXT,
CFI_POINTER ) ;
WinCloseClipbrd( hab ) ;
WinSetWindowText( hwnd, pSharedMem ) ;
}
else {
WinSetWindowText( hwnd, "Unable to open Clipboard." ) ;
}
}
else {
WinSetWindowText( hwnd, "Unable to allocate Shared Memory for PUT operation.");
}
DosPostEventSem( hevClipbrdReady );
return 0L;
case WM_INITMENU:
break ;
case WM_COMMAND:
switch( COMMANDMSG( &msg) -> cmd)
{
case MN_EXIT:
WinPostMsg( hwnd, WM_QUIT, NULL, NULL) ;
break ;
default:
break ;
}
break ;
default:
break;
}
return (*pfnwp)( hwnd, msg, mp1, mp2) ;
}