An application can use its own message port for the IDCMP instead of the one set up by Intuition, although some care is required. As described earlier, IDCMP communication takes place through a pair of Exec message ports attached to a window: the userport and the windowport. The UserPort is the port where the application receives IDCMP messages from Intuition. The WindowPort is the reply port where Intuition receives replies from the application (via the replymsg() function). In the simplest case, Intuition allocates (and deallocates) both of these ports when the program opens a window with non-NULL IDCMP flags. Intuition will also allocate these ports if the application calls modifyidcmp() with non-null flags for a window that has null idcmp flags. These port variables will be set to NULL if there is no message port allocated, otherwise they will contain a pointer to a message port. If the windowport is not already opened when either openwindow() or modifyidcmp() is called, it will be allocated and initialized. The userport is checked separately to see whether it is already opened. When Intuition initializes the userport, it also allocates a signal bit with a call to allocsignal(). since the application makes the call to openwindowtaglist() or modifyidcmp(), this signal bit is valid for the application's task. The address of the application's task is saved in the sigtask variable of the message port. The program may choose to supply its own userport. this might be done in an environment where the program is using several windows and would prefer to monitor the input using only one message port. This is done by with the following procedure: 1. Create a port for the IDCMP by calling either the Exec function createmsgport() or the amiga.lib function createport(), both of which return a pointer to a port. (CreateMsgPort() is a new Exec function in V36 and can therefore only be used on systems running Release 2 or a later version of the OS.) 2. Open the windows with no IDCMP flags set. This will prevent Intuition from allocating a port for this window. 3. Place a pointer to the port created in step 1 into the userport field of the window structure. 4. Call modifyidcmp() to set the desired idcmp flags for the port. Intuition will use the port supplied with the window. Be Careful with Shared IDCMP Ports. ----------------------------------- If the application is sharing an IDCMP among several windows, it must be very careful not to call modifyidcmp(window,null) for any of the windows that are using the shared port, as this will free the port and the signal bit. 5. When an application decides to close a window that has a shared IDCMP, there may be messages waiting at the port for any of the windows including the window being closed. It is essential that messages destined for a given window be removed and replied to before that window is closed. closewindowsafely(), listed in the next example, performs proper message cleanup before closing such a window. It also sets the window's userport to null so that intuition knows not to delete the port, which should be done by the application in this case. It is incorrect (and dangerous) to simply call closewindow() on a window that has a shared IDCMP. Note that closewindowsafely() assumes that the window has a userport. 6. After all windows have been closed, and the port has been removed from each, delete the port that was created in step 1. Use the amiga.lib function deleteport() (if createport() was used) or the Exec function deletemsgport() (if createmsgport() was used). closing a window with a shared idcmp