1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
7 * Purpose : DDE signals and processes functionality for DDE
8 ***************************************************************************
12 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
24 #include "shm_semaph.h"
25 #include "shm_main_blk.h"
32 int curr_proc_idx= -1;
34 enum stop_wait_op stop_wait_op=CONT;
36 sigjmp_buf env_get_ack;
37 sigjmp_buf env_wait_x;
39 #define IDX_TO_HWND(idx) (0xfffe - (idx))
40 #define HWND_TO_IDX(wnd) (0xfffe - (wnd))
41 #define DDE_WIN_INFO(win) ( main_block->windows[HWND_TO_IDX(win)] )
42 #define DDE_WIN2PROC(win) ( DDE_WIN_INFO(win).proc_idx )
43 #define DDE_IsRemoteWindow(win) ( (win)<0xffff && (win)>=(0xffff-DDE_PROCS))
47 #define DDE_MSG_SIZE sizeof(MSG16)
48 #define FREE_WND (WORD)(-2)
49 #define DELETED_WND (WORD)(-3)
50 static char *msg_type[4]={"********", "DDE_SEND", "DDE_POST", "DDE_ACK"};
54 char filler[DDE_MSG_SIZE];
57 typedef struct fifo_element {
59 struct fifo_element *next;
63 fifo_element *first; /* first element in the fifo or NULL */
64 fifo_element *last; /* last element in the fifo or NULL */
66 static struct fifo fifo = {NULL,NULL};
68 void dde_proc_delete(int proc_idx);
70 void dde_proc_add_fifo(int val)
72 fifo_element *created;
74 created= (fifo_element*) xmalloc( sizeof(fifo_element) );
81 fifo.last->next= created;
85 /* get an item from the fifo, and return it.
86 * If fifo is empty, return -1
88 int dde_proc_shift_fifo()
91 fifo_element *deleted;
93 if (fifo.first == NULL)
98 fifo.first= deleted->next;
99 if (fifo.first == NULL)
106 static void print_dde_message(char *desc, MSG16 *msg);
108 /* This should be run only when main_block is first allocated. */
109 void dde_proc_init(dde_proc proc)
113 for (proc_num=0 ; proc_num<DDE_PROCS ; proc_num++, proc++) {
121 /* add current process to the list of processes */
122 void dde_proc_add(dde_proc procs)
127 shm_write_wait(main_block->sem);
129 /* find free proc_idx and allocate it */
130 for (proc_idx=0, proc=procs ; proc_idx<DDE_PROCS ; proc_idx++, proc++)
134 if (proc_idx<DDE_PROCS) { /* got here beacuse a free was found ? */
135 dde_msg_setup(&proc->msg);
137 curr_proc_idx=proc_idx;
138 shm_sem_init(&proc->sem);
142 WARN(dde,"Can't allocate process\n");
144 shm_write_signal(main_block->sem);
147 /* wait for dde - acknowledge message - or timout */
148 static BOOL32 get_ack()
150 struct timeval timeout;
152 struct msg_dat ack_buff;
154 /* timeout after exactly one seconf */
158 sigsetjmp(env_get_ack, 1);
159 /* get here after normal execution, or after siglongjmp */
161 do { /* loop to wait for DDE_ACK */
163 stop_wait_op=CONT; /* sensitive code: disallow siglongjmp */
164 size= msgrcv( main_block->proc[curr_proc_idx].msg , &ack_buff.dat,
165 1, DDE_ACK, IPC_NOWAIT);
167 TRACE(msg,"get_ack: received DDE_ACK message\n");
170 if (DDE_GetRemoteMessage()) {
171 had_SIGUSR2=1; /* might have recieved SIGUSR2 */
173 stop_wait_op=STOP_WAIT_ACK; /* allow siglongjmp */
175 } while (had_SIGUSR2); /* loop if SIGUSR2 was recieved */
177 /* siglongjmp should be enabled at this moment */
178 select( 0, NULL, NULL, NULL, &timeout );
179 stop_wait_op=CONT; /* disallow further siglongjmp */
181 /* timeout !! (otherwise there would have been a siglongjmp) */
185 /* Transfer one message to a given process */
186 static BOOL32 DDE_DoOneMessage (int proc_idx, int size, struct msgbuf *msgbuf)
188 dde_proc proc= &main_block->proc[ proc_idx ];
191 if (proc_idx == curr_proc_idx)
194 if (kill(proc->pid,0) < 0) {
195 /* pid does not exist, or not our */
196 dde_proc_delete(proc_idx);
200 if (TRACE_ON(dde) || WARN_ON_dde) {
201 MSG16 *msg=(MSG16*) &msgbuf->mtext;
203 if (msgbuf->mtype==DDE_SEND)
204 title="sending dde:";
205 else if (msgbuf->mtype==DDE_POST)
206 title="posting dde:";
210 print_dde_message(title, msg);
212 WARN(dde, "Unknown message type=0x%lx\n", msgbuf->mtype);
214 TRACE(msg, "to proc_idx=%d (pid=%d), queue=%u\n",
215 proc_idx, proc->pid, (unsigned)proc->msg);
216 if ( proc->msg != -1) {
217 TRACE(msg, "doing...(type=%s)\n", msg_type[msgbuf->mtype]);
218 size=msgsnd (proc->msg, msgbuf, size, 0);
224 kill(proc->pid,SIGUSR2); /* tell the process there is a message */
226 TRACE(msg, "Trying to get acknowledgment from msg queue=%d\n",
228 Yield16(); /* force task switch, and */
229 /* acknowledgment sending */
234 WARN(dde,"get_ack: DDE_DoOneMessage: timeout\n");
239 WARN(msg, "message not sent, target has no message queue\n");
244 /* Do some sort of premitive hash table */
245 static HWND16 HWND_Local2Remote(HWND16 orig)
250 WND_DATA *deleted= NULL;
253 dde_wnd_idx= orig % DDE_WINDOWS;
254 for ( i=0 ; i < DDE_WINDOWS ; i++, dde_wnd_idx++) {
255 if (dde_wnd_idx >= DDE_WINDOWS)
256 dde_wnd_idx -= DDE_WINDOWS; /* wrap-around */
258 tested= &main_block->windows[ dde_wnd_idx ];
259 if (tested->proc_idx == FREE_WND)
262 if (deleted == NULL && tested->proc_idx == DELETED_WND) {
264 deleted_idx= dde_wnd_idx;
265 } else if (tested->wnd == orig && tested->proc_idx == curr_proc_idx) {
266 return IDX_TO_HWND(dde_wnd_idx);
269 if (deleted != NULL) { /* deleted is preferable */
270 /* free item, allocate it */
271 deleted->proc_idx= curr_proc_idx;
273 return IDX_TO_HWND(deleted_idx);
275 if (tested->proc_idx == FREE_WND) {
276 tested->proc_idx= curr_proc_idx;
278 return IDX_TO_HWND(dde_wnd_idx);
281 WARN(dde, "Can't map any more windows to DDE windows\n");
285 static BOOL32 DDE_DoMessage( MSG16 *msg, int type )
289 MSG16 *remote_message;
290 struct msg_dat msg_dat;
293 if (msg->wParam == 0)
296 if (main_block==NULL) {
297 if (msg->message >= WM_DDE_FIRST && msg->message <= WM_DDE_LAST)
304 if (msg->wParam == (HWND16)-1)
307 if ( ! DDE_IsRemoteWindow(msg->hwnd) && msg->hwnd!= (HWND16)-1)
310 TRACE(msg, "(hwnd=0x%x,msg=0x%x,..) - %s\n",
311 (int)msg->hwnd,(int)msg->message,msg_type[type]);
314 TRACE(msg, "(hwnd=0x%x,msg=0x%x,..) -- HWND_BROADCAST !\n",
315 (int)msg->hwnd,(int)msg->message);
316 remote_message=(void*)&msg_dat.dat.mtext;
318 memcpy(remote_message, msg, sizeof(*msg));
319 remote_message->wParam= HWND_Local2Remote(msg->wParam);
320 if (remote_message->wParam == 0)
323 msg_dat.dat.mtype=type;
325 if (msg->hwnd == (HWND16)-1) {
327 for ( proc_idx=0; proc_idx < DDE_PROCS ; proc_idx++) {
328 if (proc_idx == curr_proc_idx)
330 if (main_block->proc[ proc_idx ].msg != -1)
331 success|=DDE_DoOneMessage(proc_idx, DDE_MSG_SIZE, &msg_dat.dat);
335 return DDE_DoOneMessage(DDE_WIN2PROC(msg->hwnd), DDE_MSG_SIZE,
340 BOOL32 DDE_SendMessage( MSG16 *msg)
342 return DDE_DoMessage(msg, DDE_SEND);
345 BOOL32 DDE_PostMessage( MSG16 *msg)
347 return DDE_DoMessage(msg, DDE_POST);
351 void dde_proc_send_ack(HWND16 wnd, BOOL32 val) {
354 static struct msgbuf msg_ack={DDE_ACK,{'0'}};
356 proc=DDE_WIN2PROC(wnd);
357 msg=main_block->proc[proc].msg;
358 TRACE(msg,"sending ACK to wnd=%4x, proc=%d,msg=%d, pid=%d\n",
359 wnd,proc,msg,main_block->proc[proc].pid);
361 msg_ack.mtext[0]=val;
362 msgsnd (msg, &msg_ack, 1, 0);
363 kill(main_block->proc[proc].pid, SIGUSR2);
366 /* return true (non zero) if had a remote message */
367 #undef DDE_GetRemoteMessage
369 int DDE_GetRemoteMessage()
371 static int nesting=0; /* to avoid infinite recursion */
373 MSG16 *remote_message;
375 struct msg_dat msg_dat;
376 BOOL32 was_sent; /* sent/received */
380 if (curr_proc_idx==-1) /* do we have DDE initialized ? */
385 ERR(msg, "suspecting infinite recursion, exiting");
389 remote_message=(void*)&msg_dat.dat.mtext;
391 /* test for SendMessage */
392 size= msgrcv( main_block->proc[curr_proc_idx].msg , &msg_dat.dat,
393 DDE_MSG_SIZE, DDE_SEND, IPC_NOWAIT);
395 if (size==DDE_MSG_SIZE) { /* is this a correct message (if any) ?*/
397 TRACE(msg, "DDE:receive sent message. msg=%04x wPar=%04x"
399 remote_message->message, remote_message->wParam,
400 remote_message->lParam);
402 size= msgrcv( main_block->proc[curr_proc_idx].msg , &msg_dat.dat,
403 DDE_MSG_SIZE, DDE_POST, IPC_NOWAIT);
405 if (size==DDE_MSG_SIZE) { /* is this a correct message (if any) ?*/
407 TRACE(msg, "DDE:receive posted message. "
408 "msg=%04x wPar=%04x lPar=%08lx\n",
409 remote_message->message, remote_message->wParam,
410 remote_message->lParam);
413 return 0; /* no DDE message found */
416 /* At this point we are sure that there is a DDE message,
417 * was_sent is TRUE is the message was sent, and false if it was posted
425 title="receive sent dde:";
427 title="receive posted dde:";
428 print_dde_message(title, remote_message);
431 if (remote_message->hwnd != (HWND16) -1 ) {
432 HWND16 dde_window= DDE_WIN_INFO(remote_message->hwnd).wnd;
433 /* we should know exactly where to send the message (locally)*/
435 TRACE(dde, "SendMessage(wnd=0x%04x, msg=0x%04x, wPar=0x%04x,"
436 "lPar=0x%08x\n", dde_window, remote_message->message,
437 remote_message->wParam, (int)remote_message->lParam);
439 /* execute the recieved message */
440 passed= SendMessage16(dde_window, remote_message->message,
441 remote_message->wParam, remote_message->lParam);
443 /* Tell the sended, that the message is here */
444 dde_proc_send_ack(remote_message->wParam, passed);
447 passed= PostMessage16(dde_window, remote_message->message,
448 remote_message->wParam, remote_message->lParam);
449 if (passed == FALSE) {
450 /* Tell the sender, that the message is here, and failed */
451 dde_proc_send_ack(remote_message->wParam, FALSE);
454 /* ack will be sent later, at the first peek/get message */
455 dde_proc_add_fifo(remote_message->wParam);
462 /* iterate through all the windows */
463 for (wndPtr = WIN_FindWndPtr(GetTopWindow32(GetDesktopWindow32()));
465 wndPtr = wndPtr->next)
467 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION) {
469 SendMessage16( wndPtr->hwndSelf, remote_message->message,
470 remote_message->wParam, remote_message->lParam );
472 PostMessage16( wndPtr->hwndSelf, remote_message->message,
473 remote_message->wParam, remote_message->lParam );
477 /* replay with DDE_ACK after broadcasting in DDE_GetRemoteMessage */
478 dde_proc_send_ack(remote_message->wParam, TRUE);
488 ack_wnd= dde_proc_shift_fifo();
490 dde_proc_send_ack(ack_wnd, TRUE);
491 usleep(10000); /* force unix task switch */
496 void dde_msg_setup(int *msg_ptr)
498 *msg_ptr= msgget (IPC_PRIVATE, IPC_CREAT | 0700);
500 perror("dde_msg_setup fails to get message queue");
503 /* do we have dde handling in the window ?
504 * If we have, atom usage will make this instance of wine set up
507 void DDE_TestDDE(HWND16 hwnd)
510 if (in_test++) return;
511 if (main_block != NULL) {
515 TRACE(msg,"(0x%04x)\n", hwnd);
518 /* just send a message to see how things are going */
519 SendMessage16( hwnd, WM_DDE_INITIATE, 0, 0);
523 void dde_proc_delete(int proc_idx)
525 dde_proc_done(&main_block->proc[proc_idx]);
527 void stop_wait(int a)
531 switch(stop_wait_op) {
533 siglongjmp(env_get_ack,1);
534 break; /* never reached */
536 siglongjmp(env_wait_x,1);
537 break; /* never reached */
543 static void print_dde_message(char *desc, MSG16 *msg)
545 /* extern const char *MessageTypeNames[];*/
546 extern int debug_last_handle_size;
550 DDEADVISE *ddeadvise;
553 dbg_decl_str(dde, 2048);
555 if (is_dde_handle(msg->lParam & 0xffff) )
556 ptr=DDE_AttachHandle(msg->lParam&0xffff, NULL);
559 wStatus=LOWORD(msg->lParam);
560 hWord=HIWORD(msg->lParam);
562 dsprintf(dde,"%s", desc);
563 dsprintf(dde,"%04x %04x==%s %04x %08lx ",
564 msg->hwnd, msg->message,"",/*MessageTypeNames[msg->message],*/
565 msg->wParam, msg->lParam);
566 switch(msg->message) {
567 case WM_DDE_INITIATE:
570 case WM_DDE_TERMINATE:
574 /* DDEADVISE: hOptions in WM_DDE_ADVISE message */
577 dsprintf(dde,"fDeferUpd=%d,fAckReq=%d,cfFormat=0x%x",
578 ddeadvise->fDeferUpd, ddeadvise->fAckReq,
579 ddeadvise->cfFormat);
581 dsprintf(dde,"NO-DATA");
582 dsprintf(dde," atom=0x%x",hWord);
585 case WM_DDE_UNADVISE:
586 dsprintf(dde,"format=0x%x, atom=0x%x",wStatus,hWord);
589 ddeack=(DDEACK*)&wStatus;
590 dsprintf(dde,"bAppReturnCode=%d,fBusy=%d,fAck=%d",
591 ddeack->bAppReturnCode, ddeack->fBusy, ddeack->fAck);
593 dsprintf(dde,"(True)");
595 dsprintf(dde,"(False)");
601 dsprintf(dde,"fResponse=%d,fRelease=%d,"
602 "fAckReq=%d,cfFormat=0x%x,value=\"%.*s\"",
603 ddedata->fResponse, ddedata->fRelease,
604 ddedata->fAckReq, ddedata->cfFormat,
605 debug_last_handle_size- (int)sizeof(*ddedata)+1,
608 dsprintf(dde,"NO-DATA");
609 dsprintf(dde," atom=0x%04x",hWord);
615 dsprintf(dde,"fRelease=%d,cfFormat=0x%x,value[0]='%c'",
616 ddepoke->fRelease, ddepoke->cfFormat, ddepoke->Value[0]);
618 dsprintf(dde,"NO-DATA");
619 dsprintf(dde," atom=0x%04x",hWord);
622 TRACE(dde,"%s\n", dbg_str(dde));
625 void dde_proc_done(dde_proc proc)
628 msgctl(proc->msg, IPC_RMID, NULL);
631 shm_delete_chain(&proc->shmid);
632 shm_sem_done(&proc->sem);
635 /* delete entry, if old junk */
636 void dde_proc_refresh(dde_proc proc)
641 if (kill(proc->pid, 0) != -1)
644 /* get here if entry non empty, and the process does not exist */
652 for (i=0 ; i < DDE_WINDOWS ; i++)
653 main_block->windows[i].proc_idx = FREE_WND;
656 static BOOL32 DDE_ProcHasWindows(int proc_idx)
661 for ( i=0 ; i < DDE_WINDOWS ; i++) {
662 tested= &main_block->windows[ i ];
664 if (tested->proc_idx == proc_idx)
669 /* Look for hwnd in the hash table of DDE windows,
670 * Delete it from there. If there are no more windows for this
671 * process, remove the process from the DDE data-structure.
672 * If there are no more processes - delete the whole DDE struff.
674 * This is inefficient, but who cares for the efficiency of this rare
677 void DDE_DestroyWindow(HWND16 hwnd)
683 if (main_block == NULL)
686 dde_wnd_idx= hwnd % DDE_WINDOWS;
688 for ( i=0 ; i < DDE_WINDOWS ; i++, dde_wnd_idx++) {
689 if (dde_wnd_idx >= DDE_WINDOWS)
690 dde_wnd_idx -= DDE_WINDOWS; /* wrap-around */
692 tested= &main_block->windows[ dde_wnd_idx ];
693 if (tested->proc_idx == FREE_WND)
694 return; /* No window will get deleted here */
696 if (tested->wnd == hwnd && tested->proc_idx == curr_proc_idx) {
698 tested->proc_idx= DELETED_WND;
699 if (DDE_ProcHasWindows( curr_proc_idx ))
701 while (dde_reschedule()) /* make sure there are no other */
702 /* processes waiting for acknowledgment */
704 dde_proc_delete( curr_proc_idx );
705 if (DDE_no_of_attached() == 1)
708 shmdt( (void *) main_block);
716 #endif /* CONFIG_IPC */