4 * Copyright (c) Tuomo Valkonen 1999-2006.
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.
17 #include <libtu/minmax.h>
18 #include <libtu/objp.h>
19 #include <libextl/extl.h>
23 #include "clientwin.h"
29 /*{{{ Implementation */
32 WNamespace ioncore_internal_ns={NULL, FALSE};
33 WNamespace ioncore_clientwin_ns={NULL, FALSE};
36 static bool initialise_ns(WNamespace *ns)
44 ns->initialised=(ns->rb!=NULL);
46 return ns->initialised;
50 static int parseinst(const char *name, const char **startinst)
52 const char *p2, *p3=NULL;
61 p2=strrchr(name, '<');
65 inst=strtoul(p2+1, (char**)&p3, 10);
67 if(inst<0 || p3!=name+l-1)
75 static int parseinst_simple(const char *inststr)
84 inst=strtoul(inststr+1, (char**)&end, 10);
86 if(inst>=0 && end!=NULL && *end=='>')
90 warn(TR("Corrupt instance number %s."), inststr);
95 #define COMPARE_FN ((Rb_compfn*)compare_nameinfos)
97 static int compare_nameinfos(const WRegionNameInfo *ni1,
98 const WRegionNameInfo *ni2)
104 /* Handle unnamed regions. */
109 return (ni1==ni2 ? 0 : (ni1<ni2 ? -1 : 1));
110 }else if(ni2->name==NULL){
114 /* Special case: inst_off<0 means that -inst_off-1 is the actual
115 * instance number and the name does not contain it.
121 l1=strlen(ni1->name);
126 l2=strlen(ni2->name);
128 /* Check name part first */
130 mc=strncmp(ni1->name, ni2->name, minof(l1, l2));
136 return (l1<l2 ? -1 : 1);
138 /* Same name, different instance */
141 i1=parseinst_simple(ni1->name+ni1->inst_off);
143 i1=-ni1->inst_off-1; /*???*/
146 i2=parseinst_simple(ni2->name+ni2->inst_off);
148 i2=-ni2->inst_off-1; /*???*/
151 return (i1<i2 ? -1 : 1);
153 /* Same name and instance */
158 static bool insert_reg(Rb_node tree, WRegion *reg)
160 assert(reg->ni.node==NULL);
161 reg->ni.node=(void*)rb_insertg(tree, &(reg->ni), reg, COMPARE_FN);
162 return (reg->ni.node!=NULL);
165 static bool separated(const WRegionNameInfo *ni1,
166 const WRegionNameInfo *ni2, int *i1ret)
172 assert(ni1->name!=NULL);
175 /* Shouldn't happen the way this function is called below; unnamed
176 * regions are before others in the traversal order of the tree.
178 *i1ret=parseinst_simple(ni1->name+ni1->inst_off);
182 assert(ni1->inst_off>=0 && ni2->inst_off>=0);
187 i1=parseinst_simple(ni1->name+ni1->inst_off);
193 if(memcmp(ni1->name, ni2->name, l1)!=0)
196 i2=parseinst_simple(ni2->name+ni2->inst_off);
203 void region_unregister(WRegion *reg)
205 if(reg->ni.node!=NULL){
206 rb_delete_node((Rb_node)reg->ni.node);
210 if(reg->ni.name!=NULL){
218 static bool make_full_name(WRegionNameInfo *ni, const char *name, int inst,
221 assert(ni->name==NULL);
223 if(inst==0 && !append_always)
224 ni->name=scopy(name);
226 libtu_asprintf(&(ni->name), "%s<%d>", name, inst);
231 ni->inst_off=strlen(name);
237 static bool do_use_name(WRegion *reg, WNamespace *ns, const char *name,
238 int instrq, bool failchange)
241 WRegionNameInfo ni={NULL, 0, NULL};
242 const char *dummy=NULL;
247 parsed_inst=parseinst(name, &dummy);
252 /* If there's something that looks like an instance at the end of
253 * name, we will append the instance number.
255 if(parsed_inst>=0 && inst==0 && failchange)
259 WRegionNameInfo tmpni;
260 tmpni.name=(char*)name;
261 tmpni.inst_off=-instrq-1;
262 node=rb_find_gkey_n(ns->rb, &tmpni, COMPARE_FN, &found);
264 if(rb_val(node)==(void*)reg){
265 /* The region already has the requested name */
276 WRegionNameInfo tmpni;
281 tmpni.name=(char*)name;
283 node=rb_find_gkey_n(ns->rb, &tmpni, COMPARE_FN, &found);
287 Rb_node next=rb_next(node);
289 if(rb_val(node)==(void*)reg){
290 /* The region already has a name of requested form */
294 if(next==rb_nil(ns->rb) ||
295 separated((const WRegionNameInfo*)node->k.key,
296 (const WRegionNameInfo*)next->k.key, &inst)){
297 /* 'inst' should be next free instance after increment
298 * as separation was greater then one.
304 /* 'inst' should be instance of next after increment
305 * as separation was one.
313 if(!make_full_name(&ni, name, inst, parsed_inst>=0))
317 rb_find_gkey_n(ns->rb, &ni, COMPARE_FN, &found);
322 region_unregister(reg);
324 reg->ni.name=ni.name;
325 reg->ni.inst_off=ni.inst_off;
327 if(!insert_reg(ns->rb, reg)){
338 static bool use_name_anyinst(WRegion *reg, WNamespace *ns, const char *name)
340 return do_use_name(reg, ns, name, -1, FALSE);
344 static bool use_name_exact(WRegion *reg, WNamespace *ns, const char *name)
346 return do_use_name(reg, ns, name, 0, TRUE);
350 static bool use_name_parseany(WRegion *reg, WNamespace *ns, const char *name)
353 const char *startinst;
357 inst=parseinst(name, &startinst);
360 int realnamelen=startinst-name;
361 char *realname=ALLOC_N(char, realnamelen+1);
363 memcpy(realname, name, realnamelen);
364 realname[realnamelen]='\0';
365 retval=do_use_name(reg, ns, realname, inst, FALSE);
371 return do_use_name(reg, ns, name, 0, FALSE);
383 * Returns the name for \var{reg}.
387 const char *region_name(WRegion *reg)
393 static bool do_set_name(bool (*fn)(WRegion *reg, WNamespace *ns, const char *p),
394 WRegion *reg, WNamespace *ns, const char *p)
399 if(!initialise_ns(ns))
409 if(nm==NULL || *nm=='\0'){
410 region_unregister(reg);
411 ret=insert_reg(ns->rb, reg);
419 region_notify_change(reg, "name");
425 bool region_register(WRegion *reg)
427 assert(reg->ni.name==NULL);
429 if(!initialise_ns(&ioncore_internal_ns))
432 return use_name_anyinst(reg, &ioncore_internal_ns, OBJ_TYPESTR(reg));
436 bool clientwin_register(WClientWin *cwin)
438 WRegion *reg=(WRegion*)cwin;
440 assert(reg->ni.name==NULL);
442 if(!initialise_ns(&ioncore_clientwin_ns))
445 return insert_reg(ioncore_clientwin_ns.rb, (WRegion*)cwin);
450 * Set the name of \var{reg} to \var{p}. If the name is already in use,
451 * an instance number suffix \code{<n>} will be attempted. If \var{p} has
452 * such a suffix, it will be modified, otherwise such a suffix will be
453 * added. Setting \var{p} to nil will cause current name to be removed.
456 bool region_set_name(WRegion *reg, const char *p)
458 /* return do_set_name(use_name_parseany, reg, &ioncore_internal_ns, p);*/
459 return do_set_name(use_name_parseany, reg,
460 OBJ_IS(reg, WClientWin) ? &ioncore_clientwin_ns : &ioncore_internal_ns,
466 * Similar to \fnref{WRegion.set_name} except if the name is already in use,
467 * other instance numbers will not be attempted. The string \var{p} should
468 * not contain a \code{<n>} suffix or this function will fail.
471 bool region_set_name_exact(WRegion *reg, const char *p)
473 return do_set_name(use_name_exact, reg, &ioncore_internal_ns, p);
477 bool clientwin_set_name(WClientWin *cwin, const char *p)
479 return do_set_name(use_name_anyinst, (WRegion*)cwin,
480 &ioncore_clientwin_ns, p);
487 /*{{{ Lookup and list */
490 static WRegion *do_lookup_region(WNamespace *ns, const char *cname,
496 const char *instptr=NULL;
498 if(cname==NULL || !ns->initialised)
501 parseinst(cname, &instptr);
502 assert(instptr!=NULL);
504 ni.name=(char*)cname;
505 ni.inst_off=instptr-cname;
507 node=rb_find_gkey_n(ns->rb, &ni, COMPARE_FN, &found);
512 return (WRegion*)node->v.val;
517 * Attempt to find a non-client window region with name \var{name} and type
518 * inheriting \var{typenam}.
522 WRegion *ioncore_lookup_region(const char *name, const char *typenam)
524 return do_lookup_region(&ioncore_internal_ns, name, typenam);
529 * Attempt to find a client window with name \var{name}.
533 WClientWin *ioncore_lookup_clientwin(const char *name)
535 return (WClientWin*)do_lookup_region(&ioncore_clientwin_ns, name,
540 static ExtlTab do_list(WNamespace *ns, const char *typenam)
547 return extl_table_none();
549 tab=extl_create_table();
551 rb_traverse(node, ns->rb){
552 WRegion *reg=(WRegion*)node->v.val;
556 if(typenam!=NULL && !obj_is_str((Obj*)reg, typenam))
559 if(extl_table_seti_o(tab, n+1, (Obj*)reg))
568 * Find all non-client window regions inheriting \var{typenam}.
572 ExtlTab ioncore_region_list(const char *typenam)
574 return do_list(&ioncore_internal_ns, typenam);
579 * Return a list of all client windows.
583 ExtlTab ioncore_clientwin_list()
585 return do_list(&ioncore_clientwin_ns, NULL);
595 const char *region_displayname(WRegion *reg)
597 const char *ret=NULL;
598 CALL_DYN_RET(ret, const char *, region_displayname, reg, (reg));
603 char *region_make_label(WRegion *reg, int maxw, GrBrush *brush)
605 const char *name=region_displayname(reg);
610 return grbrush_make_label(brush, name, maxw);