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__)
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(MSG)
48 #define FREE_WND (WORD)(-2)
49 #define DELETED_WND (WORD)(-3)
50 #if defined(DEBUG_MSG) || defined(DEBUG_RUNTIME)
51 static char *msg_type
[4]={"********", "DDE_SEND", "DDE_POST", "DDE_ACK"};
56 char filler
[DDE_MSG_SIZE
];
59 typedef struct fifo_element
{
61 struct fifo_element
*next
;
65 fifo_element
*first
; /* first element in the fifo or NULL */
66 fifo_element
*last
; /* last element in the fifo or NULL */
68 static struct fifo fifo
= {NULL
,NULL
};
70 void dde_proc_delete(int proc_idx
);
72 void dde_proc_add_fifo(int val
)
74 fifo_element
*created
;
76 created
= (fifo_element
*) malloc( sizeof(fifo_element
) );
83 fifo
.last
->next
= created
;
87 /* get an item from the fifo, and return it.
88 * If fifo is empty, return -1
90 int dde_proc_shift_fifo()
93 fifo_element
*deleted
;
95 if (fifo
.first
== NULL
)
100 fifo
.first
= deleted
->next
;
101 if (fifo
.first
== NULL
)
108 static void print_dde_message(char *desc
, MSG
*msg
);
110 /* This should be run only when main_block is first allocated. */
111 void dde_proc_init(dde_proc proc
)
115 for (proc_num
=0 ; proc_num
<DDE_PROCS
; proc_num
++, proc
++) {
123 /* add current process to the list of processes */
124 void dde_proc_add(dde_proc procs
)
128 dprintf_dde(stddeb
,"dde_proc_add(..)\n");
129 shm_write_wait(main_block
->sem
);
131 /* find free proc_idx and allocate it */
132 for (proc_idx
=0, proc
=procs
; proc_idx
<DDE_PROCS
; proc_idx
++, proc
++)
136 if (proc_idx
<DDE_PROCS
) { /* got here beacuse a free was found ? */
137 dde_msg_setup(&proc
->msg
);
139 curr_proc_idx
=proc_idx
;
140 shm_sem_init(&proc
->sem
);
144 fprintf(stderr
,"dde_proc_add: Can't allocate process\n");
146 shm_write_signal(main_block
->sem
);
149 /* wait for dde - acknowledge message - or timout */
150 static BOOL
get_ack()
152 struct timeval timeout
;
154 struct msg_dat ack_buff
;
156 /* timeout after exactly one seconf */
160 sigsetjmp(env_get_ack
, 1);
161 /* get here after normal execution, or after siglongjmp */
163 do { /* loop to wait for DDE_ACK */
165 stop_wait_op
=CONT
; /* sensitive code: disallow siglongjmp */
166 size
= msgrcv( main_block
->proc
[curr_proc_idx
].msg
, &ack_buff
.dat
,
167 1, DDE_ACK
, IPC_NOWAIT
);
169 dprintf_msg(stddeb
,"get_ack: received DDE_ACK message\n");
172 if (DDE_GetRemoteMessage()) {
173 had_SIGUSR2
=1; /* might have recieved SIGUSR2 */
175 stop_wait_op
=STOP_WAIT_ACK
; /* allow siglongjmp */
177 } while (had_SIGUSR2
); /* loop if SIGUSR2 was recieved */
179 /* siglongjmp should be enabled at this moment */
180 select( 0, NULL
, NULL
, NULL
, &timeout
);
181 stop_wait_op
=CONT
; /* disallow further siglongjmp */
183 /* timeout !! (otherwise there would have been a siglongjmp) */
187 /* Transfer one message to a given process */
188 static BOOL
DDE_DoOneMessage (int proc_idx
, int size
, struct msgbuf
*msgbuf
)
190 dde_proc proc
= &main_block
->proc
[ proc_idx
];
193 if (proc_idx
== curr_proc_idx
)
196 if (kill(proc
->pid
,0) < 0) {
197 /* pid does not exist, or not our */
198 dde_proc_delete(proc_idx
);
203 MSG
*msg
=(MSG
*) &msgbuf
->mtext
;
205 if (msgbuf
->mtype
==DDE_SEND
)
206 title
="sending dde:";
207 else if (msgbuf
->mtype
==DDE_POST
)
208 title
="posting dde:";
212 print_dde_message(title
, msg
);
214 fprintf(stddeb
,"Unknown message type=0x%lx\n",msgbuf
->mtype
);
217 "DDE_DoOneMessage: to proc_idx=%d (pid=%d), queue=%u\n",
218 proc_idx
, proc
->pid
, (unsigned)proc
->msg
);
219 if ( proc
->msg
!= -1) {
220 dprintf_msg(stddeb
, "DDE_DoOneMessage: doing...(type=%s)\n",
221 msg_type
[msgbuf
->mtype
]);
222 size
=msgsnd (proc
->msg
, msgbuf
, size
, 0);
228 kill(proc
->pid
,SIGUSR2
); /* tell the process there is a message */
230 dprintf_msg(stddeb
,"DDE_DoOneMessage: "
231 "Trying to get acknowledgment from msg queue=%d\n",
233 Yield(); /* force task switch, and */
234 /* acknowledgment sending */
239 fprintf(stderr
,"get_ack: DDE_DoOneMessage: timeout\n");
244 dprintf_msg(stddeb
,"DDE_DoOneMessage: message not sent, "
245 "target has no message queue\n");
250 /* Do some sort of premitive hash table */
251 static HWND
HWND_Local2Remote(HWND orig
)
256 WND_DATA
*deleted
= NULL
;
259 dde_wnd_idx
= orig
% DDE_WINDOWS
;
260 for ( i
=0 ; i
< DDE_WINDOWS
; i
++, dde_wnd_idx
++) {
261 if (dde_wnd_idx
>= DDE_WINDOWS
)
262 dde_wnd_idx
-= DDE_WINDOWS
; /* wrap-around */
264 tested
= &main_block
->windows
[ dde_wnd_idx
];
265 if (tested
->proc_idx
== FREE_WND
)
268 if (deleted
== NULL
&& tested
->proc_idx
== DELETED_WND
) {
270 deleted_idx
= dde_wnd_idx
;
271 } else if (tested
->wnd
== orig
&& tested
->proc_idx
== curr_proc_idx
) {
272 return IDX_TO_HWND(dde_wnd_idx
);
275 if (deleted
!= NULL
) { /* deleted is preferable */
276 /* free item, allocate it */
277 deleted
->proc_idx
= curr_proc_idx
;
279 return IDX_TO_HWND(deleted_idx
);
281 if (tested
->proc_idx
== FREE_WND
) {
282 tested
->proc_idx
= curr_proc_idx
;
284 return IDX_TO_HWND(dde_wnd_idx
);
288 "HWND_Local2Remote: Can't map any more windows to DDE windows\n");
292 static BOOL
DDE_DoMessage( MSG
*msg
, int type
)
297 struct msg_dat msg_dat
;
300 if (msg
->wParam
== 0)
303 if (main_block
==NULL
) {
304 if (msg
->message
>= WM_DDE_FIRST
&& msg
->message
<= WM_DDE_LAST
)
311 if (msg
->wParam
== (HWND
)-1)
314 if ( ! DDE_IsRemoteWindow(msg
->hwnd
) && msg
->hwnd
!= (HWND
)-1)
317 dprintf_msg(stddeb
, "%s: DDE_DoMessage(hwnd=0x%x,msg=0x%x,..)\n",
318 msg_type
[type
], (int)msg
->hwnd
,(int)msg
->message
);
322 "DDE_DoMessage(hwnd=0x%x,msg=0x%x,..) // HWND_BROADCAST !\n",
323 (int)msg
->hwnd
,(int)msg
->message
);
324 remote_message
=(void*)&msg_dat
.dat
.mtext
;
326 memcpy(remote_message
, msg
, sizeof(*msg
));
327 remote_message
->wParam
= HWND_Local2Remote(msg
->wParam
);
328 if (remote_message
->wParam
== 0)
331 msg_dat
.dat
.mtype
=type
;
333 if (msg
->hwnd
== (HWND
)-1) {
335 for ( proc_idx
=0; proc_idx
< DDE_PROCS
; proc_idx
++) {
336 if (proc_idx
== curr_proc_idx
)
338 if (main_block
->proc
[ proc_idx
].msg
!= -1)
339 success
|=DDE_DoOneMessage(proc_idx
, DDE_MSG_SIZE
, &msg_dat
.dat
);
343 return DDE_DoOneMessage(DDE_WIN2PROC(msg
->hwnd
), DDE_MSG_SIZE
,
348 BOOL
DDE_SendMessage( MSG
*msg
)
350 return DDE_DoMessage(msg
, DDE_SEND
);
353 BOOL
DDE_PostMessage( MSG
*msg
)
355 return DDE_DoMessage(msg
, DDE_POST
);
359 void dde_proc_send_ack(HWND wnd
, BOOL val
) {
362 static struct msgbuf msg_ack
={DDE_ACK
,{'0'}};
364 proc
=DDE_WIN2PROC(wnd
);
365 msg
=main_block
->proc
[proc
].msg
;
366 dprintf_msg(stddeb
,"DDE_GetRemoteMessage: sending ACK "
367 "to wnd=%4x, proc=%d,msg=%d, pid=%d\n",wnd
,proc
,msg
,
368 main_block
->proc
[proc
].pid
371 msg_ack
.mtext
[0]=val
;
372 msgsnd (msg
, &msg_ack
, 1, 0);
373 kill(main_block
->proc
[proc
].pid
, SIGUSR2
);
376 /* return true (non zero) if had a remote message */
377 #undef DDE_GetRemoteMessage
379 int DDE_GetRemoteMessage()
381 static int nesting
=0; /* to avoid infinite recursion */
385 struct msg_dat msg_dat
;
386 BOOL was_sent
; /* sent/received */
391 if (curr_proc_idx
==-1) /* do we have DDE initialized ? */
396 fprintf(stderr
,"DDE_GetRemoteMessage: suspecting infinite recursion, exiting");
400 remote_message
=(void*)&msg_dat
.dat
.mtext
;
402 /* test for SendMessage */
403 size
= msgrcv( main_block
->proc
[curr_proc_idx
].msg
, &msg_dat
.dat
,
404 DDE_MSG_SIZE
, DDE_SEND
, IPC_NOWAIT
);
406 if (size
==DDE_MSG_SIZE
) { /* is this a correct message (if any) ?*/
409 "DDE:receive sent message. msg=%04x wPar=%04x"
411 remote_message
->message
, remote_message
->wParam
,
412 remote_message
->lParam
);
414 size
= msgrcv( main_block
->proc
[curr_proc_idx
].msg
, &msg_dat
.dat
,
415 DDE_MSG_SIZE
, DDE_POST
, IPC_NOWAIT
);
417 if (size
==DDE_MSG_SIZE
) { /* is this a correct message (if any) ?*/
420 "DDE:receive posted message. "
421 "msg=%04x wPar=%04x lPar=%08lx\n",
422 remote_message
->message
, remote_message
->wParam
,
423 remote_message
->lParam
);
426 return 0; /* no DDE message found */
429 /* At this point we are sure that there is a DDE message,
430 * was_sent is TRUE is the message was sent, and false if it was posted
438 title
="receive sent dde:";
440 title
="receive posted dde:";
441 print_dde_message(title
, remote_message
);
444 if (remote_message
->hwnd
!= (HWND
) -1 ) {
445 HWND dde_window
= DDE_WIN_INFO(remote_message
->hwnd
).wnd
;
446 /* we should know exactly where to send the message (locally)*/
449 "SendMessage(wnd=0x%04x, msg=0x%04x, wPar=0x%04x,"
451 dde_window
, remote_message
->message
,
452 remote_message
->wParam
, (int)remote_message
->lParam
);
454 /* execute the recieved message */
455 passed
= SendMessage(dde_window
, remote_message
->message
,
456 remote_message
->wParam
, remote_message
->lParam
);
458 /* Tell the sended, that the message is here */
459 dde_proc_send_ack(remote_message
->wParam
, passed
);
462 passed
= PostMessage(dde_window
, remote_message
->message
,
463 remote_message
->wParam
, remote_message
->lParam
);
464 if (passed
== FALSE
) {
465 /* Tell the sender, that the message is here, and failed */
466 dde_proc_send_ack(remote_message
->wParam
, FALSE
);
469 /* ack will be sent later, at the first peek/get message */
470 dde_proc_add_fifo(remote_message
->wParam
);
477 /* iterate through all the windows */
478 for (hwnd
= GetTopWindow(GetDesktopWindow());
479 hwnd
&& (window
= WIN_FindWndPtr(hwnd
))!=NULL
;
480 hwnd
= window
->hwndNext
) {
481 if (window
->dwStyle
& WS_POPUP
|| window
->dwStyle
& WS_CAPTION
) {
483 SendMessage( hwnd
, remote_message
->message
,
484 remote_message
->wParam
, remote_message
->lParam
);
486 PostMessage( hwnd
, remote_message
->message
,
487 remote_message
->wParam
, remote_message
->lParam
);
491 /* replay with DDE_ACK after broadcasting in DDE_GetRemoteMessage */
492 dde_proc_send_ack(remote_message
->wParam
, TRUE
);
502 ack_wnd
= dde_proc_shift_fifo();
504 dde_proc_send_ack(ack_wnd
, TRUE
);
505 usleep(10000); /* force unix task switch */
510 void dde_msg_setup(int *msg_ptr
)
512 *msg_ptr
= msgget (IPC_PRIVATE
, IPC_CREAT
| 0700);
514 perror("dde_msg_setup fails to get message queue");
517 /* do we have dde handling in the window ?
518 * If we have, atom usage will make this instance of wine set up
521 void DDE_TestDDE(HWND hwnd
)
524 if (main_block
!= NULL
)
526 dprintf_msg(stddeb
,"DDE_TestDDE(0x%04x)\n", hwnd
);
529 /* just send a message to see how things are going */
530 SendMessage( hwnd
, WM_DDE_INITIATE
, 0, 0);
533 void dde_proc_delete(int proc_idx
)
535 dde_proc_done(&main_block
->proc
[proc_idx
]);
537 void stop_wait(int a
)
541 switch(stop_wait_op
) {
543 siglongjmp(env_get_ack
,1);
544 break; /* never reached */
546 siglongjmp(env_wait_x
,1);
547 break; /* never reached */
553 static void print_dde_message(char *desc
, MSG
*msg
)
555 extern const char *MessageTypeNames
[];
556 extern int debug_last_handle_size
;
560 DDEADVISE
*ddeadvise
;
564 if (is_dde_handle(msg
->lParam
& 0xffff) )
565 ptr
=DDE_AttachHandle(msg
->lParam
&0xffff, NULL
);
568 wStatus
=LOWORD(msg
->lParam
);
569 hWord
=HIWORD(msg
->lParam
);
571 fprintf(stddeb
,"%s", desc
);
572 fprintf(stddeb
,"%04x %04x==%s %04x %08lx ",
573 msg
->hwnd
, msg
->message
,MessageTypeNames
[msg
->message
],
574 msg
->wParam
, msg
->lParam
);
575 switch(msg
->message
) {
576 case WM_DDE_INITIATE
:
579 case WM_DDE_TERMINATE
:
583 /* DDEADVISE: hOptions in WM_DDE_ADVISE message */
586 fprintf(stddeb
,"fDeferUpd=%d,fAckReq=%d,cfFormat=0x%x",
587 ddeadvise
->fDeferUpd
, ddeadvise
->fAckReq
,
588 ddeadvise
->cfFormat
);
590 fprintf(stddeb
,"NO-DATA");
591 fprintf(stddeb
," atom=0x%x",hWord
);
594 case WM_DDE_UNADVISE
:
595 fprintf(stddeb
,"format=0x%x, atom=0x%x",wStatus
,hWord
);
598 ddeack
=(DDEACK
*)&wStatus
;
599 fprintf(stddeb
,"bAppReturnCode=%d,fBusy=%d,fAck=%d",
600 ddeack
->bAppReturnCode
, ddeack
->fBusy
, ddeack
->fAck
);
602 fprintf(stddeb
,"(True)");
604 fprintf(stddeb
,"(False)");
610 fprintf(stddeb
,"fResponse=%d,fRelease=%d,"
611 "fAckReq=%d,cfFormat=0x%x,value=\"%.*s\"",
612 ddedata
->fResponse
, ddedata
->fRelease
,
613 ddedata
->fAckReq
, ddedata
->cfFormat
,
614 debug_last_handle_size
- (int)sizeof(*ddedata
)+1,
617 fprintf(stddeb
,"NO-DATA");
618 fprintf(stddeb
," atom=0x%04x",hWord
);
624 fprintf(stddeb
,"fRelease=%d,cfFormat=0x%x,value[0]='%c'",
625 ddepoke
->fRelease
, ddepoke
->cfFormat
, ddepoke
->Value
[0]);
627 fprintf(stddeb
,"NO-DATA");
628 fprintf(stddeb
," atom=0x%04x",hWord
);
631 fprintf(stddeb
,"\n");
634 void dde_proc_done(dde_proc proc
)
637 msgctl(proc
->msg
, IPC_RMID
, NULL
);
640 shm_delete_chain(&proc
->shmid
);
641 shm_sem_done(&proc
->sem
);
644 /* delete entry, if old junk */
645 void dde_proc_refresh(dde_proc proc
)
650 if (kill(proc
->pid
, 0) != -1)
653 /* get here if entry non empty, and the process does not exist */
661 for (i
=0 ; i
< DDE_WINDOWS
; i
++)
662 main_block
->windows
[i
].proc_idx
= FREE_WND
;
665 static BOOL
DDE_ProcHasWindows(int proc_idx
)
670 for ( i
=0 ; i
< DDE_WINDOWS
; i
++) {
671 tested
= &main_block
->windows
[ i
];
673 if (tested
->proc_idx
== proc_idx
)
678 /* Look for hwnd in the hash table of DDE windows,
679 * Delete it from there. If there are no more windows for this
680 * process, remove the process from the DDE data-structure.
681 * If there are no more processes - delete the whole DDE struff.
683 * This is inefficient, but who cares for the efficiency of this rare
686 void DDE_DestroyWindow(HWND hwnd
)
692 if (main_block
== NULL
)
695 dde_wnd_idx
= hwnd
% DDE_WINDOWS
;
697 for ( i
=0 ; i
< DDE_WINDOWS
; i
++, dde_wnd_idx
++) {
698 if (dde_wnd_idx
>= DDE_WINDOWS
)
699 dde_wnd_idx
-= DDE_WINDOWS
; /* wrap-around */
701 tested
= &main_block
->windows
[ dde_wnd_idx
];
702 if (tested
->proc_idx
== FREE_WND
)
703 return; /* No window will get deleted here */
705 if (tested
->wnd
== hwnd
&& tested
->proc_idx
== curr_proc_idx
) {
707 tested
->proc_idx
= DELETED_WND
;
708 if (DDE_ProcHasWindows( curr_proc_idx
))
710 while (dde_reschedule()) /* make sure there are no other */
711 /* processes waiting for acknowledgment */
713 dde_proc_delete( curr_proc_idx
);
714 if (DDE_no_of_attached() == 1)
717 shmdt( (void *) main_block
);
725 #endif /* CONFIG_IPC */