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