ntdll: Use cpu_info to check for AVX availability.
[wine/zf.git] / dlls / user32 / dde_server.c
blobe11c0c20d3bdf21b70f6bbcee0597d4236cc3ba7
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 <wchar.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "dde.h"
35 #include "ddeml.h"
36 #include "win.h"
37 #include "wine/debug.h"
38 #include "dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 const char WDML_szServerConvClassA[] = "WineDdeServerConvA";
43 const WCHAR WDML_szServerConvClassW[] = L"WineDdeServerConvW";
45 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
46 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
48 /******************************************************************************
49 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
51 * PARAMS
52 * idInst [I] Instance identifier
53 * hszTopic [I] Handle to topic name string
54 * hszItem [I] Handle to item name string
56 * RETURNS
57 * Success: TRUE
58 * Failure: FALSE
60 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
62 WDML_INSTANCE* pInstance;
63 WDML_LINK* pLink;
64 HDDEDATA hDdeData;
65 HGLOBAL hItemData;
66 WDML_CONV* pConv;
67 ATOM atom;
68 UINT count;
70 TRACE("(%d,%p,%p)\n", idInst, hszTopic, hszItem);
72 pInstance = WDML_GetInstance(idInst);
74 if (pInstance == NULL)
75 return FALSE;
77 atom = WDML_MakeAtomFromHsz(hszItem);
78 if (!atom) return FALSE;
80 /* first compute the number of links which will trigger a message */
81 count = 0;
82 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
84 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
86 count++;
89 if (count >= CADV_LATEACK)
91 FIXME("too high value for count\n");
92 count &= 0xFFFF;
95 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
97 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
99 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
100 hszTopic, hszItem, 0, --count, 0);
102 if (hDdeData == CBR_BLOCK)
104 /* MS doc is not consistent here */
105 FIXME("CBR_BLOCK returned for ADVREQ\n");
106 continue;
108 if (hDdeData)
110 if (pLink->transactionType & XTYPF_NODATA)
112 TRACE("no data\n");
113 hItemData = 0;
115 else
117 TRACE("with data\n");
119 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
122 pConv = WDML_GetConv(pLink->hConv, TRUE);
124 if (pConv == NULL)
126 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
127 goto theError;
130 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
131 PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
133 ERR("post message failed\n");
134 pConv->wStatus &= ~ST_CONNECTED;
135 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
136 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
137 GlobalFree(hItemData);
138 goto theError;
140 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
144 return TRUE;
146 theError:
147 GlobalDeleteAtom(atom);
148 return FALSE;
152 /******************************************************************************
153 * DdeNameService [USER32.@] {Un}registers service name of DDE server
155 * PARAMS
156 * idInst [I] Instance identifier
157 * hsz1 [I] Handle to service name string
158 * hsz2 [I] Reserved
159 * afCmd [I] Service name flags
161 * RETURNS
162 * Success: Non-zero
163 * Failure: 0
165 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
167 WDML_SERVER* pServer;
168 WDML_INSTANCE* pInstance;
169 HWND hwndServer;
170 WNDCLASSEXW wndclass;
172 TRACE("(%d,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
174 /* First check instance
176 pInstance = WDML_GetInstance(idInst);
177 if (pInstance == NULL)
179 TRACE("Instance not found as initialised\n");
180 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
181 return NULL;
184 if (hsz2 != 0L)
186 /* Illegal, reserved parameter
188 pInstance->lastError = DMLERR_INVALIDPARAMETER;
189 WARN("Reserved parameter no-zero !!\n");
190 return NULL;
192 if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
194 /* don't know if we should check this but it makes sense
195 * why supply REGISTER or filter flags if de-registering all
197 TRACE("General unregister unexpected flags\n");
198 pInstance->lastError = DMLERR_INVALIDPARAMETER;
199 return NULL;
202 switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
204 case DNS_REGISTER:
205 pServer = WDML_FindServer(pInstance, hsz1, 0);
206 if (pServer)
208 ERR("Trying to register already registered service!\n");
209 pInstance->lastError = DMLERR_DLL_USAGE;
210 return NULL;
213 TRACE("Adding service name\n");
215 WDML_IncHSZ(pInstance, hsz1);
217 pServer = WDML_AddServer(pInstance, hsz1, 0);
219 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
220 pServer->atomService, pServer->atomServiceSpec);
222 wndclass.cbSize = sizeof(wndclass);
223 wndclass.style = 0;
224 wndclass.lpfnWndProc = WDML_ServerNameProc;
225 wndclass.cbClsExtra = 0;
226 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
227 wndclass.hInstance = 0;
228 wndclass.hIcon = 0;
229 wndclass.hCursor = 0;
230 wndclass.hbrBackground = 0;
231 wndclass.lpszMenuName = NULL;
232 wndclass.lpszClassName = L"WineDdeServerName";
233 wndclass.hIconSm = 0;
235 RegisterClassExW(&wndclass);
237 hwndServer = CreateWindowW(L"WineDdeServerName", NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
238 SetWindowLongPtrW(hwndServer, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
239 SetWindowLongPtrW(hwndServer, GWL_WDML_SERVER, (ULONG_PTR)pServer);
240 TRACE("Created nameServer=%p for instance=%08x\n", hwndServer, idInst);
242 pServer->hwndServer = hwndServer;
243 break;
245 case DNS_UNREGISTER:
246 if (hsz1 == 0L)
248 /* General unregister situation
249 * terminate all server side pending conversations
251 while (pInstance->servers)
252 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
253 pInstance->servers = NULL;
254 TRACE("General de-register - finished\n");
256 else
258 WDML_RemoveServer(pInstance, hsz1, 0L);
260 break;
263 if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
265 /* Set filter flags on to hold notifications of connection
267 pServer = WDML_FindServer(pInstance, hsz1, 0);
268 if (!pServer)
270 /* trying to filter where no service names !!
272 pInstance->lastError = DMLERR_DLL_USAGE;
273 return NULL;
275 else
277 pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
280 return (HDDEDATA)TRUE;
283 /******************************************************************
284 * WDML_CreateServerConv
288 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
289 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
291 HWND hwndServerConv;
292 WDML_CONV* pConv;
294 if (pInstance->unicode)
296 WNDCLASSEXW wndclass;
298 wndclass.cbSize = sizeof(wndclass);
299 wndclass.style = 0;
300 wndclass.lpfnWndProc = WDML_ServerConvProc;
301 wndclass.cbClsExtra = 0;
302 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
303 wndclass.hInstance = 0;
304 wndclass.hIcon = 0;
305 wndclass.hCursor = 0;
306 wndclass.hbrBackground = 0;
307 wndclass.lpszMenuName = NULL;
308 wndclass.lpszClassName = WDML_szServerConvClassW;
309 wndclass.hIconSm = 0;
311 RegisterClassExW(&wndclass);
313 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
314 WS_CHILD, 0, 0, 0, 0,
315 hwndServerName, 0, 0, 0);
317 else
319 WNDCLASSEXA wndclass;
321 wndclass.cbSize = sizeof(wndclass);
322 wndclass.style = 0;
323 wndclass.lpfnWndProc = WDML_ServerConvProc;
324 wndclass.cbClsExtra = 0;
325 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
326 wndclass.hInstance = 0;
327 wndclass.hIcon = 0;
328 wndclass.hCursor = 0;
329 wndclass.hbrBackground = 0;
330 wndclass.lpszMenuName = NULL;
331 wndclass.lpszClassName = WDML_szServerConvClassA;
332 wndclass.hIconSm = 0;
334 RegisterClassExA(&wndclass);
336 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
337 WS_CHILD, 0, 0, 0, 0,
338 hwndServerName, 0, 0, 0);
341 TRACE("Created convServer=%p (nameServer=%p) for instance=%08x unicode=%d\n",
342 hwndServerConv, hwndServerName, pInstance->instanceID, pInstance->unicode);
344 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
345 hwndClient, hwndServerConv);
346 if (pConv)
348 SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
349 SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
351 /* this should be the only place using SendMessage for WM_DDE_ACK */
352 /* note: sent messages shall not use packing */
353 SendMessageW(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
354 MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), WDML_MakeAtomFromHsz(hszTopic)));
355 /* we assume we're connected since we've sent an answer...
356 * I'm not sure what we can do... it doesn't look like the return value
357 * of SendMessage is used... sigh...
359 pConv->wStatus |= ST_CONNECTED;
361 else
363 DestroyWindow(hwndServerConv);
365 return pConv;
368 /******************************************************************
369 * WDML_ServerNameProc
373 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
375 HWND hwndClient;
376 HSZ hszApp, hszTop;
377 HDDEDATA hDdeData;
378 WDML_INSTANCE* pInstance;
379 UINT_PTR uiLo, uiHi;
381 switch (iMsg)
383 case WM_DDE_INITIATE:
385 /* wParam -- sending window handle
386 LOWORD(lParam) -- application atom
387 HIWORD(lParam) -- topic atom */
389 TRACE("WM_DDE_INITIATE message received!\n");
390 hwndClient = (HWND)wParam;
392 pInstance = WDML_GetInstanceFromWnd(hwndServer);
393 if (!pInstance) return 0;
394 TRACE("idInst=%d, threadID=0x%x\n", pInstance->instanceID, GetCurrentThreadId());
396 /* don't free DDEParams, since this is a broadcast */
397 UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
399 hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
400 hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
402 if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
404 BOOL self = FALSE;
405 CONVCONTEXT cc;
406 CONVCONTEXT* pcc = NULL;
407 WDML_CONV* pConv;
408 char buf[256];
410 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
411 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
413 self = TRUE;
415 /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
416 * handled under DDEML, and if so build a default context
418 if ((GetClassNameA(hwndClient, buf, sizeof(buf)) &&
419 lstrcmpiA(buf, WDML_szClientConvClassA) == 0) ||
420 (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
421 lstrcmpiW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
423 pcc = &cc;
424 memset(pcc, 0, sizeof(*pcc));
425 pcc->cb = sizeof(*pcc);
426 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
428 if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
430 TRACE("Don't do self connection as requested\n");
432 else if (hszApp && hszTop)
434 WDML_SERVER* pServer = (WDML_SERVER*)GetWindowLongPtrW(hwndServer, GWL_WDML_SERVER);
436 /* check filters for name service */
437 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
439 /* pass on to the callback */
440 hDdeData = WDML_InvokeCallback(pInstance, XTYP_CONNECT,
441 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
442 if ((ULONG_PTR)hDdeData)
444 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
445 hszApp, hszTop);
446 if (pConv)
448 if (pcc) pConv->wStatus |= ST_ISLOCAL;
449 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
450 hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
455 else if (pInstance->servers)
457 /* pass on to the callback */
458 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
459 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
461 if (hDdeData == CBR_BLOCK)
463 /* MS doc is not consistent here */
464 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
466 else if ((ULONG_PTR)hDdeData != 0)
468 HSZPAIR* hszp;
470 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
471 if (hszp)
473 int i;
474 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
476 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
477 hszp[i].hszSvc, hszp[i].hszTopic);
478 if (pConv)
480 if (pcc) pConv->wStatus |= ST_ISLOCAL;
481 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
482 hszp[i].hszTopic, hszp[i].hszSvc, 0, (ULONG_PTR)pcc, self);
485 DdeUnaccessData(hDdeData);
487 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
492 return 0;
494 case WM_DDE_REQUEST:
495 FIXME("WM_DDE_REQUEST message received!\n");
496 return 0;
497 case WM_DDE_ADVISE:
498 FIXME("WM_DDE_ADVISE message received!\n");
499 return 0;
500 case WM_DDE_UNADVISE:
501 FIXME("WM_DDE_UNADVISE message received!\n");
502 return 0;
503 case WM_DDE_EXECUTE:
504 FIXME("WM_DDE_EXECUTE message received!\n");
505 return 0;
506 case WM_DDE_POKE:
507 FIXME("WM_DDE_POKE message received!\n");
508 return 0;
509 case WM_DDE_TERMINATE:
510 FIXME("WM_DDE_TERMINATE message received!\n");
511 return 0;
512 default:
513 break;
516 return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
519 /******************************************************************
520 * WDML_ServerQueueRequest
524 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
526 UINT_PTR uiLo, uiHi;
527 WDML_XACT* pXAct;
529 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
531 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
532 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
533 if (pXAct) pXAct->atom = uiHi;
534 return pXAct;
537 /******************************************************************
538 * WDML_ServerHandleRequest
542 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
544 HDDEDATA hDdeData = 0;
545 BOOL fAck = TRUE;
547 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
550 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
551 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
554 switch ((ULONG_PTR)hDdeData)
556 case 0:
557 TRACE("No data returned from the Callback\n");
558 fAck = FALSE;
559 break;
561 case (ULONG_PTR)CBR_BLOCK:
562 return WDML_QS_BLOCK;
564 default:
566 HGLOBAL hMem = WDML_DataHandle2Global(hDdeData, TRUE, FALSE, FALSE, FALSE);
567 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
568 ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
569 (UINT_PTR)hMem, (UINT_PTR)pXAct->atom)))
571 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
572 DdeFreeDataHandle(hDdeData);
573 GlobalFree(hMem);
574 fAck = FALSE;
577 break;
580 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
582 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
584 return WDML_QS_HANDLED;
587 /******************************************************************
588 * WDML_ServerQueueAdvise
592 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
594 UINT_PTR uiLo, uiHi;
595 WDML_XACT* pXAct;
597 /* XTYP_ADVSTART transaction:
598 establish link and save link info to InstanceInfoTable */
600 if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
601 return NULL;
603 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
604 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
605 if (pXAct)
607 pXAct->hMem = (HGLOBAL)uiLo;
608 pXAct->atom = uiHi;
610 return pXAct;
613 /******************************************************************
614 * WDML_ServerHandleAdvise
618 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
620 UINT uType;
621 WDML_LINK* pLink;
622 DDEADVISE* pDdeAdvise;
623 HDDEDATA hDdeData = 0;
624 BOOL fAck = TRUE;
626 pDdeAdvise = GlobalLock(pXAct->hMem);
627 uType = XTYP_ADVSTART |
628 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
629 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
631 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
633 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
634 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
637 switch ((ULONG_PTR)hDdeData)
639 case 0:
640 TRACE("No data returned from the Callback\n");
641 fAck = FALSE;
642 break;
644 case (ULONG_PTR)CBR_BLOCK:
645 return WDML_QS_BLOCK;
647 default:
648 /* billx: first to see if the link is already created. */
649 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
650 pXAct->hszItem, TRUE, pDdeAdvise->cfFormat);
652 if (pLink != NULL)
654 /* we found a link, and only need to modify it in case it changes */
655 pLink->transactionType = uType;
657 else
659 TRACE("Adding Link with hConv %p\n", pConv);
660 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
661 uType, pXAct->hszItem, pDdeAdvise->cfFormat);
663 break;
666 GlobalUnlock(pXAct->hMem);
667 if (fAck)
669 GlobalFree(pXAct->hMem);
671 pXAct->hMem = 0;
673 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
675 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
677 return WDML_QS_HANDLED;
680 /******************************************************************
681 * WDML_ServerQueueUnadvise
685 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
687 UINT_PTR uiLo, uiHi;
688 WDML_XACT* pXAct;
690 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
692 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
693 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
694 if (pXAct) pXAct->atom = uiHi;
695 return pXAct;
698 /******************************************************************
699 * WDML_ServerHandleUnadvise
703 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
705 WDML_LINK* pLink;
707 if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
709 ERR("Unsupported yet options (null item or clipboard format)\n");
710 return WDML_QS_ERROR;
713 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
714 pXAct->hszItem, TRUE, pXAct->wFmt);
715 if (pLink == NULL)
717 ERR("Couldn't find link for %p, dropping request\n", pXAct->hszItem);
718 FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
719 return WDML_QS_ERROR;
722 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
724 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
725 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
728 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
729 pXAct->hszItem, pXAct->wFmt);
731 /* send back ack */
732 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
733 pXAct->lParam, WM_DDE_UNADVISE);
735 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
737 return WDML_QS_HANDLED;
740 /******************************************************************
741 * WDML_QueueExecute
745 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
747 WDML_XACT* pXAct;
749 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
750 if (pXAct)
752 pXAct->hMem = (HGLOBAL)lParam;
754 return pXAct;
757 static BOOL data_looks_unicode( const WCHAR *data, DWORD size )
759 DWORD i;
761 if (size % sizeof(WCHAR)) return FALSE;
762 for (i = 0; i < size / sizeof(WCHAR); i++) if (data[i] > 255) return FALSE;
763 return TRUE;
766 /* convert data to Unicode, unless it looks like it's already Unicode */
767 static HDDEDATA map_A_to_W( DWORD instance, void *ptr, DWORD size )
769 HDDEDATA ret;
770 DWORD len;
771 const char *end;
773 if (!data_looks_unicode( ptr, size ))
775 if ((end = memchr( ptr, 0, size ))) size = end + 1 - (const char *)ptr;
776 len = MultiByteToWideChar( CP_ACP, 0, ptr, size, NULL, 0 );
777 ret = DdeCreateDataHandle( instance, NULL, len * sizeof(WCHAR), 0, 0, CF_TEXT, 0);
778 MultiByteToWideChar( CP_ACP, 0, ptr, size, (WCHAR *)DdeAccessData(ret, NULL), len );
780 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
782 return ret;
785 /* convert data to ASCII, unless it looks like it's not in Unicode format */
786 static HDDEDATA map_W_to_A( DWORD instance, void *ptr, DWORD size )
788 HDDEDATA ret;
789 DWORD len;
790 const WCHAR *end;
792 if (data_looks_unicode( ptr, size ))
794 size /= sizeof(WCHAR);
795 if ((end = wmemchr( ptr, 0, size ))) size = end + 1 - (const WCHAR *)ptr;
796 len = WideCharToMultiByte( CP_ACP, 0, ptr, size, NULL, 0, NULL, NULL );
797 ret = DdeCreateDataHandle( instance, NULL, len, 0, 0, CF_TEXT, 0);
798 WideCharToMultiByte( CP_ACP, 0, ptr, size, (char *)DdeAccessData(ret, NULL), len, NULL, NULL );
800 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
802 return ret;
805 /******************************************************************
806 * WDML_ServerHandleExecute
810 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
812 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
813 BOOL fAck = FALSE, fBusy = FALSE;
815 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
817 LPVOID ptr = GlobalLock(pXAct->hMem);
818 DWORD size = GlobalSize(pXAct->hMem);
820 if (ptr)
822 if (pConv->instance->unicode) /* Unicode server, try to map A->W */
823 hDdeData = map_A_to_W( pConv->instance->instanceID, ptr, size );
824 else if (!IsWindowUnicode( pConv->hwndClient )) /* ASCII server and client, try to map W->A */
825 hDdeData = map_W_to_A( pConv->instance->instanceID, ptr, size );
826 else
827 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, ptr, size, 0, 0, CF_TEXT, 0);
828 GlobalUnlock(pXAct->hMem);
830 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
831 pConv->hszTopic, 0, hDdeData, 0L, 0L);
834 switch ((ULONG_PTR)hDdeData)
836 case (ULONG_PTR)CBR_BLOCK:
837 return WDML_QS_BLOCK;
839 case DDE_FACK:
840 fAck = TRUE;
841 break;
842 case DDE_FBUSY:
843 fBusy = TRUE;
844 break;
845 default:
846 FIXME("Unsupported returned value %p\n", hDdeData);
847 /* fall through */
848 case DDE_FNOTPROCESSED:
849 break;
851 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT_PTR)pXAct->hMem, 0, 0);
853 return WDML_QS_HANDLED;
856 /******************************************************************
857 * WDML_ServerQueuePoke
861 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
863 UINT_PTR uiLo, uiHi;
864 WDML_XACT* pXAct;
866 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
868 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
869 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
870 if (pXAct)
872 pXAct->atom = uiHi;
873 pXAct->hMem = (HGLOBAL)uiLo;
875 return pXAct;
878 /******************************************************************
879 * WDML_ServerHandlePoke
883 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
885 DDEPOKE* pDdePoke;
886 HDDEDATA hDdeData;
887 BOOL fBusy = FALSE, fAck = FALSE;
889 pDdePoke = GlobalLock(pXAct->hMem);
890 if (!pDdePoke)
892 return WDML_QS_ERROR;
895 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
897 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
898 GlobalSize(pXAct->hMem) - FIELD_OFFSET(DDEPOKE, Value),
899 0, 0, pDdePoke->cfFormat, 0);
900 if (hDdeData)
902 HDDEDATA hDdeDataOut;
904 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
905 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
906 hDdeData, 0, 0);
907 switch ((ULONG_PTR)hDdeDataOut)
909 case DDE_FACK:
910 fAck = TRUE;
911 break;
912 case DDE_FBUSY:
913 fBusy = TRUE;
914 break;
915 default:
916 FIXME("Unsupported returned value %p\n", hDdeDataOut);
917 /* fal through */
918 case DDE_FNOTPROCESSED:
919 break;
921 DdeFreeDataHandle(hDdeData);
924 GlobalUnlock(pXAct->hMem);
926 if (!fAck)
928 GlobalFree(pXAct->hMem);
930 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
932 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
934 return WDML_QS_HANDLED;
937 /******************************************************************
938 * WDML_ServerQueueTerminate
942 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
944 WDML_XACT* pXAct;
946 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
947 return pXAct;
950 /******************************************************************
951 * WDML_ServerHandleTerminate
955 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
957 /* billx: two things to remove: the conv, and associated links.
958 * Respond with another WM_DDE_TERMINATE iMsg.
960 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
962 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
963 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
965 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
966 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
968 return WDML_QS_HANDLED;
971 /******************************************************************
972 * WDML_ServerHandle
976 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
978 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
980 switch (pXAct->ddeMsg)
982 case WM_DDE_INITIATE:
983 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
984 break;
985 case WM_DDE_REQUEST:
986 qs = WDML_ServerHandleRequest(pConv, pXAct);
987 break;
989 case WM_DDE_ADVISE:
990 qs = WDML_ServerHandleAdvise(pConv, pXAct);
991 break;
993 case WM_DDE_UNADVISE:
994 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
995 break;
997 case WM_DDE_EXECUTE:
998 qs = WDML_ServerHandleExecute(pConv, pXAct);
999 break;
1001 case WM_DDE_POKE:
1002 qs = WDML_ServerHandlePoke(pConv, pXAct);
1003 break;
1005 case WM_DDE_TERMINATE:
1006 qs = WDML_ServerHandleTerminate(pConv, pXAct);
1007 break;
1009 case WM_DDE_ACK:
1010 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1011 break;
1013 default:
1014 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
1016 return qs;
1019 /******************************************************************
1020 * WDML_ServerConvProc
1024 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
1026 WDML_INSTANCE* pInstance;
1027 WDML_CONV* pConv;
1028 WDML_XACT* pXAct = NULL;
1030 TRACE("%p %04x %08lx %08lx\n", hwndServer, iMsg, wParam, lParam);
1032 if (iMsg == WM_DESTROY)
1034 pConv = WDML_GetConvFromWnd(hwndServer);
1035 if (pConv && !(pConv->wStatus & ST_TERMINATED))
1037 WDML_ServerHandleTerminate(pConv, NULL);
1040 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
1042 return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
1043 DefWindowProcA(hwndServer, iMsg, wParam, lParam);
1046 pInstance = WDML_GetInstanceFromWnd(hwndServer);
1047 pConv = WDML_GetConvFromWnd(hwndServer);
1049 if (!pConv)
1051 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1052 return 0;
1054 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1056 ERR("mismatch between C/S windows and conversation\n");
1057 return 0;
1059 if (pConv->instance != pInstance || pConv->instance == NULL)
1061 ERR("mismatch in instances\n");
1062 return 0;
1065 switch (iMsg)
1067 case WM_DDE_INITIATE:
1068 FIXME("WM_DDE_INITIATE message received!\n");
1069 break;
1071 case WM_DDE_REQUEST:
1072 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1073 break;
1075 case WM_DDE_ADVISE:
1076 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1077 break;
1079 case WM_DDE_UNADVISE:
1080 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1081 break;
1083 case WM_DDE_EXECUTE:
1084 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1085 break;
1087 case WM_DDE_POKE:
1088 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1089 break;
1091 case WM_DDE_TERMINATE:
1092 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1093 break;
1095 case WM_DDE_ACK:
1096 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1097 break;
1099 default:
1100 FIXME("Unsupported message %x\n", iMsg);
1101 break;
1104 if (pXAct)
1106 pXAct->lParam = lParam;
1108 if ((pConv->wStatus & ST_BLOCKED) || WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1110 TRACE("Transactions are blocked, add to the queue and exit\n");
1111 WDML_QueueTransaction(pConv, pXAct);
1113 else
1115 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1118 else
1119 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1121 return 0;