rem: hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE); vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);
add: hcount = WMIN(dock->max_icons, scr->scr_width/ICON_SIZE); vcount = WMIN(dock->max_icons, scr->scr_height/ICON_SIZE);
context: wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos){    WScreen *scr = dock->screen_ptr;    WAppIcon *btn;    WAppIconChain *chain;    unsigned char *slot_map;    int mwidth;    int r;    int x, y;    int i, done = False;    int corner;    int sx=0, sy=0, ex=scr->scr_width, ey=scr->scr_height;    int extra_count=0;    if (dock->type == WM_CLIP &&        dock != scr->workspaces[scr->current_workspace]->clip)        extra_count = scr->global_icon_count;    /* if the dock is full */    if (dock->icon_count+extra_count >= dock->max_icons) {	return False;    }    if (!wPreferences.flags.nodock && scr->dock) {        if (scr->dock->on_right_side)            ex -= ICON_SIZE + DOCK_EXTRA_SPACE;        else            sx += ICON_SIZE + DOCK_EXTRA_SPACE;    }    if (ex < dock->x_pos)        ex = dock->x_pos;    if (sx > dock->x_pos+ICON_SIZE)        sx = dock->x_pos+ICON_SIZE;#define C_NONE 0#define C_NW 1#define C_NE 2#define C_SW 3#define C_SE 4    /* check if clip is in a corner */    if (dock->type==WM_CLIP) {	if (dock->x_pos < 1 && dock->y_pos < 1)	    corner = C_NE;	else if (dock->x_pos < 1 && dock->y_pos >= (ey-ICON_SIZE))	    corner = C_SE;	else if (dock->x_pos >= (ex-ICON_SIZE)&& dock->y_pos >= (ey-ICON_SIZE))	    corner = C_SW;	else if (dock->x_pos >= (ex-ICON_SIZE) && dock->y_pos < 1)	    corner = C_NW;	else	    corner = C_NONE;    } else	corner = C_NONE;    /* If the clip is in the corner, use only slots that are in the border     * of the screen */    if (corner!=C_NONE) {	char *hmap, *vmap;	int hcount, vcount;	hcount = MIN(dock->max_icons, scr->scr_width/ICON_SIZE);	vcount = MIN(dock->max_icons, scr->scr_height/ICON_SIZE);	hmap = wmalloc(hcount+1);	memset(hmap, 0, hcount+1);	vmap = wmalloc(vcount+1);	memset(vmap, 0, vcount+1);	/* mark used positions */	switch (corner) {	 case C_NE:	    for (i=0; i<dock->max_icons; i++) {		btn = dock->icon_array[i];		if (!btn)		    continue;		if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)		    vmap[btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)		    hmap[btn->xindex] = 1;            }            for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {                btn = chain->aicon;		if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)		    vmap[btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)		    hmap[btn->xindex] = 1;            }            break;	 case C_NW:	    for (i=0; i<dock->max_icons; i++) {		btn = dock->icon_array[i];		if (!btn)		    continue;		if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)		    vmap[btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)		    hmap[-btn->xindex] = 1;            }            for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {                btn = chain->aicon;		if (btn->xindex==0 && btn->yindex > 0 && btn->yindex < vcount)		    vmap[btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)		    hmap[-btn->xindex] = 1;            }            break;	 case C_SE:	    for (i=0; i<dock->max_icons; i++) {		btn = dock->icon_array[i];		if (!btn)		    continue;		if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)		    vmap[-btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)		    hmap[btn->xindex] = 1;            }            for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {                btn = chain->aicon;		if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)		    vmap[-btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex>0 && btn->xindex<hcount)		    hmap[btn->xindex] = 1;            }            break;	 case C_SW:	 default:	    for (i=0; i<dock->max_icons; i++) {		btn = dock->icon_array[i];		if (!btn)		    continue;		if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)		    vmap[-btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)		    hmap[-btn->xindex] = 1;	    }            for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {                btn = chain->aicon;		if (btn->xindex==0 && btn->yindex < 0 && btn->yindex > -vcount)		    vmap[-btn->yindex] = 1;		else if (btn->yindex==0 && btn->xindex<0 &&btn->xindex>-hcount)		    hmap[-btn->xindex] = 1;            }	}	x=0; y=0;	done = 0;	/* search a vacant slot */	for (i=1; i<MAX(vcount, hcount); i++) {	    if (i < vcount && vmap[i]==0) {		/* found a slot */		x = 0;		y = i;		done = 1;		break;	    } else if (i < hcount && hmap[i]==0) {		/* found a slot */		x = i;		y = 0;		done = 1;		break;	    }	}	wfree(vmap);	wfree(hmap);	/* If found a slot, translate and return */        if (done) {	    if (corner==C_NW || corner==C_NE) {		*y_pos = y;	    } else {		*y_pos = -y;	    }	    if (corner==C_NE || corner==C_SE) {		*x_pos = x;	    } else {		*x_pos = -x;	    }	    return True;	}	/* else, try to find a slot somewhere else */    }    /* a map of mwidth x mwidth would be enough if we allowed icons to be     * placed outside of screen */    mwidth = (int)ceil(sqrt(dock->max_icons));    /* In the worst case (the clip is in the corner of the screen),     * the amount of icons that fit in the clip is smaller.     * Double the map to get a safe value.     */    mwidth += mwidth;    r = (mwidth-1)/2;    slot_map = wmalloc(mwidth*mwidth);    memset(slot_map, 0, mwidth*mwidth);#define XY2OFS(x,y) (MAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)    /* mark used slots in the map. If the slot falls outside the map     * (for example, when all icons are placed in line), ignore them. */    for (i=0; i<dock->max_icons; i++) {	btn = dock->icon_array[i];	if (btn)	    slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;    }    for (chain=scr->global_icons; chain!=NULL; chain=chain->next) {        slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;    }    /* Find closest slot from the center that is free by scanning the     * map from the center to outward in circular passes.     * This will not result in a neat layout, but will be optimal     * in the sense that there will not be holes left.     */    done = 0;    for (i = 1; i <= r && !done; i++) {	int tx, ty;	/* top and bottom parts of the ring */	for (x = -i; x <= i && !done; x++) {	    tx = dock->x_pos + x*ICON_SIZE;	    y = -i;	    ty = dock->y_pos + y*ICON_SIZE;	    if (slot_map[XY2OFS(x,y)]==0		&& ON_SCREEN(tx, ty, sx, ex, sy, ey)) {		*x_pos = x;		*y_pos = y;		done = 1;		break;	    }	    y = i;	    ty = dock->y_pos + y*ICON_SIZE;	    if (slot_map[XY2OFS(x,y)]==0		&& ON_SCREEN(tx, ty, sx, ex, sy, ey)) {		*x_pos = x;		*y_pos = y;		done = 1;		break;	    }	}	/* left and right parts of the ring */	for (y = -i+1; y <= i-1; y++) {	    ty = dock->y_pos + y*ICON_SIZE;	    x = -i;	    tx = dock->x_pos + x*ICON_SIZE;	    if (slot_map[XY2OFS(x,y)]==0		&& ON_SCREEN(tx, ty, sx, ex, sy, ey)) {		*x_pos = x;		*y_pos = y;		done = 1;		break;	    }	    x = i;	    tx = dock->x_pos + x*ICON_SIZE;	    if (slot_map[XY2OFS(x,y)]==0		&& ON_SCREEN(tx, ty, sx, ex, sy, ey)) {		*x_pos = x;		*y_pos = y;		done = 1;		break;	    }	}    }    wfree(slot_map);#undef XY2OFS    return done;}