Imported Upstream version 1.2.2
[platform/upstream/libSM.git] / src / SMlibint.h
1 /*
2
3 Copyright 1993, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 /*
28  * Author: Ralph Mor, X Consortium
29  */
30
31 #ifndef _SMLIBINT_H_
32 #define _SMLIBINT_H_
33
34 #include <X11/Xos.h>
35 #include <X11/Xfuncs.h>
36 #include <X11/Xmd.h>
37 #include <X11/ICE/ICEmsg.h>
38 #include <X11/ICE/ICEproto.h>
39 #include <X11/SM/SMproto.h>
40
41 #include <stdlib.h>
42
43 #ifndef NULL
44 #include <stddef.h>
45 #endif
46
47
48 /*
49  * Vendor & Release
50  */
51
52 #define SmVendorString  "MIT"
53 #define SmReleaseString "1.0"
54
55
56 /*
57  * Pad to a 64 bit boundary
58  */
59
60 #define PAD64(_bytes) ((8 - ((unsigned int) (_bytes) % 8)) % 8)
61
62 #define PADDED_BYTES64(_bytes) (_bytes + PAD64 (_bytes))
63
64
65 /*
66  * Pad to 32 bit boundary
67  */
68
69 #define PAD32(_bytes) ((4 - ((unsigned int) (_bytes) % 4)) % 4)
70
71 #define PADDED_BYTES32(_bytes) (_bytes + PAD32 (_bytes))
72
73
74 /*
75  * Number of 8 byte units in _bytes.
76  */
77
78 #define WORD64COUNT(_bytes) (((unsigned int) ((_bytes) + 7)) >> 3)
79
80
81 /*
82  * Compute the number of bytes for an ARRAY8 representation
83  */
84
85 #define ARRAY8_BYTES(_len) (4 + _len + PAD64 (4 + _len))
86
87
88
89 /*
90  * Byte swapping
91  */
92
93 /* byte swap a long literal */
94 #define lswapl(_val) ((((_val) & 0xff) << 24) |\
95                    (((_val) & 0xff00) << 8) |\
96                    (((_val) & 0xff0000) >> 8) |\
97                    (((_val) >> 24) & 0xff))
98
99 /* byte swap a short literal */
100 #define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
101
102
103 /*
104  * STORE macros
105  */
106
107 #define STORE_CARD32(_pBuf, _val) \
108 { \
109     *((CARD32 *) _pBuf) = _val; \
110     _pBuf += 4; \
111 }
112
113
114 /*
115  * EXTRACT macros
116  */
117
118 #define EXTRACT_CARD16(_pBuf, _swap, _val) \
119 { \
120     _val = *((CARD16 *) _pBuf); \
121     _pBuf += 2; \
122     if (_swap) \
123         _val = lswaps (_val); \
124 }
125
126 #define EXTRACT_CARD32(_pBuf, _swap, _val) \
127 { \
128     _val = *((CARD32 *) _pBuf); \
129     _pBuf += 4; \
130     if (_swap) \
131         _val = lswapl (_val); \
132 }
133
134
135 /*
136  * Compute the number of bytes for a LISTofPROPERTY representation
137  */
138
139 #define LISTOF_PROP_BYTES(_numProps, _props, _bytes) \
140 { \
141     int _i, _j; \
142     _bytes = 8; \
143     for (_i = 0; _i < _numProps; _i++) \
144     { \
145         _bytes += (8 + ARRAY8_BYTES (strlen (_props[_i]->name)) + \
146             ARRAY8_BYTES (strlen (_props[_i]->type))); \
147 \
148         for (_j = 0; _j < _props[_i]->num_vals; _j++) \
149             _bytes += ARRAY8_BYTES (_props[_i]->vals[_j].length); \
150     } \
151 }
152
153
154 /*
155  * STORE FOO
156  */
157
158 #define STORE_ARRAY8(_pBuf, _len, _array8) \
159 { \
160     STORE_CARD32 (_pBuf, (CARD32) _len); \
161     if (_len) \
162         memcpy (_pBuf, _array8, _len); \
163     _pBuf += _len + PAD64 (4 + _len); \
164 }
165
166 #define STORE_LISTOF_PROPERTY(_pBuf, _count, _props) \
167 { \
168     int _i, _j; \
169     STORE_CARD32 (_pBuf, _count); \
170     _pBuf += 4; \
171     for (_i = 0; _i < _count; _i++) \
172     { \
173         STORE_ARRAY8 (_pBuf, strlen (_props[_i]->name), _props[_i]->name); \
174         STORE_ARRAY8 (_pBuf, strlen (_props[_i]->type), _props[_i]->type); \
175         STORE_CARD32 (_pBuf, _props[_i]->num_vals); \
176         _pBuf += 4; \
177         for (_j = 0; _j < _props[_i]->num_vals; _j++) \
178         { \
179             STORE_ARRAY8 (_pBuf, _props[_i]->vals[_j].length, \
180                 (char *) _props[_i]->vals[_j].value); \
181         } \
182     } \
183 }
184
185
186 /*
187  * EXTRACT FOO
188  */
189
190 #define EXTRACT_ARRAY8(_pBuf, _swap, _len, _array8) \
191 { \
192     EXTRACT_CARD32 (_pBuf, _swap, _len); \
193     _array8 = malloc (_len + 1); \
194     memcpy (_array8, _pBuf, _len); \
195     _array8[_len] = '\0'; \
196     _pBuf += _len + PAD64 (4 + _len); \
197 }
198
199 #define EXTRACT_ARRAY8_AS_STRING(_pBuf, _swap, _string) \
200 { \
201     CARD32 _len; \
202     EXTRACT_CARD32 (_pBuf, _swap, _len); \
203     _string = malloc (_len + 1); \
204     memcpy (_string, _pBuf, _len); \
205     _string[_len] = '\0'; \
206     _pBuf += _len + PAD64 (4 + _len); \
207 }
208
209 #define EXTRACT_LISTOF_PROPERTY(_pBuf, _swap, _count, _props) \
210 { \
211     int _i, _j; \
212     EXTRACT_CARD32 (_pBuf, _swap, _count); \
213     _pBuf += 4; \
214     _props = malloc (_count * sizeof (SmProp *)); \
215     for (_i = 0; _i < _count; _i++) \
216     { \
217         _props[_i] = malloc (sizeof (SmProp)); \
218         EXTRACT_ARRAY8_AS_STRING (_pBuf, _swap, _props[_i]->name); \
219         EXTRACT_ARRAY8_AS_STRING (_pBuf, _swap, _props[_i]->type); \
220         EXTRACT_CARD32 (_pBuf, _swap, _props[_i]->num_vals); \
221         _pBuf += 4; \
222         _props[_i]->vals = malloc ( \
223             _props[_i]->num_vals * sizeof (SmPropValue)); \
224         for (_j = 0; _j < _props[_i]->num_vals; _j++) \
225         { \
226             char *_temp; \
227             EXTRACT_ARRAY8 (_pBuf, _swap, _props[_i]->vals[_j].length, _temp);\
228             _props[_i]->vals[_j].value = (SmPointer) _temp; \
229         } \
230     } \
231 }
232
233
234 #define SKIP_ARRAY8(_pBuf, _swap) \
235 { \
236     CARD32 _len; \
237     EXTRACT_CARD32 (_pBuf, _swap, _len); \
238     _pBuf += _len + PAD64 (4 + _len); \
239 }
240
241 #define SKIP_LISTOF_PROPERTY(_pBuf, _swap) \
242 { \
243     CARD32 _i, _j; \
244     CARD32 _count; \
245     EXTRACT_CARD32 (_pBuf, _swap, _count); \
246     _pBuf += 4; \
247     for (_i = 0; _i < _count; _i++) \
248     { \
249         CARD32 _numvals; \
250         SKIP_ARRAY8 (_pBuf, _swap); \
251         SKIP_ARRAY8 (_pBuf, _swap); \
252         EXTRACT_CARD32 (_pBuf, _swap, _numvals); \
253         _pBuf += 4; \
254         for (_j = 0; _j < _numvals; _j++) \
255             SKIP_ARRAY8 (_pBuf, _swap);\
256     } \
257 }
258
259
260 /*
261  * Client replies not processed by callbacks (we block for them).
262  */
263
264 typedef struct {
265     Status      status;         /* if 1, client successfully registered */
266     char        *client_id;
267 } _SmcRegisterClientReply;
268
269
270 /*
271  * Waiting for Interact
272  */
273
274 typedef struct _SmcInteractWait {
275     SmcInteractProc             interact_proc;
276     SmPointer                   client_data;
277     struct _SmcInteractWait     *next;
278 } _SmcInteractWait;
279
280
281 /*
282  * Waiting for SaveYourselfPhase2
283  */
284
285 typedef struct _SmcPhase2Wait {
286     SmcSaveYourselfPhase2Proc   phase2_proc;
287     SmPointer                   client_data;
288 } _SmcPhase2Wait;
289
290
291 /*
292  * Waiting for Properties Reply
293  */
294
295 typedef struct _SmcPropReplyWait {
296     SmcPropReplyProc            prop_reply_proc;
297     SmPointer                   client_data;
298     struct _SmcPropReplyWait    *next;
299 } _SmcPropReplyWait;
300
301
302
303 /*
304  * Client connection object
305  */
306
307 struct _SmcConn {
308
309     /*
310      * Some state.
311      */
312
313     unsigned int save_yourself_in_progress : 1;
314     unsigned int shutdown_in_progress : 1;
315     unsigned int unused1 : 6;                /* future use */
316     unsigned int unused2 : 8;                /* future use */
317
318
319     /*
320      * We use ICE to esablish a connection with the SM.
321      */
322
323     IceConn             iceConn;
324
325
326     /*
327      * Major and minor versions of the XSMP.
328      */
329
330     int                 proto_major_version;
331     int                 proto_minor_version;
332
333
334     /*
335      * The session manager vendor and release number.
336      */
337
338     char                *vendor;
339     char                *release;
340
341
342     /*
343      * The Client Id uniquely identifies this client to the session manager.
344      */
345
346     char                *client_id;
347
348
349     /*
350      * Callbacks to be invoked when messages arrive from the session manager.
351      * These callbacks are specified at SmcOpenConnection time.
352      */
353
354     SmcCallbacks        callbacks;
355
356
357     /*
358      * We keep track of all Interact Requests sent by the client.  When the
359      * Interact message arrives, we remove it from the list (a FIFO list
360      * is maintained).
361      */
362
363     _SmcInteractWait    *interact_waits;
364
365
366     /*
367      * If we send a SaveYourselfPhase2Request, we wait for SaveYourselfPhase2.
368      */
369
370     _SmcPhase2Wait      *phase2_wait;
371
372
373     /*
374      * We keep track of all Get Properties sent by the client.  When the
375      * Properties Reply arrives, we remove it from the list (a FIFO list
376      * is maintained).
377      */
378
379     _SmcPropReplyWait   *prop_reply_waits;
380 };
381
382
383
384 /*
385  * Session manager connection object
386  */
387
388 struct _SmsConn {
389
390     /*
391      * Some state.
392      */
393
394     unsigned int save_yourself_in_progress : 1;
395     unsigned int can_cancel_shutdown : 1;
396     unsigned int interact_in_progress : 1;
397     unsigned int unused1 : 5;                /* future use */
398     unsigned int unused2 : 8;                /* future use */
399
400
401     /*
402      * We use ICE to esablish a connection with the client.
403      */
404
405     IceConn             iceConn;
406
407
408     /*
409      * Major and minor versions of the XSMP.
410      */
411
412     int                 proto_major_version;
413     int                 proto_minor_version;
414
415
416     /*
417      * The Client Id uniquely identifies this client to the session manager.
418      */
419
420     char                *client_id;
421
422
423     /*
424      * Callbacks to be invoked when messages arrive from the client.
425      */
426
427     SmsCallbacks        callbacks;
428
429
430     /*
431      * What type of interaction is allowed - SmInteractStyle{None,Errors,Any}
432      */
433
434     char                interaction_allowed;
435 };
436
437
438
439 /*
440  * Extern declarations
441  */
442 extern void
443 _SmcProcessMessage(IceConn iceConn, IcePointer clientData, int opcode,
444                    unsigned long length, Bool swap,
445                    IceReplyWaitInfo *replyWait, Bool *replyReadyRet);
446
447 extern void
448 _SmsProcessMessage(IceConn iceConn, IcePointer clientData, int opcode,
449                    unsigned long length, Bool swap);
450
451 extern void
452 _SmcDefaultErrorHandler(SmcConn smcConn, Bool swap, int offendingMinorOpcode,
453                         unsigned long offendingSequence, int errorClass,
454                         int severity, SmPointer values);
455
456 extern void
457 _SmsDefaultErrorHandler(SmsConn smsConn, Bool swap, int offendingMinorOpcode,
458                         unsigned long offendingSequence, int errorClass,
459                         int severity, SmPointer values);
460
461 extern int     _SmcOpcode;
462 extern int     _SmsOpcode;
463
464 extern SmsNewClientProc _SmsNewClientProc;
465 extern SmPointer        _SmsNewClientData;
466
467 extern SmcErrorHandler _SmcErrorHandler;
468 extern SmsErrorHandler _SmsErrorHandler;
469
470 #endif /* _SMLIBINT_H_ */