3 Copyright 1993, 1998 The Open Group
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
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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.
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.
28 * Author: Ralph Mor, X Consortium
34 #include <X11/SM/SMlib.h>
36 #include <X11/Xtrans/Xtrans.h>
45 _SmsProtocolSetupProc (IceConn iceConn,
50 IcePointer *clientDataRet,
51 char **failureReasonRet)
58 * vendor/release are undefined for ProtocolSetup in XSMP.
68 * Allocate new SmsConn.
71 if ((smsConn = (SmsConn) malloc (sizeof (struct _SmsConn))) == NULL)
73 const char *str = "Memory allocation failed";
75 if ((*failureReasonRet = (char *) malloc (strlen (str) + 1)) != NULL)
76 strcpy (*failureReasonRet, str);
81 smsConn->iceConn = iceConn;
82 smsConn->proto_major_version = majorVersion;
83 smsConn->proto_minor_version = minorVersion;
84 smsConn->client_id = NULL;
86 smsConn->save_yourself_in_progress = False;
87 smsConn->interaction_allowed = SmInteractStyleNone;
88 smsConn->can_cancel_shutdown = False;
89 smsConn->interact_in_progress = False;
91 *clientDataRet = (IcePointer) smsConn;
95 * Now give the session manager the new smsConn and get back the
96 * callbacks to invoke when messages arrive from the client.
98 * In the future, we can use the mask return value to check
99 * if the SM is expecting an older rev of SMlib.
102 bzero ((char *) &smsConn->callbacks, sizeof (SmsCallbacks));
104 status = (*_SmsNewClientProc) (smsConn, _SmsNewClientData,
105 &mask, &smsConn->callbacks, failureReasonRet);
114 SmsInitialize(char *vendor, char *release, SmsNewClientProc newClientProc,
115 SmPointer managerData, IceHostBasedAuthProc hostBasedAuthProc,
116 int errorLength, char *errorStringRet)
118 const char *auth_names[] = {"MIT-MAGIC-COOKIE-1"};
119 IcePaAuthProc auth_procs[] = {_IcePaMagicCookie1Proc};
122 IcePaVersionRec versions[] = {
123 {SmProtoMajor, SmProtoMinor, _SmsProcessMessage}
125 int version_count = 1;
127 if (errorStringRet && errorLength > 0)
128 *errorStringRet = '\0';
132 if (errorStringRet && errorLength > 0) {
133 strncpy (errorStringRet,
134 "The SmsNewClientProc callback can't be NULL",
136 errorStringRet[errorLength - 1] = '\0';
145 if ((_SmsOpcode = IceRegisterForProtocolReply ("XSMP",
146 vendor, release, version_count, versions,
147 auth_count, auth_names, auth_procs, hostBasedAuthProc,
148 _SmsProtocolSetupProc,
149 NULL, /* IceProtocolActivateProc - we don't care about
150 when the Protocol Reply is sent, because the
151 session manager can not immediately send a
152 message - it must wait for RegisterClient. */
153 NULL /* IceIOErrorProc */
156 if (errorStringRet && errorLength > 0) {
157 strncpy (errorStringRet,
158 "Could not register XSMP protocol with ICE",
160 errorStringRet[errorLength - 1] = '\0';
166 _SmsNewClientProc = newClientProc;
167 _SmsNewClientData = managerData;
175 SmsClientHostName(SmsConn smsConn)
177 return (IceGetPeerName (smsConn->iceConn));
183 SmsRegisterClientReply(SmsConn smsConn, char *clientId)
185 IceConn iceConn = smsConn->iceConn;
187 smRegisterClientReplyMsg *pMsg;
190 if ((smsConn->client_id = (char *) malloc (strlen (clientId) + 1)) == NULL)
195 strcpy (smsConn->client_id, clientId);
197 extra = ARRAY8_BYTES (strlen (clientId));
199 IceGetHeaderExtra (iceConn, _SmsOpcode, SM_RegisterClientReply,
200 SIZEOF (smRegisterClientReplyMsg), WORD64COUNT (extra),
201 smRegisterClientReplyMsg, pMsg, pData);
203 STORE_ARRAY8 (pData, strlen (clientId), clientId);
213 SmsSaveYourself(SmsConn smsConn, int saveType, Bool shutdown,
214 int interactStyle, Bool fast)
216 IceConn iceConn = smsConn->iceConn;
217 smSaveYourselfMsg *pMsg;
219 IceGetHeader (iceConn, _SmsOpcode, SM_SaveYourself,
220 SIZEOF (smSaveYourselfMsg), smSaveYourselfMsg, pMsg);
222 pMsg->saveType = saveType;
223 pMsg->shutdown = shutdown;
224 pMsg->interactStyle = interactStyle;
229 smsConn->save_yourself_in_progress = True;
231 if (interactStyle == SmInteractStyleNone ||
232 interactStyle == SmInteractStyleErrors ||
233 interactStyle == SmInteractStyleAny)
235 smsConn->interaction_allowed = interactStyle;
239 smsConn->interaction_allowed = SmInteractStyleNone;
242 smsConn->can_cancel_shutdown = shutdown &&
243 (interactStyle == SmInteractStyleAny ||
244 interactStyle == SmInteractStyleErrors);
250 SmsSaveYourselfPhase2(SmsConn smsConn)
252 IceConn iceConn = smsConn->iceConn;
254 IceSimpleMessage (iceConn, _SmsOpcode, SM_SaveYourselfPhase2);
261 SmsInteract(SmsConn smsConn)
263 IceConn iceConn = smsConn->iceConn;
265 IceSimpleMessage (iceConn, _SmsOpcode, SM_Interact);
268 smsConn->interact_in_progress = True;
274 SmsDie(SmsConn smsConn)
276 IceConn iceConn = smsConn->iceConn;
278 IceSimpleMessage (iceConn, _SmsOpcode, SM_Die);
285 SmsSaveComplete(SmsConn smsConn)
287 IceConn iceConn = smsConn->iceConn;
289 IceSimpleMessage (iceConn, _SmsOpcode, SM_SaveComplete);
296 SmsShutdownCancelled(SmsConn smsConn)
298 IceConn iceConn = smsConn->iceConn;
300 IceSimpleMessage (iceConn, _SmsOpcode, SM_ShutdownCancelled);
303 smsConn->can_cancel_shutdown = False;
309 SmsReturnProperties(SmsConn smsConn, int numProps, SmProp **props)
311 IceConn iceConn = smsConn->iceConn;
313 smPropertiesReplyMsg *pMsg;
317 IceGetHeader (iceConn, _SmsOpcode, SM_PropertiesReply,
318 SIZEOF (smPropertiesReplyMsg), smPropertiesReplyMsg, pMsg);
320 LISTOF_PROP_BYTES (numProps, props, bytes);
321 pMsg->length += WORD64COUNT (bytes);
323 pBuf = pStart = IceAllocScratch (iceConn, bytes);
325 STORE_LISTOF_PROPERTY (pBuf, numProps, props);
327 IceWriteData (iceConn, bytes, pStart);
334 SmsCleanUp(SmsConn smsConn)
336 IceProtocolShutdown (smsConn->iceConn, _SmsOpcode);
338 if (smsConn->client_id)
339 free (smsConn->client_id);
341 free ((char *) smsConn);