]> git.decadent.org.uk Git - ion3.git/blob - mod_sp/main.c
[svn-upgrade] Integrating new upstream version, ion3 (20070203)
[ion3.git] / mod_sp / main.c
1 /*
2  * ion/mod_sp/main.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 <string.h>
13
14 #include <libtu/map.h>
15 #include <libtu/minmax.h>
16 #include <libextl/readconfig.h>
17 #include <libmainloop/hooks.h>
18
19 #include <ioncore/saveload.h>
20 #include <ioncore/screen.h>
21 #include <ioncore/mplex.h>
22 #include <ioncore/ioncore.h>
23 #include <ioncore/global.h>
24 #include <ioncore/framep.h>
25 #include <ioncore/frame.h>
26 #include <ioncore/names.h>
27
28 #include "main.h"
29 #include "exports.h"
30
31
32 /*{{{ Module information */
33
34 #include "../version.h"
35
36 char mod_sp_ion_api_version[]=ION_API_VERSION;
37
38
39 /*}}}*/
40
41
42 /*{{{ Bindmaps, config, etc. */
43
44
45 #define SP_NAME  "*scratchpad*"
46
47
48 /*}}}*/
49
50
51 /*{{{ Exports */
52
53
54 static WRegion *create_frame_scratchpad(WWindow *parent, const WFitParams *fp,
55                                         void *unused)
56 {
57     return (WRegion*)create_frame(parent, fp, FRAME_MODE_UNKNOWN);
58 }
59
60
61 static WFrame *create(WMPlex *mplex, int flags)
62 {
63     WFrame *sp;
64     WMPlexAttachParams par;
65     int sw=REGION_GEOM(mplex).w, sh=REGION_GEOM(mplex).h;
66
67     par.flags=(flags
68                |MPLEX_ATTACH_UNNUMBERED
69                |MPLEX_ATTACH_MODAL
70                |MPLEX_ATTACH_SIZEPOLICY
71                |MPLEX_ATTACH_GEOM);
72     par.szplcy=SIZEPOLICY_FREE_GLUE;
73     
74     par.geom.w=minof(sw, CF_SCRATCHPAD_DEFAULT_W);
75     par.geom.h=minof(sh, CF_SCRATCHPAD_DEFAULT_H);
76     par.geom.x=(sw-par.geom.w)/2;
77     par.geom.y=(sh-par.geom.h)/2;
78
79     sp=(WFrame*)mplex_do_attach_new((WMPlex*)mplex, &par,
80                                     create_frame_scratchpad,
81                                     NULL);
82     
83
84     if(sp==NULL){
85         warn(TR("Unable to create scratchpad."));
86     }
87     
88     region_set_name((WRegion*)sp, SP_NAME);
89     
90     return sp;
91 }
92
93
94 static bool is_scratchpad(WRegion *reg)
95 {
96     char *nm=reg->ni.name;
97     int inst_off=reg->ni.inst_off;
98     
99     if(nm==NULL)
100         return FALSE;
101     
102     if(inst_off<0)
103         return (strcmp(nm, SP_NAME)==0);
104     else
105         return (strncmp(nm, SP_NAME, inst_off)==0);
106 }
107
108
109 /*EXTL_DOC
110  * Change displayed status of some scratchpad on \var{mplex} if one is 
111  * found. The parameter \var{how} is one of (set/unset/toggle).
112  */
113 EXTL_EXPORT
114 bool mod_sp_set_shown_on(WMPlex *mplex, const char *how)
115 {
116     int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
117     WMPlexIterTmp tmp;
118     WRegion *reg;
119     bool found=FALSE;
120     
121     FOR_ALL_MANAGED_BY_MPLEX(mplex, reg, tmp){
122         if(is_scratchpad(reg)){
123             mplex_set_hidden(mplex, reg, setpar);
124             found=TRUE;
125         }
126     }
127
128     if(!found){
129         int sp=libtu_string_to_setparam(how);
130         if(sp==SETPARAM_SET || sp==SETPARAM_TOGGLE)
131             found=(create(mplex, 0)!=NULL);
132     }
133     
134     return found;
135 }
136
137
138 /*EXTL_DOC
139  * Toggle displayed status of \var{sp}.
140  * The parameter \var{how} is one of (set/unset/toggle).
141  */
142 EXTL_EXPORT
143 bool mod_sp_set_shown(WFrame *sp, const char *how)
144 {
145     if(sp!=NULL){
146         int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
147         WMPlex *mplex=OBJ_CAST(REGION_MANAGER(sp), WMPlex);
148         if(mplex!=NULL)
149             return mplex_set_hidden(mplex, (WRegion*)sp, setpar);
150     }
151     
152     return FALSE;
153 }
154
155
156 /*}}}*/
157
158
159 /*{{{ Init & deinit */
160
161
162 void mod_sp_deinit()
163 {
164     mod_sp_unregister_exports();
165 }
166
167
168 static void check_and_create()
169 {
170     WMPlexIterTmp tmp;
171     WScreen *scr;
172     WRegion *reg;
173     
174     /* No longer needed, free the memory the list uses. */
175     hook_remove(ioncore_post_layout_setup_hook, check_and_create);
176     
177     FOR_ALL_SCREENS(scr){
178         FOR_ALL_MANAGED_BY_MPLEX((WMPlex*)scr, reg, tmp){
179             if(is_scratchpad(reg))
180                 return;
181         }
182         
183         create(&scr->mplex, MPLEX_ATTACH_HIDDEN);
184     }
185 }
186     
187
188 bool mod_sp_init()
189 {
190     if(!mod_sp_register_exports())
191         return FALSE;
192
193     extl_read_config("cfg_sp", NULL, FALSE);
194     
195     if(ioncore_g.opmode==IONCORE_OPMODE_INIT){
196         hook_add(ioncore_post_layout_setup_hook, check_and_create);
197     }else{
198         check_and_create();
199     }
200     
201     return TRUE;
202 }
203
204
205 /*}}}*/
206