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