mshtml: Added IHTMLWindow2::get_history implementation.
[wine/multimedia.git] / dlls / user32 / dde_server.c
blob9d834a847429efd40b3dbe796dfe15d199327106
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 "winnls.h"
33 #include "dde.h"
34 #include "ddeml.h"
35 #include "win.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38 #include "dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static const WCHAR szServerNameClass[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','N','a','m','e',0};
43 const char WDML_szServerConvClassA[] = "WineDdeServerConvA";
44 const WCHAR WDML_szServerConvClassW[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','C','o','n','v','W',0};
46 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
47 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
49 /******************************************************************************
50 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
52 * PARAMS
53 * idInst [I] Instance identifier
54 * hszTopic [I] Handle to topic name string
55 * hszItem [I] Handle to item name string
57 * RETURNS
58 * Success: TRUE
59 * Failure: FALSE
61 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
63 WDML_INSTANCE* pInstance = NULL;
64 WDML_LINK* pLink = NULL;
65 HDDEDATA hDdeData = 0;
66 HGLOBAL hItemData = 0;
67 WDML_CONV* pConv = NULL;
68 ATOM atom = 0;
69 UINT count;
71 TRACE("(%d,%p,%p)\n", idInst, hszTopic, hszItem);
73 pInstance = WDML_GetInstance(idInst);
75 if (pInstance == NULL)
76 return FALSE;
78 atom = WDML_MakeAtomFromHsz(hszItem);
79 if (!atom) return FALSE;
81 /* first compute the number of links which will trigger a message */
82 count = 0;
83 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
85 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
87 count++;
90 if (count >= CADV_LATEACK)
92 FIXME("too high value for count\n");
93 count &= 0xFFFF;
96 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
98 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
100 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
101 hszTopic, hszItem, 0, --count, 0);
103 if (hDdeData == CBR_BLOCK)
105 /* MS doc is not consistent here */
106 FIXME("CBR_BLOCK returned for ADVREQ\n");
107 continue;
109 if (hDdeData)
111 if (pLink->transactionType & XTYPF_NODATA)
113 TRACE("no data\n");
114 hItemData = 0;
116 else
118 TRACE("with data\n");
120 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
123 pConv = WDML_GetConv(pLink->hConv, TRUE);
125 if (pConv == NULL)
127 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
128 goto theError;
131 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
132 PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
134 ERR("post message failed\n");
135 pConv->wStatus &= ~ST_CONNECTED;
136 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
137 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
138 GlobalFree(hItemData);
139 goto theError;
141 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
145 return TRUE;
147 theError:
148 GlobalDeleteAtom(atom);
149 return FALSE;
153 /******************************************************************************
154 * DdeNameService [USER32.@] {Un}registers service name of DDE server
156 * PARAMS
157 * idInst [I] Instance identifier
158 * hsz1 [I] Handle to service name string
159 * hsz2 [I] Reserved
160 * afCmd [I] Service name flags
162 * RETURNS
163 * Success: Non-zero
164 * Failure: 0
166 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
168 WDML_SERVER* pServer;
169 WDML_INSTANCE* pInstance;
170 HWND hwndServer;
171 WNDCLASSEXW wndclass;
173 TRACE("(%d,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
175 /* First check instance
177 pInstance = WDML_GetInstance(idInst);
178 if (pInstance == NULL)
180 TRACE("Instance not found as initialised\n");
181 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
182 return NULL;
185 if (hsz2 != 0L)
187 /* Illegal, reserved parameter
189 pInstance->lastError = DMLERR_INVALIDPARAMETER;
190 WARN("Reserved parameter no-zero !!\n");
191 return NULL;
193 if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
195 /* don't know if we should check this but it makes sense
196 * why supply REGISTER or filter flags if de-registering all
198 TRACE("General unregister unexpected flags\n");
199 pInstance->lastError = DMLERR_INVALIDPARAMETER;
200 return NULL;
203 switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
205 case DNS_REGISTER:
206 pServer = WDML_FindServer(pInstance, hsz1, 0);
207 if (pServer)
209 ERR("Trying to register already registered service!\n");
210 pInstance->lastError = DMLERR_DLL_USAGE;
211 return NULL;
214 TRACE("Adding service name\n");
216 WDML_IncHSZ(pInstance, hsz1);
218 pServer = WDML_AddServer(pInstance, hsz1, 0);
220 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
221 pServer->atomService, pServer->atomServiceSpec);
223 wndclass.cbSize = sizeof(wndclass);
224 wndclass.style = 0;
225 wndclass.lpfnWndProc = WDML_ServerNameProc;
226 wndclass.cbClsExtra = 0;
227 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
228 wndclass.hInstance = 0;
229 wndclass.hIcon = 0;
230 wndclass.hCursor = 0;
231 wndclass.hbrBackground = 0;
232 wndclass.lpszMenuName = NULL;
233 wndclass.lpszClassName = szServerNameClass;
234 wndclass.hIconSm = 0;
236 RegisterClassExW(&wndclass);
238 hwndServer = CreateWindowW(szServerNameClass, NULL,
239 WS_POPUP, 0, 0, 0, 0,
240 0, 0, 0, 0);
242 SetWindowLongPtrW(hwndServer, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
243 SetWindowLongPtrW(hwndServer, GWL_WDML_SERVER, (ULONG_PTR)pServer);
244 TRACE("Created nameServer=%p for instance=%08x\n", hwndServer, idInst);
246 pServer->hwndServer = hwndServer;
247 break;
249 case DNS_UNREGISTER:
250 if (hsz1 == 0L)
252 /* General unregister situation
253 * terminate all server side pending conversations
255 while (pInstance->servers)
256 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
257 pInstance->servers = NULL;
258 TRACE("General de-register - finished\n");
260 else
262 WDML_RemoveServer(pInstance, hsz1, 0L);
264 break;
267 if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
269 /* Set filter flags on to hold notifications of connection
271 pServer = WDML_FindServer(pInstance, hsz1, 0);
272 if (!pServer)
274 /* trying to filter where no service names !!
276 pInstance->lastError = DMLERR_DLL_USAGE;
277 return NULL;
279 else
281 pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
284 return (HDDEDATA)TRUE;
287 /******************************************************************
288 * WDML_CreateServerConv
292 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
293 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
295 HWND hwndServerConv;
296 WDML_CONV* pConv;
298 if (pInstance->unicode)
300 WNDCLASSEXW wndclass;
302 wndclass.cbSize = sizeof(wndclass);
303 wndclass.style = 0;
304 wndclass.lpfnWndProc = WDML_ServerConvProc;
305 wndclass.cbClsExtra = 0;
306 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
307 wndclass.hInstance = 0;
308 wndclass.hIcon = 0;
309 wndclass.hCursor = 0;
310 wndclass.hbrBackground = 0;
311 wndclass.lpszMenuName = NULL;
312 wndclass.lpszClassName = WDML_szServerConvClassW;
313 wndclass.hIconSm = 0;
315 RegisterClassExW(&wndclass);
317 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
318 WS_CHILD, 0, 0, 0, 0,
319 hwndServerName, 0, 0, 0);
321 else
323 WNDCLASSEXA wndclass;
325 wndclass.cbSize = sizeof(wndclass);
326 wndclass.style = 0;
327 wndclass.lpfnWndProc = WDML_ServerConvProc;
328 wndclass.cbClsExtra = 0;
329 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
330 wndclass.hInstance = 0;
331 wndclass.hIcon = 0;
332 wndclass.hCursor = 0;
333 wndclass.hbrBackground = 0;
334 wndclass.lpszMenuName = NULL;
335 wndclass.lpszClassName = WDML_szServerConvClassA;
336 wndclass.hIconSm = 0;
338 RegisterClassExA(&wndclass);
340 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
341 WS_CHILD, 0, 0, 0, 0,
342 hwndServerName, 0, 0, 0);
345 TRACE("Created convServer=%p (nameServer=%p) for instance=%08x unicode=%d\n",
346 hwndServerConv, hwndServerName, pInstance->instanceID, pInstance->unicode);
348 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
349 hwndClient, hwndServerConv);
350 if (pConv)
352 SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
353 SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
355 /* this should be the only place using SendMessage for WM_DDE_ACK */
356 /* note: sent messages shall not use packing */
357 SendMessageW(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
358 MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), WDML_MakeAtomFromHsz(hszTopic)));
359 /* we assume we're connected since we've sent an answer...
360 * I'm not sure what we can do... it doesn't look like the return value
361 * of SendMessage is used... sigh...
363 pConv->wStatus |= ST_CONNECTED;
365 else
367 DestroyWindow(hwndServerConv);
369 return pConv;
372 /******************************************************************
373 * WDML_ServerNameProc
377 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
379 HWND hwndClient;
380 HSZ hszApp, hszTop;
381 HDDEDATA hDdeData = 0;
382 WDML_INSTANCE* pInstance;
383 UINT_PTR uiLo, uiHi;
385 switch (iMsg)
387 case WM_DDE_INITIATE:
389 /* wParam -- sending window handle
390 LOWORD(lParam) -- application atom
391 HIWORD(lParam) -- topic atom */
393 TRACE("WM_DDE_INITIATE message received!\n");
394 hwndClient = (HWND)wParam;
396 pInstance = WDML_GetInstanceFromWnd(hwndServer);
397 if (!pInstance) return 0;
398 TRACE("idInst=%d, threadID=0x%x\n", pInstance->instanceID, GetCurrentThreadId());
400 /* don't free DDEParams, since this is a broadcast */
401 UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
403 hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
404 hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
406 if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
408 BOOL self = FALSE;
409 CONVCONTEXT cc;
410 CONVCONTEXT* pcc = NULL;
411 WDML_CONV* pConv;
412 char buf[256];
414 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
415 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
417 self = TRUE;
419 /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
420 * handled under DDEML, and if so build a default context
422 if ((GetClassNameA(hwndClient, buf, sizeof(buf)) &&
423 lstrcmpiA(buf, WDML_szClientConvClassA) == 0) ||
424 (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
425 lstrcmpiW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
427 pcc = &cc;
428 memset(pcc, 0, sizeof(*pcc));
429 pcc->cb = sizeof(*pcc);
430 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
432 if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
434 TRACE("Don't do self connection as requested\n");
436 else if (hszApp && hszTop)
438 WDML_SERVER* pServer = (WDML_SERVER*)GetWindowLongPtrW(hwndServer, GWL_WDML_SERVER);
440 /* check filters for name service */
441 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
443 /* pass on to the callback */
444 hDdeData = WDML_InvokeCallback(pInstance, XTYP_CONNECT,
445 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
446 if ((ULONG_PTR)hDdeData)
448 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
449 hszApp, hszTop);
450 if (pConv)
452 if (pcc) pConv->wStatus |= ST_ISLOCAL;
453 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
454 hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
459 else if (pInstance->servers)
461 /* pass on to the callback */
462 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
463 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
465 if (hDdeData == CBR_BLOCK)
467 /* MS doc is not consistent here */
468 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
470 else if ((ULONG_PTR)hDdeData != 0)
472 HSZPAIR* hszp;
474 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
475 if (hszp)
477 int i;
478 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
480 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
481 hszp[i].hszSvc, hszp[i].hszTopic);
482 if (pConv)
484 if (pcc) pConv->wStatus |= ST_ISLOCAL;
485 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
486 hszp[i].hszTopic, hszp[i].hszSvc, 0, (ULONG_PTR)pcc, self);
489 DdeUnaccessData(hDdeData);
491 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
496 return 0;
498 case WM_DDE_REQUEST:
499 FIXME("WM_DDE_REQUEST message received!\n");
500 return 0;
501 case WM_DDE_ADVISE:
502 FIXME("WM_DDE_ADVISE message received!\n");
503 return 0;
504 case WM_DDE_UNADVISE:
505 FIXME("WM_DDE_UNADVISE message received!\n");
506 return 0;
507 case WM_DDE_EXECUTE:
508 FIXME("WM_DDE_EXECUTE message received!\n");
509 return 0;
510 case WM_DDE_POKE:
511 FIXME("WM_DDE_POKE message received!\n");
512 return 0;
513 case WM_DDE_TERMINATE:
514 FIXME("WM_DDE_TERMINATE message received!\n");
515 return 0;
516 default:
517 break;
520 return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
523 /******************************************************************
524 * WDML_ServerQueueRequest
528 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
530 UINT_PTR uiLo, uiHi;
531 WDML_XACT* pXAct;
533 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
535 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
536 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
537 if (pXAct) pXAct->atom = uiHi;
538 return pXAct;
541 /******************************************************************
542 * WDML_ServerHandleRequest
546 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
548 HDDEDATA hDdeData = 0;
549 BOOL fAck = TRUE;
551 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
554 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
555 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
558 switch ((ULONG_PTR)hDdeData)
560 case 0:
561 TRACE("No data returned from the Callback\n");
562 fAck = FALSE;
563 break;
565 case (ULONG_PTR)CBR_BLOCK:
566 return WDML_QS_BLOCK;
568 default:
570 HGLOBAL hMem = WDML_DataHandle2Global(hDdeData, TRUE, FALSE, FALSE, FALSE);
571 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
572 ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
573 (UINT_PTR)hMem, (UINT_PTR)pXAct->atom)))
575 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
576 DdeFreeDataHandle(hDdeData);
577 GlobalFree(hMem);
578 fAck = FALSE;
581 break;
584 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
586 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
588 return WDML_QS_HANDLED;
591 /******************************************************************
592 * WDML_ServerQueueAdvise
596 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
598 UINT_PTR uiLo, uiHi;
599 WDML_XACT* pXAct;
601 /* XTYP_ADVSTART transaction:
602 establish link and save link info to InstanceInfoTable */
604 if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
605 return NULL;
607 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
608 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
609 if (pXAct)
611 pXAct->hMem = (HGLOBAL)uiLo;
612 pXAct->atom = uiHi;
614 return pXAct;
617 /******************************************************************
618 * WDML_ServerHandleAdvise
622 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
624 UINT uType;
625 WDML_LINK* pLink;
626 DDEADVISE* pDdeAdvise;
627 HDDEDATA hDdeData = 0;
628 BOOL fAck = TRUE;
630 pDdeAdvise = GlobalLock(pXAct->hMem);
631 uType = XTYP_ADVSTART |
632 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
633 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
635 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
637 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
638 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
641 switch ((ULONG_PTR)hDdeData)
643 case 0:
644 TRACE("No data returned from the Callback\n");
645 fAck = FALSE;
646 break;
648 case (ULONG_PTR)CBR_BLOCK:
649 return WDML_QS_BLOCK;
651 default:
652 /* billx: first to see if the link is already created. */
653 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
654 pXAct->hszItem, TRUE, pDdeAdvise->cfFormat);
656 if (pLink != NULL)
658 /* we found a link, and only need to modify it in case it changes */
659 pLink->transactionType = uType;
661 else
663 TRACE("Adding Link with hConv %p\n", pConv);
664 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
665 uType, pXAct->hszItem, pDdeAdvise->cfFormat);
667 break;
670 GlobalUnlock(pXAct->hMem);
671 if (fAck)
673 GlobalFree(pXAct->hMem);
675 pXAct->hMem = 0;
677 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
679 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
681 return WDML_QS_HANDLED;
684 /******************************************************************
685 * WDML_ServerQueueUnadvise
689 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
691 UINT_PTR uiLo, uiHi;
692 WDML_XACT* pXAct;
694 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
696 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
697 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
698 if (pXAct) pXAct->atom = uiHi;
699 return pXAct;
702 /******************************************************************
703 * WDML_ServerHandleUnadvise
707 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
709 WDML_LINK* pLink;
711 if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
713 ERR("Unsupported yet options (null item or clipboard format)\n");
714 return WDML_QS_ERROR;
717 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
718 pXAct->hszItem, TRUE, pXAct->wFmt);
719 if (pLink == NULL)
721 ERR("Couldn't find link for %p, dropping request\n", pXAct->hszItem);
722 FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
723 return WDML_QS_ERROR;
726 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
728 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
729 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
732 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
733 pXAct->hszItem, pXAct->wFmt);
735 /* send back ack */
736 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
737 pXAct->lParam, WM_DDE_UNADVISE);
739 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
741 return WDML_QS_HANDLED;
744 /******************************************************************
745 * WDML_QueueExecute
749 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
751 WDML_XACT* pXAct;
753 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
754 if (pXAct)
756 pXAct->hMem = (HGLOBAL)lParam;
758 return pXAct;
761 static BOOL data_looks_unicode( const WCHAR *data, DWORD size )
763 DWORD i;
765 if (size % sizeof(WCHAR)) return FALSE;
766 for (i = 0; i < size / sizeof(WCHAR); i++) if (data[i] > 255) return FALSE;
767 return TRUE;
770 /* convert data to Unicode, unless it looks like it's already Unicode */
771 static HDDEDATA map_A_to_W( DWORD instance, void *ptr, DWORD size )
773 HDDEDATA ret;
774 DWORD len;
775 const char *end;
777 if (!data_looks_unicode( ptr, size ))
779 if ((end = memchr( ptr, 0, size ))) size = end + 1 - (const char *)ptr;
780 len = MultiByteToWideChar( CP_ACP, 0, ptr, size, NULL, 0 );
781 ret = DdeCreateDataHandle( instance, NULL, len * sizeof(WCHAR), 0, 0, CF_TEXT, 0);
782 MultiByteToWideChar( CP_ACP, 0, ptr, size, (WCHAR *)DdeAccessData(ret, NULL), len );
784 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
786 return ret;
789 /* convert data to ASCII, unless it looks like it's not in Unicode format */
790 static HDDEDATA map_W_to_A( DWORD instance, void *ptr, DWORD size )
792 HDDEDATA ret;
793 DWORD len;
794 const WCHAR *end;
796 if (data_looks_unicode( ptr, size ))
798 size /= sizeof(WCHAR);
799 if ((end = memchrW( ptr, 0, size ))) size = end + 1 - (const WCHAR *)ptr;
800 len = WideCharToMultiByte( CP_ACP, 0, ptr, size, NULL, 0, NULL, NULL );
801 ret = DdeCreateDataHandle( instance, NULL, len, 0, 0, CF_TEXT, 0);
802 WideCharToMultiByte( CP_ACP, 0, ptr, size, (char *)DdeAccessData(ret, NULL), len, NULL, NULL );
804 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
806 return ret;
809 /******************************************************************
810 * WDML_ServerHandleExecute
814 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
816 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
817 BOOL fAck = FALSE, fBusy = FALSE;
819 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
821 LPVOID ptr = GlobalLock(pXAct->hMem);
822 DWORD size = GlobalSize(pXAct->hMem);
824 if (ptr)
826 if (pConv->instance->unicode) /* Unicode server, try to map A->W */
827 hDdeData = map_A_to_W( pConv->instance->instanceID, ptr, size );
828 else if (!IsWindowUnicode( pConv->hwndClient )) /* ASCII server and client, try to map W->A */
829 hDdeData = map_W_to_A( pConv->instance->instanceID, ptr, size );
830 else
831 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, ptr, size, 0, 0, CF_TEXT, 0);
832 GlobalUnlock(pXAct->hMem);
834 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
835 pConv->hszTopic, 0, hDdeData, 0L, 0L);
838 switch ((ULONG_PTR)hDdeData)
840 case (ULONG_PTR)CBR_BLOCK:
841 return WDML_QS_BLOCK;
843 case DDE_FACK:
844 fAck = TRUE;
845 break;
846 case DDE_FBUSY:
847 fBusy = TRUE;
848 break;
849 default:
850 FIXME("Unsupported returned value %p\n", hDdeData);
851 /* fall through */
852 case DDE_FNOTPROCESSED:
853 break;
855 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT_PTR)pXAct->hMem, 0, 0);
857 return WDML_QS_HANDLED;
860 /******************************************************************
861 * WDML_ServerQueuePoke
865 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
867 UINT_PTR uiLo, uiHi;
868 WDML_XACT* pXAct;
870 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
872 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
873 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
874 if (pXAct)
876 pXAct->atom = uiHi;
877 pXAct->hMem = (HGLOBAL)uiLo;
879 return pXAct;
882 /******************************************************************
883 * WDML_ServerHandlePoke
887 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
889 DDEPOKE* pDdePoke;
890 HDDEDATA hDdeData;
891 BOOL fBusy = FALSE, fAck = FALSE;
893 pDdePoke = GlobalLock(pXAct->hMem);
894 if (!pDdePoke)
896 return WDML_QS_ERROR;
899 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
901 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
902 GlobalSize(pXAct->hMem) - FIELD_OFFSET(DDEPOKE, Value),
903 0, 0, pDdePoke->cfFormat, 0);
904 if (hDdeData)
906 HDDEDATA hDdeDataOut;
908 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
909 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
910 hDdeData, 0, 0);
911 switch ((ULONG_PTR)hDdeDataOut)
913 case DDE_FACK:
914 fAck = TRUE;
915 break;
916 case DDE_FBUSY:
917 fBusy = TRUE;
918 break;
919 default:
920 FIXME("Unsupported returned value %p\n", hDdeDataOut);
921 /* fal through */
922 case DDE_FNOTPROCESSED:
923 break;
925 DdeFreeDataHandle(hDdeData);
928 GlobalUnlock(pXAct->hMem);
930 if (!fAck)
932 GlobalFree(pXAct->hMem);
934 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
936 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
938 return WDML_QS_HANDLED;
941 /******************************************************************
942 * WDML_ServerQueueTerminate
946 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
948 WDML_XACT* pXAct;
950 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
951 return pXAct;
954 /******************************************************************
955 * WDML_ServerHandleTerminate
959 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
961 /* billx: two things to remove: the conv, and associated links.
962 * Respond with another WM_DDE_TERMINATE iMsg.
964 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
966 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
967 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
969 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
970 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
972 return WDML_QS_HANDLED;
975 /******************************************************************
976 * WDML_ServerHandle
980 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
982 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
984 switch (pXAct->ddeMsg)
986 case WM_DDE_INITIATE:
987 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
988 break;
989 case WM_DDE_REQUEST:
990 qs = WDML_ServerHandleRequest(pConv, pXAct);
991 break;
993 case WM_DDE_ADVISE:
994 qs = WDML_ServerHandleAdvise(pConv, pXAct);
995 break;
997 case WM_DDE_UNADVISE:
998 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
999 break;
1001 case WM_DDE_EXECUTE:
1002 qs = WDML_ServerHandleExecute(pConv, pXAct);
1003 break;
1005 case WM_DDE_POKE:
1006 qs = WDML_ServerHandlePoke(pConv, pXAct);
1007 break;
1009 case WM_DDE_TERMINATE:
1010 qs = WDML_ServerHandleTerminate(pConv, pXAct);
1011 break;
1013 case WM_DDE_ACK:
1014 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1015 break;
1017 default:
1018 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
1020 return qs;
1023 /******************************************************************
1024 * WDML_ServerConvProc
1028 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
1030 WDML_INSTANCE* pInstance;
1031 WDML_CONV* pConv;
1032 WDML_XACT* pXAct = NULL;
1034 TRACE("%p %04x %08lx %08lx\n", hwndServer, iMsg, wParam, lParam);
1036 if (iMsg == WM_DESTROY)
1038 pConv = WDML_GetConvFromWnd(hwndServer);
1039 if (pConv && !(pConv->wStatus & ST_TERMINATED))
1041 WDML_ServerHandleTerminate(pConv, NULL);
1044 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
1046 return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
1047 DefWindowProcA(hwndServer, iMsg, wParam, lParam);
1050 pInstance = WDML_GetInstanceFromWnd(hwndServer);
1051 pConv = WDML_GetConvFromWnd(hwndServer);
1053 if (!pConv)
1055 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1056 return 0;
1058 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1060 ERR("mismatch between C/S windows and conversation\n");
1061 return 0;
1063 if (pConv->instance != pInstance || pConv->instance == NULL)
1065 ERR("mismatch in instances\n");
1066 return 0;
1069 switch (iMsg)
1071 case WM_DDE_INITIATE:
1072 FIXME("WM_DDE_INITIATE message received!\n");
1073 break;
1075 case WM_DDE_REQUEST:
1076 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1077 break;
1079 case WM_DDE_ADVISE:
1080 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1081 break;
1083 case WM_DDE_UNADVISE:
1084 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1085 break;
1087 case WM_DDE_EXECUTE:
1088 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1089 break;
1091 case WM_DDE_POKE:
1092 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1093 break;
1095 case WM_DDE_TERMINATE:
1096 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1097 break;
1099 case WM_DDE_ACK:
1100 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1101 break;
1103 default:
1104 FIXME("Unsupported message %x\n", iMsg);
1105 break;
1108 if (pXAct)
1110 pXAct->lParam = lParam;
1112 if ((pConv->wStatus & ST_BLOCKED) || WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1114 TRACE("Transactions are blocked, add to the queue and exit\n");
1115 WDML_QueueTransaction(pConv, pXAct);
1117 else
1119 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1122 else
1123 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1125 return 0;