1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
|
/* $Xorg: smproxy.c,v 1.6 2001/02/09 02:05:35 xorgcvs Exp $ */
/******************************************************************************
Copyright 1994, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
Author: Ralph Mor, X Consortium
******************************************************************************/
/* $XFree86: xc/programs/smproxy/smproxy.c,v 3.8 2001/10/28 03:34:25 tsi Exp $ */
#include "smproxy.h"
#include <unistd.h>
#include <X11/Xmu/WinUtil.h>
XtAppContext appContext;
Display *disp;
Atom wmProtocolsAtom;
Atom wmSaveYourselfAtom;
Atom wmStateAtom;
Atom smClientIdAtom;
Atom wmClientLeaderAtom;
Bool debug = 0;
SmcConn proxy_smcConn;
XtInputId proxy_iceInputId;
char *proxy_clientId = NULL;
WinInfo *win_head = NULL;
int proxy_count = 0;
int die_count = 0;
Bool ok_to_die = 0;
Bool caught_error = 0;
Bool sent_save_done = 0;
int Argc;
char **Argv;
Bool HasSaveYourself ( Window window );
Bool HasXSMPsupport ( Window window );
WinInfo * GetClientLeader ( WinInfo *winptr );
char * CheckFullyQuantifiedName ( char *name, int *newstring );
void FinishSaveYourself ( WinInfo *winInfo, Bool has_WM_SAVEYOURSELF );
void SaveYourselfCB ( SmcConn smcConn, SmPointer clientData, int saveType,
Bool shutdown, int interactStyle, Bool fast );
void DieCB ( SmcConn smcConn, SmPointer clientData );
void SaveCompleteCB ( SmcConn smcConn, SmPointer clientData );
void ShutdownCancelledCB ( SmcConn smcConn, SmPointer clientData );
void ProcessIceMsgProc ( XtPointer client_data, int *source, XtInputId *id );
void NullIceErrorHandler ( IceConn iceConn, Bool swap,
int offendingMinorOpCode,
unsigned long offendingSequence,
int errorClass, int severity, IcePointer values );
void ConnectClientToSM ( WinInfo *winInfo );
int MyErrorHandler ( Display *display, XErrorEvent *event );
Bool LookupWindow ( Window window, WinInfo **ptr_ret, WinInfo **prev_ptr_ret );
WinInfo * AddNewWindow ( Window window );
void RemoveWindow ( WinInfo *winptr );
void Got_WM_STATE ( WinInfo *winptr );
void HandleCreate ( XCreateWindowEvent *event );
void HandleDestroy ( XDestroyWindowEvent *event );
void HandleUpdate ( XPropertyEvent *event );
void ProxySaveYourselfPhase2CB ( SmcConn smcConn, SmPointer clientData );
void ProxySaveYourselfCB ( SmcConn smcConn, SmPointer clientData,
int saveType, Bool shutdown, int interactStyle,
Bool fast );
void ProxyDieCB ( SmcConn smcConn, SmPointer clientData );
void ProxySaveCompleteCB ( SmcConn smcConn, SmPointer clientData );
void ProxyShutdownCancelledCB ( SmcConn smcConn, SmPointer clientData );
Status ConnectProxyToSM ( char *previous_id );
void CheckForExistingWindows ( Window root );
Bool
HasSaveYourself (window)
Window window;
{
Atom *protocols;
int numProtocols;
int i, found;
protocols = NULL;
if (XGetWMProtocols (disp, window, &protocols, &numProtocols) != True)
return (False);
found = 0;
if (protocols != NULL)
{
for (i = 0; i < numProtocols; i++)
if (protocols[i] == wmSaveYourselfAtom)
found = 1;
XFree (protocols);
}
return (found);
}
Bool
HasXSMPsupport (window)
Window window;
{
XTextProperty tp;
Bool hasIt = 0;
if (XGetTextProperty (disp, window, &tp, smClientIdAtom))
{
if (tp.encoding == XA_STRING && tp.format == 8 && tp.nitems != 0)
hasIt = 1;
if (tp.value)
XFree ((char *) tp.value);
}
return (hasIt);
}
WinInfo *
GetClientLeader (winptr)
WinInfo *winptr;
{
Atom actual_type;
int actual_format;
unsigned long nitems, bytesafter;
unsigned long *datap = NULL;
WinInfo *leader_winptr = NULL;
Bool failure = 0;
if (XGetWindowProperty (disp, winptr->window, wmClientLeaderAtom,
0L, 1L, False, AnyPropertyType, &actual_type, &actual_format,
&nitems, &bytesafter, (unsigned char **) &datap) == Success)
{
if (actual_type == XA_WINDOW && actual_format == 32 &&
nitems == 1 && bytesafter == 0)
{
Window leader_win = *((Window *) datap);
if (!LookupWindow (leader_win, &leader_winptr, NULL))
failure = 1;
}
if (datap)
XFree (datap);
}
if (failure)
{
/* The client leader was defined, but we couldn't find the window */
return (NULL);
}
else if (leader_winptr)
{
/* We found the real client leader */
return (leader_winptr);
}
else
{
/* There is no client leader defined, return this window */
return (winptr);
}
}
char *
CheckFullyQuantifiedName (name, newstring)
char *name;
int *newstring;
{
/*
* Due to a bug in Xlib (for hpux in particular), some clients
* will have a WM_CLIENT_MACHINE that is not fully quantified.
* For example, we might get "excon" instead of "excon.x.org".
* This really stinks. The best we can do is tag on our own
* domain name.
*/
if (strchr (name, '.') != NULL)
{
*newstring = 0;
return (name);
}
else
{
char hostnamebuf[80];
char *firstDot;
gethostname (hostnamebuf, sizeof hostnamebuf);
firstDot = strchr (hostnamebuf, '.');
if (!firstDot)
{
*newstring = 0;
return (name);
}
else
{
int bytes = strlen (name) + strlen (firstDot + 1) + 2;
char *newptr;
newptr = (char *) malloc (bytes);
sprintf (newptr, "%s.%s", name, firstDot + 1);
*newstring = 1;
return (newptr);
}
}
}
void FinishSaveYourself (winInfo, has_WM_SAVEYOURSELF)
WinInfo *winInfo;
Bool has_WM_SAVEYOURSELF;
{
SmProp prop1, prop2, prop3, *props[3];
SmPropValue prop1val, prop2val, prop3val;
int i;
if (!winInfo->got_first_save_yourself)
{
char userId[20], restartService[80];
char *fullyQuantifiedName;
int newstring;
prop1.name = SmProgram;
prop1.type = SmARRAY8;
prop1.num_vals = 1;
prop1.vals = &prop1val;
prop1val.value = (SmPointer) winInfo->wm_command[0];
prop1val.length = strlen (winInfo->wm_command[0]);
sprintf (userId, "%ld", (long)getuid());
prop2.name = SmUserID;
prop2.type = SmARRAY8;
prop2.num_vals = 1;
prop2.vals = &prop2val;
prop2val.value = (SmPointer) userId;
prop2val.length = strlen (userId);
fullyQuantifiedName = CheckFullyQuantifiedName (
(char *) winInfo->wm_client_machine.value, &newstring);
sprintf (restartService, "rstart-rsh/%s", fullyQuantifiedName);
if (newstring)
free (fullyQuantifiedName);
prop3.name = "_XC_RestartService";
prop3.type = SmLISTofARRAY8;
prop3.num_vals = 1;
prop3.vals = &prop3val;
prop3val.value = (SmPointer) restartService;
prop3val.length = strlen (restartService);
props[0] = &prop1;
props[1] = &prop2;
props[2] = &prop3;
SmcSetProperties (winInfo->smc_conn, 3, props);
winInfo->got_first_save_yourself = 1;
}
prop1.name = SmRestartCommand;
prop1.type = SmLISTofARRAY8;
prop1.num_vals = winInfo->wm_command_count;
prop1.vals = (SmPropValue *) malloc (
winInfo->wm_command_count * sizeof (SmPropValue));
if (!prop1.vals)
{
SmcSaveYourselfDone (winInfo->smc_conn, False);
return;
}
for (i = 0; i < winInfo->wm_command_count; i++)
{
prop1.vals[i].value = (SmPointer) winInfo->wm_command[i];
prop1.vals[i].length = strlen (winInfo->wm_command[i]);
}
prop2.name = SmCloneCommand;
prop2.type = SmLISTofARRAY8;
prop2.num_vals = winInfo->wm_command_count;
prop2.vals = prop1.vals;
props[0] = &prop1;
props[1] = &prop2;
SmcSetProperties (winInfo->smc_conn, 2, props);
free ((char *) prop1.vals);
/*
* If the client doesn't support WM_SAVE_YOURSELF, we should
* return failure for the save, since we really don't know if
* the application needed to save state.
*/
SmcSaveYourselfDone (winInfo->smc_conn, has_WM_SAVEYOURSELF);
}
void
SaveYourselfCB (smcConn, clientData, saveType, shutdown, interactStyle, fast)
SmcConn smcConn;
SmPointer clientData;
int saveType;
Bool shutdown;
int interactStyle;
Bool fast;
{
WinInfo *winInfo = (WinInfo *) clientData;
if (!winInfo->has_save_yourself)
{
FinishSaveYourself (winInfo, False);
}
else
{
XClientMessageEvent saveYourselfMessage;
/* Send WM_SAVE_YOURSELF */
saveYourselfMessage.type = ClientMessage;
saveYourselfMessage.window = winInfo->window;
saveYourselfMessage.message_type = wmProtocolsAtom;
saveYourselfMessage.format = 32;
saveYourselfMessage.data.l[0] = wmSaveYourselfAtom;
saveYourselfMessage.data.l[1] = CurrentTime;
if (XSendEvent (disp, winInfo->window, False, NoEventMask,
(XEvent *) &saveYourselfMessage))
{
winInfo->waiting_for_update = 1;
if (debug)
{
printf ("Sent SAVE YOURSELF to 0x%x\n",
(unsigned int)winInfo->window);
printf ("\n");
}
}
else
{
if (debug)
{
printf ("Failed to send SAVE YOURSELF to 0x%x\n",
(unsigned int)winInfo->window);
printf ("\n");
}
}
}
}
void
DieCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
WinInfo *winInfo = (WinInfo *) clientData;
SmcCloseConnection (winInfo->smc_conn, 0, NULL);
winInfo->smc_conn = NULL;
XtRemoveInput (winInfo->input_id);
/* Now tell the client to die */
if (debug)
printf ("Trying to kill 0x%x\n", (unsigned int)winInfo->window);
XSync (disp, 0);
XKillClient (disp, winInfo->window);
XSync (disp, 0);
/*
* Proxy must exit when all clients die, and the proxy itself
* must have received a Die.
*/
die_count++;
if (die_count == proxy_count && ok_to_die)
{
exit (0);
}
}
void
SaveCompleteCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
/*
* Nothing to do here.
*/
}
void
ShutdownCancelledCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
/*
* Since we did not request to interact or request save yourself
* phase 2, we know we already sent the save yourself done, so
* there is nothing to do here.
*/
}
void
ProcessIceMsgProc (client_data, source, id)
XtPointer client_data;
int *source;
XtInputId *id;
{
IceConn ice_conn = (IceConn) client_data;
IceProcessMessages (ice_conn, NULL, NULL);
}
void
NullIceErrorHandler (iceConn, swap,
offendingMinorOpcode, offendingSequence, errorClass, severity, values)
IceConn iceConn;
Bool swap;
int offendingMinorOpcode;
unsigned long offendingSequence;
int errorClass;
int severity;
IcePointer values;
{
return;
}
void
ConnectClientToSM (winInfo)
WinInfo *winInfo;
{
char errorMsg[256];
unsigned long mask;
SmcCallbacks callbacks;
IceConn ice_conn;
char *prevId;
mask = SmcSaveYourselfProcMask | SmcDieProcMask |
SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask;
callbacks.save_yourself.callback = SaveYourselfCB;
callbacks.save_yourself.client_data = (SmPointer) winInfo;
callbacks.die.callback = DieCB;
callbacks.die.client_data = (SmPointer) winInfo;
callbacks.save_complete.callback = SaveCompleteCB;
callbacks.save_complete.client_data = (SmPointer) winInfo;
callbacks.shutdown_cancelled.callback = ShutdownCancelledCB;
callbacks.shutdown_cancelled.client_data = (SmPointer) winInfo;
prevId = LookupClientID (winInfo);
/*
* In case a protocol error occurs when opening the connection,
* (e.g. an authentication error), we set a null error handler
* before the open, then restore the default handler after the open.
*/
IceSetErrorHandler (NullIceErrorHandler);
winInfo->smc_conn = SmcOpenConnection (
NULL, /* use SESSION_MANAGER env */
(SmPointer) winInfo, /* force a new connection */
SmProtoMajor,
SmProtoMinor,
mask,
&callbacks,
prevId,
&winInfo->client_id,
256, errorMsg);
IceSetErrorHandler (NULL);
if (winInfo->smc_conn == NULL)
return;
ice_conn = SmcGetIceConnection (winInfo->smc_conn);
winInfo->input_id = XtAppAddInput (
appContext,
IceConnectionNumber (ice_conn),
(XtPointer) XtInputReadMask,
ProcessIceMsgProc,
(XtPointer) ice_conn);
if (debug)
{
printf ("Connected to SM, window = 0x%x\n",
(unsigned int)winInfo->window);
printf ("\n");
}
proxy_count++;
}
int
MyErrorHandler (display, event)
Display *display;
XErrorEvent *event;
{
caught_error = 1;
return 0;
}
Bool
LookupWindow (window, ptr_ret, prev_ptr_ret)
Window window;
WinInfo **ptr_ret;
WinInfo **prev_ptr_ret;
{
WinInfo *ptr, *prev;
ptr = win_head;
prev = NULL;
while (ptr)
{
if (ptr->window == window)
break;
else
{
prev = ptr;
ptr = ptr->next;
}
}
if (ptr)
{
if (ptr_ret)
*ptr_ret = ptr;
if (prev_ptr_ret)
*prev_ptr_ret = prev;
return (1);
}
else
return (0);
}
WinInfo *
AddNewWindow (window)
Window window;
{
WinInfo *newptr;
if (LookupWindow (window, NULL, NULL))
return (NULL);
newptr = (WinInfo *) malloc (sizeof (WinInfo));
if (newptr == NULL)
return (NULL);
newptr->next = win_head;
win_head = newptr;
newptr->window = window;
newptr->smc_conn = NULL;
newptr->tested_for_sm_client_id = 0;
newptr->client_id = NULL;
newptr->wm_command = NULL;
newptr->wm_command_count = 0;
newptr->class.res_name = NULL;
newptr->class.res_class = NULL;
newptr->wm_name = NULL;
newptr->wm_client_machine.value = NULL;
newptr->wm_client_machine.nitems = 0;
newptr->has_save_yourself = 0;
newptr->waiting_for_update = 0;
newptr->got_first_save_yourself = 0;
return (newptr);
}
void
RemoveWindow (winptr)
WinInfo *winptr;
{
WinInfo *ptr, *prev;
if (LookupWindow (winptr->window, &ptr, &prev))
{
if (prev == NULL)
win_head = ptr->next;
else
prev->next = ptr->next;
if (ptr->client_id)
free (ptr->client_id);
if (ptr->wm_command)
XFreeStringList (ptr->wm_command);
if (ptr->wm_name)
XFree (ptr->wm_name);
if (ptr->wm_client_machine.value)
XFree (ptr->wm_client_machine.value);
if (ptr->class.res_name)
XFree (ptr->class.res_name);
if (ptr->class.res_class)
XFree (ptr->class.res_class);
free ((char *) ptr);
}
}
void
Got_WM_STATE (winptr)
WinInfo *winptr;
{
WinInfo *leader_winptr;
/*
* If we already got WM_STATE and tested for SM_CLIENT_ID, we
* shouldn't do it again.
*/
if (winptr->tested_for_sm_client_id)
{
return;
}
/*
* Set a null error handler, in case this window goes away
* behind our back.
*/
caught_error = 0;
XSetErrorHandler (MyErrorHandler);
/*
* Get the client leader window.
*/
leader_winptr = GetClientLeader (winptr);
if (caught_error)
{
caught_error = 0;
RemoveWindow (winptr);
XSetErrorHandler (NULL);
return;
}
/*
* If we already checked for SM_CLIENT_ID on the client leader
* window, don't do it again.
*/
if (!leader_winptr || leader_winptr->tested_for_sm_client_id)
{
caught_error = 0;
XSetErrorHandler (NULL);
return;
}
leader_winptr->tested_for_sm_client_id = 1;
if (!HasXSMPsupport (leader_winptr->window))
{
XFetchName (disp, leader_winptr->window, &leader_winptr->wm_name);
XGetCommand (disp, leader_winptr->window,
&leader_winptr->wm_command,
&leader_winptr->wm_command_count);
XGetClassHint (disp, leader_winptr->window, &leader_winptr->class);
XGetWMClientMachine (disp, leader_winptr->window,
&leader_winptr->wm_client_machine);
if (leader_winptr->wm_name != NULL &&
leader_winptr->wm_command != NULL &&
leader_winptr->wm_command_count > 0 &&
leader_winptr->class.res_name != NULL &&
leader_winptr->class.res_class != NULL &&
leader_winptr->wm_client_machine.value != NULL &&
leader_winptr->wm_client_machine.nitems != 0)
{
leader_winptr->has_save_yourself =
HasSaveYourself (leader_winptr->window);
ConnectClientToSM (leader_winptr);
}
}
XSync (disp, 0);
XSetErrorHandler (NULL);
if (caught_error)
{
caught_error = 0;
RemoveWindow (leader_winptr);
}
}
void
HandleCreate (event)
XCreateWindowEvent *event;
{
Atom actual_type;
int actual_format;
unsigned long nitems, bytesafter;
unsigned long *datap = NULL;
WinInfo *winptr;
Bool got_wm_state = 0;
/*
* We are waiting for all proxy connections to close so we can die.
* Don't handle new connections.
*/
if (ok_to_die)
return;
/*
* Add the new window
*/
if ((winptr = AddNewWindow (event->window)) == NULL)
return;
/*
* Right after the window was created, it might have been destroyed,
* so the following Xlib calls might fail. Need to catch the error
* by installing an error handler.
*/
caught_error = 0;
XSetErrorHandler (MyErrorHandler);
/*
* Select for Property Notify on the window so we can determine
* when WM_STATE is defined. To avoid a race condition, we must
* do this _before_ we check for WM_STATE right now.
*
* Select for Substructure Notify so we can determine when the
* window is destroyed.
*/
XSelectInput (disp, event->window,
SubstructureNotifyMask | PropertyChangeMask);
/*
* WM_STATE may already be there. Check now.
*/
if (XGetWindowProperty (disp, event->window, wmStateAtom,
0L, 2L, False, AnyPropertyType,
&actual_type, &actual_format, &nitems, &bytesafter,
(unsigned char **) &datap) == Success && datap)
{
if (nitems > 0)
got_wm_state = 1;
if (datap)
XFree ((char *) datap);
}
XSync (disp, 0);
XSetErrorHandler (NULL);
if (caught_error)
{
caught_error = 0;
RemoveWindow (winptr);
}
else if (got_wm_state)
{
Got_WM_STATE (winptr);
}
}
void
HandleDestroy (event)
XDestroyWindowEvent *event;
{
WinInfo *winptr;
if (LookupWindow (event->window, &winptr, NULL))
{
if (winptr->smc_conn)
{
SmcCloseConnection (winptr->smc_conn, 0, NULL);
XtRemoveInput (winptr->input_id);
proxy_count--;
}
if (debug)
{
printf ("Removed window (window = 0x%x)\n",
(unsigned int)winptr->window);
printf ("\n");
}
RemoveWindow (winptr);
}
}
void
HandleUpdate (event)
XPropertyEvent *event;
{
Window window = event->window;
WinInfo *winptr;
if (!LookupWindow (window, &winptr, NULL))
return;
if (event->atom == wmStateAtom)
{
Got_WM_STATE (winptr);
}
else if (event->atom == XA_WM_COMMAND && winptr->waiting_for_update)
{
/* Finish off the Save Yourself */
if (winptr->wm_command)
{
XFreeStringList (winptr->wm_command);
winptr->wm_command = NULL;
winptr->wm_command_count = 0;
}
XGetCommand (disp, window,
&winptr->wm_command,
&winptr->wm_command_count);
winptr->waiting_for_update = 0;
FinishSaveYourself (winptr, True);
}
}
void
ProxySaveYourselfPhase2CB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
char *filename;
Bool success = True;
SmProp prop1, prop2, prop3, *props[3];
SmPropValue prop1val, prop2val, prop3val;
char discardCommand[80];
int numVals, i;
static int first_time = 1;
if (first_time)
{
char userId[20];
char hint = SmRestartIfRunning;
prop1.name = SmProgram;
prop1.type = SmARRAY8;
prop1.num_vals = 1;
prop1.vals = &prop1val;
prop1val.value = Argv[0];
prop1val.length = strlen (Argv[0]);
sprintf (userId, "%ld", (long)getuid());
prop2.name = SmUserID;
prop2.type = SmARRAY8;
prop2.num_vals = 1;
prop2.vals = &prop2val;
prop2val.value = (SmPointer) userId;
prop2val.length = strlen (userId);
prop3.name = SmRestartStyleHint;
prop3.type = SmCARD8;
prop3.num_vals = 1;
prop3.vals = &prop3val;
prop3val.value = (SmPointer) &hint;
prop3val.length = 1;
props[0] = &prop1;
props[1] = &prop2;
props[2] = &prop3;
SmcSetProperties (smcConn, 3, props);
first_time = 0;
}
if ((filename = WriteProxyFile ()) == NULL)
{
success = False;
goto finishUp;
}
prop1.name = SmRestartCommand;
prop1.type = SmLISTofARRAY8;
prop1.vals = (SmPropValue *) malloc (
(Argc + 4) * sizeof (SmPropValue));
if (!prop1.vals)
{
success = False;
goto finishUp;
}
numVals = 0;
for (i = 0; i < Argc; i++)
{
if (strcmp (Argv[i], "-clientId") == 0 ||
strcmp (Argv[i], "-restore") == 0)
{
i++;
}
else
{
prop1.vals[numVals].value = (SmPointer) Argv[i];
prop1.vals[numVals++].length = strlen (Argv[i]);
}
}
prop1.vals[numVals].value = (SmPointer) "-clientId";
prop1.vals[numVals++].length = 9;
prop1.vals[numVals].value = (SmPointer) proxy_clientId;
prop1.vals[numVals++].length = strlen (proxy_clientId);
prop1.vals[numVals].value = (SmPointer) "-restore";
prop1.vals[numVals++].length = 8;
prop1.vals[numVals].value = (SmPointer) filename;
prop1.vals[numVals++].length = strlen (filename);
prop1.num_vals = numVals;
sprintf (discardCommand, "rm %s", filename);
prop2.name = SmDiscardCommand;
prop2.type = SmARRAY8;
prop2.num_vals = 1;
prop2.vals = &prop2val;
prop2val.value = (SmPointer) discardCommand;
prop2val.length = strlen (discardCommand);
props[0] = &prop1;
props[1] = &prop2;
SmcSetProperties (smcConn, 2, props);
free ((char *) prop1.vals);
finishUp:
SmcSaveYourselfDone (smcConn, success);
sent_save_done = 1;
if (filename)
free (filename);
}
void
ProxySaveYourselfCB (smcConn, clientData, saveType,
shutdown, interactStyle, fast)
SmcConn smcConn;
SmPointer clientData;
int saveType;
Bool shutdown;
int interactStyle;
Bool fast;
{
/*
* We want the proxy to respond to the Save Yourself after all
* the regular XSMP clients have finished with the save (and possibly
* interacted with the user).
*/
if (!SmcRequestSaveYourselfPhase2 (smcConn,
ProxySaveYourselfPhase2CB, NULL))
{
SmcSaveYourselfDone (smcConn, False);
sent_save_done = 1;
}
else
sent_save_done = 0;
}
void
ProxyDieCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
SmcCloseConnection (proxy_smcConn, 0, NULL);
XtRemoveInput (proxy_iceInputId);
if (die_count == proxy_count)
exit (0);
else
ok_to_die = 1;
}
void
ProxySaveCompleteCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
;
}
void
ProxyShutdownCancelledCB (smcConn, clientData)
SmcConn smcConn;
SmPointer clientData;
{
if (!sent_save_done)
{
SmcSaveYourselfDone (smcConn, False);
sent_save_done = 1;
}
}
Status
ConnectProxyToSM (previous_id)
char *previous_id;
{
char errorMsg[256];
unsigned long mask;
SmcCallbacks callbacks;
IceConn iceConn;
mask = SmcSaveYourselfProcMask | SmcDieProcMask |
SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask;
callbacks.save_yourself.callback = ProxySaveYourselfCB;
callbacks.save_yourself.client_data = (SmPointer) NULL;
callbacks.die.callback = ProxyDieCB;
callbacks.die.client_data = (SmPointer) NULL;
callbacks.save_complete.callback = ProxySaveCompleteCB;
callbacks.save_complete.client_data = (SmPointer) NULL;
callbacks.shutdown_cancelled.callback = ProxyShutdownCancelledCB;
callbacks.shutdown_cancelled.client_data = (SmPointer) NULL;
proxy_smcConn = SmcOpenConnection (
NULL, /* use SESSION_MANAGER env */
(SmPointer) appContext,
SmProtoMajor,
SmProtoMinor,
mask,
&callbacks,
previous_id,
&proxy_clientId,
256, errorMsg);
if (proxy_smcConn == NULL)
return (0);
iceConn = SmcGetIceConnection (proxy_smcConn);
proxy_iceInputId = XtAppAddInput (
appContext,
IceConnectionNumber (iceConn),
(XtPointer) XtInputReadMask,
ProcessIceMsgProc,
(XtPointer) iceConn);
return (1);
}
void
CheckForExistingWindows (root)
Window root;
{
Window dontCare1, dontCare2, *children, client_window;
unsigned int nchildren, i;
XCreateWindowEvent event;
/*
* We query the root tree for all windows created thus far.
* Note that at any moment after XQueryTree is called, a
* window may be deleted. So we must take extra care to make
* sure a window really exists.
*/
XQueryTree (disp, root, &dontCare1, &dontCare2, &children, &nchildren);
for (i = 0; i < nchildren; i++)
{
event.window = children[i];
HandleCreate (&event);
caught_error = 0;
XSetErrorHandler (MyErrorHandler);
client_window = XmuClientWindow (disp, children[i]);
XSetErrorHandler (NULL);
if (!caught_error && client_window != children[i])
{
event.window = client_window;
HandleCreate (&event);
}
}
}
int
main (int argc, char *argv[])
{
char *restore_filename = NULL;
char *client_id = NULL;
int i, zero = 0;
Argc = argc;
Argv = argv;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'd': /* -debug */
debug = 1;
continue;
case 'c': /* -clientId */
if (++i >= argc) goto usage;
client_id = argv[i];
continue;
case 'r': /* -restore */
if (++i >= argc) goto usage;
restore_filename = argv[i];
continue;
}
}
usage:
fprintf (stderr,
"usage: %s [-clientId id] [-restore file] [-debug]\n", argv[0]);
exit (1);
}
XtToolkitInitialize ();
appContext = XtCreateApplicationContext ();
if (!(disp = XtOpenDisplay (appContext, NULL, "SM-PROXY", "SM-PROXY",
NULL, 0, &zero, NULL)))
{
fprintf (stderr, "smproxy: unable to open display\n");
exit (1);
}
if (restore_filename)
ReadProxyFile (restore_filename);
if (!ConnectProxyToSM (client_id))
{
fprintf (stderr, "smproxy: unable to connect to session manager\n");
exit (1);
}
wmProtocolsAtom = XInternAtom (disp, "WM_PROTOCOLS", False);
wmSaveYourselfAtom = XInternAtom (disp, "WM_SAVE_YOURSELF", False);
wmStateAtom = XInternAtom (disp, "WM_STATE", False);
smClientIdAtom = XInternAtom (disp, "SM_CLIENT_ID", False);
wmClientLeaderAtom = XInternAtom (disp, "WM_CLIENT_LEADER", False);
for (i = 0; i < ScreenCount (disp); i++)
{
Window root = RootWindow (disp, i);
XSelectInput (disp, root, SubstructureNotifyMask | PropertyChangeMask);
CheckForExistingWindows (root);
}
while (1)
{
XEvent event;
XtAppNextEvent (appContext, &event);
switch (event.type)
{
case CreateNotify:
HandleCreate (&event.xcreatewindow);
break;
case DestroyNotify:
HandleDestroy (&event.xdestroywindow);
break;
case PropertyNotify:
HandleUpdate (&event.xproperty);
break;
default:
XtDispatchEvent (&event);
break;
}
}
exit(0);
}
|