e29f019da087683abd8ccd4a6ae2e7fb9a2dc28a
[framework/uifw/xorg/lib/libice.git] / src / replywait.c
1 /******************************************************************************
2
3
4 Copyright 1993, 1998  The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34
35
36 void
37 _IceAddReplyWait (
38         IceConn                 iceConn,
39         IceReplyWaitInfo        *replyWait
40 )
41 {
42     /*
43      * Add this replyWait to the end of the list (only if the
44      * replyWait is not already in the list).
45      */
46
47     _IceSavedReplyWait  *savedReplyWait;
48     _IceSavedReplyWait  *prev, *last;
49
50     prev = NULL;
51     last = iceConn->saved_reply_waits;
52
53     while (last)
54     {
55         if (last->reply_wait == replyWait)
56             return;
57
58         prev = last;
59         last = last->next;
60     }
61         
62     savedReplyWait = (_IceSavedReplyWait *) malloc (
63         sizeof (_IceSavedReplyWait));
64
65     savedReplyWait->reply_wait = replyWait;
66     savedReplyWait->reply_ready = False;
67     savedReplyWait->next = NULL;
68
69     if (prev == NULL)
70         iceConn->saved_reply_waits = savedReplyWait;
71     else
72         prev->next = savedReplyWait;
73 }
74
75
76 \f
77 IceReplyWaitInfo *
78 _IceSearchReplyWaits (
79         IceConn iceConn,
80         int     majorOpcode
81 )
82 {
83     /*
84      * Return the first replyWait in the list with the given majorOpcode
85      */
86
87     _IceSavedReplyWait  *savedReplyWait = iceConn->saved_reply_waits;
88
89     while (savedReplyWait && !savedReplyWait->reply_ready &&
90         savedReplyWait->reply_wait->major_opcode_of_request != majorOpcode)
91     {
92         savedReplyWait = savedReplyWait->next;
93     }
94
95     return (savedReplyWait ? savedReplyWait->reply_wait : NULL);
96 }
97
98
99 \f
100 void
101 _IceSetReplyReady (
102         IceConn                 iceConn,
103         IceReplyWaitInfo        *replyWait
104 )
105 {
106     /*
107      * The replyWait specified has a reply ready.
108      */
109
110     _IceSavedReplyWait  *savedReplyWait = iceConn->saved_reply_waits;
111
112     while (savedReplyWait && savedReplyWait->reply_wait != replyWait)
113         savedReplyWait = savedReplyWait->next;
114
115     if (savedReplyWait)
116         savedReplyWait->reply_ready = True;
117 }
118
119
120 \f
121 Bool
122 _IceCheckReplyReady (
123         IceConn                 iceConn,
124         IceReplyWaitInfo        *replyWait
125 )
126 {
127     _IceSavedReplyWait  *savedReplyWait = iceConn->saved_reply_waits;
128     _IceSavedReplyWait  *prev = NULL;
129     Bool                found = False;
130     Bool                ready;
131
132     while (savedReplyWait && !found)
133     {
134         if (savedReplyWait->reply_wait == replyWait)
135             found = True;
136         else
137         {
138             prev = savedReplyWait;
139             savedReplyWait = savedReplyWait->next;
140         }
141     }
142
143     ready = found && savedReplyWait->reply_ready;
144
145     if (ready)
146     {
147         if (prev == NULL)
148             iceConn->saved_reply_waits = savedReplyWait->next;
149         else
150             prev->next = savedReplyWait->next;
151         
152         free ((char *) savedReplyWait);
153     }
154
155     return (ready);
156 }