| 1 |
/* $XConsortium: utm_send.c /main/5 1995/07/15 20:38:59 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 <stdio.h> |
| 30 |
#include <Xm/TransferT.h> |
| 31 |
#include "utm_send.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/* |
| 36 |
* The following keeps track of multiple utm requests and |
| 37 |
* pulls the right data off |
| 38 |
*/ |
| 39 |
|
| 40 |
typedef struct _DataQueueRec { |
| 41 |
XtPointer data; |
| 42 |
struct _DataQueueRec *next, *prev; |
| 43 |
} DataQueueRec; |
| 44 |
|
| 45 |
static DataQueueRec *dataQueue, *dataQueueTail = NULL; |
| 46 |
|
| 47 |
|
| 48 |
static void EnqueueUtmData( |
| 49 |
XtPointer |
| 50 |
); |
| 51 |
|
| 52 |
static XtPointer DequeueUtmData( |
| 53 |
void |
| 54 |
); |
| 55 |
|
| 56 |
|
| 57 |
/*================================================================* |
| 58 |
| The following functions are used to handle selection/UTM | |
| 59 |
| requests made from the REQUESTORS side to the owner of the | |
| 60 |
| selection. | |
| 61 |
| To send a request, the only function needed is UTMSendMessage. | |
| 62 |
*================================================================*/ |
| 63 |
|
| 64 |
/*----------------------------------------------------------------* |
| 65 |
| UTMSendMessage | |
| 66 |
| Send a message with parameters. | |
| 67 |
| Arguments: | |
| 68 |
| widget - requestor. MUST have destination callback set | |
| 69 |
| to UTMDestinationProc below. |
| 70 |
| selection - selection to convert against. | |
| 71 |
| target - target to make request against. | |
| 72 |
| param - data to send to selection owner. | |
| 73 |
| param_len - length of param in fmt units. | |
| 74 |
| param_fmt - param format (should be WSM_PROTO_FMT). | |
| 75 |
| callback - function to invoke when done. | |
| 76 |
| closure - data to pass to the callback. | |
| 77 |
| time - timestamp of the event; not CurrentTime. | |
| 78 |
*----------------------------------------------------------------*/ |
| 79 |
void |
| 80 |
UTMSendMessage( |
| 81 |
Widget w, |
| 82 |
Atom selection, |
| 83 |
Atom target, |
| 84 |
XtPointer param, |
| 85 |
unsigned long paramLen, |
| 86 |
int paramFmt, |
| 87 |
XtCallbackProc doneProc, |
| 88 |
XtPointer closure, |
| 89 |
Time time) |
| 90 |
{ |
| 91 |
UTMPackageRec *pUtmData; |
| 92 |
|
| 93 |
|
| 94 |
/* make sure timestamp is valid |
| 95 |
if (time == 0) time = GetTimestamp(XtDisplay(w)); */ |
| 96 |
|
| 97 |
/* allocates and builds utm package to send. */ |
| 98 |
pUtmData = (UTMPackageRec *) XtMalloc(sizeof(UTMPackageRec)); |
| 99 |
pUtmData->fmt = paramFmt; |
| 100 |
pUtmData->len = paramLen; |
| 101 |
pUtmData->target = target; |
| 102 |
pUtmData->param = param; |
| 103 |
pUtmData->doneProc = doneProc; |
| 104 |
pUtmData->closure = closure; |
| 105 |
|
| 106 |
/* |
| 107 |
* queue-up the data which is pulled off in the |
| 108 |
* UTMDestinationProc below. Note that the widget |
| 109 |
* passed in MUST have its destinationCallback set |
| 110 |
* to UTMDestionationProc! |
| 111 |
*/ |
| 112 |
|
| 113 |
EnqueueUtmData(pUtmData); |
| 114 |
|
| 115 |
/* |
| 116 |
* This causes the DestinationCB to be invoked. |
| 117 |
* That is where that param data is transfered. |
| 118 |
*/ |
| 119 |
if (!XmeNamedSink(w, |
| 120 |
selection, |
| 121 |
XmCOPY, |
| 122 |
NULL, /* location_data. holds param data (client data), |
| 123 |
free in TransferDone proc. */ |
| 124 |
time)) |
| 125 |
fprintf(stderr, "UTM Error: UTMSendMessage failed.\n"); |
| 126 |
|
| 127 |
} /* UTMSendMessage */ |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
/*----------------------------------------------------------------* |
| 132 |
| UTMDestinationProc | |
| 133 |
| Invoked by UTM when a sink has been set-up and a request | |
| 134 |
| initiated against an owner of a selection. | |
| 135 |
| Arguments: w - the requestor widget | |
| 136 |
| clientData - not used. data is in the queue. | |
| 137 |
| callData - pointer to the data passed in the call | |
| 138 |
| XmeNamedSink(). This will assume that | |
| 139 |
| the structure pointed to is a UTMDataRec.| |
| 140 |
| Returns: none | |
| 141 |
| | |
| 142 |
| Comments: | |
| 143 |
| Xt's selection mechanism will not work if the same time- | |
| 144 |
| stamp is used. prevTimeStamp contains the time of the last | |
| 145 |
| conversion and if same (or greater) it is incremented. | |
| 146 |
*----------------------------------------------------------------*/ |
| 147 |
void |
| 148 |
UTMDestinationProc ( |
| 149 |
Widget w, |
| 150 |
XtPointer clientData, |
| 151 |
XtPointer callData) |
| 152 |
{ |
| 153 |
static Time prevTimestamp = 0; |
| 154 |
static Time increment = 0; |
| 155 |
Time time; |
| 156 |
XmDestinationCallbackStruct *dcs = (XmDestinationCallbackStruct *)callData; |
| 157 |
UTMPackageRec *pUtmData; |
| 158 |
|
| 159 |
if (dcs == NULL) |
| 160 |
{ |
| 161 |
/* bad data - just return. */ |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
/* if duplicate timestamps are used, then make sure they're different. */ |
| 166 |
if (prevTimestamp == dcs->time) |
| 167 |
{ |
| 168 |
time = prevTimestamp + (++increment); |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
increment = 0; |
| 173 |
prevTimestamp = time = dcs->time; |
| 174 |
} |
| 175 |
|
| 176 |
/* pull off the real client data */ |
| 177 |
pUtmData = (UTMPackageRec *)DequeueUtmData(); |
| 178 |
|
| 179 |
/* if no UTMData, then transfer can't be done. Punt. */ |
| 180 |
if (pUtmData == NULL) |
| 181 |
return; |
| 182 |
|
| 183 |
/* Setup the parameters to pass. */ |
| 184 |
XmTransferSetParameters(dcs->transfer_id, |
| 185 |
pUtmData->param, /* ptr to parameter data. */ |
| 186 |
pUtmData->fmt, /* param format (8,16,32) */ |
| 187 |
pUtmData->len, /* size of param in fmt units */ |
| 188 |
dcs->selection); /* not used */ |
| 189 |
|
| 190 |
/* Make the transfer. This invokes the selection owner's ConvertCB. |
| 191 |
* When done, UTMReplyReceived callback proc is invoked. |
| 192 |
*/ |
| 193 |
XmTransferValue(dcs->transfer_id, |
| 194 |
pUtmData->target, /* this is the target to convert against.*/ |
| 195 |
pUtmData->doneProc, /* an XtCallbackProc called when done. */ |
| 196 |
pUtmData->closure, /* client data passed to done proc */ |
| 197 |
time); |
| 198 |
|
| 199 |
/* don't need the structure any more. */ |
| 200 |
XtFree((char*)pUtmData); |
| 201 |
|
| 202 |
} /* UTMDestinationProc */ |
| 203 |
|
| 204 |
|
| 205 |
/*----------------------------------------------------------------* |
| 206 |
| EnqueueUtmData | |
| 207 |
| Puts data onto the queue between sending messages and having | |
| 208 |
| the destination callback invoked. | |
| 209 |
*----------------------------------------------------------------*/ |
| 210 |
static void |
| 211 |
EnqueueUtmData(XtPointer data) |
| 212 |
{ |
| 213 |
DataQueueRec *dataRec = (DataQueueRec *)XtMalloc(sizeof(DataQueueRec)); |
| 214 |
|
| 215 |
dataRec->data = data; |
| 216 |
dataRec->next = dataQueue; |
| 217 |
dataRec->prev = NULL; |
| 218 |
|
| 219 |
if (dataQueue == NULL) |
| 220 |
dataQueueTail = dataRec; |
| 221 |
else |
| 222 |
dataQueue->prev = dataRec; |
| 223 |
dataQueue = dataRec; |
| 224 |
|
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
/*----------------------------------------------------------------* |
| 231 |
| DequeueUtmData | |
| 232 |
| Retreives data from the data queue after the destination proc | |
| 233 |
| was called. Just grab the last entry in the queue. | |
| 234 |
*----------------------------------------------------------------*/ |
| 235 |
static XtPointer |
| 236 |
DequeueUtmData() |
| 237 |
{ |
| 238 |
DataQueueRec *ptr = dataQueueTail; |
| 239 |
XtPointer dataPtr = NULL; |
| 240 |
|
| 241 |
if (ptr) |
| 242 |
{ |
| 243 |
dataQueueTail = ptr->prev; |
| 244 |
|
| 245 |
if (ptr->prev != NULL) |
| 246 |
ptr->prev->next = NULL; |
| 247 |
else |
| 248 |
dataQueue = NULL; |
| 249 |
|
| 250 |
dataPtr = ptr->data; |
| 251 |
XtFree((char*)ptr); |
| 252 |
} |
| 253 |
else |
| 254 |
fprintf(stderr, "UTM ERROR: Data not found in queue.\n"); |
| 255 |
|
| 256 |
return (dataPtr); |
| 257 |
} |