2 * ion/ioncore/saveload.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
16 #include <libtu/objp.h>
17 #include <libextl/readconfig.h>
18 #include <libextl/extl.h>
32 static bool loading_layout=FALSE;
33 static bool layout_load_error=FALSE;
36 /*{{{ Session management module support */
39 static SMAddCallback *add_cb;
40 static SMCfgCallback *cfg_cb;
41 static SMPHolderCallback *ph_cb;
42 static bool clientwin_was_missing=FALSE;
45 void ioncore_set_sm_callbacks(SMAddCallback *add, SMCfgCallback *cfg)
52 void ioncore_get_sm_callbacks(SMAddCallback **add, SMCfgCallback **cfg)
59 void ioncore_set_sm_pholder_callback(SMPHolderCallback *phcb)
65 void ioncore_clientwin_load_missing()
67 clientwin_was_missing=TRUE;
74 /*{{{ Load support functions */
77 WRegion *create_region_load(WWindow *par, const WFitParams *fp,
80 char *objclass=NULL, *name=NULL;
81 WRegionLoadCreateFn* fn=NULL;
82 WRegClassInfo *info=NULL;
85 char *grouped_name=NULL;
87 if(!extl_table_gets_s(tab, "type", &objclass))
93 info=ioncore_lookup_regclass(objclass, FALSE);
98 warn(TR("Unknown class \"%s\", cannot create region."),
100 layout_load_error=loading_layout;
106 clientwin_was_missing=FALSE;
108 reg=fn(par, fp, tab);
111 if(clientwin_was_missing && add_cb!=NULL && ph_cb!=NULL){
112 WPHolder *ph=ph_cb();
115 destroy_obj((Obj*)ph);
119 if(!OBJ_IS(reg, WClientWin)){
120 if(extl_table_gets_s(tab, "name", &name)){
121 region_set_name(reg, name);
136 /*{{{ Save support functions */
139 bool region_supports_save(WRegion *reg)
141 return HAS_DYN(reg, region_get_configuration);
145 ExtlTab region_get_base_configuration(WRegion *reg)
150 tab=extl_create_table();
152 extl_table_sets_s(tab, "type", OBJ_TYPESTR(reg));
154 name=region_name(reg);
156 if(name!=NULL && !OBJ_IS(reg, WClientWin))
157 extl_table_sets_s(tab, "name", name);
163 static bool get_config_clientwins=TRUE;
166 ExtlTab region_get_configuration(WRegion *reg)
168 ExtlTab tab=extl_table_none();
169 if(get_config_clientwins || !OBJ_IS(reg, WClientWin)){
170 CALL_DYN_RET(tab, ExtlTab, region_get_configuration, reg, (reg));
177 * Get configuration tree. If \var{clientwins} is unset, client windows
180 EXTL_EXPORT_AS(WRegion, get_configuration)
181 ExtlTab region_get_configuration_extl(WRegion *reg, bool clientwins)
185 get_config_clientwins=clientwins;
187 tab=region_get_configuration(reg);
189 get_config_clientwins=TRUE;
198 /*{{{ save_workspaces, load_workspaces */
201 static const char backup_msg[]=DUMMY_TR(
202 "There were errors loading layout. Backing up current layout savefile as\n"
204 "If you are _not_ running under a session manager and wish to restore your\n"
205 "old layout, copy this backup file over the layout savefile found in the\n"
206 "same directory while Ion is not running and after having fixed your other\n"
207 "configuration files that are causing this problem. (Maybe a missing\n"
211 bool ioncore_init_layout()
218 ok=extl_read_savefile("saved_layout", &tab);
221 layout_load_error=FALSE;
223 FOR_ALL_SCREENS(scr){
224 ExtlTab scrtab=extl_table_none();
227 /* Potential Xinerama or such support should set the screen ID
228 * of the root window to less than zero, and number its own
229 * fake screens up from 0.
235 scrok=extl_table_geti_t(tab, screen_id(scr), &scrtab);
237 n+=(TRUE==screen_init_layout(scr, scrtab));
240 extl_unref_table(scrtab);
243 loading_layout=FALSE;
245 if(layout_load_error){
247 char tm[]="saved_layout.backup-YYYYMMDDHHMMSS\0\0\0\0";
250 strftime(tm+20, 15, "%Y%m%d%H%M%S", localtime(&t));
251 backup=extl_get_savefile(tm);
253 warn(TR("Unable to get file for layout backup."));
256 if(access(backup, F_OK)==0){
257 warn(TR("Backup file %s already exists."), backup);
261 warn(TR(backup_msg), backup);
262 if(!extl_serialise(backup, tab))
263 warn(TR("Failed backup."));
268 warn(TR("Unable to initialise layout on any screen."));
276 bool ioncore_save_layout()
279 ExtlTab tab=extl_create_table();
282 if(tab==extl_table_none())
285 FOR_ALL_SCREENS(scr){
292 scrtab=region_get_configuration((WRegion*)scr);
294 if(scrtab==extl_table_none()){
295 warn(TR("Unable to get configuration for screen %d."),
298 extl_table_seti_t(tab, screen_id(scr), scrtab);
299 extl_unref_table(scrtab);
303 ret=extl_write_savefile("saved_layout", tab);
305 extl_unref_table(tab);
308 warn(TR("Unable to save layout."));