ion->notion
[notion/jeffpc.git] / ioncore / names.c
blob57cfd6fc0a9d6448618c0f256a1ec5e336aaf92d
1 /*
2 * ion/ioncore/names.c
4 * Copyright (c) Tuomo Valkonen 1999-2009.
6 * See the included file LICENSE for details.
7 */
10 #include <string.h>
11 #include <limits.h>
13 #include <libtu/rb.h>
14 #include <libtu/minmax.h>
15 #include <libtu/objp.h>
16 #include <libextl/extl.h>
18 #include "common.h"
19 #include "global.h"
20 #include "region.h"
21 #include "clientwin.h"
22 #include "names.h"
23 #include "strings.h"
24 #include "gr.h"
27 /*{{{ Implementation */
30 WNamespace ioncore_internal_ns={NULL, FALSE};
31 WNamespace ioncore_clientwin_ns={NULL, FALSE};
34 static bool initialise_ns(WNamespace *ns)
36 if(ns->initialised)
37 return TRUE;
39 if(ns->rb==NULL)
40 ns->rb=make_rb();
42 ns->initialised=(ns->rb!=NULL);
44 return ns->initialised;
48 static int parseinst(const char *name, const char **startinst)
50 const char *p2, *p3=NULL;
51 int inst;
52 int l=strlen(name);
54 *startinst=name+l;
56 if(name[l-1]!='>')
57 return -1;
59 p2=strrchr(name, '<');
60 if(p2==NULL)
61 return -1;
63 inst=strtoul(p2+1, (char**)&p3, 10);
65 if(inst<0 || p3!=name+l-1)
66 return -1;
68 *startinst=p2;
69 return inst;
73 static int parseinst_simple(const char *inststr)
75 const char *end=NULL;
76 int inst;
78 if(*inststr=='\0')
79 return 0;
81 if(*inststr=='<'){
82 inst=strtoul(inststr+1, (char**)&end, 10);
84 if(inst>=0 && end!=NULL && *end=='>')
85 return inst;
88 warn(TR("Corrupt instance number %s."), inststr);
89 return -1;
93 #define COMPARE_FN ((Rb_compfn*)compare_nameinfos)
95 static int compare_nameinfos(const WRegionNameInfo *ni1,
96 const WRegionNameInfo *ni2)
98 int l1=0, l2=0;
99 int i1=0, i2=0;
100 int mc=0;
102 /* Handle unnamed regions. */
104 if(ni1->name==NULL){
105 if(ni2->name!=NULL)
106 return 1;
107 return (ni1==ni2 ? 0 : (ni1<ni2 ? -1 : 1));
108 }else if(ni2->name==NULL){
109 return -1;
112 /* Special case: inst_off<0 means that -inst_off-1 is the actual
113 * instance number and the name does not contain it.
116 if(ni1->inst_off>=0)
117 l1=ni1->inst_off;
118 else
119 l1=strlen(ni1->name);
121 if(ni2->inst_off>=0)
122 l2=ni2->inst_off;
123 else
124 l2=strlen(ni2->name);
126 /* Check name part first */
128 mc=strncmp(ni1->name, ni2->name, minof(l1, l2));
130 if(mc!=0)
131 return mc;
133 if(l1!=l2)
134 return (l1<l2 ? -1 : 1);
136 /* Same name, different instance */
138 if(ni1->inst_off>=0)
139 i1=parseinst_simple(ni1->name+ni1->inst_off);
140 else
141 i1=-ni1->inst_off-1; /*???*/
143 if(ni2->inst_off>=0)
144 i2=parseinst_simple(ni2->name+ni2->inst_off);
145 else
146 i2=-ni2->inst_off-1; /*???*/
148 if(i1!=i2)
149 return (i1<i2 ? -1 : 1);
151 /* Same name and instance */
153 return 0;
156 static bool insert_reg(Rb_node tree, WRegion *reg)
158 assert(reg->ni.node==NULL);
159 reg->ni.node=(void*)rb_insertg(tree, &(reg->ni), reg, COMPARE_FN);
160 return (reg->ni.node!=NULL);
163 static bool separated(const WRegionNameInfo *ni1,
164 const WRegionNameInfo *ni2, int *i1ret)
166 int l1, l2;
167 int i1, i2;
168 int mc;
170 assert(ni1->name!=NULL);
172 if(ni2->name==NULL){
173 /* Shouldn't happen the way this function is called below; unnamed
174 * regions are before others in the traversal order of the tree.
176 *i1ret=parseinst_simple(ni1->name+ni1->inst_off);
177 return TRUE;
180 assert(ni1->inst_off>=0 && ni2->inst_off>=0);
182 l1=ni1->inst_off;
183 l2=ni2->inst_off;
185 i1=parseinst_simple(ni1->name+ni1->inst_off);
186 *i1ret=i1;
188 if(l1!=l2)
189 return TRUE;
191 if(memcmp(ni1->name, ni2->name, l1)!=0)
192 return TRUE;
194 i2=parseinst_simple(ni2->name+ni2->inst_off);
196 return (i2>(i1+1));
201 void region_unregister(WRegion *reg)
203 if(reg->ni.node!=NULL){
204 rb_delete_node((Rb_node)reg->ni.node);
205 reg->ni.node=NULL;
208 if(reg->ni.name!=NULL){
209 free(reg->ni.name);
210 reg->ni.name=NULL;
211 reg->ni.inst_off=0;
216 static bool make_full_name(WRegionNameInfo *ni, const char *name, int inst,
217 bool append_always)
219 assert(ni->name==NULL);
221 if(inst==0 && !append_always)
222 ni->name=scopy(name);
223 else
224 libtu_asprintf(&(ni->name), "%s<%d>", name, inst);
226 if(ni->name==NULL)
227 return FALSE;
229 ni->inst_off=strlen(name);
231 return TRUE;
235 static bool do_use_name(WRegion *reg, WNamespace *ns, const char *name,
236 int instrq, bool failchange)
238 int parsed_inst=-1;
239 WRegionNameInfo ni={NULL, 0, NULL};
240 const char *dummy=NULL;
241 Rb_node node;
242 int inst=-1;
243 int found=0;
245 parsed_inst=parseinst(name, &dummy);
247 if(!ns->initialised)
248 return FALSE;
250 /* If there's something that looks like an instance at the end of
251 * name, we will append the instance number.
253 if(parsed_inst>=0 && inst==0 && failchange)
254 return FALSE;
256 if(instrq>=0){
257 WRegionNameInfo tmpni;
258 tmpni.name=(char*)name;
259 tmpni.inst_off=-instrq-1;
260 node=rb_find_gkey_n(ns->rb, &tmpni, COMPARE_FN, &found);
261 if(found){
262 if(rb_val(node)==(void*)reg){
263 /* The region already has the requested name */
264 return TRUE;
266 if(failchange)
267 return FALSE;
268 }else{
269 inst=instrq;
273 if(inst<0){
274 WRegionNameInfo tmpni;
276 found=0;
277 inst=0;
279 tmpni.name=(char*)name;
280 tmpni.inst_off=-1;
281 node=rb_find_gkey_n(ns->rb, &tmpni, COMPARE_FN, &found);
283 if(found){
284 while(1){
285 Rb_node next=rb_next(node);
287 if(rb_val(node)==(void*)reg){
288 /* The region already has a name of requested form */
289 return TRUE;
292 if(next==rb_nil(ns->rb) ||
293 separated((const WRegionNameInfo*)node->k.key,
294 (const WRegionNameInfo*)next->k.key, &inst)){
295 /* 'inst' should be next free instance after increment
296 * as separation was greater then one.
298 inst++;
299 break;
302 /* 'inst' should be instance of next after increment
303 * as separation was one.
305 inst++;
306 node=next;
311 if(!make_full_name(&ni, name, inst, parsed_inst>=0))
312 return FALSE;
315 rb_find_gkey_n(ns->rb, &ni, COMPARE_FN, &found);
317 assert(!found);
320 region_unregister(reg);
322 reg->ni.name=ni.name;
323 reg->ni.inst_off=ni.inst_off;
325 if(!insert_reg(ns->rb, reg)){
326 free(reg->ni.name);
327 reg->ni.name=NULL;
328 reg->ni.inst_off=0;
329 return FALSE;
332 return TRUE;
336 static bool use_name_anyinst(WRegion *reg, WNamespace *ns, const char *name)
338 return do_use_name(reg, ns, name, -1, FALSE);
342 static bool use_name_exact(WRegion *reg, WNamespace *ns, const char *name)
344 return do_use_name(reg, ns, name, 0, TRUE);
348 static bool use_name_parseany(WRegion *reg, WNamespace *ns, const char *name)
350 int l, inst;
351 const char *startinst;
353 l=strlen(name);
355 inst=parseinst(name, &startinst);
356 if(inst>=0){
357 bool retval=FALSE;
358 int realnamelen=startinst-name;
359 char *realname=ALLOC_N(char, realnamelen+1);
360 if(realname!=NULL){
361 memcpy(realname, name, realnamelen);
362 realname[realnamelen]='\0';
363 retval=do_use_name(reg, ns, realname, inst, FALSE);
364 free(realname);
366 return retval;
369 return do_use_name(reg, ns, name, 0, FALSE);
374 /*}}}*/
377 /*{{{ Interface */
380 /*EXTL_DOC
381 * Returns the name for \var{reg}.
383 EXTL_SAFE
384 EXTL_EXPORT_MEMBER
385 const char *region_name(WRegion *reg)
387 return reg->ni.name;
391 static bool do_set_name(bool (*fn)(WRegion *reg, WNamespace *ns, const char *p),
392 WRegion *reg, WNamespace *ns, const char *p)
394 bool ret=TRUE;
395 char *nm=NULL;
397 if(!initialise_ns(ns))
398 return FALSE;
400 if(p!=NULL){
401 nm=scopy(p);
402 if(nm==NULL)
403 return FALSE;
404 str_stripws(nm);
407 if(nm==NULL || *nm=='\0'){
408 region_unregister(reg);
409 ret=insert_reg(ns->rb, reg);
410 }else{
411 ret=fn(reg, ns, nm);
414 if(nm!=NULL)
415 free(nm);
417 region_notify_change(reg, ioncore_g.notifies.name);
419 return ret;
423 bool region_register(WRegion *reg)
425 assert(reg->ni.name==NULL);
427 if(!initialise_ns(&ioncore_internal_ns))
428 return FALSE;
430 return use_name_anyinst(reg, &ioncore_internal_ns, OBJ_TYPESTR(reg));
434 bool clientwin_register(WClientWin *cwin)
436 WRegion *reg=(WRegion*)cwin;
438 assert(reg->ni.name==NULL);
440 if(!initialise_ns(&ioncore_clientwin_ns))
441 return FALSE;
443 return insert_reg(ioncore_clientwin_ns.rb, (WRegion*)cwin);
447 /*EXTL_DOC
448 * Set the name of \var{reg} to \var{p}. If the name is already in use,
449 * an instance number suffix \codestr{<n>} will be attempted. If \var{p} has
450 * such a suffix, it will be modified, otherwise such a suffix will be
451 * added. Setting \var{p} to nil will cause current name to be removed.
453 EXTL_EXPORT_MEMBER
454 bool region_set_name(WRegion *reg, const char *p)
456 /* return do_set_name(use_name_parseany, reg, &ioncore_internal_ns, p);*/
457 return do_set_name(use_name_parseany, reg,
458 OBJ_IS(reg, WClientWin) ? &ioncore_clientwin_ns : &ioncore_internal_ns,
463 /*EXTL_DOC
464 * Similar to \fnref{WRegion.set_name} except if the name is already in use,
465 * other instance numbers will not be attempted. The string \var{p} should
466 * not contain a \codestr{<n>} suffix or this function will fail.
468 EXTL_EXPORT_MEMBER
469 bool region_set_name_exact(WRegion *reg, const char *p)
471 return do_set_name(use_name_exact, reg, &ioncore_internal_ns, p);
475 bool clientwin_set_name(WClientWin *cwin, const char *p)
477 return do_set_name(use_name_anyinst, (WRegion*)cwin,
478 &ioncore_clientwin_ns, p);
482 /*}}}*/
485 /*{{{ Lookup and list */
488 static WRegion *do_lookup_region(WNamespace *ns, const char *cname,
489 const char *typenam)
491 WRegionNameInfo ni;
492 Rb_node node;
493 int found=0;
494 const char *instptr=NULL;
496 if(cname==NULL || !ns->initialised)
497 return NULL;
499 parseinst(cname, &instptr);
500 assert(instptr!=NULL);
502 ni.name=(char*)cname;
503 ni.inst_off=instptr-cname;
505 node=rb_find_gkey_n(ns->rb, &ni, COMPARE_FN, &found);
507 if(!found)
508 return NULL;
510 if(typenam!=NULL && !obj_is_str((Obj*)node->v.val, typenam))
511 return NULL;
513 return (WRegion*)node->v.val;
517 /*EXTL_DOC
518 * Attempt to find a non-client window region with name \var{name} and type
519 * inheriting \var{typenam}.
521 EXTL_SAFE
522 EXTL_EXPORT
523 WRegion *ioncore_lookup_region(const char *name, const char *typenam)
525 return do_lookup_region(&ioncore_internal_ns, name, typenam);
529 /*EXTL_DOC
530 * Attempt to find a client window with name \var{name}.
532 EXTL_SAFE
533 EXTL_EXPORT
534 WClientWin *ioncore_lookup_clientwin(const char *name)
536 return (WClientWin*)do_lookup_region(&ioncore_clientwin_ns, name,
537 "WClientWin");
541 static bool do_list(ExtlFn fn, WNamespace *ns, const char *typenam)
543 Rb_node node;
544 int n=0;
546 if(!ns->initialised)
547 return FALSE;
549 rb_traverse(node, ns->rb){
550 WRegion *reg=(WRegion*)node->v.val;
552 assert(reg!=NULL);
554 if(typenam!=NULL && !obj_is_str((Obj*)reg, typenam))
555 continue;
557 if(!extl_iter_obj(fn, (Obj*)reg))
558 return FALSE;
561 return TRUE;
565 /*EXTL_DOC
566 * Iterate over all non-client window regions with (inherited) class
567 * \var{typenam} until \var{iterfn} returns \code{false}.
568 * The function is called in protected mode.
569 * This routine returns \code{true} if it reaches the end of list
570 * without this happening.
572 EXTL_SAFE
573 EXTL_EXPORT
574 bool ioncore_region_i(ExtlFn fn, const char *typenam)
576 return do_list(fn, &ioncore_internal_ns, typenam);
580 /*EXTL_DOC
581 * Iterate over client windows until \var{iterfn} returns \code{false}.
582 * The function is called in protected mode.
583 * This routine returns \code{true} if it reaches the end of list
584 * without this happening.
586 EXTL_SAFE
587 EXTL_EXPORT
588 bool ioncore_clientwin_i(ExtlFn fn)
590 return do_list(fn, &ioncore_clientwin_ns, NULL);
594 /*}}}*/
597 /*{{{ Displayname */
600 const char *region_displayname(WRegion *reg)
602 const char *ret=NULL;
603 CALL_DYN_RET(ret, const char *, region_displayname, reg, (reg));
604 return ret;
608 char *region_make_label(WRegion *reg, int maxw, GrBrush *brush)
610 const char *name=region_displayname(reg);
612 if(name==NULL)
613 return NULL;
615 return grbrush_make_label(brush, name, maxw);
619 /*}}}*/