msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / user32 / dde_server.c
blob52adb9a8bd28ec0072f3f06ecd2e54762aafd42f
1 /*
2 * DDEML library
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
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.
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.
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
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
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};
44 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
45 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
47 /******************************************************************************
48 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
50 * PARAMS
51 * idInst [I] Instance identifier
52 * hszTopic [I] Handle to topic name string
53 * hszItem [I] Handle to item name string
55 * RETURNS
56 * Success: TRUE
57 * Failure: FALSE
59 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
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;
69 TRACE("(%d,%p,%p)\n", idInst, hszTopic, hszItem);
71 pInstance = WDML_GetInstance(idInst);
73 if (pInstance == NULL || pInstance->links == NULL)
74 return FALSE;
76 atom = WDML_MakeAtomFromHsz(hszItem);
77 if (!atom) return FALSE;
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)
83 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
85 count++;
88 if (count >= CADV_LATEACK)
90 FIXME("too high value for count\n");
91 count &= 0xFFFF;
94 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
96 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
98 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
99 hszTopic, hszItem, 0, --count, 0);
101 if (hDdeData == CBR_BLOCK)
103 /* MS doc is not consistent here */
104 FIXME("CBR_BLOCK returned for ADVREQ\n");
105 continue;
107 if (hDdeData)
109 if (pLink->transactionType & XTYPF_NODATA)
111 TRACE("no data\n");
112 hItemData = 0;
114 else
116 TRACE("with data\n");
118 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
121 pConv = WDML_GetConv(pLink->hConv, TRUE);
123 if (pConv == NULL)
125 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
126 goto theError;
129 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
130 PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
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;
139 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
143 return TRUE;
145 theError:
146 if (atom) GlobalDeleteAtom(atom);
147 return FALSE;
151 /******************************************************************************
152 * DdeNameService [USER32.@] {Un}registers service name of DDE server
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
160 * RETURNS
161 * Success: Non-zero
162 * Failure: 0
164 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
166 WDML_SERVER* pServer;
167 WDML_INSTANCE* pInstance;
168 HWND hwndServer;
169 WNDCLASSEXW wndclass;
171 TRACE("(%d,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
173 /* First check instance
175 pInstance = WDML_GetInstance(idInst);
176 if (pInstance == NULL)
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;
183 if (hsz2 != 0L)
185 /* Illegal, reserved parameter
187 pInstance->lastError = DMLERR_INVALIDPARAMETER;
188 WARN("Reserved parameter no-zero !!\n");
189 return NULL;
191 if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
193 /* don't know if we should check this but it makes sense
194 * why supply REGISTER or filter flags if de-registering all
196 TRACE("General unregister unexpected flags\n");
197 pInstance->lastError = DMLERR_INVALIDPARAMETER;
198 return NULL;
201 switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
203 case DNS_REGISTER:
204 pServer = WDML_FindServer(pInstance, hsz1, 0);
205 if (pServer)
207 ERR("Trying to register already registered service!\n");
208 pInstance->lastError = DMLERR_DLL_USAGE;
209 return NULL;
212 TRACE("Adding service name\n");
214 WDML_IncHSZ(pInstance, hsz1);
216 pServer = WDML_AddServer(pInstance, hsz1, 0);
218 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
219 pServer->atomService, pServer->atomServiceSpec);
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;
234 RegisterClassExW(&wndclass);
236 hwndServer = CreateWindowW(szServerNameClass, NULL,
237 WS_POPUP, 0, 0, 0, 0,
238 0, 0, 0, 0);
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);
244 pServer->hwndServer = hwndServer;
245 break;
247 case DNS_UNREGISTER:
248 if (hsz1 == 0L)
250 /* General unregister situation
251 * terminate all server side pending conversations
253 while (pInstance->servers)
254 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
255 pInstance->servers = NULL;
256 TRACE("General de-register - finished\n");
258 else
260 WDML_RemoveServer(pInstance, hsz1, 0L);
262 break;
265 if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
267 /* Set filter flags on to hold notifications of connection
269 pServer = WDML_FindServer(pInstance, hsz1, 0);
270 if (!pServer)
272 /* trying to filter where no service names !!
274 pInstance->lastError = DMLERR_DLL_USAGE;
275 return NULL;
277 else
279 pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
282 return (HDDEDATA)TRUE;
285 /******************************************************************
286 * WDML_CreateServerConv
290 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
291 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
293 HWND hwndServerConv;
294 WDML_CONV* pConv;
296 if (pInstance->unicode)
298 WNDCLASSEXW wndclass;
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;
313 RegisterClassExW(&wndclass);
315 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
316 WS_CHILD, 0, 0, 0, 0,
317 hwndServerName, 0, 0, 0);
319 else
321 WNDCLASSEXA wndclass;
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;
336 RegisterClassExA(&wndclass);
338 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
339 WS_CHILD, 0, 0, 0, 0,
340 hwndServerName, 0, 0, 0);
343 TRACE("Created convServer=%p (nameServer=%p) for instance=%08x unicode=%d\n",
344 hwndServerConv, hwndServerName, pInstance->instanceID, pInstance->unicode);
346 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
347 hwndClient, hwndServerConv);
348 if (pConv)
350 SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
351 SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
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...
361 pConv->wStatus |= ST_CONNECTED;
363 else
365 DestroyWindow(hwndServerConv);
367 return pConv;
370 /******************************************************************
371 * WDML_ServerNameProc
375 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
377 HWND hwndClient;
378 HSZ hszApp, hszTop;
379 HDDEDATA hDdeData = 0;
380 WDML_INSTANCE* pInstance;
381 UINT_PTR uiLo, uiHi;
383 switch (iMsg)
385 case WM_DDE_INITIATE:
387 /* wParam -- sending window handle
388 LOWORD(lParam) -- application atom
389 HIWORD(lParam) -- topic atom */
391 TRACE("WM_DDE_INITIATE message received!\n");
392 hwndClient = (HWND)wParam;
394 pInstance = WDML_GetInstanceFromWnd(hwndServer);
395 TRACE("idInst=%d, threadID=0x%x\n", pInstance->instanceID, GetCurrentThreadId());
396 if (!pInstance) return 0;
398 /* don't free DDEParams, since this is a broadcast */
399 UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
401 hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
402 hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
404 if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
406 BOOL self = FALSE;
407 CONVCONTEXT cc;
408 CONVCONTEXT* pcc = NULL;
409 WDML_CONV* pConv;
410 char buf[256];
412 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
413 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
415 self = TRUE;
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
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))
425 pcc = &cc;
426 memset(pcc, 0, sizeof(*pcc));
427 pcc->cb = sizeof(*pcc);
428 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
430 if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
432 TRACE("Don't do self connection as requested\n");
434 else if (hszApp && hszTop)
436 WDML_SERVER* pServer = (WDML_SERVER*)GetWindowLongPtrW(hwndServer, GWL_WDML_SERVER);
438 /* check filters for name service */
439 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
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)
446 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
447 hszApp, hszTop);
448 if (pConv)
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);
457 else if (pInstance->servers)
459 /* pass on to the callback */
460 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
461 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
463 if (hDdeData == CBR_BLOCK)
465 /* MS doc is not consistent here */
466 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
468 else if ((ULONG_PTR)hDdeData != 0)
470 HSZPAIR* hszp;
472 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
473 if (hszp)
475 int i;
476 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
478 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
479 hszp[i].hszSvc, hszp[i].hszTopic);
480 if (pConv)
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);
487 DdeUnaccessData(hDdeData);
489 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
494 return 0;
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;
518 return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
521 /******************************************************************
522 * WDML_ServerQueueRequest
526 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
528 UINT_PTR uiLo, uiHi;
529 WDML_XACT* pXAct;
531 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
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;
539 /******************************************************************
540 * WDML_ServerHandleRequest
544 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
546 HDDEDATA hDdeData = 0;
547 BOOL fAck = TRUE;
549 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
552 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
553 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
556 switch ((ULONG_PTR)hDdeData)
558 case 0:
559 TRACE("No data returned from the Callback\n");
560 fAck = FALSE;
561 break;
563 case (ULONG_PTR)CBR_BLOCK:
564 return WDML_QS_BLOCK;
566 default:
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)))
573 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
574 DdeFreeDataHandle(hDdeData);
575 GlobalFree(hMem);
576 fAck = FALSE;
579 break;
582 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
584 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
586 return WDML_QS_HANDLED;
589 /******************************************************************
590 * WDML_ServerQueueAdvise
594 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
596 UINT_PTR uiLo, uiHi;
597 WDML_XACT* pXAct;
599 /* XTYP_ADVSTART transaction:
600 establish link and save link info to InstanceInfoTable */
602 if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
603 return NULL;
605 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
606 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
607 if (pXAct)
609 pXAct->hMem = (HGLOBAL)uiLo;
610 pXAct->atom = uiHi;
612 return pXAct;
615 /******************************************************************
616 * WDML_ServerHandleAdvise
620 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
622 UINT uType;
623 WDML_LINK* pLink;
624 DDEADVISE* pDdeAdvise;
625 HDDEDATA hDdeData = 0;
626 BOOL fAck = TRUE;
628 pDdeAdvise = GlobalLock(pXAct->hMem);
629 uType = XTYP_ADVSTART |
630 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
631 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
633 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
635 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
636 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
639 switch ((ULONG_PTR)hDdeData)
641 case 0:
642 TRACE("No data returned from the Callback\n");
643 fAck = FALSE;
644 break;
646 case (ULONG_PTR)CBR_BLOCK:
647 return WDML_QS_BLOCK;
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);
654 if (pLink != NULL)
656 /* we found a link, and only need to modify it in case it changes */
657 pLink->transactionType = uType;
659 else
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);
665 break;
668 GlobalUnlock(pXAct->hMem);
669 if (fAck)
671 GlobalFree(pXAct->hMem);
673 pXAct->hMem = 0;
675 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
677 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
679 return WDML_QS_HANDLED;
682 /******************************************************************
683 * WDML_ServerQueueUnadvise
687 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
689 UINT_PTR uiLo, uiHi;
690 WDML_XACT* pXAct;
692 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
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;
700 /******************************************************************
701 * WDML_ServerHandleUnadvise
705 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
707 WDML_LINK* pLink;
709 if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
711 ERR("Unsupported yet options (null item or clipboard format)\n");
712 return WDML_QS_ERROR;
715 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
716 pXAct->hszItem, TRUE, pXAct->wFmt);
717 if (pLink == NULL)
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;
724 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
726 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
727 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
730 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
731 pXAct->hszItem, pXAct->wFmt);
733 /* send back ack */
734 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
735 pXAct->lParam, WM_DDE_UNADVISE);
737 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
739 return WDML_QS_HANDLED;
742 /******************************************************************
743 * WDML_QueueExecute
747 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
749 WDML_XACT* pXAct;
751 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
752 if (pXAct)
754 pXAct->hMem = (HGLOBAL)lParam;
756 return pXAct;
759 /******************************************************************
760 * WDML_ServerHandleExecute
764 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
766 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
767 BOOL fAck = FALSE, fBusy = FALSE;
769 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
771 LPVOID ptr = GlobalLock(pXAct->hMem);
773 if (ptr)
775 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, ptr, GlobalSize(pXAct->hMem),
776 0, 0, CF_TEXT, 0);
777 GlobalUnlock(pXAct->hMem);
779 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
780 pConv->hszTopic, 0, hDdeData, 0L, 0L);
783 switch ((ULONG_PTR)hDdeData)
785 case (ULONG_PTR)CBR_BLOCK:
786 return WDML_QS_BLOCK;
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;
800 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT_PTR)pXAct->hMem, 0, 0);
802 return WDML_QS_HANDLED;
805 /******************************************************************
806 * WDML_ServerQueuePoke
810 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
812 UINT_PTR uiLo, uiHi;
813 WDML_XACT* pXAct;
815 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
817 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
818 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
819 if (pXAct)
821 pXAct->atom = uiHi;
822 pXAct->hMem = (HGLOBAL)uiLo;
824 return pXAct;
827 /******************************************************************
828 * WDML_ServerHandlePoke
832 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
834 DDEPOKE* pDdePoke;
835 HDDEDATA hDdeData;
836 BOOL fBusy = FALSE, fAck = FALSE;
838 pDdePoke = GlobalLock(pXAct->hMem);
839 if (!pDdePoke)
841 return WDML_QS_ERROR;
844 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
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)
851 HDDEDATA hDdeDataOut;
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)
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;
870 DdeFreeDataHandle(hDdeData);
873 GlobalUnlock(pXAct->hMem);
875 if (!fAck)
877 GlobalFree(pXAct->hMem);
879 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
881 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
883 return WDML_QS_HANDLED;
886 /******************************************************************
887 * WDML_ServerQueueTerminate
891 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
893 WDML_XACT* pXAct;
895 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
896 return pXAct;
899 /******************************************************************
900 * WDML_ServerHandleTerminate
904 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
906 /* billx: two things to remove: the conv, and associated links.
907 * Respond with another WM_DDE_TERMINATE iMsg.
909 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
911 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
912 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
914 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
915 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
917 return WDML_QS_HANDLED;
920 /******************************************************************
921 * WDML_ServerHandle
925 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
927 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
929 switch (pXAct->ddeMsg)
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;
938 case WM_DDE_ADVISE:
939 qs = WDML_ServerHandleAdvise(pConv, pXAct);
940 break;
942 case WM_DDE_UNADVISE:
943 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
944 break;
946 case WM_DDE_EXECUTE:
947 qs = WDML_ServerHandleExecute(pConv, pXAct);
948 break;
950 case WM_DDE_POKE:
951 qs = WDML_ServerHandlePoke(pConv, pXAct);
952 break;
954 case WM_DDE_TERMINATE:
955 qs = WDML_ServerHandleTerminate(pConv, pXAct);
956 break;
958 case WM_DDE_ACK:
959 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
960 break;
962 default:
963 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
965 return qs;
968 /******************************************************************
969 * WDML_ServerConvProc
973 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
975 WDML_INSTANCE* pInstance;
976 WDML_CONV* pConv;
977 WDML_XACT* pXAct = NULL;
979 TRACE("%p %04x %08lx %08lx\n", hwndServer, iMsg, wParam, lParam);
981 if (iMsg == WM_DESTROY)
983 pConv = WDML_GetConvFromWnd(hwndServer);
984 if (pConv && !(pConv->wStatus & ST_TERMINATED))
986 WDML_ServerHandleTerminate(pConv, NULL);
989 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
991 return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
992 DefWindowProcA(hwndServer, iMsg, wParam, lParam);
995 pInstance = WDML_GetInstanceFromWnd(hwndServer);
996 pConv = WDML_GetConvFromWnd(hwndServer);
998 if (!pConv)
1000 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1001 return 0;
1003 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1005 ERR("mismatch between C/S windows and conversation\n");
1006 return 0;
1008 if (pConv->instance != pInstance || pConv->instance == NULL)
1010 ERR("mismatch in instances\n");
1011 return 0;
1014 switch (iMsg)
1016 case WM_DDE_INITIATE:
1017 FIXME("WM_DDE_INITIATE message received!\n");
1018 break;
1020 case WM_DDE_REQUEST:
1021 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1022 break;
1024 case WM_DDE_ADVISE:
1025 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1026 break;
1028 case WM_DDE_UNADVISE:
1029 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1030 break;
1032 case WM_DDE_EXECUTE:
1033 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1034 break;
1036 case WM_DDE_POKE:
1037 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1038 break;
1040 case WM_DDE_TERMINATE:
1041 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1042 break;
1044 case WM_DDE_ACK:
1045 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1046 break;
1048 default:
1049 FIXME("Unsupported message %x\n", iMsg);
1050 break;
1053 if (pXAct)
1055 pXAct->lParam = lParam;
1057 if ((pConv->wStatus & ST_BLOCKED) || WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1059 TRACE("Transactions are blocked, add to the queue and exit\n");
1060 WDML_QueueTransaction(pConv, pXAct);
1062 else
1064 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1067 else
1068 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1070 return 0;