Take RGB mask into account when doing color keying.
[wine] / dlls / user / dde_server.c
1 /*
2  * DDEML library
3  *
4  * Copyright 1997 Alexandre Julliard
5  * Copyright 1997 Len White
6  * Copyright 1999 Keith Matthews
7  * Copyright 2000 Corel
8  * Copyright 2001 Eric Pouech
9  * Copyright 2003, 2004, 2005 Dmitry Timoshkov
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "dde.h"
34 #include "ddeml.h"
35 #include "win.h"
36 #include "wine/debug.h"
37 #include "dde_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
40
41 static const WCHAR szServerNameClass[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','N','a','m','e',0};
42 const char WDML_szServerConvClassA[] = "WineDdeServerConvA";
43 const WCHAR WDML_szServerConvClassW[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','C','o','n','v','W',0};
44
45 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
46 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
47
48 /******************************************************************************
49  * DdePostAdvise [USER32.@]  Send transaction to DDE callback function.
50  *
51  * PARAMS
52  *      idInst    [I] Instance identifier
53  *      hszTopic  [I] Handle to topic name string
54  *      hszItem   [I] Handle to item name string
55  *
56  * RETURNS
57  *    Success: TRUE
58  *    Failure: FALSE
59  */
60 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
61 {
62     WDML_INSTANCE*      pInstance = NULL;
63     WDML_LINK*          pLink = NULL;
64     HDDEDATA            hDdeData = 0;
65     HGLOBAL             hItemData = 0;
66     WDML_CONV*          pConv = NULL;
67     ATOM                atom = 0;
68     UINT                count;
69
70     TRACE("(%ld,%p,%p)\n", idInst, hszTopic, hszItem);
71
72     EnterCriticalSection(&WDML_CritSect);
73
74     pInstance = WDML_GetInstance(idInst);
75
76     if (pInstance == NULL || pInstance->links == NULL)
77     {
78         goto theError;
79     }
80
81     atom = WDML_MakeAtomFromHsz(hszItem);
82     if (!atom) goto theError;
83
84     /* first compute the number of links which will trigger a message */
85     count = 0;
86     for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
87     {
88         if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
89         {
90             count++;
91         }
92     }
93     if (count >= CADV_LATEACK)
94     {
95         FIXME("too high value for count\n");
96         count &= 0xFFFF;
97     }
98
99     for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
100     {
101         if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
102         {
103             hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
104                                            hszTopic, hszItem, 0, --count, 0);
105
106             if (hDdeData == (HDDEDATA)CBR_BLOCK)
107             {
108                 /* MS doc is not consistent here */
109                 FIXME("CBR_BLOCK returned for ADVREQ\n");
110                 continue;
111             }
112             if (hDdeData)
113             {
114                 if (pLink->transactionType & XTYPF_NODATA)
115                 {
116                     TRACE("no data\n");
117                     hItemData = 0;
118                 }
119                 else
120                 {
121                     TRACE("with data\n");
122
123                     hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
124                 }
125
126                 pConv = WDML_GetConv(pLink->hConv, TRUE);
127
128                 if (pConv == NULL)
129                 {
130                     if (!WDML_IsAppOwned(hDdeData))  DdeFreeDataHandle(hDdeData);
131                     goto theError;
132                 }
133
134                 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
135                                   PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
136                 {
137                     ERR("post message failed\n");
138                     pConv->wStatus &= ~ST_CONNECTED;
139                     if (!WDML_IsAppOwned(hDdeData))  DdeFreeDataHandle(hDdeData);
140                     GlobalFree(hItemData);
141                     goto theError;
142                 }
143                 if (!WDML_IsAppOwned(hDdeData))  DdeFreeDataHandle(hDdeData);
144             }
145         }
146     }
147     LeaveCriticalSection(&WDML_CritSect);
148     return TRUE;
149  theError:
150     LeaveCriticalSection(&WDML_CritSect);
151     if (atom) GlobalDeleteAtom(atom);
152     return FALSE;
153 }
154
155
156 /******************************************************************************
157  * DdeNameService [USER32.@]  {Un}registers service name of DDE server
158  *
159  * PARAMS
160  *    idInst [I] Instance identifier
161  *    hsz1   [I] Handle to service name string
162  *    hsz2   [I] Reserved
163  *    afCmd  [I] Service name flags
164  *
165  * RETURNS
166  *    Success: Non-zero
167  *    Failure: 0
168  */
169 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
170 {
171     WDML_SERVER*        pServer;
172     WDML_INSTANCE*      pInstance;
173     HDDEDATA            hDdeData;
174     HWND                hwndServer;
175     WNDCLASSEXW         wndclass;
176
177     hDdeData = NULL;
178
179     TRACE("(%ld,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
180
181     EnterCriticalSection(&WDML_CritSect);
182
183     /*  First check instance
184      */
185     pInstance = WDML_GetInstance(idInst);
186     if  (pInstance == NULL)
187     {
188         TRACE("Instance not found as initialised\n");
189         /*  Nothing has been initialised - exit now ! can return TRUE since effect is the same */
190         goto theError;
191     }
192
193     if (hsz2 != 0L)
194     {
195         /*      Illegal, reserved parameter
196          */
197         pInstance->lastError = DMLERR_INVALIDPARAMETER;
198         WARN("Reserved parameter no-zero !!\n");
199         goto theError;
200     }
201     if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
202     {
203         /*      don't know if we should check this but it makes sense
204          *      why supply REGISTER or filter flags if de-registering all
205          */
206         TRACE("General unregister unexpected flags\n");
207         pInstance->lastError = DMLERR_INVALIDPARAMETER;
208         goto theError;
209     }
210
211     switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
212     {
213     case DNS_REGISTER:
214         pServer = WDML_FindServer(pInstance, hsz1, 0);
215         if (pServer)
216         {
217             ERR("Trying to register already registered service!\n");
218             pInstance->lastError = DMLERR_DLL_USAGE;
219             goto theError;
220         }
221
222         TRACE("Adding service name\n");
223
224         WDML_IncHSZ(pInstance, hsz1);
225
226         pServer = WDML_AddServer(pInstance, hsz1, 0);
227
228         WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
229                                  pServer->atomService, pServer->atomServiceSpec);
230
231         wndclass.cbSize        = sizeof(wndclass);
232         wndclass.style         = 0;
233         wndclass.lpfnWndProc   = WDML_ServerNameProc;
234         wndclass.cbClsExtra    = 0;
235         wndclass.cbWndExtra    = 2 * sizeof(ULONG_PTR);
236         wndclass.hInstance     = 0;
237         wndclass.hIcon         = 0;
238         wndclass.hCursor       = 0;
239         wndclass.hbrBackground = 0;
240         wndclass.lpszMenuName  = NULL;
241         wndclass.lpszClassName = szServerNameClass;
242         wndclass.hIconSm       = 0;
243
244         RegisterClassExW(&wndclass);
245
246         LeaveCriticalSection(&WDML_CritSect);
247         hwndServer = CreateWindowW(szServerNameClass, NULL,
248                                    WS_POPUP, 0, 0, 0, 0,
249                                    0, 0, 0, 0);
250         EnterCriticalSection(&WDML_CritSect);
251
252         SetWindowLongPtrW(hwndServer, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
253         SetWindowLongPtrW(hwndServer, GWL_WDML_SERVER, (ULONG_PTR)pServer);
254         TRACE("Created nameServer=%p for instance=%08lx\n", hwndServer, idInst);
255
256         pServer->hwndServer = hwndServer;
257         break;
258
259     case DNS_UNREGISTER:
260         if (hsz1 == 0L)
261         {
262             /* General unregister situation
263              * terminate all server side pending conversations
264              */
265             while (pInstance->servers)
266                 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
267             pInstance->servers = NULL;
268             TRACE("General de-register - finished\n");
269         }
270         else
271         {
272             WDML_RemoveServer(pInstance, hsz1, 0L);
273         }
274         break;
275     }
276
277     if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
278     {
279         /*      Set filter flags on to hold notifications of connection
280          */
281         pServer = WDML_FindServer(pInstance, hsz1, 0);
282         if (!pServer)
283         {
284             /*  trying to filter where no service names !!
285              */
286             pInstance->lastError = DMLERR_DLL_USAGE;
287             goto theError;
288         }
289         else
290         {
291             pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
292         }
293     }
294     LeaveCriticalSection(&WDML_CritSect);
295     return (HDDEDATA)TRUE;
296
297  theError:
298     LeaveCriticalSection(&WDML_CritSect);
299     return FALSE;
300 }
301
302 /******************************************************************
303  *              WDML_CreateServerConv
304  *
305  *
306  */
307 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
308                                         HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
309 {
310     HWND        hwndServerConv;
311     WDML_CONV*  pConv;
312
313     if (pInstance->unicode)
314     {
315         WNDCLASSEXW wndclass;
316
317         wndclass.cbSize        = sizeof(wndclass);
318         wndclass.style         = 0;
319         wndclass.lpfnWndProc   = WDML_ServerConvProc;
320         wndclass.cbClsExtra    = 0;
321         wndclass.cbWndExtra    = 2 * sizeof(ULONG_PTR);
322         wndclass.hInstance     = 0;
323         wndclass.hIcon         = 0;
324         wndclass.hCursor       = 0;
325         wndclass.hbrBackground = 0;
326         wndclass.lpszMenuName  = NULL;
327         wndclass.lpszClassName = WDML_szServerConvClassW;
328         wndclass.hIconSm       = 0;
329
330         RegisterClassExW(&wndclass);
331
332         hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
333                                        WS_CHILD, 0, 0, 0, 0,
334                                        hwndServerName, 0, 0, 0);
335     }
336     else
337     {
338         WNDCLASSEXA wndclass;
339
340         wndclass.cbSize        = sizeof(wndclass);
341         wndclass.style         = 0;
342         wndclass.lpfnWndProc   = WDML_ServerConvProc;
343         wndclass.cbClsExtra    = 0;
344         wndclass.cbWndExtra    = 2 * sizeof(ULONG_PTR);
345         wndclass.hInstance     = 0;
346         wndclass.hIcon         = 0;
347         wndclass.hCursor       = 0;
348         wndclass.hbrBackground = 0;
349         wndclass.lpszMenuName  = NULL;
350         wndclass.lpszClassName = WDML_szServerConvClassA;
351         wndclass.hIconSm       = 0;
352
353         RegisterClassExA(&wndclass);
354
355         hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
356                                       WS_CHILD, 0, 0, 0, 0,
357                                       hwndServerName, 0, 0, 0);
358     }
359
360     TRACE("Created convServer=%p (nameServer=%p) for instance=%08lx\n",
361           hwndServerConv, hwndServerName, pInstance->instanceID);
362
363     pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
364                          hwndClient, hwndServerConv);
365     if (pConv)
366     {
367         SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
368         SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
369
370         /* this should be the only place using SendMessage for WM_DDE_ACK */
371         /* note: sent messages shall not use packing */
372         SendMessageW(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
373                      MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), WDML_MakeAtomFromHsz(hszTopic)));
374         /* we assume we're connected since we've sent an answer...
375          * I'm not sure what we can do... it doesn't look like the return value
376          * of SendMessage is used... sigh...
377          */
378         pConv->wStatus |= ST_CONNECTED;
379     }
380     else
381     {
382         DestroyWindow(hwndServerConv);
383     }
384     return pConv;
385 }
386
387 /******************************************************************
388  *              WDML_ServerNameProc
389  *
390  *
391  */
392 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
393 {
394     HWND                hwndClient;
395     HSZ                 hszApp, hszTop;
396     HDDEDATA            hDdeData = 0;
397     WDML_INSTANCE*      pInstance;
398     UINT_PTR            uiLo, uiHi;
399
400     switch (iMsg)
401     {
402     case WM_DDE_INITIATE:
403
404         /* wParam         -- sending window handle
405            LOWORD(lParam) -- application atom
406            HIWORD(lParam) -- topic atom */
407
408         TRACE("WM_DDE_INITIATE message received!\n");
409         hwndClient = (HWND)wParam;
410
411         pInstance = WDML_GetInstanceFromWnd(hwndServer);
412         TRACE("idInst=%ld, threadID=0x%lx\n", pInstance->instanceID, GetCurrentThreadId());
413         if (!pInstance) return 0;
414
415         /* don't free DDEParams, since this is a broadcast */
416         UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
417
418         hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
419         hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
420
421         if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
422         {
423             BOOL                self = FALSE;
424             CONVCONTEXT         cc;
425             CONVCONTEXT*        pcc = NULL;
426             WDML_CONV*          pConv;
427             char                buf[256];
428
429             if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
430                 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
431             {
432                 self = TRUE;
433             }
434             /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
435              * handled under DDEML, and if so build a default context
436              */
437            if ((GetClassNameA(hwndClient, buf, sizeof(buf)) &&
438                 lstrcmpiA(buf, WDML_szClientConvClassA) == 0) ||
439                (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
440                 lstrcmpiW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
441             {
442                 pcc = &cc;
443                 memset(pcc, 0, sizeof(*pcc));
444                 pcc->cb = sizeof(*pcc);
445                 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
446             }
447             if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
448             {
449                 TRACE("Don't do self connection as requested\n");
450             }
451             else if (hszApp && hszTop)
452             {
453                 WDML_SERVER*    pServer = (WDML_SERVER*)GetWindowLongPtrW(hwndServer, GWL_WDML_SERVER);
454
455                 /* check filters for name service */
456                 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
457                 {
458                     /* pass on to the callback  */
459                     hDdeData = WDML_InvokeCallback(pInstance, XTYP_CONNECT,
460                                                    0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
461                     if ((ULONG_PTR)hDdeData)
462                     {
463                         pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
464                                                       hszApp, hszTop);
465                         if (pConv)
466                         {
467                             if (pcc) pConv->wStatus |= ST_ISLOCAL;
468                             WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
469                                                 hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
470                         }
471                     }
472                 }
473             }
474             else if (pInstance->servers)
475             {
476                 /* pass on to the callback  */
477                 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
478                                                0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
479
480                 if (hDdeData == (HDDEDATA)CBR_BLOCK)
481                 {
482                     /* MS doc is not consistent here */
483                     FIXME("CBR_BLOCK returned for WILDCONNECT\n");
484                 }
485                 else if ((ULONG_PTR)hDdeData != 0)
486                 {
487                     HSZPAIR*    hszp;
488
489                     hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
490                     if (hszp)
491                     {
492                         int     i;
493                         for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
494                         {
495                             pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
496                                                           hszp[i].hszSvc, hszp[i].hszTopic);
497                             if (pConv)
498                             {
499                                 if (pcc) pConv->wStatus |= ST_ISLOCAL;
500                                 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
501                                                     hszp[i].hszTopic, hszp[i].hszSvc, 0, (ULONG_PTR)pcc, self);
502                             }
503                         }
504                         DdeUnaccessData(hDdeData);
505                     }
506                     if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
507                 }
508             }
509         }
510
511         return 0;
512
513     case WM_DDE_REQUEST:
514         FIXME("WM_DDE_REQUEST message received!\n");
515         return 0;
516     case WM_DDE_ADVISE:
517         FIXME("WM_DDE_ADVISE message received!\n");
518         return 0;
519     case WM_DDE_UNADVISE:
520         FIXME("WM_DDE_UNADVISE message received!\n");
521         return 0;
522     case WM_DDE_EXECUTE:
523         FIXME("WM_DDE_EXECUTE message received!\n");
524         return 0;
525     case WM_DDE_POKE:
526         FIXME("WM_DDE_POKE message received!\n");
527         return 0;
528     case WM_DDE_TERMINATE:
529         FIXME("WM_DDE_TERMINATE message received!\n");
530         return 0;
531     default:
532         break;
533     }
534
535     return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
536 }
537
538 /******************************************************************
539  *              WDML_ServerQueueRequest
540  *
541  *
542  */
543 static  WDML_XACT*      WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
544 {
545     UINT_PTR            uiLo, uiHi;
546     WDML_XACT*          pXAct;
547
548     UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
549
550     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
551                                   uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
552     if (pXAct) pXAct->atom = uiHi;
553     return pXAct;
554 }
555
556 /******************************************************************
557  *              WDML_ServerHandleRequest
558  *
559  *
560  */
561 static  WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
562 {
563     HDDEDATA            hDdeData = 0;
564     BOOL                fAck = TRUE;
565
566     if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
567     {
568
569         hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
570                                        pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
571     }
572
573     switch ((ULONG_PTR)hDdeData)
574     {
575     case 0:
576         TRACE("No data returned from the Callback\n");
577         fAck = FALSE;
578         break;
579
580     case (ULONG_PTR)CBR_BLOCK:
581         return WDML_QS_BLOCK;
582
583     default:
584         {
585             HGLOBAL     hMem = WDML_DataHandle2Global(hDdeData, TRUE, FALSE, FALSE, FALSE);
586             if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
587                               ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
588                                              (UINT_PTR)hMem, (UINT_PTR)pXAct->atom)))
589             {
590                 DdeFreeDataHandle(hDdeData);
591                 GlobalFree(hMem);
592                 fAck = FALSE;
593             }
594         }
595         break;
596     }
597
598     WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
599
600     WDML_DecHSZ(pConv->instance, pXAct->hszItem);
601
602     return WDML_QS_HANDLED;
603 }
604
605 /******************************************************************
606  *              WDML_ServerQueueAdvise
607  *
608  *
609  */
610 static  WDML_XACT*      WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
611 {
612     UINT_PTR            uiLo, uiHi;
613     WDML_XACT*          pXAct;
614
615     /* XTYP_ADVSTART transaction:
616        establish link and save link info to InstanceInfoTable */
617
618     if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
619         return NULL;
620
621     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
622                                   0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
623     if (pXAct)
624     {
625         pXAct->hMem = (HGLOBAL)uiLo;
626         pXAct->atom = uiHi;
627     }
628     return pXAct;
629 }
630
631 /******************************************************************
632  *              WDML_ServerHandleAdvise
633  *
634  *
635  */
636 static  WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
637 {
638     UINT                uType;
639     WDML_LINK*          pLink;
640     DDEADVISE*          pDdeAdvise;
641     HDDEDATA            hDdeData = 0;
642     BOOL                fAck = TRUE;
643
644     pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
645     uType = XTYP_ADVSTART |
646             (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
647             (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
648
649     if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
650     {
651         hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
652                                        (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
653     }
654
655     switch ((ULONG_PTR)hDdeData)
656     {
657     case 0:
658         TRACE("No data returned from the Callback\n");
659         fAck = FALSE;
660         break;
661
662     case (ULONG_PTR)CBR_BLOCK:
663         return WDML_QS_BLOCK;
664
665     default:
666         /* billx: first to see if the link is already created. */
667         pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
668                               pXAct->hszItem, TRUE, pDdeAdvise->cfFormat);
669
670         if (pLink != NULL)
671         {
672             /* we found a link, and only need to modify it in case it changes */
673             pLink->transactionType = uType;
674         }
675         else
676         {
677             TRACE("Adding Link with hConv %p\n", pConv);
678             WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
679                          uType, pXAct->hszItem, pDdeAdvise->cfFormat);
680         }
681         break;
682     }
683
684     GlobalUnlock(pXAct->hMem);
685     if (fAck)
686     {
687         GlobalFree(pXAct->hMem);
688     }
689     pXAct->hMem = 0;
690
691     WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
692
693     WDML_DecHSZ(pConv->instance, pXAct->hszItem);
694
695     return WDML_QS_HANDLED;
696 }
697
698 /******************************************************************
699  *              WDML_ServerQueueUnadvise
700  *
701  *
702  */
703 static  WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
704 {
705     UINT_PTR            uiLo, uiHi;
706     WDML_XACT*          pXAct;
707
708     UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
709
710     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
711                                   uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
712     if (pXAct) pXAct->atom = uiHi;
713     return pXAct;
714 }
715
716 /******************************************************************
717  *              WDML_ServerHandleUnadvise
718  *
719  *
720  */
721 static  WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
722 {
723     WDML_LINK*  pLink;
724
725     if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
726     {
727         ERR("Unsupported yet options (null item or clipboard format)\n");
728         return WDML_QS_ERROR;
729     }
730
731     pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
732                           pXAct->hszItem, TRUE, pXAct->wFmt);
733     if (pLink == NULL)
734     {
735         ERR("Couln'd find link for %p, dropping request\n", pXAct->hszItem);
736         FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
737         return WDML_QS_ERROR;
738     }
739
740     if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
741     {
742         WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
743                             pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
744     }
745
746     WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
747                     pXAct->hszItem, pXAct->wFmt);
748
749     /* send back ack */
750     WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
751                  pXAct->lParam, WM_DDE_UNADVISE);
752
753     WDML_DecHSZ(pConv->instance, pXAct->hszItem);
754
755     return WDML_QS_HANDLED;
756 }
757
758 /******************************************************************
759  *              WDML_QueueExecute
760  *
761  *
762  */
763 static  WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
764 {
765     WDML_XACT*  pXAct;
766
767     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
768     if (pXAct)
769     {
770         pXAct->hMem    = (HGLOBAL)lParam;
771     }
772     return pXAct;
773 }
774
775  /******************************************************************
776  *              WDML_ServerHandleExecute
777  *
778  *
779  */
780 static  WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
781 {
782     HDDEDATA    hDdeData = DDE_FNOTPROCESSED;
783     BOOL        fAck = FALSE, fBusy = FALSE;
784
785     if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
786     {
787         LPVOID  ptr = GlobalLock(pXAct->hMem);
788
789         if (ptr)
790         {
791             hDdeData = DdeCreateDataHandle(0, ptr, GlobalSize(pXAct->hMem),
792                                            0, 0, CF_TEXT, 0);
793             GlobalUnlock(pXAct->hMem);
794         }
795         hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
796                                        pConv->hszTopic, 0, hDdeData, 0L, 0L);
797     }
798
799     switch ((ULONG_PTR)hDdeData)
800     {
801     case (ULONG_PTR)CBR_BLOCK:
802         return WDML_QS_BLOCK;
803
804     case DDE_FACK:
805         fAck = TRUE;
806         break;
807     case DDE_FBUSY:
808         fBusy = TRUE;
809         break;
810     default:
811         FIXME("Unsupported returned value %p\n", hDdeData);
812         /* fall through */
813     case DDE_FNOTPROCESSED:
814         break;
815     }
816     WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT)pXAct->hMem, 0, 0);
817
818     return WDML_QS_HANDLED;
819 }
820
821 /******************************************************************
822  *              WDML_ServerQueuePoke
823  *
824  *
825  */
826 static  WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
827 {
828     UINT_PTR            uiLo, uiHi;
829     WDML_XACT*          pXAct;
830
831     UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
832
833     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
834                                   0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
835     if (pXAct)
836     {
837         pXAct->atom = uiHi;
838         pXAct->hMem = (HGLOBAL)uiLo;
839     }
840     return pXAct;
841 }
842
843 /******************************************************************
844  *              WDML_ServerHandlePoke
845  *
846  *
847  */
848 static  WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
849 {
850     DDEPOKE*            pDdePoke;
851     HDDEDATA            hDdeData;
852     BOOL                fBusy = FALSE, fAck = FALSE;
853
854     pDdePoke = (DDEPOKE*)GlobalLock(pXAct->hMem);
855     if (!pDdePoke)
856     {
857         return WDML_QS_ERROR;
858     }
859
860     if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
861     {
862         hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
863                                        GlobalSize(pXAct->hMem) - sizeof(DDEPOKE) + 1,
864                                        0, 0, pDdePoke->cfFormat, 0);
865         if (hDdeData)
866         {
867             HDDEDATA    hDdeDataOut;
868
869             hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
870                                               (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
871                                               hDdeData, 0, 0);
872             switch ((ULONG_PTR)hDdeDataOut)
873             {
874             case DDE_FACK:
875                 fAck = TRUE;
876                 break;
877             case DDE_FBUSY:
878                 fBusy = TRUE;
879                 break;
880             default:
881                 FIXME("Unsupported returned value %p\n", hDdeDataOut);
882                 /* fal through */
883             case DDE_FNOTPROCESSED:
884                 break;
885             }
886             DdeFreeDataHandle(hDdeData);
887         }
888     }
889     GlobalUnlock(pXAct->hMem);
890
891     if (!fAck)
892     {
893         GlobalFree(pXAct->hMem);
894     }
895     WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
896
897     WDML_DecHSZ(pConv->instance, pXAct->hszItem);
898
899     return WDML_QS_HANDLED;
900 }
901
902 /******************************************************************
903  *              WDML_ServerQueueTerminate
904  *
905  *
906  */
907 static  WDML_XACT*      WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
908 {
909     WDML_XACT*  pXAct;
910
911     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
912     return pXAct;
913 }
914
915 /******************************************************************
916  *              WDML_ServerHandleTerminate
917  *
918  *
919  */
920 static  WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
921 {
922     /* billx: two things to remove: the conv, and associated links.
923      * Respond with another WM_DDE_TERMINATE iMsg.
924      */
925     if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
926     {
927         WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
928                             0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
929     }
930     PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
931     WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
932
933     return WDML_QS_HANDLED;
934 }
935
936 /******************************************************************
937  *              WDML_ServerHandle
938  *
939  *
940  */
941 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
942 {
943     WDML_QUEUE_STATE    qs = WDML_QS_ERROR;
944
945     switch (pXAct->ddeMsg)
946     {
947     case WM_DDE_INITIATE:
948         FIXME("WM_DDE_INITIATE shouldn't be there!\n");
949         break;
950     case WM_DDE_REQUEST:
951         qs = WDML_ServerHandleRequest(pConv, pXAct);
952         break;
953
954     case WM_DDE_ADVISE:
955         qs = WDML_ServerHandleAdvise(pConv, pXAct);
956         break;
957
958     case WM_DDE_UNADVISE:
959         qs = WDML_ServerHandleUnadvise(pConv, pXAct);
960         break;
961
962     case WM_DDE_EXECUTE:
963         qs = WDML_ServerHandleExecute(pConv, pXAct);
964         break;
965
966     case WM_DDE_POKE:
967         qs = WDML_ServerHandlePoke(pConv, pXAct);
968         break;
969
970     case WM_DDE_TERMINATE:
971         qs = WDML_ServerHandleTerminate(pConv, pXAct);
972         break;
973
974     case WM_DDE_ACK:
975         WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
976         break;
977
978     default:
979         FIXME("Unsupported message %d\n", pXAct->ddeMsg);
980     }
981     return qs;
982 }
983
984 /******************************************************************
985  *              WDML_ServerConvProc
986  *
987  *
988  */
989 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
990 {
991     WDML_INSTANCE*      pInstance;
992     WDML_CONV*          pConv;
993     WDML_XACT*          pXAct = NULL;
994
995     TRACE("%p %04x %08x %08lx\n", hwndServer, iMsg, wParam , lParam);
996
997     if (iMsg == WM_DESTROY)
998     {
999         EnterCriticalSection(&WDML_CritSect);
1000         pConv = WDML_GetConvFromWnd(hwndServer);
1001         if (pConv && !(pConv->wStatus & ST_TERMINATED))
1002         {
1003             WDML_ServerHandleTerminate(pConv, NULL);
1004         }
1005         LeaveCriticalSection(&WDML_CritSect);
1006     }
1007     if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
1008     {
1009         return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
1010                                              DefWindowProcA(hwndServer, iMsg, wParam, lParam);
1011     }
1012
1013     EnterCriticalSection(&WDML_CritSect);
1014
1015     pInstance = WDML_GetInstanceFromWnd(hwndServer);
1016     pConv = WDML_GetConvFromWnd(hwndServer);
1017
1018     if (!pConv)
1019     {
1020         ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1021         goto theError;
1022     }
1023     if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1024     {
1025         ERR("mismatch between C/S windows and converstation\n");
1026         goto theError;
1027     }
1028     if (pConv->instance != pInstance || pConv->instance == NULL)
1029     {
1030         ERR("mismatch in instances\n");
1031         goto theError;
1032     }
1033
1034     switch (iMsg)
1035     {
1036     case WM_DDE_INITIATE:
1037         FIXME("WM_DDE_INITIATE message received!\n");
1038         break;
1039
1040     case WM_DDE_REQUEST:
1041         pXAct = WDML_ServerQueueRequest(pConv, lParam);
1042         break;
1043
1044     case WM_DDE_ADVISE:
1045         pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1046         break;
1047
1048     case WM_DDE_UNADVISE:
1049         pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1050         break;
1051
1052     case WM_DDE_EXECUTE:
1053         pXAct = WDML_ServerQueueExecute(pConv, lParam);
1054         break;
1055
1056     case WM_DDE_POKE:
1057         pXAct = WDML_ServerQueuePoke(pConv, lParam);
1058         break;
1059
1060     case WM_DDE_TERMINATE:
1061         pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1062         break;
1063
1064     case WM_DDE_ACK:
1065         WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1066         break;
1067
1068     default:
1069         FIXME("Unsupported message %x\n", iMsg);
1070     }
1071
1072     if (pXAct)
1073     {
1074         pXAct->lParam = lParam;
1075         if (WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1076         {
1077             WDML_QueueTransaction(pConv, pXAct);
1078         }
1079         else
1080         {
1081             WDML_FreeTransaction(pInstance, pXAct, TRUE);
1082         }
1083     }
1084  theError:
1085     LeaveCriticalSection(&WDML_CritSect);
1086     return 0;
1087 }