common: flesh out enum lab_edge and prefer over wlr_edges/wlr_direction

I like the new common/edge.h. I don't like how inconsistently we use it.

Current situation:

 - enum wlr_edges and wlr_direction are designed to be used as bitset,
   and are defined compatibly

 - enum lab_edge is *also* designed to be used as bitset, but
   incompatible with the others (LEFT/RIGHT come before UP/DOWN)

 - we use an inconsistent mix of all three *AND* uint32_t (usually with
   the WLR_EDGE constants rather than the LAB_EDGE constants), and
   convert between them on an ad-hoc basis, sometimes implicitly

Let's clean this up:

 - reorder enum lab_edge to be compatible with the two wlr enums
   (check this by static_assert)

 - use TOP/BOTTOM naming rather than UP/DOWN (matches wlr_edges)

 - add constants for the remaining possible combinations of the 4 edges

 - use lab_edge for all internal edge/direction fields, consistently

 - add lab_edge_is_cardinal() as a sanity check before casting to
   enum wlr_direction, and then eliminate all of direction.c/h

Instead of "enum wlr_edges direction", we now have
"enum lab_edge direction" which is not that much better. At least we
are now clear that we're overloading one enum with two meanings.
This commit is contained in:
John Lindgren 2025-08-26 20:27:34 -04:00
parent 4d1be7eada
commit ef766d16f0
26 changed files with 294 additions and 320 deletions

View file

@ -1,44 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "common/direction.h"
#include <assert.h>
#include <wlr/types/wlr_output_layout.h>
#include "view.h"
bool
direction_from_edge(enum lab_edge edge, enum wlr_direction *direction)
{
switch (edge) {
case LAB_EDGE_LEFT:
*direction = WLR_DIRECTION_LEFT;
return true;
case LAB_EDGE_RIGHT:
*direction = WLR_DIRECTION_RIGHT;
return true;
case LAB_EDGE_UP:
*direction = WLR_DIRECTION_UP;
return true;
case LAB_EDGE_DOWN:
*direction = WLR_DIRECTION_DOWN;
return true;
default:
return false;
}
}
enum wlr_direction
direction_get_opposite(enum wlr_direction direction)
{
switch (direction) {
case WLR_DIRECTION_RIGHT:
return WLR_DIRECTION_LEFT;
case WLR_DIRECTION_LEFT:
return WLR_DIRECTION_RIGHT;
case WLR_DIRECTION_DOWN:
return WLR_DIRECTION_UP;
case WLR_DIRECTION_UP:
return WLR_DIRECTION_DOWN;
default:
assert(0); /* Unreachable */
return WLR_DIRECTION_UP;
}
}

View file

@ -1,21 +1,36 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "common/edge.h"
#include <assert.h>
#include <strings.h>
#include <wlr/util/edges.h>
#include <wlr/types/wlr_output_layout.h>
static_assert((int)LAB_EDGE_TOP == (int)WLR_EDGE_TOP
&& (int)LAB_EDGE_BOTTOM == (int)WLR_EDGE_BOTTOM
&& (int)LAB_EDGE_LEFT == (int)WLR_EDGE_LEFT
&& (int)LAB_EDGE_RIGHT == (int)WLR_EDGE_RIGHT,
"enum lab_edge does not match enum wlr_edges");
static_assert((int)LAB_EDGE_TOP == (int)WLR_DIRECTION_UP
&& (int)LAB_EDGE_BOTTOM == (int)WLR_DIRECTION_DOWN
&& (int)LAB_EDGE_LEFT == (int)WLR_DIRECTION_LEFT
&& (int)LAB_EDGE_RIGHT == (int)WLR_DIRECTION_RIGHT,
"enum lab_edge does not match enum wlr_direction");
enum lab_edge
lab_edge_parse(const char *direction, bool tiled, bool any)
{
if (!direction) {
return LAB_EDGE_INVALID;
return LAB_EDGE_NONE;
}
if (!strcasecmp(direction, "left")) {
return LAB_EDGE_LEFT;
} else if (!strcasecmp(direction, "up")) {
return LAB_EDGE_UP;
return LAB_EDGE_TOP;
} else if (!strcasecmp(direction, "right")) {
return LAB_EDGE_RIGHT;
} else if (!strcasecmp(direction, "down")) {
return LAB_EDGE_DOWN;
return LAB_EDGE_BOTTOM;
}
if (any) {
@ -28,17 +43,31 @@ lab_edge_parse(const char *direction, bool tiled, bool any)
if (!strcasecmp(direction, "center")) {
return LAB_EDGE_CENTER;
} else if (!strcasecmp(direction, "up-left")) {
return LAB_EDGE_UPLEFT;
return LAB_EDGES_TOP_LEFT;
} else if (!strcasecmp(direction, "up-right")) {
return LAB_EDGE_UPRIGHT;
return LAB_EDGES_TOP_RIGHT;
} else if (!strcasecmp(direction, "down-left")) {
return LAB_EDGE_DOWNLEFT;
return LAB_EDGES_BOTTOM_LEFT;
} else if (!strcasecmp(direction, "down-right")) {
return LAB_EDGE_DOWNRIGHT;
return LAB_EDGES_BOTTOM_RIGHT;
}
}
return LAB_EDGE_INVALID;
return LAB_EDGE_NONE;
}
bool
lab_edge_is_cardinal(enum lab_edge edge)
{
switch (edge) {
case LAB_EDGE_TOP:
case LAB_EDGE_BOTTOM:
case LAB_EDGE_LEFT:
case LAB_EDGE_RIGHT:
return true;
default:
return false;
}
}
enum lab_edge
@ -49,11 +78,11 @@ lab_edge_invert(enum lab_edge edge)
return LAB_EDGE_RIGHT;
case LAB_EDGE_RIGHT:
return LAB_EDGE_LEFT;
case LAB_EDGE_UP:
return LAB_EDGE_DOWN;
case LAB_EDGE_DOWN:
return LAB_EDGE_UP;
case LAB_EDGE_TOP:
return LAB_EDGE_BOTTOM;
case LAB_EDGE_BOTTOM:
return LAB_EDGE_TOP;
default:
return LAB_EDGE_INVALID;
return LAB_EDGE_NONE;
}
}

View file

@ -1,5 +1,4 @@
labwc_sources += files(
'direction.c',
'box.c',
'buf.c',
'dir.c',