]> git.decadent.org.uk Git - ion3.git/blob - mod_sm/sm.c
9451489f8b2382e4ec9847847a4ee2f25dff9a23
[ion3.git] / mod_sm / sm.c
1 /*
2  * ion/mod_sm/sm.c
3  *
4  * Copyright (c) Tuomo Valkonen 2004-2008. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include <libtu/misc.h>
13 #include <libtu/parser.h>
14 #include <libtu/tokenizer.h>
15 #include <libtu/util.h>
16
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"
29
30
31 /*{{{ Module information */
32
33
34 #include "../version.h"
35
36 char mod_sm_ion_api_version[]=ION_API_VERSION;
37
38
39 /*}}}*/
40
41
42 /*{{{ Manage callback */
43
44
45 static bool sm_do_manage(WClientWin *cwin, const WManageParams *param)
46 {
47     int transient_mode=TRANSIENT_MODE_OFF;
48     WPHolder *ph;
49     bool ret;
50     
51     if(param->tfor!=NULL)
52         return FALSE;
53     
54     ph=mod_sm_match_cwin_to_saved(cwin);
55     if(ph==NULL)
56         return FALSE;
57     
58     ret=pholder_attach(ph, 0, (WRegion*)cwin);
59     
60     destroy_obj((Obj*)ph);
61     
62     return ret;
63 }
64
65
66 /*}}}*/
67
68
69 /*{{{ Init/deinit */
70
71
72 static void mod_sm_set_sessiondir()
73 {
74     const char *smdir=NULL, *id=NULL;
75     char *tmp;
76     bool ok=FALSE;
77     
78     smdir=getenv("SM_SAVE_DIR");
79     id=getenv("GNOME_DESKTOP_SESSION_ID");
80
81     /* Running under SM, try to use a directory specific
82      * to the session.
83      */
84     if(smdir!=NULL){
85         tmp=scat3(smdir, "/", libtu_progbasename());
86     }else if(id!=NULL){
87         tmp=scat("gnome-session-", id);
88         if(tmp!=NULL){
89             char *p=tmp;
90             while(1){
91                 p=strpbrk(p, "/ :?*");
92                 if(p==NULL)
93                     break;
94                 *p='-';
95                 p++;
96             }
97         }
98     }else{
99         tmp=scopy("default-session-sm");
100     }
101         
102     if(tmp!=NULL){
103         ok=extl_set_sessiondir(tmp);
104         free(tmp);
105     }
106     
107     if(!ok)
108         warn(TR("Failed to set session directory."));
109 }
110
111
112
113 void mod_sm_deinit()
114 {
115     ioncore_set_smhook(NULL);
116
117     hook_remove(clientwin_do_manage_alt, (WHookDummy*)sm_do_manage);
118
119     ioncore_set_sm_callbacks(NULL, NULL);
120     
121     mod_sm_unregister_exports();
122     
123     mod_sm_close();
124 }
125
126
127 int mod_sm_init()
128 {
129     if(ioncore_g.sm_client_id!=NULL)
130         mod_sm_set_ion_id(ioncore_g.sm_client_id);
131     
132     if(!mod_sm_init_session())
133         goto err;
134
135     if(extl_sessiondir()==NULL)
136         mod_sm_set_sessiondir();
137     
138     if(!mod_sm_register_exports())
139         goto err;
140
141     ioncore_set_sm_callbacks(mod_sm_add_match, mod_sm_get_configuration);
142     
143     hook_add(clientwin_do_manage_alt, (WHookDummy*)sm_do_manage);
144
145     ioncore_set_smhook(mod_sm_smhook);
146     
147     return TRUE;
148     
149 err:
150     mod_sm_deinit();
151     return FALSE;
152 }
153
154
155 /*}}}*/
156