Re: [dwm] ntags initialization

2007-10-10 Thread Anselm R. Garbe
On Wed, Oct 10, 2007 at 05:35:16PM +0200, Szabolcs Nagy wrote:
 On 10/10/07, Anselm R. Garbe [EMAIL PROTECTED] wrote:
  One question to this again. Actually I can't find any official
  reference why this should work in the C89 standard, call me
  blind or whatever. In C99 this definately works.
 
  Do you have a pointer to confirm that this works with C89 as
  well?
 
 iirc c89 standard (aka iso c90 or ansi c) is not freely available
 
 so what i usually do to check conformance:
   gcc -ansi -pedantic
 
 it seems that
   int a[] = {1,2,3};
   int b[sizeof(a)/sizeof(int)];
 compiles fine in c89 mode

Well, ok, let's face on C99. I changed things as follows: the
Client struct looks like before. However I kept the static
initialization of ntags, seltags and the newly introduced
prevtags. This means that third-party patches may access any
variable except the latter ones (actually I see no reason why
you want to access seltags, prevtags or ntags from a patch). The
tagging approach is quite central to dwm and needs no additions
in my opinion.

Instead of looping for tags initialization in Client-tags I use
memcpy from now on.

See latest patch for further information and let me know any
bugs if you encounter them. Otherwise dwm-4.6 is on its way.

Regards,
-- 
 Anselm R. Garbe  http://www.suckless.org/  GPG key: 0D73F361



Re: [dwm] ntags initialization

2007-10-06 Thread Brendan MacDonell
On Fri, Oct 05, 2007 at 23:33:44 CEST, Anselm R. Garbe wrote:
 You miss that seltags and Client-tags are both globally defined
 in the same context as the tags definition. Afaik you can't
 evaluate the sizeof operator and you can't perform a division
 operation in a non-functional scope, so I don't see how you
 would like to replace the allocation of seltags and Client-tags
 using emallocz.

sizeof is a compile-time constant, and any expression containing only
compile-time constants (literal numbers, sizeof, etc; no variables or
functions) can be evaluated at compile time. To demonstrate, consider
the attached patch against changeset 1025 (written in standard, albeit
little-used, C)  which removes the dynamic allocation of seltags and
Client-tags and replaces it with constant initialization. Due to
added whitespace where some declarations were rearranged, and a
comment, the number of LOC decreased only by 2.
--- dwm.c.old	2007-10-06 10:02:01.680419843 -0300
+++ dwm.c	2007-10-06 11:28:34.323541304 -0300
@@ -57,21 +57,6 @@
 
 /* typedefs */
 typedef struct Client Client;
-struct Client {
-	char name[256];
-	int x, y, w, h;
-	int rx, ry, rw, rh; /* revert geometry */
-	int basew, baseh, incw, inch, maxw, maxh, minw, minh;
-	int minax, maxax, minay, maxay;
-	long flags;
-	unsigned int border, oldborder;
-	Bool isbanned, isfixed, ismax, isfloating, wasfloating;
-	Bool *tags;
-	Client *next;
-	Client *prev;
-	Client *snext;
-	Window win;
-};
 
 typedef struct {
 	int x, y, w, h;
@@ -195,7 +180,7 @@
 double mwfact;
 int screen, sx, sy, sw, sh, wax, way, waw, wah;
 int (*xerrorxlib)(Display *, XErrorEvent *);
-unsigned int bh, bpos, ntags;
+unsigned int bh, bpos;
 unsigned int blw = 0;
 unsigned int ltidx = 0; /* default */
 unsigned int nlayouts = 0;
@@ -218,7 +203,6 @@
 Atom wmatom[WMLast], netatom[NetLast];
 Bool otherwm, readin;
 Bool running = True;
-Bool *seltags;
 Bool selscreen = True;
 Client *clients = NULL;
 Client *sel = NULL;
@@ -232,6 +216,26 @@
 /* configuration, allows nested code to access above variables */
 #include config.h
 
+/* Statically define the number of tags. */
+unsigned int ntags = sizeof tags / sizeof tags[0];
+Bool seltags[sizeof tags / sizeof tags[0]] = {[0] = True};
+
+struct Client {
+	char name[256];
+	int x, y, w, h;
+	int rx, ry, rw, rh; /* revert geometry */
+	int basew, baseh, incw, inch, maxw, maxh, minw, minh;
+	int minax, maxax, minay, maxay;
+	long flags;
+	unsigned int border, oldborder;
+	Bool isbanned, isfixed, ismax, isfloating, wasfloating;
+	Bool tags[sizeof tags / sizeof tags[0]];
+	Client *next;
+	Client *prev;
+	Client *snext;
+	Window win;
+};
+
 /* functions*/
 void
 applyrules(Client *c) {
@@ -393,7 +397,6 @@
 	XFreeCursor(dpy, cursor[CurMove]);
 	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
 	XSync(dpy, False);
-	free(seltags);
 }
 
 void
@@ -1006,7 +1009,6 @@
 	XWindowChanges wc;
 
 	c = emallocz(sizeof(Client));
-	c-tags = emallocz(ntags * sizeof(Bool));
 	c-win = w;
 	c-x = wa-x;
 	c-y = wa-y;
@@ -1467,9 +1469,6 @@
 
 	/* init tags */
 	compileregs();
-	ntags = sizeof tags / sizeof tags[0];
-	seltags = emallocz(sizeof(Bool) * ntags);
-	seltags[0] = True;
 
 	/* init appearance */
 	dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
@@ -1703,7 +1702,6 @@
 		focus(NULL);
 	XUngrabButton(dpy, AnyButton, AnyModifier, c-win);
 	setclientstate(c, WithdrawnState);
-	free(c-tags);
 	free(c);
 	XSync(dpy, False);
 	XSetErrorHandler(xerror);


Re: [dwm] ntags initialization

2007-10-05 Thread Anselm R. Garbe
On Thu, Oct 04, 2007 at 08:17:51AM +0100, [EMAIL PROTECTED] wrote:
 feature by looking at the dwm code when I found:
 
   for
 (ntags = 0; tags[ntags]; ntags++);
 
 which,  I suppose,  is used to 
 initialize the `ntags' variable. However this point is not clear to me 
 since
 `tags' is statically defined, then it would be possible to 
 compute statically `ntags' as
 
   ntags = sizeof tags / sizeof tags[0] - 
 1;

That's true, but an evolutionary artefact ;) I'm going to
replace this with the sizeof calculation, but with the exception
to also remove the NULL array-terminator. This makes the -1
calculation obsolete.

 BE CAREFUL with the tags definition beginning with dwm 4.6,
 NULL won't be allowed anymore!!!

 and, as a side effect, knowing statically `ntags' allows to define 
 `seltags' as an array without using
 `emallocz()'. 
 What I am missing?

You miss that seltags and Client-tags are both globally defined
in the same context as the tags definition. Afaik you can't
evaluate the sizeof operator and you can't perform a division
operation in a non-functional scope, so I don't see how you
would like to replace the allocation of seltags and Client-tags
using emallocz.

Regards,
-- 
 Anselm R. Garbe  http://www.suckless.org/  GPG key: 0D73F361