Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / user / dde / client.c
blob0f6295e671e336aaa95f4e8cf9c72366d7b54673
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * DDEML library
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
9 * Copyright 2000 Corel
10 * Copyright 2001 Eric Pouech
11 * Copyright 2004, 2005 Dmitry Timoshkov
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <stdarg.h>
29 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "winnls.h"
36 #include "dde.h"
37 #include "ddeml.h"
38 #include "win.h"
39 #include "wine/debug.h"
40 #include "dde/dde_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
44 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
45 const WCHAR WDML_szClientConvClass[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t',0};
47 /******************************************************************************
48 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
50 * PARAMS
51 * idInst [I] Instance identifier
52 * hszService [I] Handle to service name string
53 * hszTopic [I] Handle to topic name string
54 * hConvList [I] Handle to conversation list
55 * pCC [I] Pointer to structure with context data
57 * RETURNS
58 * Success: Handle to new conversation list
59 * Failure: 0
61 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
62 HCONVLIST hConvList, PCONVCONTEXT pCC)
64 FIXME("(%ld,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
65 return (HCONVLIST)1;
68 /*****************************************************************
69 * DdeQueryNextServer [USER32.@]
71 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
73 FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
74 return 0;
77 /******************************************************************************
78 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
81 * PARAMS
82 * hConvList [I] Handle to conversation list
84 * RETURNS
85 * Success: TRUE
86 * Failure: FALSE
88 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
90 FIXME("(%p): stub\n", hConvList);
91 return TRUE;
94 /*****************************************************************
95 * DdeConnect (USER32.@)
97 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
98 PCONVCONTEXT pCC)
100 HWND hwndClient;
101 WDML_INSTANCE* pInstance;
102 WDML_CONV* pConv = NULL;
103 ATOM aSrv = 0, aTpc = 0;
104 WNDCLASSEXW wndclass;
106 TRACE("(0x%lx,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
108 EnterCriticalSection(&WDML_CritSect);
110 pInstance = WDML_GetInstance(idInst);
111 if (!pInstance)
113 goto theEnd;
116 /* make sure this conv is never created */
117 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
118 if (pConv != NULL)
120 ERR("This Conv already exists: (%p)\n", pConv);
121 goto theEnd;
124 /* we need to establish a conversation with
125 server, so create a window for it */
127 wndclass.cbSize = sizeof(wndclass);
128 wndclass.style = 0;
129 wndclass.lpfnWndProc = WDML_ClientProc;
130 wndclass.cbClsExtra = 0;
131 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
132 wndclass.hInstance = 0;
133 wndclass.hIcon = 0;
134 wndclass.hCursor = 0;
135 wndclass.hbrBackground = 0;
136 wndclass.lpszMenuName = NULL;
137 wndclass.lpszClassName = WDML_szClientConvClass;
138 wndclass.hIconSm = 0;
140 RegisterClassExW(&wndclass);
142 hwndClient = CreateWindowW(WDML_szClientConvClass, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
144 SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
146 if (hszService)
148 aSrv = WDML_MakeAtomFromHsz(hszService);
149 if (!aSrv) goto theEnd;
151 if (hszTopic)
153 aTpc = WDML_MakeAtomFromHsz(hszTopic);
154 if (!aTpc) goto theEnd;
157 LeaveCriticalSection(&WDML_CritSect);
159 /* note: sent messages shall not use packing */
160 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
161 SMTO_ABORTIFHUNG, 2000, NULL );
163 EnterCriticalSection(&WDML_CritSect);
165 pInstance = WDML_GetInstance(idInst);
166 if (!pInstance)
168 goto theEnd;
171 /* At this point, Client WM_DDE_ACK should have saved hwndServer
172 for this instance id and hwndClient if server responds.
173 So get HCONV and return it. And add it to conv list */
174 pConv = WDML_GetConvFromWnd(hwndClient);
175 if (pConv == NULL || pConv->hwndServer == 0)
177 ERR("Done with INITIATE, but no Server window available\n");
178 pConv = NULL;
179 goto theEnd;
181 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
182 pConv->wConvst = XST_CONNECTED;
184 /* finish init of pConv */
185 if (pCC != NULL)
187 pConv->convContext = *pCC;
189 else
191 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
192 pConv->convContext.cb = sizeof(pConv->convContext);
193 pConv->convContext.iCodePage = CP_WINUNICODE;
196 theEnd:
197 LeaveCriticalSection(&WDML_CritSect);
199 if (aSrv) GlobalDeleteAtom(aSrv);
200 if (aTpc) GlobalDeleteAtom(aTpc);
201 return (HCONV)pConv;
204 /*****************************************************************
205 * DdeReconnect (DDEML.37)
206 * DdeReconnect (USER32.@)
208 HCONV WINAPI DdeReconnect(HCONV hConv)
210 WDML_CONV* pConv;
211 WDML_CONV* pNewConv = NULL;
212 ATOM aSrv = 0, aTpc = 0;
214 TRACE("(%p)\n", hConv);
216 EnterCriticalSection(&WDML_CritSect);
217 pConv = WDML_GetConv(hConv, FALSE);
218 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
220 BOOL ret;
222 /* to reestablist a connection, we have to make sure that:
223 * 1/ pConv is the converstation attached to the client window (it wouldn't be
224 * if a call to DdeReconnect would have already been done...)
225 * FIXME: is this really an error ???
226 * 2/ the pConv conversation had really been deconnected
228 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
229 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
231 HWND hwndClient = pConv->hwndClient;
232 HWND hwndServer = pConv->hwndServer;
233 ATOM aSrv, aTpc;
235 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
237 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
238 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
239 if (!aSrv || !aTpc) goto theEnd;
241 LeaveCriticalSection(&WDML_CritSect);
243 /* note: sent messages shall not use packing */
244 ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
245 MAKELPARAM(aSrv, aTpc));
247 EnterCriticalSection(&WDML_CritSect);
249 pConv = WDML_GetConv(hConv, FALSE);
250 if (pConv == NULL)
252 FIXME("Should fail reconnection\n");
253 goto theEnd;
256 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
258 /* re-establish all links... */
259 WDML_LINK* pLink;
261 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
263 if (pLink->hConv == hConv)
265 /* try to reestablish the links... */
266 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
267 pLink->transactionType, 1000, NULL);
271 else
273 /* reset the conversation as it was */
274 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
279 theEnd:
280 LeaveCriticalSection(&WDML_CritSect);
282 if (aSrv) GlobalDeleteAtom(aSrv);
283 if (aTpc) GlobalDeleteAtom(aTpc);
284 return (HCONV)pNewConv;
287 /******************************************************************
288 * WDML_ClientQueueAdvise
290 * Creates and queue an WM_DDE_ADVISE transaction
292 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
294 DDEADVISE* pDdeAdvise;
295 WDML_XACT* pXAct;
296 ATOM atom;
298 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
300 atom = WDML_MakeAtomFromHsz(hszItem);
301 if (!atom) return NULL;
303 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
304 if (!pXAct)
306 GlobalDeleteAtom(atom);
307 return NULL;
310 pXAct->wType = wType & ~0x0F;
311 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
312 /* FIXME: hMem is unfreed for now... should be deleted in server */
314 /* pack DdeAdvise */
315 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
316 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
317 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
318 pDdeAdvise->cfFormat = wFmt;
319 GlobalUnlock(pXAct->hMem);
321 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
323 return pXAct;
326 /******************************************************************
327 * WDML_HandleAdviseReply
329 * handles the reply to an advise request
331 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
333 DDEACK ddeAck;
334 UINT_PTR uiLo, uiHi;
335 HSZ hsz;
337 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
339 return WDML_QS_PASS;
342 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
343 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
345 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
346 return WDML_QS_PASS;
348 GlobalDeleteAtom(uiHi);
349 FreeDDElParam(WM_DDE_ACK, msg->lParam);
351 WDML_ExtractAck(uiLo, &ddeAck);
353 if (ddeAck.fAck)
355 WDML_LINK* pLink;
357 /* billx: first to see if the link is already created. */
358 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
359 pXAct->hszItem, TRUE, pXAct->wFmt);
360 if (pLink != NULL)
362 /* we found a link, and only need to modify it in case it changes */
363 pLink->transactionType = pXAct->wType;
365 else
367 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
368 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
370 pXAct->hDdeData = (HDDEDATA)1;
372 else
374 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
375 GlobalFree(pXAct->hMem);
376 pXAct->hDdeData = NULL;
379 return WDML_QS_HANDLED;
382 /******************************************************************
383 * WDML_ClientQueueUnadvise
385 * queues an unadvise transaction
387 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
389 WDML_XACT* pXAct;
390 ATOM atom;
392 TRACE("XTYP_ADVSTOP transaction\n");
394 atom = WDML_MakeAtomFromHsz(hszItem);
395 if (!atom) return NULL;
397 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
398 if (!pXAct)
400 GlobalDeleteAtom(atom);
401 return NULL;
404 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
405 * on the specified item.
407 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
408 return pXAct;
411 /******************************************************************
412 * WDML_HandleUnadviseReply
416 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
418 DDEACK ddeAck;
419 UINT_PTR uiLo, uiHi;
420 HSZ hsz;
422 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
424 return WDML_QS_PASS;
427 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
428 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
430 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
431 return WDML_QS_PASS;
433 FreeDDElParam(WM_DDE_ACK, msg->lParam);
434 GlobalDeleteAtom(uiHi);
436 WDML_ExtractAck(uiLo, &ddeAck);
438 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
440 if (!ddeAck.fAck)
442 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
443 pXAct->hDdeData = NULL;
445 else
447 /* billx: remove the link */
448 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
449 pXAct->hszItem, pXAct->wFmt);
450 pXAct->hDdeData = (HDDEDATA)1;
452 return WDML_QS_HANDLED;
455 /******************************************************************
456 * WDML_ClientQueueRequest
460 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
462 WDML_XACT* pXAct;
463 ATOM atom;
465 TRACE("XTYP_REQUEST transaction\n");
467 atom = WDML_MakeAtomFromHsz(hszItem);
468 if (!atom) return NULL;
470 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
471 if (!pXAct)
473 GlobalDeleteAtom(atom);
474 return NULL;
477 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
479 return pXAct;
482 /******************************************************************
483 * WDML_HandleRequestReply
487 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
489 DDEACK ddeAck;
490 WINE_DDEHEAD wdh;
491 UINT_PTR uiLo, uiHi;
492 HSZ hsz;
494 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
495 return WDML_QS_PASS;
497 switch (msg->message)
499 case WM_DDE_ACK:
500 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
501 FreeDDElParam(WM_DDE_ACK, msg->lParam);
502 GlobalDeleteAtom(uiHi);
503 WDML_ExtractAck(uiLo, &ddeAck);
504 pXAct->hDdeData = 0;
505 if (ddeAck.fAck)
506 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
507 TRACE("Negative answer...\n");
508 break;
510 case WM_DDE_DATA:
511 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
512 TRACE("Got the result (%08x)\n", uiLo);
514 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
516 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
517 return WDML_QS_PASS;
519 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
520 if (wdh.fRelease)
522 GlobalFree((HGLOBAL)uiLo);
524 if (wdh.fAckReq)
526 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
528 else
530 GlobalDeleteAtom(uiHi);
531 FreeDDElParam(WM_DDE_ACK, msg->lParam);
533 break;
535 default:
536 FreeDDElParam(msg->message, msg->lParam);
537 return WDML_QS_PASS;
540 return WDML_QS_HANDLED;
543 /******************************************************************
544 * WDML_BuildExecuteCommand
546 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
547 * It also takes care of string conversion between the two window procedures
549 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
551 HGLOBAL hMem;
552 BOOL clientUnicode, serverUnicode;
553 DWORD memSize;
555 clientUnicode = IsWindowUnicode(pConv->hwndClient);
556 serverUnicode = IsWindowUnicode(pConv->hwndServer);
558 if (clientUnicode == serverUnicode)
560 memSize = cbData;
562 else
564 if (clientUnicode)
566 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
568 else
570 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
574 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
576 if (hMem)
578 LPBYTE pDst;
580 pDst = GlobalLock(hMem);
581 if (pDst)
583 if (clientUnicode == serverUnicode)
585 memcpy(pDst, pData, cbData);
587 else
589 if (clientUnicode)
591 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
593 else
595 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
599 GlobalUnlock(hMem);
601 else
603 GlobalFree(hMem);
604 hMem = 0;
607 return hMem;
610 /******************************************************************
611 * WDML_ClientQueueExecute
615 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
617 WDML_XACT* pXAct;
619 TRACE("XTYP_EXECUTE transaction\n");
621 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
622 if (!pXAct)
623 return NULL;
625 if (cbData == (DWORD)-1)
627 HDDEDATA hDdeData = (HDDEDATA)pData;
629 pData = DdeAccessData(hDdeData, &cbData);
630 if (pData)
632 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
633 DdeUnaccessData(hDdeData);
636 else
638 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
641 pXAct->lParam = (LPARAM)pXAct->hMem;
643 return pXAct;
646 /******************************************************************
647 * WDML_HandleExecuteReply
651 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
653 DDEACK ddeAck;
654 UINT_PTR uiLo, uiHi;
656 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
658 return WDML_QS_PASS;
661 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
662 FreeDDElParam(WM_DDE_ACK, msg->lParam);
664 if ((HANDLE)uiHi != pXAct->hMem)
666 return WDML_QS_PASS;
669 WDML_ExtractAck(uiLo, &ddeAck);
670 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
672 TRACE("hDdeData = %p\n", pXAct->hDdeData);
673 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
675 return WDML_QS_HANDLED;
678 /******************************************************************
679 * WDML_ClientQueuePoke
683 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
684 UINT wFmt, HSZ hszItem)
686 WDML_XACT* pXAct;
687 ATOM atom;
689 TRACE("XTYP_POKE transaction\n");
691 atom = WDML_MakeAtomFromHsz(hszItem);
692 if (!atom) return NULL;
694 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
695 if (!pXAct)
697 GlobalDeleteAtom(atom);
698 return NULL;
701 if (cbData == (DWORD)-1)
703 pXAct->hMem = (HDDEDATA)pData;
705 else
707 DDEPOKE* ddePoke;
709 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
710 ddePoke = GlobalLock(pXAct->hMem);
711 if (ddePoke)
713 memcpy(ddePoke->Value, pData, cbData);
714 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
715 ddePoke->cfFormat = wFmt;
716 GlobalUnlock(pXAct->hMem);
720 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
722 return pXAct;
725 /******************************************************************
726 * WDML_HandlePokeReply
730 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
732 DDEACK ddeAck;
733 UINT_PTR uiLo, uiHi;
734 HSZ hsz;
736 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
738 return WDML_QS_PASS;
741 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
742 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
743 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
745 return WDML_QS_PASS;
747 FreeDDElParam(WM_DDE_ACK, msg->lParam);
748 GlobalDeleteAtom(uiHi);
750 WDML_ExtractAck(uiLo, &ddeAck);
751 GlobalFree(pXAct->hMem);
753 pXAct->hDdeData = (HDDEDATA)TRUE;
754 return TRUE;
757 /******************************************************************
758 * WDML_ClientQueueTerminate
760 * Creates and queue an WM_DDE_TERMINATE transaction
762 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
764 WDML_XACT* pXAct;
766 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
767 if (!pXAct)
768 return NULL;
770 pXAct->lParam = 0;
771 pConv->wStatus &= ~ST_CONNECTED;
773 return pXAct;
776 /******************************************************************
777 * WDML_HandleTerminateReply
779 * handles the reply to a terminate request
781 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
783 if (msg->message != WM_DDE_TERMINATE)
785 /* FIXME: should delete data passed here */
786 return WDML_QS_SWALLOWED;
789 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
791 FIXME("hmmm shouldn't happen\n");
792 return WDML_QS_PASS;
794 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
796 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
797 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
799 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
800 return WDML_QS_HANDLED;
803 /******************************************************************
804 * WDML_HandleReplyData
808 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
810 UINT_PTR uiLo, uiHi;
811 HDDEDATA hDdeDataIn, hDdeDataOut;
812 WDML_LINK* pLink;
813 WINE_DDEHEAD wdh;
814 HSZ hsz;
816 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
817 /* wParam -- sending window handle */
818 /* lParam -- hDdeData & item HSZ */
820 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
821 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
823 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
825 /* billx:
826 * For hot link, data should be passed to its callback with
827 * XTYP_ADVDATA and callback should return the proper status.
829 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
830 uiLo ? TRUE : FALSE, wdh.cfFormat);
831 if (!pLink)
833 WDML_DecHSZ(pConv->instance, hsz);
834 DdeFreeDataHandle(hDdeDataIn);
835 return WDML_QS_PASS;
838 if (hDdeDataIn != 0 && wdh.fAckReq)
840 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
841 if (msg->lParam)
842 msg->lParam = 0;
844 else
846 GlobalDeleteAtom(uiHi);
849 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
850 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
852 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
854 if (uiLo) GlobalFree((HANDLE)uiLo);
857 DdeFreeDataHandle(hDdeDataIn);
859 WDML_DecHSZ(pConv->instance, hsz);
860 if (msg->lParam)
861 FreeDDElParam(WM_DDE_DATA, msg->lParam);
863 return WDML_QS_HANDLED;
866 /******************************************************************
867 * WDML_HandleIncomingTerminate
871 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
873 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
874 return WDML_QS_PASS;
876 pConv->wStatus |= ST_TERMINATED;
877 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
879 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
880 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
882 if (pConv->wStatus & ST_CONNECTED)
884 /* don't care about result code (if server exists or not) */
885 PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
886 pConv->wStatus &= ~ST_CONNECTED;
888 /* have to keep connection around to allow reconnection */
889 return WDML_QS_HANDLED;
892 /******************************************************************
893 * WDML_HandleReply
895 * handles any incoming reply, and try to match to an already sent request
897 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
899 WDML_XACT* pXAct = pConv->transactions;
900 WDML_QUEUE_STATE qs;
902 if (pConv->transactions)
904 /* first check message against a pending transaction, if any */
905 switch (pXAct->ddeMsg)
907 case WM_DDE_ADVISE:
908 qs = WDML_HandleAdviseReply(pConv, msg, pXAct);
909 break;
910 case WM_DDE_UNADVISE:
911 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct);
912 break;
913 case WM_DDE_EXECUTE:
914 qs = WDML_HandleExecuteReply(pConv, msg, pXAct);
915 break;
916 case WM_DDE_REQUEST:
917 qs = WDML_HandleRequestReply(pConv, msg, pXAct);
918 break;
919 case WM_DDE_POKE:
920 qs = WDML_HandlePokeReply(pConv, msg, pXAct);
921 break;
922 case WM_DDE_TERMINATE:
923 qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
924 break;
925 default:
926 qs = WDML_QS_ERROR;
927 FIXME("oooch\n");
930 else
932 qs = WDML_QS_PASS;
935 /* now check the results */
936 switch (qs)
938 case WDML_QS_ERROR:
939 case WDML_QS_SWALLOWED:
940 *hdd = 0;
941 break;
942 case WDML_QS_HANDLED:
943 /* ok, we have resolved a pending transaction
944 * notify callback if asynchronous, and remove it in any case
946 WDML_UnQueueTransaction(pConv, pXAct);
947 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
949 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
950 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
951 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
952 qs = WDML_QS_PASS;
954 else
956 *hdd = pXAct->hDdeData;
958 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
959 break;
960 case WDML_QS_PASS:
961 /* no pending transaction found, try a warm/hot link or a termination request */
962 switch (msg->message)
964 case WM_DDE_DATA:
965 qs = WDML_HandleIncomingData(pConv, msg, hdd);
966 break;
967 case WM_DDE_TERMINATE:
968 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
969 break;
971 break;
972 case WDML_QS_BLOCK:
973 FIXME("shouldn't be used on client side\n");
974 break;
977 return qs;
980 /******************************************************************
981 * WDML_SyncWaitTransactionReply
983 * waits until an answer for a sent request is received
984 * time out is also handled. only used for synchronous transactions
986 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct)
988 DWORD dwTime;
989 DWORD err;
990 WDML_CONV* pConv;
992 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
994 /* FIXME: time 32 bit wrap around */
995 dwTimeout += GetCurrentTime();
997 while ((dwTime = GetCurrentTime()) < dwTimeout)
999 /* we cannot be in the crit sect all the time because when client and server run in a
1000 * single process they need to share the access to the internal data
1002 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1003 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1005 BOOL ret = FALSE;
1006 MSG msg;
1007 WDML_CONV* pConv;
1008 HDDEDATA hdd;
1010 EnterCriticalSection(&WDML_CritSect);
1012 pConv = WDML_GetConv(hConv, FALSE);
1013 if (pConv == NULL)
1015 LeaveCriticalSection(&WDML_CritSect);
1016 /* conversation no longer available... return failure */
1017 break;
1019 while (PeekMessageW(&msg, pConv->hwndClient, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1021 /* check that either pXAct has been processed or no more xActions are pending */
1022 ret = (pConv->transactions == pXAct);
1023 ret = WDML_HandleReply(pConv, &msg, &hdd) == WDML_QS_HANDLED &&
1024 (pConv->transactions == NULL || ret);
1025 if (ret) break;
1027 LeaveCriticalSection(&WDML_CritSect);
1028 if (ret)
1030 return hdd;
1035 TRACE("Timeout !!\n");
1037 EnterCriticalSection(&WDML_CritSect);
1039 pConv = WDML_GetConv(hConv, FALSE);
1040 if (pConv != NULL)
1042 if (pConv->transactions)
1044 switch (pConv->transactions->ddeMsg)
1046 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1047 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1048 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1049 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1050 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1051 default: err = DMLERR_INVALIDPARAMETER; break;
1054 pConv->instance->lastError = err;
1057 LeaveCriticalSection(&WDML_CritSect);
1059 return 0;
1062 /*****************************************************************
1063 * DdeClientTransaction (USER32.@)
1065 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1066 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1068 WDML_CONV* pConv;
1069 WDML_XACT* pXAct;
1070 HDDEDATA hDdeData = 0;
1072 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1073 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1075 if (hConv == 0)
1077 ERR("Invalid conversation handle\n");
1078 return 0;
1081 EnterCriticalSection(&WDML_CritSect);
1083 pConv = WDML_GetConv(hConv, TRUE);
1084 if (pConv == NULL)
1086 /* cannot set error... cannot get back to DDE instance */
1087 goto theError;
1090 switch (wType)
1092 case XTYP_EXECUTE:
1093 if (hszItem != 0 || wFmt != 0)
1095 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1096 goto theError;
1098 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1099 break;
1100 case XTYP_POKE:
1101 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1102 break;
1103 case XTYP_ADVSTART|XTYPF_NODATA:
1104 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1105 case XTYP_ADVSTART:
1106 case XTYP_ADVSTART|XTYPF_ACKREQ:
1107 if (pData)
1109 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1110 goto theError;
1112 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1113 break;
1114 case XTYP_ADVSTOP:
1115 if (pData)
1117 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1118 goto theError;
1120 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1121 break;
1122 case XTYP_REQUEST:
1123 if (pData)
1125 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1126 goto theError;
1128 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1129 break;
1130 default:
1131 FIXME("Unknown transation\n");
1132 /* unknown transaction type */
1133 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1134 goto theError;
1137 if (pXAct == NULL)
1139 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1140 goto theError;
1143 WDML_QueueTransaction(pConv, pXAct);
1145 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1147 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1148 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1149 pConv->wStatus &= ~ST_CONNECTED;
1150 WDML_UnQueueTransaction(pConv, pXAct);
1151 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1152 goto theError;
1154 pXAct->dwTimeout = dwTimeout;
1155 /* FIXME: should set the app bits on *pdwResult */
1157 if (dwTimeout == TIMEOUT_ASYNC)
1159 if (pdwResult)
1161 *pdwResult = MAKELONG(0, pXAct->xActID);
1163 hDdeData = (HDDEDATA)1;
1165 else
1167 DWORD count, i;
1169 if (pdwResult)
1171 *pdwResult = 0L;
1173 count = WDML_CritSect.RecursionCount;
1174 for (i = 0; i < count; i++)
1175 LeaveCriticalSection(&WDML_CritSect);
1176 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct);
1177 for (i = 0; i < count; i++)
1178 EnterCriticalSection(&WDML_CritSect);
1180 LeaveCriticalSection(&WDML_CritSect);
1182 return hDdeData;
1183 theError:
1184 LeaveCriticalSection(&WDML_CritSect);
1185 return 0;
1188 /*****************************************************************
1189 * DdeAbandonTransaction (USER32.@)
1191 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1193 WDML_INSTANCE* pInstance;
1194 WDML_CONV* pConv;
1195 WDML_XACT* pXAct;
1197 TRACE("(%08lx,%p,%08ld);\n", idInst, hConv, idTransaction);
1199 EnterCriticalSection(&WDML_CritSect);
1200 if ((pInstance = WDML_GetInstance(idInst)))
1202 if (hConv)
1204 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1206 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1208 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1209 (idTransaction == 0 || pXAct->xActID == idTransaction))
1211 WDML_UnQueueTransaction(pConv, pXAct);
1212 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1217 else
1219 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1221 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1222 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1224 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1226 WDML_UnQueueTransaction(pConv, pXAct);
1227 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1233 LeaveCriticalSection(&WDML_CritSect);
1235 return TRUE;
1238 /******************************************************************
1239 * WDML_ClientProc
1241 * Window Proc created on client side for each conversation
1243 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1245 UINT uiLo, uiHi;
1246 WDML_CONV* pConv = NULL;
1247 HSZ hszSrv, hszTpc;
1249 TRACE("%p %04x %08x %08lx\n", hwnd, iMsg, wParam , lParam);
1251 if (iMsg == WM_DDE_ACK &&
1252 /* in the initial WM_INITIATE sendmessage */
1253 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1255 /* In response to WM_DDE_INITIATE, save server window */
1256 WCHAR buf[256];
1257 WDML_INSTANCE* pInstance;
1259 /* note: sent messages do not need packing */
1260 uiLo = LOWORD(lParam);
1261 uiHi = HIWORD(lParam);
1263 /* FIXME: convlist should be handled here */
1264 if (pConv)
1266 /* we already have started the conv with a server, drop other replies */
1267 GlobalDeleteAtom(uiLo);
1268 GlobalDeleteAtom(uiHi);
1269 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1270 return 0;
1273 pInstance = WDML_GetInstanceFromWnd(hwnd);
1275 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1276 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1278 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1280 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1281 pConv->wStatus |= ST_CONNECTED;
1282 pConv->wConvst = XST_INIT1;
1284 /* check if server is handled by DDEML */
1285 if (GetClassNameW((HWND)wParam, buf, sizeof(buf)/sizeof(WCHAR)) &&
1286 lstrcmpiW(buf, WDML_szServerConvClass) == 0)
1288 pConv->wStatus |= ST_ISLOCAL;
1291 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1293 GlobalDeleteAtom(uiLo);
1294 GlobalDeleteAtom(uiHi);
1296 /* accept conversation */
1297 return 1;
1300 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1302 EnterCriticalSection(&WDML_CritSect);
1304 pConv = WDML_GetConvFromWnd(hwnd);
1306 if (pConv)
1308 MSG msg;
1309 HDDEDATA hdd;
1311 msg.hwnd = hwnd;
1312 msg.message = iMsg;
1313 msg.wParam = wParam;
1314 msg.lParam = lParam;
1316 WDML_HandleReply(pConv, &msg, &hdd);
1319 LeaveCriticalSection(&WDML_CritSect);
1320 return 0;
1323 return DefWindowProcW(hwnd, iMsg, wParam, lParam);
1326 /*****************************************************************
1327 * DdeDisconnect (USER32.@)
1329 BOOL WINAPI DdeDisconnect(HCONV hConv)
1331 WDML_CONV* pConv = NULL;
1332 WDML_XACT* pXAct;
1333 DWORD count, i;
1334 BOOL ret = FALSE;
1336 TRACE("(%p)\n", hConv);
1338 if (hConv == 0)
1340 ERR("DdeDisconnect(): hConv = 0\n");
1341 return FALSE;
1344 EnterCriticalSection(&WDML_CritSect);
1345 pConv = WDML_GetConv(hConv, TRUE);
1346 if (pConv != NULL)
1348 if (pConv->wStatus & ST_CLIENT)
1350 /* FIXME: should abandon all pending transactions */
1351 pXAct = WDML_ClientQueueTerminate(pConv);
1352 if (pXAct != NULL)
1354 count = WDML_CritSect.RecursionCount;
1355 for (i = 0; i < count; i++)
1356 LeaveCriticalSection(&WDML_CritSect);
1357 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1358 (WPARAM)pConv->hwndClient, pXAct->lParam))
1359 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct);
1360 for (i = 0; i < count; i++)
1361 EnterCriticalSection(&WDML_CritSect);
1362 ret = TRUE;
1363 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1364 /* still have to destroy data assosiated with conversation */
1365 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1367 else
1369 FIXME("Not implemented yet for a server side conversation\n");
1373 LeaveCriticalSection(&WDML_CritSect);
1375 return ret;
1378 /*****************************************************************
1379 * DdeImpersonateClient (USER32.@)
1381 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1383 WDML_CONV* pConv;
1384 BOOL ret = FALSE;
1386 TRACE("(%p)\n", hConv);
1388 EnterCriticalSection(&WDML_CritSect);
1389 pConv = WDML_GetConv(hConv, TRUE);
1390 if (pConv)
1392 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1394 LeaveCriticalSection(&WDML_CritSect);
1395 return ret;