diff --git a/common/src/displays.c b/common/src/displays.c index a3d7658f..e6eac1f8 100644 --- a/common/src/displays.c +++ b/common/src/displays.c @@ -126,29 +126,46 @@ bool lgDisplaysPack( order[j] = v; } - unsigned maxWidth = 0, totalWidth = 0; + unsigned maxWidth = 0; for(unsigned i = 0; i < count; ++i) - { if (in[i].width > maxWidth) maxWidth = in[i].width; - totalWidth += in[i].width; - } - /* Candidate shelf widths: the widest single display (a pure vertical stack), - * every running total in packing order (which produces the natural "N per - * row" arrangements), and the sum of all widths (a single row). Every - * candidate is at least maxWidth so no display is ever too wide to place. */ - unsigned candidates[KVMFR_MAX_DISPLAYS + 2]; + /* Candidate shelf widths: the total width of every subset of displays. + * + * Any shelf is some set of displays sitting side by side, so its width is a + * subset sum; enumerating them covers every arrangement worth trying. Only + * considering running totals in packing order misses layouts where the + * displays that share a shelf are not the first few — a 4K above two 1440p + * side by side, for instance, since the 4K sorts first but belongs alone. + * + * With at most 8 displays this is 255 subsets, which costs nothing. Every + * candidate is raised to maxWidth so no display is ever too wide to place. */ + unsigned candidates[1u << KVMFR_MAX_DISPLAYS]; unsigned candidateCount = 0; - candidates[candidateCount++] = maxWidth; - unsigned running = 0; - for(unsigned i = 0; i < count; ++i) + for(uint32_t mask = 1; mask < (1u << count); ++mask) { - running += in[order[i]].width; - candidates[candidateCount++] = running < maxWidth ? maxWidth : running; + unsigned width = 0; + for(unsigned i = 0; i < count; ++i) + if (mask & (1u << i)) + width += in[i].width; + + if (width < maxWidth) + width = maxWidth; + + // duplicates are common when displays share a width, so drop them + bool seen = false; + for(unsigned c = 0; c < candidateCount; ++c) + if (candidates[c] == width) + { + seen = true; + break; + } + + if (!seen) + candidates[candidateCount++] = width; } - candidates[candidateCount++] = totalWidth < maxWidth ? maxWidth : totalWidth; unsigned bestX[KVMFR_MAX_DISPLAYS], bestY[KVMFR_MAX_DISPLAYS]; unsigned bestWidth = 0, bestHeight = 0; diff --git a/common/tests/test_displays.c b/common/tests/test_displays.c index 186a769c..a66e7002 100644 --- a/common/tests/test_displays.c +++ b/common/tests/test_displays.c @@ -203,6 +203,27 @@ int main(void) }; run("3 identical 1080p displays", triple, 3); + /* One 4K next to two 1440p. The best layout puts the 4K on its own shelf + * with the two 1440p side by side below it, which is only found if the + * candidate shelf widths include sums of displays that are not the first + * few in packing order. */ + const LGDisplayInfo mixed[3] = { + { .id = 0, .name = "4K", .x = 0, .y = 0, .width = 3840, .height = 2160, + .primary = true }, + { .id = 1, .name = "1440pA", .x = 3840, .y = 0, .width = 2560, .height = 1440 }, + { .id = 2, .name = "1440pB", .x = 6400, .y = 0, .width = 2560, .height = 1440 }, + }; + run("4K plus two 1440p", mixed, 3); + { + KVMFRDisplay o[KVMFR_MAX_DISPLAYS]; + unsigned aw, ah, dw, dh; + if (lgDisplaysPack(mixed, 3, 1, o, &aw, &ah, &dw, &dh)) + check((uint64_t)aw * ah <= 18432000ULL, + "4K + 2x1440p packs into at most 5120x3600 (got %ux%u)", aw, ah); + else + check(false, "4K + 2x1440p packed"); + } + const LGDisplayInfo single[1] = { { .id = 0, .name = "only", .x = 0, .y = 0, .width = 2560, .height = 1440 }, }; diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res b/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res index 4d6d530b..81c23877 100644 Binary files a/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res and b/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res differ diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj b/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj index 524f2027..7950528d 100644 Binary files a/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj and b/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj differ diff --git a/host/build-win/VERSION b/host/build-win/VERSION index 9580216f..4f69cdb3 100644 --- a/host/build-win/VERSION +++ b/host/build-win/VERSION @@ -1 +1 @@ -B7-380-gb327abf6c6+ \ No newline at end of file +B7-385-gb73ffa1b33+ \ No newline at end of file diff --git a/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj b/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj index bb9ebc7c..3ba210b3 100644 Binary files a/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj and b/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj differ diff --git a/host/build-win/include/version.h b/host/build-win/include/version.h index e9b48932..fd114bce 100644 --- a/host/build-win/include/version.h +++ b/host/build-win/include/version.h @@ -1,2 +1,2 @@ -#define LG_VERSION_STR "B7-380-gb327abf6c6+" +#define LG_VERSION_STR "B7-385-gb73ffa1b33+" #define LG_CURRENT_YEAR 2026 diff --git a/host/build-win/version.c b/host/build-win/version.c index 0d28d0ca..8f6cfbf1 100644 --- a/host/build-win/version.c +++ b/host/build-win/version.c @@ -1 +1 @@ -const char * BUILD_VERSION = "B7-380-gb327abf6c6+"; \ No newline at end of file +const char * BUILD_VERSION = "B7-385-gb73ffa1b33+"; \ No newline at end of file