Skip to content

Commit

Permalink
GetDeskNumber fix, cleanup, and drop support for negative desks.
Browse files Browse the repository at this point in the history
The function GetDeskNumber would not properly wrap more than once.
For instance the command `GotoDesk 3 0 0` should return a desk between
the minimum desk 0 and the maximum desk 0, but instead this would move
the desk `2` from the current desk, because the wrapping code would
only ever occur once.

In fixing the issue, this also cleans up the logic in the
GetDeskNumber function by better handling all the if cases
and use the modulus function to do the wrapping.

Last, support for negative desks is dropped, since it is not
part of the EWMH spec.
  • Loading branch information
somiaj authored and ThomasAdam committed Nov 29, 2024
1 parent 27ffd68 commit 3f39eb3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 88 deletions.
9 changes: 5 additions & 4 deletions doc/fvwm3_manpage_source.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7585,8 +7585,8 @@ interpreted as a relative desk number. Two arguments are understood as
a relative and an absolute desk number. Three arguments specify a
relative desk and the minimum and maximum of the allowable range. Four
arguments specify the relative, absolute, minimum and maximum values.
(Desktop numbers can be negative). If a literal _prev_ is given as the
single argument, the last visited desk number is used.
If a literal _prev_ is given as the single argument, the last visited
desk number is used.
+
If _arg1_ is non zero then the next desktop number is the current
desktop number plus _arg1_.
Expand All @@ -7604,8 +7604,9 @@ different desktop.
+
The number of active desktops is determined dynamically. Only desktops
which contain windows or are currently being displayed are active.
Desktop numbers must be between 2147483647 and -2147483648 (is that
enough?).
Desktop numbers must be between 0 and 2147483647 (is that enough?).
Negative desktop numbers are not supported by the EWMH specifications
and are no longer supported in fvwm.

*GotoDeskAndPage* screen | prev | _desk_ _xpage_ _ypage_::
Switches the current viewport to another desktop and page, similar to
Expand Down
138 changes: 54 additions & 84 deletions fvwm/virtual.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ static void _drag_viewport(const exec_context_t *exc, int scroll_speed)

/*
*
* Parse arguments for "Desk" and "MoveToDesk" (formerly "WindowsDesk"):
* Parse arguments for "GotoDesk" and "MoveToDesk":
*
* (nil) : desk number = current desk
* n : desk number = current desk + n
Expand All @@ -287,9 +287,7 @@ static void _drag_viewport(const exec_context_t *exc, int scroll_speed)
*/
static int GetDeskNumber(struct monitor *mon, char *action, int current_desk)
{
int n;
int m;
int is_relative;
int desk;
int val[4];
int min, max;
Expand All @@ -301,93 +299,63 @@ static int GetDeskNumber(struct monitor *mon, char *action, int current_desk)
if (MatchToken(action, "next"))
{
if (current_desk + 1 >= number_of_desktops(mon))
return (0);
return 0;
else
return (current_desk + 1);
}
n = GetIntegerArguments(action, NULL, &(val[0]), 4);
if (n <= 0)
{
return mon->virtual_scr.CurrentDesk;
return current_desk + 1;
}
if (n == 1)
{
if (is_tracking_shared)
return val[0];
return current_desk + val[0];
}
desk = current_desk;
m = 0;
if (val[0] == 0)
{
/* absolute desk number */
desk = val[1];
is_relative = 0;

switch (GetIntegerArguments(action, NULL, &(val[0]), 4)) {
case 2:
if (val[0] == 0) {
desk = val[1];
goto found_desk;
}
case 1:
desk = val[0];
if (!is_tracking_shared)
desk += current_desk;
goto found_desk;
case 3:
m = 1;
break;
case 4:
m = 2;
break;
default:
return mon->virtual_scr.CurrentDesk;
}
else
{
/* relative desk number */
desk += val[0];
is_relative = 1;

/* Handle limits. */
if (val[m] < val[m + 1]) {
min = val[m];
max = val[m + 1];
} else {
/* min > max is nonsense, so swap 'em. */
min = val[m + 1];
max = val[m];
}
if (n == 3)
{
m = 1;
if (min < 0) {
desk = min;
goto found_desk;
}
if (n == 4)
{
m = 2;

/* Return absolute desk number. */
if (val[0] == 0 && m == 2) {
desk = val[1];
if (desk < min)
desk = min;
if (desk > max)
desk = max;
goto found_desk;
}
if (n > 2)
{
/* handle limits */
if (val[m] <= val[m+1])
{
min = val[m];
max = val[m+1];
}
else
{
/* min > max is nonsense, so swap 'em. */
min = val[m+1];
max = val[m];
}
if (is_relative)
{
/* Relative moves wrap around. */
if (desk < min)
{
desk += (max - min + 1);
}
else if (desk > max)
{
desk -= (max - min + 1);
}
}
else if (desk < min)
{
/* Relative move outside of range, wrap around. */
if (val[0] < 0)
{
desk = max;
}
else
{
desk = min;
}
}
else if (desk > max)
{
/* Move outside of range, truncate. */
if (val[0] > 0)
{
desk = min;
}
else
{
desk = max;
}
}

desk = (current_desk + val[0]) % (max - min + 1);
desk += desk < 0 ? max + 1 : min;

found_desk:
if (desk < 0) {
fvwm_debug(__func__, "Negative desks are not supported.");
return current_desk;
}

return desk;
Expand Down Expand Up @@ -2524,6 +2492,8 @@ void CMD_GotoDesk(F_CMD_ARGS)
m = monitor_get_current();

new_desk = GetDeskNumber(m, action, m->virtual_scr.CurrentDesk);
if (m->virtual_scr.CurrentDesk == new_desk)
return;

if (is_tracking_shared) {
/* Check to see if this monitor is requesting a desktop which
Expand Down

0 comments on commit 3f39eb3

Please sign in to comment.