Debian Codegraph @

clients/mwm/WmWsmLib/pack.c

raw
1 /* $XConsortium: pack.c /main/5 1995/07/15 20:38:40 drk $ */
2 /*
3 * Motif
4 *
5 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
6 *
7 * These libraries and programs are free software; you can
8 * redistribute them and/or modify them under the terms of the GNU
9 * Lesser General Public License as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * These libraries and programs are distributed in the hope that
14 * they will be useful, but WITHOUT ANY WARRANTY; without even the
15 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU Lesser General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with these librararies and programs; if not, write
21 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
22 * Floor, Boston, MA 02110-1301 USA
23 *
24 */
25 /*
26 * HISTORY
27 */
28
29 #include "wsm_proto.h"
30
31 /* this will redifine bzero to memset if needed. */
32 #include <Xm/XmosP.h>
33
34 #define COUNT_SIZE (sizeof(CARD16))
35 #define BOOL_SIZE 1 /* at least 1 byte for a boolean. */
36
37 /*
38 * More intuitive names for various functions.
39 */
40
41 #define PackBoolean(d, num) PackCARD8((d), (CARD8) (num))
42 #define PackListNum(d, num) PackCARD16((d), (CARD16) (num))
43 #define PackWindow(d, num) PackCARD32((d), (CARD32) (num))
44 #define PackProperty(d, num) PackCARD32((d), (CARD32) (num))
45
46 #define UnpackBoolean(data) ((Boolean) UnpackCARD8(data))
47 #define UnpackListNum(data) ((int) UnpackCARD16(data))
48 #define UnpackWindow(data) ((Window) UnpackCARD32(data))
49 #define UnpackProperty(data) ((WindowProperty) UnpackCARD32(data))
50
51 #define START_CHECK_MASK 0x80
52
53 /************************************************************
54 *
55 * Function definitions.
56 *
57 ************************************************************/
58
59 /*
60 * Routines for getting the amount of space that a request will take in the
61 * protocol data stream.
62 */
63
64 static int WinInfoSizePacked(Display *, int, WSMWinInfo *);
65 static int WinEntrySizePacked(Display *, int, WSMWinEntry *);
66 static int WinDataSizePacked(Display *, int,
67 WSMWinData *, int, WSMConfigFormatType);
68
69 /*
70 * Routines for packing protocol data stream.
71 */
72
73 static MessageData PackConfigFormat(MessageData, WSMConfigFormatReply *);
74 static MessageData PackSingleAttribute(MessageData, WSMAttribute *, int);
75 static MessageData PackWinInfo(Display *, int, MessageData,
76 WSMWinInfo *);
77 static MessageData PackWinEntry(Display *, int, MessageData,
78 WSMWinEntry *);
79 static MessageData PackWinData(Display *, int, MessageData,
80 WSMWinData *, int, WSMConfigFormatType);
81 /* public */ MessageData PackCARD16(MessageData, CARD16);
82 /* public */ MessageData PackCARD8(MessageData, CARD8);
83 /* public */ MessageData PackString(MessageData, String);
84 /* public */ MessageData PackCARD32(MessageData, CARD32);
85
86 /*
87 * Routines for unpacking protocol data stream.
88 */
89
90 static void UnpackWinData(MessageData *, Display *,
91 int, WSMConfigFormatType, WSMWinData **, int *);
92 static void UnpackSingleWinDataRec(MessageData *, WSMAttribute *,WSMWinData *);
93 static void UnpackWinInfo(MessageData *, Display *, int, WSMWinInfo *);
94 static void UnpackWinEntry(MessageData *, Display *, int, WSMWinEntry *);
95
96 /* public */ String UnpackString(MessageData *);
97 /* public */ CARD32 UnpackCARD32(MessageData *);
98 /* public */ CARD16 UnpackCARD16(MessageData *);
99 /* public */ CARD8 UnpackCARD8(MessageData *);
100
101
102 /************************************************************
103 *
104 * These two routines take data stored in a structure that
105 * humans can understand and pack it down into nice little
106 * protocol requests defined by the OSF WSM Protocol spec.
107 *
108 ************************************************************/
109
110 /* Function Name: _WSMPackRequest
111 * Description: Packs up the request passed into an appropriate
112 * message data structure.
113 * Arguments: dpy - the display we will be sending this message on.
114 * screen_num - The screen we will be sending this message to.
115 * request - request to send.
116 * RETURNED msg_data_len - The length of the message data.
117 * RETURNED error - An error code if something went wrong.
118 * Returns: A pointer to a message data struct.
119 *
120 * NOTE: The message data struct must be freed by the calling routine.
121 */
122
123 MessageData
124 _WSMPackRequest(Display *dpy, int screen_num, WSMRequest *request,
125 unsigned long *msg_data_len, WSMErrorCode *error)
126 {
127 MessageData data, save;
128 int i;
129 long size;
130 WSMWinInfo *win_info;
131 WSMConfigFormatData *global_attrs, *win_attrs, *icon_attrs;
132
133 *msg_data_len = 0; /* default value. */
134
135 global_attrs = _WSMGetConfigFormat(dpy, screen_num, WSM_GLOBAL_FMT);
136 win_attrs = _WSMGetConfigFormat(dpy, screen_num, WSM_WINDOW_FMT);
137 icon_attrs = _WSMGetConfigFormat(dpy, screen_num, WSM_ICON_FMT);
138
139 *error = WSM_SUCCESS;
140
141 if (_WSMRequiresConfigFormat(request->any.type) &&
142 (global_attrs->num_attrs == 0) &&
143 (win_attrs->num_attrs == 0) && (icon_attrs->num_attrs == 0))
144 {
145 *error = WSM_ERROR_NO_CONFIG_FORMAT;
146 return(NULL);
147 }
148 if (((request->any.type == WSM_CONNECT) ||
149 (request->any.type == WSM_CONFIG_FMT)) &&
150 ((global_attrs->num_attrs != 0) ||
151 (win_attrs->num_attrs != 0) || (icon_attrs->num_attrs != 0)))
152 {
153 *error = WSM_ERROR_ALREADY_HAVE_CONFIG_FORMAT;
154 return(NULL);
155 }
156
157 /* before sending extensions make sure screen_info config format
158 is cleared */
159 /* may want to remove this and leave it up to application writer */
160 if ((request->any.type == WSM_EXTENSIONS) &&
161 ((global_attrs->num_attrs != 0) ||
162 (win_attrs->num_attrs != 0) || (icon_attrs->num_attrs != 0)))
163 {
164 _WSMClearConfigScreenInfo(dpy,screen_num);
165 }
166 /*
167 * Calculate the size of the request.
168 */
169 switch (request->any.type) {
170 case WSM_CONNECT:
171 size = COUNT_SIZE;
172 size += sizeof(CARD8) * request->connect.num_versions;
173 break;
174 case WSM_EXTENSIONS:
175 size = COUNT_SIZE;
176 for (i = 0; i < request->extensions.num_extensions; i++) {
177 size += (strlen(request->extensions.extension_suggestions[i]) +
178 COUNT_SIZE);
179 }
180 break;
181 case WSM_CONFIG_FMT:
182 size = 0;
183 break;
184 case WSM_GET_STATE:
185 size = sizeof(CARD32) + BOOL_SIZE;
186 break;
187 case WSM_SET_STATE:
188 size = COUNT_SIZE; /* The count of window info. */
189 win_info = request->set_state.win_info_list;
190 for (i = 0; i < request->set_state.num_win_info_list; i++,win_info++)
191 size += WinInfoSizePacked(dpy, screen_num, win_info);
192 break;
193 case WSM_REG_WINDOW:
194 size = sizeof(CARD32);
195 break;
196 case WSM_WM_GET_BACKGROUND_WINDOW:
197 size = sizeof(CARD16);
198 break;
199 case WSM_WM_SET_BACKGROUND_WINDOW:
200 size = sizeof(CARD32);
201 break;
202 case WSM_WM_WINDOWS:
203 size = sizeof(CARD32); /* flag */
204 size += COUNT_SIZE; /* prop count */
205 size += request->wm_windows.num_window_properties * sizeof(Window);
206 size += COUNT_SIZE; /* num match attributes */
207 for (i=0; i<request->wm_windows.num_match_attributes; i++)
208 {
209 size += COUNT_SIZE; /* num attributes */
210 size += 2 *
211 request->wm_windows.match_attributes[i]->num_attributes *
212 sizeof(WindowProperty);
213 }
214 break;
215 case WSM_WM_FOCUS:
216 size = 0;
217 break;
218 case WSM_WM_POINTER:
219 size = 0;
220 break;
221 default:
222 return(NULL);
223 }
224
225 save = data = (MessageData) XtMalloc(size);
226
227 /*
228 * Fill in the message.
229 */
230 switch (request->any.type) {
231 case WSM_CONNECT:
232 data = PackListNum(data, request->connect.num_versions);
233 for (i = 0; i < request->connect.num_versions; i++)
234 data = PackCARD8(data, (CARD8) request->connect.known_versions[i]);
235 break;
236 case WSM_EXTENSIONS:
237 data = PackListNum(data, request->extensions.num_extensions);
238 for (i = 0; i < request->extensions.num_extensions; i++)
239 data=PackString(data,request->extensions.extension_suggestions[i]);
240 break;
241 case WSM_CONFIG_FMT:
242 break;
243 case WSM_GET_STATE:
244 data = PackWindow(data, request->get_state.window);
245 (void) PackBoolean(data, request->get_state.diffs_allowed);
246 break;
247 case WSM_SET_STATE:
248 win_info = request->set_state.win_info_list;
249 data = PackListNum(data, request->set_state.num_win_info_list);
250 for (i = 0; i < request->set_state.num_win_info_list; i++, win_info++)
251 data = PackWinInfo(dpy, screen_num, data, win_info);
252 break;
253 case WSM_REG_WINDOW:
254 (void) PackWindow(data, request->register_window.window);
255 break;
256 case WSM_WM_GET_BACKGROUND_WINDOW:
257 (void) PackCARD16(data, request->get_background.screen);
258 break;
259 case WSM_WM_SET_BACKGROUND_WINDOW:
260 (void) PackWindow(data, request->set_background.window);
261 break;
262 case WSM_WM_WINDOWS:
263 {
264 int i, j;
265
266 data = PackCARD32(data, request->wm_windows.location_flag);
267 data = PackListNum(data, request->wm_windows.num_window_properties);
268 for (i=0; i<request->wm_windows.num_window_properties; i++)
269 data = PackProperty(data, request->wm_windows.window_properties[i]);
270
271 data = PackListNum(data, request->wm_windows.num_match_attributes);
272 for (i = 0; i < request->wm_windows.num_match_attributes; i++)
273 {
274 AttributePair *match_attr;
275
276 match_attr = request->wm_windows.match_attributes[i];
277 data = PackListNum(data, match_attr->num_attributes);
278
279 for (j = 0; j < match_attr->num_attributes; j++)
280 data = PackProperty(data, match_attr->allowed_attributes[j]);
281
282 for (j = 0; j < match_attr->num_attributes; j++)
283 data = PackProperty(data, match_attr->prohibited_attributes[j]);
284 }
285 }
286 break;
287 case WSM_WM_FOCUS:
288 break;
289 case WSM_WM_POINTER:
290 break;
291 default:
292 break;
293 }
294
295 *msg_data_len = size;
296 return(save);
297 } /* _WSMPackRequest */
298
299 /* Function Name: _WSMPackReply
300 * Description: Packs up the Reply passed into an appropriate
301 * message data structure.
302 * Arguments: dpy - the display we will be sending this message on.
303 * screen_num - The screen we will be sending this message to.
304 * reply - reply to send.
305 * RETURNED msg_data_len - The length of the message data.
306 * Returns: A pointer to a message data struct.
307 *
308 * NOTE: The message data struct must be freed by the calling routine.
309 */
310
311 MessageData
312 _WSMPackReply(Display *dpy, int screen_num,
313 WSMReply *reply, unsigned long *msg_data_len)
314 {
315 WSMAttribute *attr;
316 WSMWinInfo *win_info;
317 WSMWinEntry *win_entry;
318 register int i;
319 long size;
320 MessageData data, save;
321
322 *msg_data_len = 0; /* default value. */
323
324 switch (reply->any.type) {
325 case WSM_CONNECT:
326 size = sizeof(CARD8);
327 break;
328 case WSM_EXTENSIONS:
329 size = COUNT_SIZE;
330 for (i = 0; i < reply->extensions.num_extensions; i++) {
331 size += (strlen(reply->extensions.extensions[i]) + COUNT_SIZE);
332 }
333 break;
334 case WSM_CONFIG_FMT:
335 size = BOOL_SIZE; /* accept_diffs. */
336 size += COUNT_SIZE; /* The count of global attributes. */
337 attr = reply->config_format.global_formats;
338 for (i = 0; i < reply->config_format.num_global_formats; i++) {
339 /*
340 * Bytes in string
341 */
342 size += strlen(XrmQuarkToString(attr[i].nameq)) * sizeof(CARD8);
343 /*
344 * len of string + size + is_list.
345 */
346 size += COUNT_SIZE + sizeof(CARD8) + BOOL_SIZE;
347 }
348 size += COUNT_SIZE; /* The count of window attributes. */
349 attr = reply->config_format.window_formats;
350 for (i = 0; i < reply->config_format.num_window_formats; i++) {
351 /*
352 * Bytes in string
353 */
354 size += strlen(XrmQuarkToString(attr[i].nameq)) * sizeof(CARD8);
355 /*
356 * len of string + size + is_list.
357 */
358 size += COUNT_SIZE + sizeof(CARD8) + BOOL_SIZE;
359 }
360 size += COUNT_SIZE; /* The count of icon attributes. */
361 attr = reply->config_format.icon_formats;
362 for (i = 0; i < reply->config_format.num_icon_formats; i++) {
363 /*
364 * Bytes in string
365 */
366 size += strlen(XrmQuarkToString(attr[i].nameq)) * sizeof(CARD8);
367 /*
368 * len of string + size + is_list.
369 */
370 size += COUNT_SIZE + sizeof(CARD8) + BOOL_SIZE;
371 }
372 break;
373 case WSM_GET_STATE:
374 size = COUNT_SIZE; /* The count of window info. */
375 win_info = reply->get_state.win_info_list;
376 for (i = 0; i < reply->get_state.num_win_info_list; i++,win_info++)
377 size += WinInfoSizePacked(dpy, screen_num, win_info);
378 break;
379 case WSM_SET_STATE:
380 size = 0;
381 break;
382 case WSM_REG_WINDOW:
383 size = WinDataSizePacked(dpy, screen_num,
384 reply->register_window.window_data,
385 reply->register_window.num_window_data,
386 WSM_WINDOW_FMT);
387 break;
388 case WSM_WM_GET_BACKGROUND_WINDOW:
389 size = sizeof(CARD32);
390 break;
391 case WSM_WM_SET_BACKGROUND_WINDOW:
392 size = sizeof(CARD32);
393 break;
394 case WSM_WM_WINDOWS:
395 size = COUNT_SIZE; /* The count of window entries. */
396 win_entry = reply->wm_windows.win_entry_list;
397 for (i = 0; i < reply->wm_windows.num_win_entry_list; i++,win_entry++)
398 size += WinEntrySizePacked(dpy, screen_num, win_entry);
399 break;
400 case WSM_WM_FOCUS:
401 size = sizeof(CARD32);
402 break;
403 case WSM_WM_POINTER:
404 size = sizeof(CARD32); /* window */
405 size += sizeof(CARD32); /* flag */
406 break;
407 default:
408 /*
409 * ||| Error Message.
410 */
411 return(NULL);
412 }
413
414 save = data = (MessageData) XtMalloc(size);
415
416 switch (reply->any.type) {
417 case WSM_CONNECT:
418 (void) PackCARD8(data, (CARD8) (reply->connect.version & 0xFF));
419 break;
420 case WSM_EXTENSIONS:
421 data = PackListNum(data, reply->extensions.num_extensions);
422 for (i = 0; i < reply->extensions.num_extensions; i++)
423 data = PackString(data, reply->extensions.extensions[i]);
424 break;
425 case WSM_CONFIG_FMT:
426 PackConfigFormat(data, &(reply->config_format));
427 break;
428 case WSM_GET_STATE:
429 win_info = reply->get_state.win_info_list;
430 data = PackListNum(data, reply->get_state.num_win_info_list);
431 for (i = 0; i < reply->get_state.num_win_info_list; i++, win_info++)
432 data = PackWinInfo(dpy, screen_num, data, win_info);
433 break;
434 case WSM_SET_STATE:
435 break;
436 case WSM_REG_WINDOW:
437 data = PackWinData(dpy, screen_num, data,
438 reply->register_window.window_data,
439 reply->register_window.num_window_data,
440 WSM_WINDOW_FMT);
441 break;
442 case WSM_WM_GET_BACKGROUND_WINDOW:
443 (void) PackWindow(data, reply->get_background.window);
444 break;
445 case WSM_WM_SET_BACKGROUND_WINDOW:
446 (void) PackWindow(data, reply->set_background.window);
447 break;
448 case WSM_WM_WINDOWS:
449 win_entry = reply->wm_windows.win_entry_list;
450 data = PackListNum(data, reply->wm_windows.num_win_entry_list);
451 for (i = 0; i < reply->wm_windows.num_win_entry_list; i++, win_entry++)
452 data = PackWinEntry(dpy, screen_num, data, win_entry);
453 break;
454 case WSM_WM_FOCUS:
455 (void) PackWindow(data, reply->wm_focus.window);
456 break;
457 case WSM_WM_POINTER:
458 (void) PackWindow(data, reply->wm_pointer.window);
459 (void) PackCARD32(data, reply->wm_pointer.location_flag);
460 break;
461 default:
462 break;
463 }
464
465 *msg_data_len = size;
466 return(save);
467 }/* _WSMPackReply */
468
469 /************************************************************
470 *
471 * These routines take data on the wire and put it into a structure
472 * that can be manipulated by the app.
473 *
474 ************************************************************/
475
476 /* Function Name: _WSMUnpackRequest
477 * Description: Unpacks the byte stream from the wire into a request
478 * message structure.
479 * Arguments: dpy - The Display.
480 * screen_num - The Screen number.
481 * data - The bits from accross the wire.
482 * len - The number of bits sent - Better error checking
483 * makes this more necessary.
484 * type - The type of message received.
485 * RETURNED request - This request structure is filled in with the data
486 * unpacked from the protocol.
487 * Returns: none.
488 */
489
490 /*ARGSUSED*/
491 void
492 _WSMUnpackRequest(Display *dpy, int screen_num, MessageData data,
493 unsigned long len, WSMRequestType type, WSMRequest *request)
494 {
495 register int i;
496 request->any.type = type; /* Save the type. */
497 request->any.allocated = False;
498
499 switch (request->any.type) {
500 case WSM_CONNECT:
501 request->connect.num_versions = UnpackListNum(&data);
502 request->connect.known_versions =
503 (short *) XtMalloc(sizeof(short) * request->connect.num_versions);
504 request->connect.allocated = True;
505 for (i = 0; i < request->connect.num_versions; i++)
506 request->connect.known_versions[i] = (short) UnpackCARD8(&data);
507 break;
508 case WSM_EXTENSIONS:
509 {
510 register int num;
511 register String *ptr;
512
513 num = request->extensions.num_extensions = UnpackListNum(&data);
514 ptr = (String *) XtMalloc(sizeof(String) * num);
515 request->extensions.extension_suggestions = ptr;
516 request->extensions.allocated = True;
517 for (i = 0; i < num; i++, ptr++)
518 *ptr = UnpackString(&data);
519 }
520 break;
521 case WSM_CONFIG_FMT:
522 break;
523 case WSM_GET_STATE:
524 request->get_state.window = UnpackWindow(&data);
525 request->get_state.diffs_allowed = UnpackBoolean(&data);
526 break;
527 case WSM_SET_STATE:
528 {
529 int num = UnpackListNum(&data);
530
531 request->set_state.num_win_info_list = num;
532 request->set_state.win_info_list =
533 (WSMWinInfo *) XtMalloc(sizeof(WSMWinInfo) * num);
534 request->extensions.allocated = True;
535
536 for (i = 0; i < num; i++) {
537 UnpackWinInfo(&data, dpy, screen_num,
538 request->set_state.win_info_list + i);
539 }
540 }
541 break;
542 case WSM_REG_WINDOW:
543 request->register_window.window = UnpackWindow(&data);
544 break;
545 case WSM_WM_GET_BACKGROUND_WINDOW:
546 request->get_background.screen = (int)UnpackCARD16(&data);
547 break;
548 case WSM_WM_SET_BACKGROUND_WINDOW:
549 request->set_background.window = UnpackWindow(&data);
550 break;
551 case WSM_WM_WINDOWS:
552 {
553 int num, i, j;
554
555 request->extensions.allocated = True;
556 request->wm_windows.location_flag = UnpackCARD32(&data);
557
558 num = request->wm_windows.num_window_properties = UnpackListNum(&data);
559 request->wm_windows.window_properties =
560 (WindowProperty *) XtMalloc(sizeof(WindowProperty) * num);
561 for (i=0; i<num; i++)
562 request->wm_windows.window_properties[i] = UnpackProperty(&data);
563
564
565 num = request->wm_windows.num_match_attributes = UnpackListNum(&data);
566 request->wm_windows.match_attributes =
567 (AttributePair **) XtMalloc(sizeof(AttributePair*) * num);
568
569 for (i=0; i<request->wm_windows.num_match_attributes; i++)
570 {
571 num = UnpackListNum(&data);
572
573 request->wm_windows.match_attributes[i] = (AttributePair *)
574 XtMalloc(sizeof(AttributePair) * num * 2 + sizeof(int));
575
576 request->wm_windows.match_attributes[i]->num_attributes = num;
577 for (j=0; j<num; j++)
578 {
579 request->wm_windows.match_attributes[i]->allowed_attributes[j] =
580 UnpackProperty(&data);
581 }
582 for (j=0; j<num; j++)
583 {
584 request->wm_windows.match_attributes[i]->prohibited_attributes[j] =
585 UnpackProperty(&data);
586 }
587 }
588 }
589 break;
590 case WSM_WM_FOCUS:
591 break;
592 case WSM_WM_POINTER:
593 break;
594 default:
595 break;
596 }
597 } /* _WSMUnpackRequest */
598
599 /* Function Name: _WSMUnpackReply
600 * Description: Unpacks the byte stream from the wire into a reply
601 * message structure.
602 * Arguments: dpy - The Display.
603 * screen_num - The Screen number.
604 * data - the bits from accross the wire.
605 * len - The number of bits sent - Better error checking
606 * makes this more necessary.
607 * type - The type of message received.
608 * RETURNED reply - This reply structure is filled in with the data
609 * unpacked from the protocol.
610 * Returns: none.
611 */
612
613 /*ARGSUSED*/
614 void
615 _WSMUnpackReply(Display *dpy, int screen_num, MessageData data,
616 unsigned long len, WSMRequestType type, WSMReply *reply)
617 {
618 register int i;
619 reply->any.type = type; /* Save the type. */
620 reply->any.allocated = False;
621
622 switch (reply->any.type) {
623 case WSM_CONNECT:
624 if (data != NULL)
625 reply->connect.version = (short) UnpackCARD8(&data);
626 else
627 fprintf(stderr, "Error - Connection request reply data is empty!\n");
628 break;
629 case WSM_EXTENSIONS:
630 {
631 register int num;
632 register String *ptr;
633
634 num = reply->extensions.num_extensions = UnpackListNum(&data);
635 ptr = (String *) XtMalloc(sizeof(String) * num);
636 reply->extensions.allocated = True;
637 reply->extensions.extensions = ptr;
638 for (i = 0; i < num; i++, ptr++)
639 *ptr = UnpackString(&data);
640 }
641 break;
642 case WSM_CONFIG_FMT:
643 {
644 register int types;
645
646 WSMConfigFormatReply * config_format = &(reply->config_format);
647 WSMConfigFormatData *fmt;
648 WSMScreenInfo *scr_info = _WSMGetScreenInfo(dpy, screen_num);
649
650 config_format->accepts_diffs = UnpackBoolean(&data);
651
652 for (types = 0; types < 3; types++) {
653 switch(types) {
654 case WSM_GLOBAL_FMT:
655 fmt = &(scr_info->global);
656 break;
657 case WSM_WINDOW_FMT:
658 fmt = &(scr_info->window);
659 break;
660 case WSM_ICON_FMT:
661 fmt = &(scr_info->icon);
662 break;
663 }
664
665 fmt->num_attrs = UnpackListNum(&data);
666 fmt->attr_list = (WSMAttribute *)
667 XtMalloc(sizeof(WSMAttribute) * fmt->num_attrs);
668
669 for (i = 0; i < fmt->num_attrs; i++) {
670 String str = UnpackString(&data);
671 fmt->attr_list[i].nameq = XrmStringToQuark(str);
672 XtFree(str);
673 fmt->attr_list[i].size = UnpackCARD8(&data);
674 fmt->attr_list[i].is_list = UnpackCARD8(&data);
675 }
676 }
677
678 /*
679 * No need to allocate this, since they are just pointers
680 * back to global data.
681 */
682
683 config_format->global_formats = scr_info->global.attr_list;
684 config_format->num_global_formats = scr_info->global.num_attrs;
685 config_format->window_formats = scr_info->window.attr_list;
686 config_format->num_window_formats = scr_info->window.num_attrs;
687 config_format->icon_formats = scr_info->icon.attr_list;
688 config_format->num_icon_formats = scr_info->icon.num_attrs;
689 }
690 break;
691 case WSM_GET_STATE:
692 {
693 int num =reply->get_state.num_win_info_list = UnpackListNum(&data);
694 reply->get_state.win_info_list =
695 (WSMWinInfo *) XtMalloc(sizeof(WSMWinInfo) * num);
696 reply->get_state.allocated = True;
697
698 for (i = 0; i < num; i++)
699 UnpackWinInfo(&data, dpy, screen_num,
700 reply->get_state.win_info_list + i);
701 }
702 break;
703 case WSM_SET_STATE:
704 break;
705 case WSM_REG_WINDOW:
706 if (data != NULL)
707 {
708 UnpackWinData(&data, dpy, screen_num, WSM_WINDOW_FMT,
709 &(reply->register_window.window_data),
710 &(reply->register_window.num_window_data));
711 reply->register_window.allocated = True;
712 }
713 else
714 fprintf(stderr, "Error - Register Window reply data is empty!\n");
715 break;
716 case WSM_WM_GET_BACKGROUND_WINDOW:
717 reply->get_background.window = UnpackWindow(&data);
718 break;
719 case WSM_WM_SET_BACKGROUND_WINDOW:
720 reply->set_background.window = UnpackWindow(&data);
721 break;
722 case WSM_WM_WINDOWS:
723 {
724 int num;
725
726 num = reply->wm_windows.num_win_entry_list = UnpackListNum(&data);
727 reply->wm_windows.win_entry_list =
728 (WSMWinEntry *) XtMalloc(sizeof(WSMWinEntry) * num);
729 reply->wm_windows.allocated = True;
730
731 for (i = 0; i < num; i++)
732 UnpackWinEntry(&data, dpy, screen_num,
733 reply->wm_windows.win_entry_list + i);
734 }
735 break;
736 case WSM_WM_FOCUS:
737 reply->wm_focus.window = UnpackWindow(&data);
738 break;
739 case WSM_WM_POINTER:
740 reply->wm_pointer.location_flag = UnpackCARD32(&data);
741 break;
742 default:
743 break;
744 }
745 } /* _WSMUnpackReply */
746
747 /************************************************************
748 *
749 * Internal Routines for getting size of packed requests.
750 *
751 ************************************************************/
752
753 /* Function Name: WinInfoSizePacked
754 * Description: Returns the size this window structure will take
755 * when packed into a protocol data stream.
756 * Arguments: dpy, screen_num - The display and screen of the client we
757 * are talking to.
758 * win_info - The window info whose size me are checking.
759 * Returns: the size (in bytes)
760 */
761
762 static int
763 WinInfoSizePacked(Display *dpy, int screen_num, WSMWinInfo *win_info)
764 {
765 int size = sizeof(CARD32);
766 size += WinDataSizePacked(dpy, screen_num,
767 win_info->data_list, win_info->num_data_list,
768 _WSMGetConfigFormatType(win_info->window));
769 return(size);
770 }
771
772 /* Function Name: WinEntrySizePacked
773 * Description: Returns the size this window structure will take
774 * when packed into a protocol data stream.
775 * Arguments: dpy, screen_num - The display and screen of the client we
776 * are talking to.
777 * win_entry - The window entry whose size we are checking.
778 * Returns: the size (in bytes)
779 */
780
781 static int
782 WinEntrySizePacked(Display *dpy, int screen_num, WSMWinEntry *win_entry)
783 {
784 int size;
785
786 size = sizeof(CARD32);
787 size += sizeof(CARD32) * win_entry->num_windows;
788 size = sizeof(CARD32);
789 size += sizeof(WindowProperty) * win_entry->num_match_properties;
790
791 return(size);
792 }
793
794 /* Function Name: WinDataSizePacked
795 * Description: The size that will be taken up with this window data
796 * once it is packed into the protocol stream.
797 * Arguments: dpy, screen_num - The screen nmuber and display that
798 * we are talking to.
799 * win_data - The data stored in a window data structure list.
800 * num - number of items in the list.
801 * fmt - The type of format to use to pack this data.
802 * Returns: The size.
803 */
804
805 static int
806 WinDataSizePacked(Display *dpy, int screen_num,
807 WSMWinData *win_data, int num, WSMConfigFormatType fmt)
808 {
809 register int i, size;
810 WSMConfigFormatData *fmt_data = _WSMGetConfigFormat(dpy, screen_num, fmt);
811
812 size = fmt_data->num_attrs / 8 + 1; /* Size of header bits. */
813 for ( i = 0; i < num; i++, win_data++) {
814 WSMAttribute *attr = _WSMGetMatchingAttr(win_data->nameq, fmt_data);
815 if (attr == NULL) {
816 /*
817 * ||| ERROR.
818 */
819 continue;
820 }
821
822 switch(win_data->type) {
823 case WSM_VALUE_DATA:
824 size += attr->size/8;
825 break;
826 default: /* All others are lists. */
827 size += COUNT_SIZE;
828 size += ((attr->size/8) * win_data->data_len);
829 break;
830 }
831 }
832
833 return(size);
834 }
835
836 /************************************************************
837 *
838 * Internal Routines for packing data into the protocol stream.
839 *
840 ************************************************************/
841
842 /* Function Name: PackString
843 * Description: Packs a string into the data stream.
844 * Arguments: data - The data stream to pack into.
845 * str - The string to pack.
846 * Returns: data - A pointer into the next empty location in the
847 * data stream.
848 */
849
850 MessageData
851 PackString(MessageData data, String str)
852 {
853 register int i, len = strlen(str);
854
855 data = PackListNum(data, len);
856 for (i = 0; i < len; i++, str++)
857 data = PackCARD8(data, *str);
858
859 return(data);
860 }
861
862 /* Function Name: PackConfigFormat
863 * Description: Packs the configuration format into the data stream.
864 * Arguments: data - the datastream.
865 * config - the reply config format message.
866 * Returns: data - A pointer into the next empty location in the
867 * data stream.
868 */
869
870 static MessageData
871 PackConfigFormat(MessageData data, WSMConfigFormatReply *config)
872 {
873 data = PackBoolean(data, config->accepts_diffs);
874 data = PackSingleAttribute(data, config->global_formats,
875 config->num_global_formats);
876 data = PackSingleAttribute(data, config->window_formats,
877 config->num_window_formats);
878 data = PackSingleAttribute(data, config->icon_formats,
879 config->num_icon_formats);
880 return(data);
881 }
882
883 /* Function Name: PackSingleAttribute
884 * Description: Packs a single attribute into a the data stream.
885 * Arguments: data - the datastream.
886 * attr_list, num - The attribute list to pack into the stream.
887 * The number of items in the list.
888 * Returns: data - A pointer into the next empty location in the
889 * data stream.
890 */
891
892 static MessageData
893 PackSingleAttribute(MessageData data, WSMAttribute *attr_list, int num)
894 {
895 register int i;
896
897 data = PackListNum(data, num);
898 for (i = 0; i < num; i++, attr_list++) {
899 data = PackString(data, XrmQuarkToString(attr_list->nameq));
900 data = PackCARD8(data, (CARD8) attr_list->size);
901 data = PackBoolean(data, attr_list->is_list);
902 }
903 return(data);
904 }
905
906 /* Function Name: PackWinInfo
907 * Description: Packs the window information into the data stream.
908 * Arguments: dpy, screen_num - The display and screen of the client
909 * that we are talking to.
910 * data - The message stream.
911 * win_info - The window info list to pack into the data stream.
912 * Returns: data - A pointer into the next empty location in the
913 * data stream.
914 */
915
916 static MessageData
917 PackWinInfo(Display *dpy,
918 int screen_num, MessageData data, WSMWinInfo *win_info)
919 {
920 data = PackWindow(data, win_info->window);
921 return(PackWinData(dpy, screen_num, data,
922 win_info->data_list, win_info->num_data_list,
923 _WSMGetConfigFormatType(win_info->window)));
924 }
925
926 /* Function Name: PackWinEntry
927 * Description: Packs the window information into the data stream.
928 * Arguments: dpy, screen_num - The display and screen of the client
929 * that we are talking to.
930 * data - The message stream.
931 * win_info - The window info list to pack into the data stream.
932 * Returns: data - A pointer into the next empty location in the
933 * data stream.
934 */
935
936 static MessageData
937 PackWinEntry(Display *dpy,
938 int screen_num, MessageData data, WSMWinEntry *win_entry)
939 {
940 int i;
941
942 data = PackListNum(data, win_entry->num_windows);
943 for (i = 0; i < win_entry->num_windows; i++)
944 data = PackWindow(data, win_entry->windows[i]);
945
946 data = PackListNum(data, win_entry->num_match_properties);
947 for (i = 0; i < win_entry->num_match_properties; i++)
948 data = PackProperty(data, win_entry->match_properties[i]);
949
950 return(data);
951 }
952
953 /* Function Name: PackWinData
954 * Description: Packs the window data into the data stream.
955 * Arguments: dpy, screen_num - The display and screen of the client
956 * that we are talking to.
957 * data - The message stream.
958 * win_data, num - The window data to pack
959 * into the data stream, and its size.
960 * fmt - The type of config format to use.
961 * Returns: data - A pointer into the next empty location in the
962 * data stream.
963 */
964
965 static MessageData
966 PackWinData(Display *dpy, int screen_num, MessageData data,
967 WSMWinData *win_data, int num, WSMConfigFormatType fmt)
968 {
969 WSMConfigFormatData *conf_fmt = _WSMGetConfigFormat(dpy, screen_num, fmt);
970 MessageData mask_pos = data;
971 WSMAttribute *attr;
972 register int i, size = (conf_fmt->num_attrs / 8) + 1;
973
974 data = (MessageData) ((char *)data + size);
975 bzero((char *) mask_pos, size); /* Set all bits to zero initially*/
976
977 attr = conf_fmt->attr_list;
978 for (i = 0; i < conf_fmt->num_attrs ; i++, attr++) {
979 WSMWinData *this_data;
980
981 this_data = _WSMGetMatchingWinData(win_data, num, attr->nameq);
982 if (this_data != NULL) {
983 if (this_data->type != WSM_VALUE_DATA)
984 data = PackListNum(data, this_data->data_len);
985
986 switch(this_data->type) {
987 case WSM_CHAR_LIST_DATA:
988 {
989 register int j;
990 char * ptr = this_data->data.char_ptr;
991 for (j = 0 ; j < this_data->data_len; j++, ptr++)
992 data = PackCARD8(data, (CARD8) *ptr);
993 break;
994 }
995 case WSM_SHORT_LIST_DATA:
996 {
997 register int j;
998 short * ptr = this_data->data.short_ptr;
999 for (j = 0 ; j < this_data->data_len; j++, ptr++)
1000 data = PackCARD16(data, (CARD16) *ptr);
1001 break;
1002 }
1003 case WSM_LONG_LIST_DATA:
1004 {
1005 register int j;
1006 long * ptr = this_data->data.long_ptr;
1007 for (j = 0 ; j < this_data->data_len; j++, ptr++)
1008 data = PackCARD32(data, (CARD32) *ptr);
1009 break;
1010 }
1011 case WSM_VALUE_DATA:
1012 switch(attr->size) {
1013 case 8:
1014 data = PackCARD8(data, (CARD8) this_data->data.value);
1015 break;
1016 case 16:
1017 data = PackCARD16(data, (CARD16) this_data->data.value);
1018 break;
1019 case 32:
1020 data = PackCARD32(data, (CARD32) this_data->data.value);
1021 break;
1022 }
1023 case WSM_NONE:
1024 break;
1025 }
1026
1027 /*
1028 * The first item sets the MSBit, the 8th Item sets the LSBit.
1029 * The Nineth sets the MSBit in the second byte. The unused
1030 * bits are the LSBits of the last byte.
1031 */
1032
1033 (*(unsigned char *)mask_pos) |= (char)(1 << (7 - (i % 8)));
1034 }
1035
1036 /*
1037 * When we get to the end of a char, move to the next one.
1038 */
1039
1040 if ((i % 8) == 7)
1041 mask_pos = ((char *)mask_pos + 1);
1042 }
1043
1044 return(data);
1045 }
1046
1047 /* Function Name: PackCARD32
1048 * Description: Packs an 32 bit value into the data stream.
1049 * Arguments: data - The data stream to pack into.
1050 * val - The 32 bit value to pack.
1051 * Returns: data - A pointer into the next empty location in the
1052 * data stream.
1053 */
1054
1055 MessageData
1056 PackCARD32(MessageData data, CARD32 val)
1057 {
1058 CARD16 bottom = val & (0xFFFF);
1059 CARD16 top = val >> 16;
1060
1061 data = PackCARD16(data, top);
1062 data = PackCARD16(data, bottom);
1063 return(data);
1064 }
1065
1066 /* Function Name: PackCARD16
1067 * Description: Packs an 16 bit value into the data stream.
1068 * Arguments: data - The data stream to pack into.
1069 * val - The 16 bit value to pack.
1070 * Returns: data - A pointer into the next empty location in the
1071 * data stream.
1072 */
1073
1074 MessageData
1075 PackCARD16(MessageData data, CARD16 val)
1076 {
1077 CARD8 bottom = val & (0xFF);
1078 CARD8 top = val >> 8;
1079
1080 data = PackCARD8(data, top);
1081 data = PackCARD8(data, bottom);
1082 return(data);
1083 }
1084
1085 /* Function Name: PackCARD8
1086 * Description: Packs an 8 bit value into the data stream.
1087 * Arguments: data - The data stream to pack into.
1088 * val - The 8 bit value to pack.
1089 * Returns: data - A pointer into the next empty location in the
1090 * data stream.
1091 */
1092
1093 MessageData
1094 PackCARD8(MessageData data, CARD8 val)
1095 {
1096 CARD8 *ptr = (CARD8 *) data;
1097
1098 *ptr = (CARD8) val;
1099 data = ((char*)data) + 1;
1100
1101 return(data);
1102 }
1103
1104 /************************************************************
1105 *
1106 * Internal routines for unpacking the data from the
1107 * protocol stream.
1108 *
1109 ************************************************************/
1110
1111 /* Function Name: UnpackWinData
1112 * Description: Unpacks the window data from the protocol stream.
1113 * Arguments: data_ptr - A pointer to the message data stream.
1114 * dpy, screen_num - The display and screen of the client
1115 * that we are talking to.
1116 * fmt - The window format to use.
1117 * RETURNED win_data - The window data information.
1118 * RETURNED num - The number of window data structs in "win_data".
1119 * Returns: none
1120 */
1121
1122 static void
1123 UnpackWinData(MessageData *data_ptr, Display *dpy, int screen_num,
1124 WSMConfigFormatType fmt,
1125 WSMWinData **win_data, int *num)
1126 {
1127 register int i, size;
1128 WSMConfigFormatData *conf_fmt = _WSMGetConfigFormat(dpy, screen_num, fmt);
1129 unsigned char *bit_mask, *current_mask, check_mask;
1130 WSMAttribute *attr;
1131 WSMWinData *ptr;
1132
1133 /*
1134 * Figure out how many values have been specified so we know how much
1135 * memory to allocate.
1136 */
1137
1138 size = conf_fmt->num_attrs / 8 + 1;
1139 current_mask = bit_mask = (unsigned char *) XtMalloc(size);
1140
1141 /*
1142 * Unpack the bits from the wire.
1143 */
1144 for (i = 0; i < size; i++, current_mask++)
1145 *current_mask = UnpackCARD8(data_ptr);
1146
1147 current_mask = bit_mask;
1148 check_mask = START_CHECK_MASK;
1149 for (*num = 0, i = 0; i < conf_fmt->num_attrs; i++) {
1150 if (*current_mask & check_mask)
1151 (*num)++;
1152
1153 check_mask >>= 1;
1154 if ((i % 8) == 7) {
1155 current_mask++;
1156 check_mask = START_CHECK_MASK;
1157 }
1158 }
1159
1160 ptr = *win_data = (WSMWinData *) XtMalloc(*num * sizeof(WSMWinData));
1161
1162 /*
1163 * For each value that was specified pack the data into the window
1164 * data record.
1165 */
1166
1167 current_mask = bit_mask;
1168 check_mask = START_CHECK_MASK;
1169 attr = conf_fmt->attr_list;
1170 for (i = 0; i < conf_fmt->num_attrs; i++, attr++) {
1171 if (*current_mask & check_mask)
1172 UnpackSingleWinDataRec(data_ptr, attr, ptr++);
1173
1174 check_mask >>= 1;
1175 if ((i % 8) == 7) {
1176 current_mask++;
1177 check_mask = START_CHECK_MASK;
1178 }
1179 }
1180
1181 XtFree((XtPointer) bit_mask);
1182 }
1183
1184 /* Function Name: UnpackSingleWinDataRec
1185 * Description: Packs a single window data record.
1186 * Arguments: data_ptr - The data pointer.
1187 * attr - The attribute record that defines general information
1188 * on how to unpack this data.
1189 * win_data - the window data struct.
1190 * Returns: none
1191 */
1192
1193 static void
1194 UnpackSingleWinDataRec(MessageData *data_ptr,
1195 WSMAttribute *attr, WSMWinData *win_data)
1196 {
1197 register int i;
1198
1199 /*
1200 * Set these no matter what the size.
1201 */
1202
1203 win_data->nameq = attr->nameq; /* Save the name. */
1204
1205 if (attr->is_list) {
1206 win_data->data_len = UnpackListNum(data_ptr);
1207 }
1208 else {
1209 win_data->type = WSM_VALUE_DATA;
1210 win_data->data_len = 0; /* unused... */
1211 }
1212
1213 /*
1214 * Based on the size, and whether or not this is a list, unpack
1215 * the data from the wire.
1216 */
1217
1218 switch(attr->size) {
1219 case 8:
1220 if (attr->is_list) {
1221 char *local;
1222
1223 win_data->type = WSM_CHAR_LIST_DATA;
1224 local = win_data->data.char_ptr =
1225 (char *) XtMalloc(sizeof(char) * win_data->data_len);
1226 for (i = 0; i < win_data->data_len; i++, local++)
1227 *local = UnpackCARD8(data_ptr);
1228 }
1229 else
1230 win_data->data.value = UnpackCARD8(data_ptr);
1231
1232 break;
1233 case 16:
1234 if (attr->is_list) {
1235 short *local;
1236
1237 win_data->type = WSM_SHORT_LIST_DATA;
1238 local = win_data->data.short_ptr =
1239 (short *) XtMalloc(sizeof(short) * win_data->data_len);
1240 for (i = 0; i < win_data->data_len; i++, local++)
1241 *local = UnpackCARD16(data_ptr);
1242 }
1243 else
1244 win_data->data.value = UnpackCARD16(data_ptr);
1245
1246 break;
1247 case 32:
1248 if (attr->is_list) {
1249 long *local;
1250
1251 win_data->type = WSM_LONG_LIST_DATA;
1252 local = win_data->data.long_ptr =
1253 (long *) XtMalloc(sizeof(long) * win_data->data_len);
1254 for (i = 0; i < win_data->data_len; i++, local++)
1255 *local = UnpackCARD32(data_ptr);
1256 }
1257 else
1258 win_data->data.value = UnpackCARD32(data_ptr);
1259
1260 break;
1261 }
1262 }
1263
1264 /* Function Name: UnpackWinInfo
1265 * Description: Unpacks the window information from the protocol stream.
1266 * Arguments: data_ptr - A pointer to the message data stream.
1267 * dpy, screen_num - The display and screen of the client
1268 * that we are talking to.
1269 * win_info - The window information.
1270 * Returns: none
1271 */
1272
1273 static void
1274 UnpackWinInfo(MessageData *data, Display *dpy,
1275 int screen_num, WSMWinInfo *win_info)
1276 {
1277 win_info->window = UnpackWindow(data);
1278 UnpackWinData(data, dpy, screen_num,
1279 _WSMGetConfigFormatType(win_info->window),
1280 &(win_info->data_list), &(win_info->num_data_list));
1281 }
1282
1283 /* Function Name: UnpackWinEntry
1284 * Description: Unpacks the window entry information from the protocol stream.
1285 * Arguments: data_ptr - A pointer to the message data stream.
1286 * dpy, screen_num - The display and screen of the client
1287 * that we are talking to.
1288 * win_entry - The window entry information.
1289 * Returns: none
1290 */
1291
1292 static void
1293 UnpackWinEntry(MessageData *data, Display *dpy,
1294 int screen_num, WSMWinEntry *win_entry)
1295 {
1296 int i;
1297
1298 win_entry->num_windows = UnpackListNum(data);
1299 win_entry->windows = (Window *) XtMalloc(sizeof(Window) * win_entry->num_windows);
1300
1301 for (i=0; i<win_entry->num_windows; i++)
1302 win_entry->windows[i] = UnpackWindow(data);
1303
1304 win_entry->num_match_properties = UnpackListNum(data);
1305 win_entry->match_properties =
1306 (WindowProperty *) XtMalloc(sizeof(WindowProperty) *
1307 win_entry->num_match_properties);
1308
1309 for (i=0; i<win_entry->num_match_properties; i++)
1310 win_entry->match_properties[i] = UnpackProperty(data);
1311 }
1312
1313 /* Function Name: UnpackString
1314 * Description: Unpacks a string from the protocol data stream.
1315 * Arguments: data - Pointer to the data stream.
1316 * Returns: the string from the data stream.
1317 *
1318 * NOTE: Data is modified to point to the next empty location in the stream.
1319 */
1320
1321 String
1322 UnpackString(MessageData *data_ptr)
1323 {
1324 register int i;
1325 int len = UnpackListNum(data_ptr);
1326 char *str, *top = XtMalloc((len + 1) * sizeof(char));
1327
1328 for (str = top, i = 0; i < len; i++, str++) {
1329 *str = (char) UnpackCARD8(data_ptr);
1330 }
1331 *str = '\0';
1332
1333 return((String) top);
1334 }
1335
1336 /* Function Name: UnpackCARD32
1337 * Description: Unpacks an 32 bit value from the protocol data stream.
1338 * Arguments: data - Pointer to the data stream.
1339 * Returns: the CARD32 from the data stream.
1340 *
1341 * NOTE: data is modified to point to the next empty location in the stream.
1342 */
1343
1344 CARD32
1345 UnpackCARD32(MessageData *data_ptr)
1346 {
1347 CARD32 val = UnpackCARD16(data_ptr) << 16;
1348 val |= UnpackCARD16(data_ptr);
1349 return(val);
1350 }
1351
1352 /* Function Name: UnpackCARD16
1353 * Description: Unpacks an 16 bit value from the protocol data stream.
1354 * Arguments: data - Pointer to the data stream.
1355 * Returns: the CARD16 from the data stream.
1356 *
1357 * NOTE: data is modified to point to the next empty location in the stream.
1358 */
1359
1360 CARD16
1361 UnpackCARD16(MessageData *data_ptr)
1362 {
1363 CARD16 val = UnpackCARD8(data_ptr) << 8;
1364 val |= UnpackCARD8(data_ptr);
1365 return(val);
1366 }
1367
1368 /* Function Name: UnpackCARD8
1369 * Description: Unpacks an 8 bit value from the protocol data stream.
1370 * Arguments: data - Pointer to the data stream.
1371 * Returns: the CARD8 from the data stream.
1372 *
1373 * NOTE: data is modified to point to the next empty location in the stream.
1374 */
1375
1376 CARD8
1377 UnpackCARD8(MessageData *data_ptr)
1378 {
1379 CARD8 ret_val = *((char *) *data_ptr);
1380
1381 *data_ptr = ((char *) *data_ptr) + 1;
1382
1383 return(ret_val);
1384 }