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
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
|
/* $Id$ */
/*-
* Copyright (c) 2003-2006 Benedikt Meurer <benny@xfce.org>
* Copyright (c) 2008 Brian Tarricone <bjt23@cornell.edu>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* The session id generator was taken from the KDE session manager.
* Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <X11/ICE/ICElib.h>
#include <X11/SM/SMlib.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include <gtk/gtk.h>
#include <libwnck/libwnck.h>
#include <libxfcegui4/libxfcegui4.h>
#include <libxfsm/xfsm-splash-engine.h>
#include <libxfsm/xfsm-util.h>
#include <xfce4-session/chooser-icon.h>
#include <xfce4-session/shutdown.h>
#include <xfce4-session/xfsm-chooser.h>
#include <xfce4-session/xfsm-global.h>
#include <xfce4-session/xfsm-legacy.h>
#include <xfce4-session/xfsm-manager.h>
#include <xfce4-session/xfsm-startup.h>
#define DEFAULT_SESSION_NAME "Default"
struct _XfsmManager
{
GObject parent;
XfsmManagerState state;
gint shutdown_type;
gboolean session_chooser;
gchar *session_name;
gchar *session_file;
gboolean compat_gnome;
gboolean compat_kde;
GQueue *starting_properties;
GQueue *pending_properties;
GQueue *restart_properties;
GQueue *running_clients;
gboolean failsafe_mode;
GQueue *failsafe_clients;
guint die_timeout_id;
};
typedef struct _XfsmManagerClass
{
GObjectClass parent;
} XfsmManagerClass;
/*
Prototypes
*/
static void xfsm_manager_class_init (XfsmManagerClass *klass);
static void xfsm_manager_init (XfsmManager *manager);
static void xfsm_manager_finalize (GObject *obj);
static gboolean xfsm_manager_startup (XfsmManager *manager);
static void xfsm_manager_start_client_save_timeout (XfsmManager *manager,
XfsmClient *client);
static void xfsm_manager_cancel_client_save_timeout (XfsmManager *manager,
XfsmClient *client);
static gboolean xfsm_manager_save_timeout (gpointer user_data);
static void xfsm_manager_load_settings (XfsmManager *manager,
XfceRc *rc);
static gboolean xfsm_manager_load_session (XfsmManager *manager);
G_DEFINE_TYPE(XfsmManager, xfsm_manager, G_TYPE_OBJECT)
static void
xfsm_manager_class_init (XfsmManagerClass *klass)
{
GObjectClass *gobject_class = (GObjectClass *)klass;
gobject_class->finalize = xfsm_manager_finalize;
}
static void
xfsm_manager_init (XfsmManager *manager)
{
manager->state = XFSM_MANAGER_STARTUP;
manager->session_chooser = FALSE;
manager->failsafe_mode = TRUE;
manager->shutdown_type = SHUTDOWN_LOGOUT;
manager->pending_properties = g_queue_new ();
manager->starting_properties = g_queue_new ();
manager->restart_properties = g_queue_new ();
manager->running_clients = g_queue_new ();
manager->failsafe_clients = g_queue_new ();
}
static void
xfsm_manager_finalize (GObject *obj)
{
XfsmManager *manager = XFSM_MANAGER(obj);
if (manager->die_timeout_id != 0)
g_source_remove (manager->die_timeout_id);
g_queue_foreach (manager->pending_properties, (GFunc) xfsm_properties_free, NULL);
g_queue_free (manager->pending_properties);
g_queue_foreach (manager->starting_properties, (GFunc) xfsm_properties_free, NULL);
g_queue_free (manager->starting_properties);
g_queue_foreach (manager->restart_properties, (GFunc) xfsm_properties_free, NULL);
g_queue_free (manager->restart_properties);
g_queue_foreach (manager->running_clients, (GFunc) g_object_unref, NULL);
g_queue_free (manager->running_clients);
g_queue_foreach (manager->failsafe_clients, (GFunc) xfsm_failsafe_client_free, NULL);
g_queue_free (manager->failsafe_clients);
g_free (manager->session_name);
g_free (manager->session_file);
G_OBJECT_CLASS (xfsm_manager_parent_class)->finalize (obj);
}
XfsmManager *
xfsm_manager_new (void)
{
return g_object_new (XFSM_TYPE_MANAGER, NULL);
}
static gboolean
xfsm_manager_startup (XfsmManager *manager)
{
xfsm_startup_foreign (manager);
g_queue_sort (manager->pending_properties, (GCompareDataFunc) xfsm_properties_compare, NULL);
xfsm_startup_begin (manager);
return FALSE;
}
static void
xfsm_manager_restore_active_workspace (XfsmManager *manager,
XfceRc *rc)
{
WnckWorkspace *workspace;
GdkDisplay *display;
WnckScreen *screen;
gchar buffer[1024];
gint n, m;
display = gdk_display_get_default ();
for (n = 0; n < gdk_display_get_n_screens (display); ++n)
{
g_snprintf (buffer, 1024, "Screen%d_ActiveWorkspace", n);
if (!xfce_rc_has_entry (rc, buffer))
continue;
m = xfce_rc_read_int_entry (rc, buffer, 0);
screen = wnck_screen_get (n);
wnck_screen_force_update (screen);
if (wnck_screen_get_workspace_count (screen) > m)
{
workspace = wnck_screen_get_workspace (screen, m);
wnck_workspace_activate (workspace, GDK_CURRENT_TIME);
}
}
}
void
xfsm_manager_handle_failed_client (XfsmManager *manager,
XfsmProperties *properties)
{
/* Handle apps that failed to start here */
if (properties->discard_command != NULL)
{
xfsm_verbose ("Client Id = %s failed to start, running discard "
"command now.\n\n", properties->client_id);
g_spawn_sync (properties->current_directory,
properties->discard_command,
properties->environment,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
NULL, NULL,
NULL, NULL);
}
if (g_queue_peek_head (manager->starting_properties) == NULL)
xfsm_startup_session_continue (manager);
}
static gboolean
xfsm_manager_choose_session (XfsmManager *manager,
XfceRc *rc)
{
XfsmSessionInfo *session;
GdkPixbuf *preview_default = NULL;
gboolean load = FALSE;
GList *sessions = NULL;
GList *lp;
gchar **groups;
gchar *name;
gint result;
gint n;
groups = xfce_rc_get_groups (rc);
for (n = 0; groups[n] != NULL; ++n)
{
if (strncmp (groups[n], "Session: ", 9) == 0)
{
xfce_rc_set_group (rc, groups[n]);
session = g_new0 (XfsmSessionInfo, 1);
session->name = groups[n] + 9;
session->atime = xfce_rc_read_int_entry (rc, "LastAccess", 0);
session->preview = xfsm_load_session_preview (session->name);
if (session->preview == NULL)
{
if (G_UNLIKELY (preview_default == NULL))
{
preview_default = gdk_pixbuf_from_pixdata (&chooser_icon_data,
FALSE, NULL);
}
session->preview = GDK_PIXBUF (g_object_ref (preview_default));
}
sessions = g_list_append (sessions, session);
}
}
if (preview_default != NULL)
g_object_unref (preview_default);
if (sessions != NULL)
{
result = xfsm_splash_screen_choose (splash_screen, sessions,
manager->session_name, &name);
if (result == XFSM_CHOOSE_LOGOUT)
{
xfce_rc_close (rc);
exit (EXIT_SUCCESS);
}
else if (result == XFSM_CHOOSE_LOAD)
{
load = TRUE;
}
if (manager->session_name != NULL)
g_free (manager->session_name);
manager->session_name = name;
for (lp = sessions; lp != NULL; lp = lp->next)
{
session = (XfsmSessionInfo *) lp->data;
g_object_unref (session->preview);
g_free (session);
}
g_list_free (sessions);
}
g_strfreev (groups);
return load;
}
static gboolean
xfsm_manager_load_session (XfsmManager *manager)
{
XfsmProperties *properties;
gchar buffer[1024];
XfceRc *rc;
gint count;
if (!g_file_test (manager->session_file, G_FILE_TEST_IS_REGULAR))
return FALSE;
rc = xfce_rc_simple_open (manager->session_file, FALSE);
if (G_UNLIKELY (rc == NULL))
return FALSE;
if (manager->session_chooser && !xfsm_manager_choose_session (manager, rc))
{
xfce_rc_close (rc);
return FALSE;
}
g_snprintf (buffer, 1024, "Session: %s", manager->session_name);
xfce_rc_set_group (rc, buffer);
count = xfce_rc_read_int_entry (rc, "Count", 0);
if (G_UNLIKELY (count <= 0))
{
xfce_rc_close (rc);
return FALSE;
}
while (count-- > 0)
{
g_snprintf (buffer, 1024, "Client%d_", count);
properties = xfsm_properties_load (rc, buffer);
if (G_UNLIKELY (properties == NULL))
continue;
if (xfsm_properties_check (properties))
g_queue_push_tail (manager->pending_properties, properties);
else
xfsm_properties_free (properties);
}
/* load legacy applications */
xfsm_legacy_load_session (rc);
xfce_rc_close (rc);
return g_queue_peek_head (manager->pending_properties) != NULL;
}
static gboolean
xfsm_manager_load_failsafe (XfsmManager *manager,
XfceRc *rc)
{
FailsafeClient *fclient;
const gchar *old_group;
GdkDisplay *display;
gchar **command;
gchar command_entry[256];
gchar screen_entry[256];
gint count;
gint i;
gint n_screen;
if (!xfce_rc_has_group (rc, "Failsafe Session"))
return FALSE;
display = gdk_display_get_default ();
old_group = xfce_rc_get_group (rc);
xfce_rc_set_group (rc, "Failsafe Session");
count = xfce_rc_read_int_entry (rc, "Count", 0);
for (i = 0; i < count; ++i)
{
g_snprintf (command_entry, 256, "Client%d_Command", i);
command = xfce_rc_read_list_entry (rc, command_entry, NULL);
if (G_UNLIKELY (command == NULL))
continue;
g_snprintf (screen_entry, 256, "Client%d_PerScreen", i);
if (xfce_rc_read_bool_entry (rc, screen_entry, FALSE))
{
for (n_screen = 0; n_screen < gdk_display_get_n_screens (display); ++n_screen)
{
fclient = g_new0 (FailsafeClient, 1);
fclient->command = xfce_rc_read_list_entry (rc, command_entry, NULL);
fclient->screen = gdk_display_get_screen (display, n_screen);
g_queue_push_tail (manager->failsafe_clients, fclient);
}
}
else
{
fclient = g_new0 (FailsafeClient, 1);
fclient->command = command;
fclient->screen = gdk_screen_get_default ();
g_queue_push_tail (manager->failsafe_clients, fclient);
}
}
xfce_rc_set_group (rc, old_group);
return g_queue_peek_head (manager->failsafe_clients) != NULL;
}
static void
xfsm_manager_load_settings (XfsmManager *manager,
XfceRc *rc)
{
gboolean session_loaded = FALSE;
const gchar *name;
name = xfce_rc_read_entry (rc, "SessionName", NULL);
if (name != NULL && *name != '\0')
manager->session_name = g_strdup (name);
else
manager->session_name = g_strdup (DEFAULT_SESSION_NAME);
xfce_rc_set_group (rc, "Chooser");
manager->session_chooser = xfce_rc_read_bool_entry (rc, "AlwaysDisplay", FALSE);
session_loaded = xfsm_manager_load_session (manager);
if (session_loaded)
{
xfsm_verbose ("Session \"%s\" loaded successfully.\n\n", manager->session_name);
manager->failsafe_mode = FALSE;
}
else
{
if (!xfsm_manager_load_failsafe (manager, rc))
{
fprintf (stderr, "xfce4-session: Unable to load failsafe session, exiting. Please check\n"
" the value of the environment variable XDG_CONFIG_DIRS\n"
" and make sure that it includes the following path:\n\n"
" " SYSCONFDIR "/xdg\n\n");
xfce_rc_close (rc);
exit (EXIT_FAILURE);
}
manager->failsafe_mode = TRUE;
}
}
void
xfsm_manager_load (XfsmManager *manager,
XfceRc *rc)
{
gchar *display_name;
gchar *resource_name;
#ifdef HAVE_OS_CYGWIN
gchar *s;
#endif
xfce_rc_set_group (rc, "Compatibility");
manager->compat_gnome = xfce_rc_read_bool_entry (rc, "LaunchGnome", FALSE);
manager->compat_kde = xfce_rc_read_bool_entry (rc, "LaunchKDE", FALSE);
xfce_rc_set_group (rc, "General");
display_name = xfce_gdk_display_get_fullname (gdk_display_get_default ());
#ifdef HAVE_OS_CYGWIN
/* rename a colon (:) to a hash (#) under cygwin. windows doesn't like
* filenames with a colon... */
for (s = display_name; *s != '\0'; ++s)
if (*s == ':')
*s = '#';
#endif
resource_name = g_strconcat ("sessions/xfce4-session-", display_name, NULL);
manager->session_file = xfce_resource_save_location (XFCE_RESOURCE_CACHE, resource_name, TRUE);
g_free (resource_name);
g_free (display_name);
xfsm_manager_load_settings (manager, rc);
}
gboolean
xfsm_manager_restart (XfsmManager *manager)
{
GdkPixbuf *preview;
unsigned steps;
g_assert (manager->session_name != NULL);
/* setup legacy application handling */
xfsm_legacy_init ();
/* tell splash screen that the session is starting now */
preview = xfsm_load_session_preview (manager->session_name);
if (preview == NULL)
preview = gdk_pixbuf_from_pixdata (&chooser_icon_data, FALSE, NULL);
steps = g_queue_get_length (manager->failsafe_mode ? manager->failsafe_clients : manager->pending_properties);
xfsm_splash_screen_start (splash_screen, manager->session_name, preview, steps);
g_object_unref (preview);
g_idle_add ((GSourceFunc) xfsm_manager_startup, manager);
return TRUE;
}
void
xfsm_manager_signal_startup_done (XfsmManager *manager)
{
gchar buffer[1024];
XfceRc *rc;
xfsm_verbose ("Manager finished startup, entering IDLE mode now\n\n");
manager->state = XFSM_MANAGER_IDLE;
if (!manager->failsafe_mode)
{
/* restore active workspace, this has to be done after the
* window manager is up, so we do it last.
*/
g_snprintf (buffer, 1024, "Session: %s", manager->session_name);
rc = xfce_rc_simple_open (manager->session_file, TRUE);
xfce_rc_set_group (rc, buffer);
xfsm_manager_restore_active_workspace (manager, rc);
xfce_rc_close (rc);
/* start legacy applications now */
xfsm_legacy_startup ();
}
}
XfsmClient*
xfsm_manager_new_client (XfsmManager *manager,
SmsConn sms_conn,
gchar **error)
{
XfsmClient *client = NULL;
if (G_UNLIKELY (manager->state != XFSM_MANAGER_IDLE)
&& G_UNLIKELY (manager->state != XFSM_MANAGER_STARTUP))
{
if (error != NULL)
*error = "We don't accept clients while in CheckPoint/Shutdown state!";
return NULL;
}
client = xfsm_client_new (sms_conn);
return client;
}
static gint
xfsm_properties_queue_find (gconstpointer a,
gconstpointer b)
{
XfsmProperties *properties = XFSM_PROPERTIES (a);
const gchar *previous_id = (const gchar *) b;
if (strcmp (properties->client_id, previous_id) == 0)
return 0;
return 1;
}
gboolean
xfsm_manager_register_client (XfsmManager *manager,
XfsmClient *client,
const gchar *previous_id)
{
XfsmProperties *properties = NULL;
gchar *client_id;
GList *lp;
if (previous_id != NULL)
{
lp = g_queue_find_custom (manager->starting_properties,
previous_id,
xfsm_properties_queue_find);
if (lp != NULL)
{
properties = XFSM_PROPERTIES (lp->data);
g_queue_delete_link (manager->starting_properties, lp);
}
else
{
lp = g_queue_find_custom (manager->pending_properties,
previous_id,
xfsm_properties_queue_find);
if (lp != NULL)
{
properties = XFSM_PROPERTIES (lp->data);
g_queue_delete_link (manager->pending_properties, lp);
}
}
if (properties != NULL && properties->startup_timeout_id != 0)
{
g_source_remove (properties->startup_timeout_id);
properties->startup_timeout_id = 0;
}
/* If previous_id is invalid, the SM will send a BadValue error message
* to the client and reverts to register state waiting for another
* RegisterClient message.
*/
if (properties == NULL)
return FALSE;
xfsm_client_set_initial_properties (client, properties);
}
else
{
client_id = xfsm_generate_client_id (xfsm_client_get_sms_connection (client));
properties = xfsm_properties_new (client_id,
SmsClientHostName (xfsm_client_get_sms_connection (client)));
xfsm_client_set_initial_properties (client, properties);
g_free (client_id);
}
g_queue_push_tail (manager->running_clients, client);
SmsRegisterClientReply (xfsm_client_get_sms_connection (client), (char *) xfsm_client_get_id (client));
if (previous_id == NULL)
{
SmsSaveYourself (xfsm_client_get_sms_connection (client), SmSaveLocal, False, SmInteractStyleNone, False);
xfsm_client_set_state (client, XFSM_CLIENT_SAVINGLOCAL);
xfsm_manager_start_client_save_timeout (manager, client);
}
if ((manager->failsafe_mode || previous_id != NULL)
&& manager->state == XFSM_MANAGER_STARTUP)
{
/* Only continue the startup if we are either in Failsafe mode (which
* means that we don't have any previous_id at all) or the previous_id
* matched one of the starting_properties. If there was no match
* above, previous_id will be NULL here.
* See http://bugs.xfce.org/view_bug_page.php?f_id=212 for details.
*/
if (g_queue_peek_head (manager->starting_properties) == NULL)
xfsm_startup_session_continue (manager);
}
return TRUE;
}
void
xfsm_manager_start_interact (XfsmManager *manager,
XfsmClient *client)
{
/* notify client of interact */
SmsInteract (xfsm_client_get_sms_connection (client));
xfsm_client_set_state (client, XFSM_CLIENT_INTERACTING);
/* stop save yourself timeout */
xfsm_manager_cancel_client_save_timeout (manager, client);
}
void
xfsm_manager_interact (XfsmManager *manager,
XfsmClient *client,
int dialog_type)
{
GList *lp;
if (G_UNLIKELY (xfsm_client_get_state (client) != XFSM_CLIENT_SAVING))
{
xfsm_verbose ("Client Id = %s, requested INTERACT, but client is not in SAVING mode\n"
" Client will be disconnected now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
}
else if (G_UNLIKELY (manager->state != XFSM_MANAGER_CHECKPOINT)
&& G_UNLIKELY (manager->state != XFSM_MANAGER_SHUTDOWN))
{
xfsm_verbose ("Client Id = %s, requested INTERACT, but manager is not in CheckPoint/Shutdown mode\n"
" Client will be disconnected now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
}
else
{
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) == XFSM_CLIENT_INTERACTING)
{
xfsm_client_set_state (client, XFSM_CLIENT_WAITFORINTERACT);
return;
}
}
xfsm_manager_start_interact (manager, client);
}
}
void
xfsm_manager_interact_done (XfsmManager *manager,
XfsmClient *client,
gboolean cancel_shutdown)
{
GList *lp;
if (G_UNLIKELY (xfsm_client_get_state (client) != XFSM_CLIENT_INTERACTING))
{
xfsm_verbose ("Client Id = %s, send INTERACT DONE, but client is not in INTERACTING state\n"
" Client will be disconnected now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
return;
}
else if (G_UNLIKELY (manager->state != XFSM_MANAGER_CHECKPOINT)
&& G_UNLIKELY (manager->state != XFSM_MANAGER_SHUTDOWN))
{
xfsm_verbose ("Client Id = %s, send INTERACT DONE, but manager is not in CheckPoint/Shutdown state\n"
" Client will be disconnected now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
return;
}
xfsm_client_set_state (client, XFSM_CLIENT_SAVING);
/* Setting the cancel-shutdown field to True indicates that the user
* has requested that the entire shutdown be cancelled. Cancel-
* shutdown may only be True if the corresponding SaveYourself
* message specified True for the shutdown field and Any or Error
* for the interact-style field. Otherwise, cancel-shutdown must be
* False.
*/
if (cancel_shutdown && manager->state == XFSM_MANAGER_SHUTDOWN)
{
/* we go into checkpoint state from here... */
manager->state = XFSM_MANAGER_CHECKPOINT;
/* If a shutdown is in progress, the user may have the option
* of cancelling the shutdown. If the shutdown is cancelled
* (specified in the "Interact Done" message), the session
* manager should send a "Shutdown Cancelled" message to each
* client that requested to interact.
*/
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) != XFSM_CLIENT_WAITFORINTERACT)
continue;
/* reset all clients that are waiting for interact */
xfsm_client_set_state (client, XFSM_CLIENT_SAVING);
SmsShutdownCancelled (xfsm_client_get_sms_connection (client));
}
}
else
{
/* let next client interact */
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) == XFSM_CLIENT_WAITFORINTERACT)
{
xfsm_manager_start_interact (manager, client);
break;
}
}
}
/* restart save yourself timeout for client */
xfsm_manager_start_client_save_timeout (manager, client);
}
void
xfsm_manager_save_yourself (XfsmManager *manager,
XfsmClient *client,
gint save_type,
gboolean shutdown,
gint interact_style,
gboolean fast,
gboolean global)
{
gboolean shutdown_save = TRUE;
GList *lp;
if (G_UNLIKELY (xfsm_client_get_state (client) != XFSM_CLIENT_IDLE))
{
xfsm_verbose ("Client Id = %s, requested SAVE YOURSELF, but client is not in IDLE mode.\n"
" Client will be nuked now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
return;
}
else if (G_UNLIKELY (manager->state != XFSM_MANAGER_IDLE))
{
xfsm_verbose ("Client Id = %s, requested SAVE YOURSELF, but manager is not in IDLE mode.\n"
" Client will be nuked now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
return;
}
if (!global)
{
/* client requests a local checkpoint. We slightly ignore
* shutdown here, since it does not make sense for a local
* checkpoint.
*/
SmsSaveYourself (xfsm_client_get_sms_connection (client), save_type, FALSE, interact_style, fast);
xfsm_client_set_state (client, XFSM_CLIENT_SAVINGLOCAL);
xfsm_manager_start_client_save_timeout (manager, client);
}
else
{
if (!fast && shutdown && !shutdownDialog (manager->session_name, &manager->shutdown_type, &shutdown_save))
return;
if (!shutdown || shutdown_save)
{
manager->state = shutdown ? XFSM_MANAGER_SHUTDOWN : XFSM_MANAGER_CHECKPOINT;
/* handle legacy applications first! */
xfsm_legacy_perform_session_save ();
if (xfsm_client_get_state (client) == XFSM_CLIENT_INTERACTING)
{
xfsm_client_set_state (client, XFSM_CLIENT_WAITFORINTERACT);
return;
}
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
XfsmProperties *properties = xfsm_client_get_properties (client);
/* xterm's session management is broken, so we won't
* send a SAVE YOURSELF to xterms */
if (properties->program != NULL
&& strcasecmp (properties->program, "xterm") == 0)
{
continue;
}
if (xfsm_client_get_state (client) != XFSM_CLIENT_SAVINGLOCAL)
{
SmsSaveYourself (xfsm_client_get_sms_connection (client), save_type, shutdown,
interact_style, fast);
}
xfsm_client_set_state (client, XFSM_CLIENT_SAVING);
xfsm_manager_start_client_save_timeout (manager, client);
}
}
else
{
/* shutdown session without saving */
xfsm_manager_perform_shutdown (manager);
}
}
}
void
xfsm_manager_save_yourself_phase2 (XfsmManager *manager,
XfsmClient *client)
{
if (manager->state != XFSM_MANAGER_CHECKPOINT && manager->state != XFSM_MANAGER_SHUTDOWN)
{
SmsSaveYourselfPhase2 (xfsm_client_get_sms_connection (client));
xfsm_client_set_state (client, XFSM_CLIENT_SAVINGLOCAL);
xfsm_manager_start_client_save_timeout (manager, client);
}
else
{
xfsm_client_set_state (client, XFSM_CLIENT_WAITFORPHASE2);
xfsm_manager_cancel_client_save_timeout (manager, client);
if (!xfsm_manager_check_clients_saving (manager))
xfsm_manager_maybe_enter_phase2 (manager);
}
}
void
xfsm_manager_save_yourself_done (XfsmManager *manager,
XfsmClient *client,
gboolean success)
{
if (xfsm_client_get_state (client) != XFSM_CLIENT_SAVING && xfsm_client_get_state (client) != XFSM_CLIENT_SAVINGLOCAL)
{
xfsm_verbose ("Client Id = %s send SAVE YOURSELF DONE, while not being "
"in save mode. Prepare to be nuked!\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
}
/* remove client save timeout, as client responded in time */
xfsm_manager_cancel_client_save_timeout (manager, client);
if (xfsm_client_get_state (client) == XFSM_CLIENT_SAVINGLOCAL)
{
/* client completed local SaveYourself */
xfsm_client_set_state (client, XFSM_CLIENT_IDLE);
SmsSaveComplete (xfsm_client_get_sms_connection (client));
}
else if (manager->state != XFSM_MANAGER_CHECKPOINT && manager->state != XFSM_MANAGER_SHUTDOWN)
{
xfsm_verbose ("Client Id = %s, send SAVE YOURSELF DONE, but manager is not in CheckPoint/Shutdown mode.\n"
" Client will be nuked now.\n\n",
xfsm_client_get_id (client));
xfsm_manager_close_connection (manager, client, TRUE);
}
else
{
xfsm_client_set_state (client, XFSM_CLIENT_SAVEDONE);
xfsm_manager_complete_saveyourself (manager);
}
}
void
xfsm_manager_close_connection (XfsmManager *manager,
XfsmClient *client,
gboolean cleanup)
{
IceConn ice_conn;
GList *lp;
xfsm_client_set_state (client, XFSM_CLIENT_DISCONNECTED);
xfsm_manager_cancel_client_save_timeout (manager, client);
if (cleanup)
{
SmsConn sms_conn = xfsm_client_get_sms_connection (client);
ice_conn = SmsGetIceConnection (sms_conn);
SmsCleanUp (sms_conn);
IceSetShutdownNegotiation (ice_conn, False);
IceCloseConnection (ice_conn);
}
if (manager->state == XFSM_MANAGER_SHUTDOWNPHASE2)
{
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) != XFSM_CLIENT_DISCONNECTED)
return;
}
/* all clients finished the DIE phase in time */
if (manager->die_timeout_id)
{
g_source_remove (manager->die_timeout_id);
manager->die_timeout_id = 0;
}
gtk_main_quit ();
}
else if (manager->state == XFSM_MANAGER_SHUTDOWN || manager->state == XFSM_MANAGER_CHECKPOINT)
{
xfsm_verbose ("Client Id = %s, closed connection in checkpoint state\n"
" Session manager will show NO MERCY\n\n",
xfsm_client_get_id (client));
/* stupid client disconnected in CheckPoint state, prepare to be nuked! */
g_queue_remove (manager->running_clients, client);
g_object_unref (client);
xfsm_manager_complete_saveyourself (manager);
}
else
{
XfsmProperties *properties = xfsm_client_get_properties (client);
if (properties != NULL && xfsm_properties_check (properties))
{
if (properties->restart_style_hint == SmRestartAnyway)
{
g_queue_push_tail (manager->restart_properties,
xfsm_client_steal_properties (client));
}
else if (properties->restart_style_hint == SmRestartImmediately)
{
if (++properties->restart_attempts > MAX_RESTART_ATTEMPTS)
{
xfsm_verbose ("Client Id = %s, reached maximum attempts [Restart attempts = %d]\n"
" Will be re-scheduled for run on next startup\n",
properties->client_id, properties->restart_attempts);
g_queue_push_tail (manager->restart_properties,
xfsm_client_steal_properties (client));
}
#if 0
else if (xfsm_manager_run_prop_command (properties, SmRestartCommand))
{
/* XXX - add a timeout here, in case the application does not come up */
g_queue_push_tail (manager->pending_properties,
xfsm_client_steal_properties (client));
}
#endif
}
/* Run the SmDiscardCommand after the client exited in IDLE state.
* FIXME: Need to handle this better when we support restarting
* immediately properly.
* Unfortunately the spec isn't clear about the usage of the discard
* command. Have to check ksmserver/gnome-session, and come up with
* consistent behaviour.
* But for now, this work-around fixes the problem of the evergrowing
* number of xfwm4 session files when restarting xfwm4 within a session.
*/
if (manager->state == XFSM_MANAGER_IDLE && properties->discard_command != NULL)
{
xfsm_verbose ("Client Id = %s exited while in IDLE state, running "
"discard command now.\n\n", properties->client_id);
g_spawn_sync (properties->current_directory,
properties->discard_command,
properties->environment,
G_SPAWN_SEARCH_PATH,
NULL, NULL,
NULL, NULL,
NULL, NULL);
}
}
g_queue_remove (manager->running_clients, client);
g_object_unref (client);
}
}
void
xfsm_manager_close_connection_by_ice_conn (XfsmManager *manager,
IceConn ice_conn)
{
GList *lp;
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (SmsGetIceConnection (xfsm_client_get_sms_connection (client)) == ice_conn)
{
xfsm_manager_close_connection (manager, client, FALSE);
break;
}
}
/* be sure to close the Ice connection in any case */
IceSetShutdownNegotiation (ice_conn, False);
IceCloseConnection (ice_conn);
}
void
xfsm_manager_perform_shutdown (XfsmManager *manager)
{
GList *lp;
/* send SmDie message to all clients */
manager->state = XFSM_MANAGER_SHUTDOWNPHASE2;
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
SmsDie (xfsm_client_get_sms_connection (client));
}
/* give all clients the chance to close the connection */
manager->die_timeout_id = g_timeout_add (DIE_TIMEOUT,
(GSourceFunc) gtk_main_quit,
NULL);
}
gboolean
xfsm_manager_check_clients_saving (XfsmManager *manager)
{
GList *lp;
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) == XFSM_CLIENT_SAVING)
return TRUE;
}
return FALSE;
}
gboolean
xfsm_manager_maybe_enter_phase2 (XfsmManager *manager)
{
gboolean entered_phase2 = FALSE;
GList *lp;
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_state (client) == XFSM_CLIENT_WAITFORPHASE2)
{
entered_phase2 = TRUE;
SmsSaveYourselfPhase2 (xfsm_client_get_sms_connection (client));
xfsm_client_set_state (client, XFSM_CLIENT_SAVING);
xfsm_manager_start_client_save_timeout (manager, client);
xfsm_verbose ("Client Id = %s enters SAVE YOURSELF PHASE2.\n\n",
xfsm_client_get_id (client));
}
}
return entered_phase2;
}
void
xfsm_manager_complete_saveyourself (XfsmManager *manager)
{
GList *lp;
/* Check if still clients in SAVING state or if we have to enter PHASE2
* now. In either case, SaveYourself cannot be completed in this run.
*/
if (xfsm_manager_check_clients_saving (manager) || xfsm_manager_maybe_enter_phase2 (manager))
return;
xfsm_verbose ("Manager finished SAVE YOURSELF, session data will be stored now.\n\n");
/* all clients done, store session data */
xfsm_manager_store_session (manager);
if (manager->state == XFSM_MANAGER_CHECKPOINT)
{
/* reset all clients to idle state */
manager->state = XFSM_MANAGER_IDLE;
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
xfsm_client_set_state (client, XFSM_CLIENT_IDLE);
SmsSaveComplete (xfsm_client_get_sms_connection (client));
}
}
else
{
/* shutdown the session */
xfsm_manager_perform_shutdown (manager);
}
}
typedef struct
{
XfsmManager *manager;
XfsmClient *client;
guint timeout_id;
} XfsmSaveTimeoutData;
static gboolean
xfsm_manager_save_timeout (gpointer user_data)
{
XfsmSaveTimeoutData *stdata = user_data;
xfsm_verbose ("Client id = %s, received SAVE TIMEOUT\n"
" Client will be disconnected now.\n\n",
xfsm_client_get_id (stdata->client));
xfsm_manager_close_connection (stdata->manager, stdata->client, TRUE);
/* returning FALSE below will free the data */
g_object_steal_data (G_OBJECT (stdata->client), "--save-timeout-id");
return FALSE;
}
static void
xfsm_manager_start_client_save_timeout (XfsmManager *manager,
XfsmClient *client)
{
XfsmSaveTimeoutData *sdata = g_new(XfsmSaveTimeoutData, 1);
sdata->manager = manager;
sdata->client = client;
/* |sdata| will get freed when the source gets removed */
sdata->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT, SAVE_TIMEOUT,
xfsm_manager_save_timeout,
sdata, (GDestroyNotify) g_free);
/* ... or, if the object gets destroyed first, the source will get
* removed and will free |sdata| for us. also, if there's a pending
* timer, this call will clear it. */
g_object_set_data_full (G_OBJECT (client), "--save-timeout-id",
GUINT_TO_POINTER (sdata->timeout_id),
(GDestroyNotify) g_source_remove);
}
static void
xfsm_manager_cancel_client_save_timeout (XfsmManager *manager,
XfsmClient *client)
{
/* clearing out the data will call g_source_remove(), which will free it */
g_object_set_data (G_OBJECT (client), "--save-timeout-id", NULL);
}
void
xfsm_manager_store_session (XfsmManager *manager)
{
WnckWorkspace *workspace;
GdkDisplay *display;
WnckScreen *screen;
XfceRc *rc;
GList *lp;
gchar prefix[64];
gchar *backup;
gchar *group;
gint count = 0;
gint n, m;
rc = xfce_rc_simple_open (manager->session_file, FALSE);
if (G_UNLIKELY (rc == NULL))
{
fprintf (stderr,
"xfce4-session: Unable to open session file %s for "
"writing. Session data will not be stored. Please check "
"your installation.\n",
manager->session_file);
return;
}
/* backup the old session file first */
if (g_file_test (manager->session_file, G_FILE_TEST_IS_REGULAR))
{
backup = g_strconcat (manager->session_file, ".bak", NULL);
unlink (backup);
link (manager->session_file, backup);
g_free (backup);
}
group = g_strconcat ("Session: ", manager->session_name, NULL);
xfce_rc_delete_group (rc, group, TRUE);
xfce_rc_set_group (rc, group);
g_free (group);
for (lp = g_queue_peek_nth_link (manager->restart_properties, 0);
lp;
lp = lp->next)
{
XfsmProperties *properties = lp->data;
g_snprintf (prefix, 64, "Client%d_", count);
xfsm_properties_store (properties, rc, prefix);
++count;
}
for (lp = g_queue_peek_nth_link (manager->running_clients, 0);
lp;
lp = lp->next)
{
XfsmClient *client = lp->data;
if (xfsm_client_get_properties (client) == NULL
|| !xfsm_properties_check (xfsm_client_get_properties (client)))
continue;
g_snprintf (prefix, 64, "Client%d_", count);
xfsm_properties_store (xfsm_client_get_properties (client), rc, prefix);
++count;
}
xfce_rc_write_int_entry (rc, "Count", count);
/* store legacy applications state */
xfsm_legacy_store_session (rc);
/* store current workspace numbers */
display = gdk_display_get_default ();
for (n = 0; n < gdk_display_get_n_screens (display); ++n)
{
screen = wnck_screen_get (n);
wnck_screen_force_update (screen);
workspace = wnck_screen_get_active_workspace (screen);
m = wnck_workspace_get_number (workspace);
g_snprintf (prefix, 64, "Screen%d_ActiveWorkspace", n);
xfce_rc_write_int_entry (rc, prefix, m);
}
/* remember time */
xfce_rc_write_int_entry (rc, "LastAccess", time (NULL));
xfce_rc_close (rc);
}
gint
xfsm_manager_get_shutdown_type (XfsmManager *manager)
{
return manager->shutdown_type;
}
GQueue *
xfsm_manager_get_queue (XfsmManager *manager,
XfsmManagerQueueType q_type)
{
switch(q_type)
{
case XFSM_MANAGER_QUEUE_PENDING_PROPS:
return manager->pending_properties;
case XFSM_MANAGER_QUEUE_STARTING_PROPS:
return manager->starting_properties;
case XFSM_MANAGER_QUEUE_RESTART_PROPS:
return manager->restart_properties;
case XFSM_MANAGER_QUEUE_RUNNING_CLIENTS:
return manager->running_clients;
case XFSM_MANAGER_QUEUE_FAILSAFE_CLIENTS:
return manager->failsafe_clients;
default:
g_warning ("Requested invalid queue type %d", (gint)q_type);
return NULL;
}
}
gboolean
xfsm_manager_get_use_failsafe_mode (XfsmManager *manager)
{
return manager->failsafe_mode;
}
gboolean
xfsm_manager_get_compat_startup (XfsmManager *manager,
XfsmManagerCompatType type)
{
switch (type)
{
case XFSM_MANAGER_COMPAT_GNOME:
return manager->compat_gnome;
case XFSM_MANAGER_COMPAT_KDE:
return manager->compat_kde;
default:
g_warning ("Invalid compat startup type %d", type);
return FALSE;
}
}
|