Released version 3-2015061300
[notion.git] / mod_sm / sm.c
blob86a20ce63f4c01b25da92cdb3ebdee405ce5209a
1 /*
2 * ion/mod_sm/sm.c
4 * Copyright (c) Tuomo Valkonen 2004-2009.
6 * See the included file LICENSE for details.
7 */
9 #include <stdlib.h>
10 #include <string.h>
12 #include <libtu/misc.h>
13 #include <libtu/parser.h>
14 #include <libtu/tokenizer.h>
15 #include <libtu/util.h>
17 #include <ioncore/common.h>
18 #include <ioncore/global.h>
19 #include <ioncore/clientwin.h>
20 #include <ioncore/saveload.h>
21 #include <ioncore/property.h>
22 #include <libextl/readconfig.h>
23 #include <ioncore/manage.h>
24 #include <ioncore/ioncore.h>
25 #include <ioncore/exec.h>
26 #include "sm_matchwin.h"
27 #include "sm_session.h"
28 #include "exports.h"
31 /*{{{ Module information */
34 #include "../version.h"
36 char mod_sm_ion_api_version[]=NOTION_API_VERSION;
39 /*}}}*/
42 /*{{{ Manage callback */
45 static bool sm_do_manage(WClientWin *cwin, const WManageParams *param)
47 WPHolder *ph;
48 bool ret;
50 if(param->tfor!=NULL)
51 return FALSE;
53 ph=mod_sm_match_cwin_to_saved(cwin);
54 if(ph==NULL)
55 return FALSE;
57 ret=pholder_attach(ph, 0, (WRegion*)cwin);
59 destroy_obj((Obj*)ph);
61 return ret;
65 /*}}}*/
68 /*{{{ Init/deinit */
71 static void mod_sm_set_sessiondir()
73 const char *smdir=NULL, *id=NULL;
74 char *tmp;
75 bool ok=FALSE;
77 smdir=getenv("SM_SAVE_DIR");
78 id=getenv("GNOME_DESKTOP_SESSION_ID");
80 /* Running under SM, try to use a directory specific
81 * to the session.
83 if(smdir!=NULL){
84 tmp=scat3(smdir, "/", libtu_progbasename());
85 }else if(id!=NULL){
86 tmp=scat("gnome-session-", id);
87 if(tmp!=NULL){
88 char *p=tmp;
89 while(1){
90 p=strpbrk(p, "/ :?*");
91 if(p==NULL)
92 break;
93 *p='-';
94 p++;
97 }else{
98 tmp=scopy("default-session-sm");
101 if(tmp!=NULL){
102 ok=extl_set_sessiondir(tmp);
103 free(tmp);
106 if(!ok)
107 warn(TR("Failed to set session directory."));
112 void mod_sm_deinit()
114 ioncore_set_smhook(NULL);
116 hook_remove(clientwin_do_manage_alt, (WHookDummy*)sm_do_manage);
118 ioncore_set_sm_callbacks(NULL, NULL);
120 mod_sm_unregister_exports();
122 mod_sm_close();
126 int mod_sm_init()
128 if(ioncore_g.sm_client_id!=NULL)
129 mod_sm_set_ion_id(ioncore_g.sm_client_id);
131 if(!mod_sm_init_session())
132 goto err;
134 if(extl_sessiondir()==NULL)
135 mod_sm_set_sessiondir();
137 if(!mod_sm_register_exports())
138 goto err;
140 ioncore_set_sm_callbacks(mod_sm_add_match, mod_sm_get_configuration);
142 hook_add(clientwin_do_manage_alt, (WHookDummy*)sm_do_manage);
144 ioncore_set_smhook(mod_sm_smhook);
146 return TRUE;
148 err:
149 mod_sm_deinit();
150 return FALSE;
154 /*}}}*/