| 1 |
/* $XConsortium: send.c /main/5 1995/07/15 20:38:50 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 <Xm/Transfer.h> |
| 30 |
#include "wsm_proto.h" |
| 31 |
#include "utm_send.h" |
| 32 |
|
| 33 |
typedef struct _RequestInfo { |
| 34 |
WSMReplyCallbackFunc reply_callback; /* The reply callback func */ |
| 35 |
XtPointer reply_data; /* The user data for the callback func */ |
| 36 |
WSMRequestType request_type; /* The kind of request for cross check. */ |
| 37 |
Atom send_atom; /* The atom that we sent this message to. */ |
| 38 |
} RequestInfo; |
| 39 |
|
| 40 |
static void UTMReplyReceived( |
| 41 |
Widget, XtPointer, XtPointer |
| 42 |
); |
| 43 |
|
| 44 |
static void ReplyReceived( |
| 45 |
Widget, XtPointer, Atom *, Atom *, XtPointer, unsigned long *, int * |
| 46 |
); |
| 47 |
|
| 48 |
extern Time GetTimestamp (Display *dpy); |
| 49 |
|
| 50 |
/* Function Name: WSMSendMessage |
| 51 |
* Description: Sends a message to WSM or WM on the screen |
| 52 |
* and display specified. |
| 53 |
* Arguments: w - any widget on this display and screen that |
| 54 |
* also has destination callback. |
| 55 |
* Note - This widget MUST have the destination |
| 56 |
* callback set to UTMDestinationProc that |
| 57 |
* is defined in utm_send.c. |
| 58 |
* send_to - either WSMWorkspaceManager or WSMWindowManager |
| 59 |
* request - the request to send. |
| 60 |
* reply_callback - The routine to call when the |
| 61 |
* reply comes in. |
| 62 |
* reply_data - Client data passed to the reply_callback. |
| 63 |
* Returns: True if an attempt is made to retrieve the selection, |
| 64 |
* False if an obvious error occured. |
| 65 |
* |
| 66 |
* NOTE: Reply callback will be called with reply == NULL if unable |
| 67 |
* to convert request. |
| 68 |
*/ |
| 69 |
|
| 70 |
Boolean |
| 71 |
WSMSendMessage(Widget w, WSMClientType send_to, WSMRequest *request, |
| 72 |
WSMReplyCallbackFunc reply_callback, XtPointer reply_data) |
| 73 |
{ |
| 74 |
int screen_num = XScreenNumberOfScreen(XtScreen(w)); |
| 75 |
Display *dpy = XtDisplay(w); |
| 76 |
Atom send_atom = _WSMGetSelectionAtom(dpy, screen_num, send_to); |
| 77 |
MessageData msg_data; |
| 78 |
unsigned long msg_len; |
| 79 |
RequestInfo *req_info; |
| 80 |
WSMErrorCode error; |
| 81 |
Time time = GetTimestamp(dpy); |
| 82 |
|
| 83 |
if (send_atom == None) { |
| 84 |
fprintf(stderr, "%s: Could not get selection atom to send message\n", |
| 85 |
"Internal Error"); |
| 86 |
return(False); |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* Package human-readable request data into protocol package. |
| 91 |
*/ |
| 92 |
msg_data = _WSMPackRequest(dpy, screen_num, request, &msg_len, &error); |
| 93 |
if (msg_data == NULL) { |
| 94 |
(*reply_callback)(w, reply_data, NULL, error); |
| 95 |
return(False); |
| 96 |
} |
| 97 |
|
| 98 |
req_info = (RequestInfo *) XtMalloc(sizeof(RequestInfo)); |
| 99 |
req_info->reply_callback = reply_callback; /* The reply callback func */ |
| 100 |
req_info->reply_data = reply_data; /* user data for the callback func */ |
| 101 |
req_info->request_type = request->any.type;/*request kind for cross check*/ |
| 102 |
req_info->send_atom = send_atom; /* Atom we sent this message to. */ |
| 103 |
|
| 104 |
if (!XtIsRealized(w)) { |
| 105 |
fprintf(stderr, "%s WSMSendMessage must be realized, and is not.\n", |
| 106 |
"Programmer Error: Widget passed to"); |
| 107 |
return(False); |
| 108 |
} |
| 109 |
|
| 110 |
UTMSendMessage(w, |
| 111 |
send_atom, |
| 112 |
_WSMReqTypeToTarget(dpy, request->any.type), |
| 113 |
(XtPointer) msg_data, msg_len, WSM_PROTO_FMT, |
| 114 |
UTMReplyReceived, req_info, |
| 115 |
time); |
| 116 |
return(True); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/************************************************************ |
| 121 |
* |
| 122 |
* Internal Routines. |
| 123 |
* |
| 124 |
************************************************************/ |
| 125 |
|
| 126 |
|
| 127 |
/* Function Name: UTMReplyReceived |
| 128 |
* Description: Called after the selection owner's convert proc |
| 129 |
* has finished. |
| 130 |
* Arguments: w - The widget who initiated the request. |
| 131 |
* clientData - |
| 132 |
* callData - |
| 133 |
* Returns: none |
| 134 |
*/ |
| 135 |
static void |
| 136 |
UTMReplyReceived(Widget w, XtPointer clientData, XtPointer callData) |
| 137 |
{ |
| 138 |
XmSelectionCallbackStruct *scs = (XmSelectionCallbackStruct *) callData; |
| 139 |
RequestInfo *req_info = (RequestInfo *) clientData; |
| 140 |
Boolean errorFound = False; |
| 141 |
Atom type = _WSMReqTypeToTarget(XtDisplay(w), req_info->request_type); |
| 142 |
|
| 143 |
/* Let's check some other values just to make sure things are ok. */ |
| 144 |
if (scs->reason != XmCR_OK) { |
| 145 |
fprintf(stderr, "ERROR: Bad reason value received in UTMReplyReceived.\n"); |
| 146 |
errorFound = True; |
| 147 |
} |
| 148 |
if (scs->type == XT_CONVERT_FAIL) { |
| 149 |
fprintf(stderr, "ERROR: Convert failure detected in UTMReplyReceived.\n"); |
| 150 |
errorFound = True; |
| 151 |
} |
| 152 |
if (scs->flags != XmSELECTION_DEFAULT) { |
| 153 |
fprintf(stderr, "ERROR: Bad flags value received in UTMReplyReceived.\n"); |
| 154 |
errorFound = True; |
| 155 |
} |
| 156 |
if (errorFound) |
| 157 |
return; |
| 158 |
|
| 159 |
ReplyReceived(w, |
| 160 |
req_info, /* the request info pointer to fill in. */ |
| 161 |
&scs->selection, |
| 162 |
&type, /* type of request that was made. */ |
| 163 |
scs->value, /* data returned from conversion. */ |
| 164 |
&scs->length, |
| 165 |
&scs->format); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/* Function Name: ReplyReceived |
| 170 |
* Description: Called when a reply is received from a request |
| 171 |
* initiated by a WSMSendMessage. |
| 172 |
* Arguments: w - The widget who initiated the request. |
| 173 |
* req_info_ptr - pointer to the request info. |
| 174 |
* selection - The selection that has been converted. |
| 175 |
* req_type_atom - The type of request that was made. |
| 176 |
* length - the amount of message data. |
| 177 |
* format - the format of the reply. |
| 178 |
* Returns: none |
| 179 |
*/ |
| 180 |
static void |
| 181 |
ReplyReceived(Widget w, XtPointer req_info_ptr, |
| 182 |
Atom *selection, Atom *req_type_atom, |
| 183 |
XtPointer value, unsigned long *length, int *format) |
| 184 |
{ |
| 185 |
RequestInfo *req_info = (RequestInfo *) req_info_ptr; |
| 186 |
WSMReply reply; |
| 187 |
Display *dpy = XtDisplay(w); |
| 188 |
int screen_num; |
| 189 |
WSMErrorCode fail_code = WSM_SUCCESS; |
| 190 |
|
| 191 |
/* |
| 192 |
* First a few checks to make sure everything is as we expect. |
| 193 |
*/ |
| 194 |
|
| 195 |
if (*selection != req_info->send_atom) { |
| 196 |
fprintf(stderr, "%s, request %d - reply %d\n", |
| 197 |
"Selection of the reply is not the same as the request", |
| 198 |
(int) req_info->send_atom, (int) *selection); |
| 199 |
|
| 200 |
fail_code = WSM_ERROR_INTERNAL; |
| 201 |
} |
| 202 |
if (*req_type_atom != _WSMReqTypeToTarget(dpy, req_info->request_type)) { |
| 203 |
if (*req_type_atom == None) { |
| 204 |
if (XGetSelectionOwner(dpy, *selection) == None) |
| 205 |
{ |
| 206 |
fail_code = WSM_ERROR_NO_SEL_OWNER; |
| 207 |
fprintf(stderr, "No owner for selection #%d\n", (int)*selection); |
| 208 |
} |
| 209 |
else |
| 210 |
fail_code = WSM_ERROR_CONVERSION_FAILED; |
| 211 |
} |
| 212 |
else if (*req_type_atom == XT_CONVERT_FAIL) |
| 213 |
fail_code = WSM_ERROR_TIMEOUT; |
| 214 |
else { |
| 215 |
fprintf(stderr, "%s, request %s - reply %d:%s\n", |
| 216 |
"Target of the reply is not the same as the request", |
| 217 |
_WSMReqTypeToName(req_info->request_type), |
| 218 |
(int) *req_type_atom, |
| 219 |
_WSMReqTypeToName(_WSMTargetToReqType(dpy, *req_type_atom))); |
| 220 |
|
| 221 |
fail_code = WSM_ERROR_INTERNAL; |
| 222 |
} |
| 223 |
} |
| 224 |
if (*format != WSM_PROTO_FMT) { |
| 225 |
fprintf(stderr, "%s, request %d - reply %d\n", |
| 226 |
"Format of the reply is not the same as the request", |
| 227 |
(int) WSM_PROTO_FMT, (int) *format); |
| 228 |
|
| 229 |
fail_code = WSM_ERROR_INTERNAL; |
| 230 |
} |
| 231 |
|
| 232 |
if (fail_code != WSM_SUCCESS) { |
| 233 |
/* |
| 234 |
* Failure, call callback with NULL reply. |
| 235 |
*/ |
| 236 |
|
| 237 |
(*req_info->reply_callback)(w, req_info->reply_data, NULL, fail_code); |
| 238 |
} |
| 239 |
else { |
| 240 |
screen_num = XScreenNumberOfScreen(XtScreen(w)); |
| 241 |
|
| 242 |
_WSMUnpackReply(dpy, screen_num, value, *length, |
| 243 |
req_info->request_type, &reply); |
| 244 |
|
| 245 |
(*req_info->reply_callback)(w, req_info->reply_data, &reply,fail_code); |
| 246 |
|
| 247 |
FreeReply(&reply); |
| 248 |
} |
| 249 |
|
| 250 |
XtFree((XtPointer) req_info_ptr); |
| 251 |
} |