]> git.decadent.org.uk Git - ion3.git/blob - ioncore/activity.c
60c051973adb125a0e1ce50db0035ba0ebcfbb19
[ion3.git] / ioncore / activity.c
1 /*
2  * ion/ioncore/activity.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-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 <libtu/setparam.h>
13 #include <libtu/minmax.h>
14 #include <libtu/objlist.h>
15 #include "common.h"
16 #include "global.h"
17 #include "region.h"
18 #include "activity.h"
19
20
21 static ObjList *actlist=NULL;
22
23
24 void region_mark_mgd_activity(WRegion *mgr)
25 {
26     bool mgr_marked;
27     
28     if(mgr==NULL)
29         return;
30     
31     mgr_marked=region_is_activity_r(mgr);
32     mgr->mgd_activity++;
33     
34     if(!mgr_marked){
35         region_notify_change(mgr, ioncore_g.notifies.sub_activity);
36         region_mark_mgd_activity(REGION_MANAGER(mgr));
37     }
38 }
39
40
41 void region_clear_mgd_activity(WRegion *mgr)
42 {
43     if(mgr==NULL)
44         return;
45     
46     mgr->mgd_activity=maxof(0, mgr->mgd_activity-1);
47     
48     if(!region_is_activity_r(mgr)){
49         region_notify_change(mgr, ioncore_g.notifies.sub_activity);
50         region_clear_mgd_activity(REGION_MANAGER(mgr));
51     }
52 }
53     
54     
55 static void propagate_activity(WRegion *reg)
56 {
57     region_mark_mgd_activity(REGION_MANAGER(reg));
58 }
59
60
61 static void propagate_clear(WRegion *reg)
62 {
63     region_clear_mgd_activity(REGION_MANAGER(reg));
64 }
65
66
67 bool region_set_activity(WRegion *reg, int sp)
68 {
69     bool set=(reg->flags&REGION_ACTIVITY);
70     bool nset=libtu_do_setparam(sp, set);
71     
72     if(!XOR(set, nset))
73         return nset;
74
75     if(nset){
76         if(REGION_IS_ACTIVE(reg))
77             return FALSE;
78     
79         reg->flags|=REGION_ACTIVITY;
80         objlist_insert_last(&actlist, (Obj*)reg);
81         
82         if(reg->mgd_activity==0)
83             propagate_activity(reg);
84     }else{
85         reg->flags&=~REGION_ACTIVITY;
86         objlist_remove(&actlist, (Obj*)reg);
87         
88         if(reg->mgd_activity==0)
89             propagate_clear(reg);
90     }
91     
92     region_notify_change(reg, ioncore_g.notifies.activity);
93     
94     return nset;
95 }
96
97
98 /*EXTL_DOC
99  * Set activity flag of \var{reg}. The \var{how} parameter most be
100  * one of (set/unset/toggle).
101  */
102 EXTL_EXPORT_AS(WRegion, set_activity)
103 bool region_set_activity_extl(WRegion *reg, const char *how)
104 {
105     return region_set_activity(reg, libtu_string_to_setparam(how));
106 }
107
108
109 /*EXTL_DOC
110  * Is activity notification set on \var{reg}.
111  */
112 EXTL_SAFE
113 EXTL_EXPORT_MEMBER
114 bool region_is_activity(WRegion *reg)
115 {
116     return (reg->flags&REGION_ACTIVITY);
117 }
118
119
120 bool region_is_activity_r(WRegion *reg)
121 {
122     return (reg->flags&REGION_ACTIVITY || reg->mgd_activity!=0);
123 }
124
125
126 /*EXTL_DOC
127  * Iterate over activity list until \var{iterfn} returns \code{false}.
128  * The function itself returns \code{true} if it reaches the end of list
129  * without this happening.
130  */
131 EXTL_SAFE
132 EXTL_EXPORT
133 bool ioncore_activity_i(ExtlFn iterfn)
134 {
135     return extl_iter_objlist(iterfn, actlist);
136 }
137
138
139 /*EXTL_DOC
140  * Return first regio non activity list.
141  */
142 EXTL_SAFE
143 EXTL_EXPORT
144 WRegion *ioncore_activity_first()
145 {
146     if(actlist==NULL)
147         return NULL;
148     return (WRegion*)actlist->watch.obj;
149 }
150
151
152 ObjList *ioncore_activity_list()
153 {
154     return actlist;
155 }
156
157
158 /*EXTL_DOC
159  * Go to first region on activity list.
160  */
161 EXTL_EXPORT
162 bool ioncore_goto_activity()
163 {
164     WRegion *reg=ioncore_activity_first();
165     
166     if(reg!=NULL)
167         return region_goto(reg);
168     else
169         return FALSE;
170 }
171