Skip to content

Commit 0532b64

Browse files
committed
wayland: dpi
1 parent efa6671 commit 0532b64

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/ruisapp/glue/linux/glue_wayland.cxx

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ class output_wrapper
758758
std::string description;
759759
uint32_t scale = 1;
760760

761+
uint32_t get_dpi() const noexcept
762+
{
763+
auto dpi = (this->resolution * utki::mm_per_inch).comp_div(this->physical_size_mm);
764+
return (dpi.x() + dpi.y()) / 2;
765+
}
766+
761767
output_wrapper(wl_registry& registry, uint32_t id, uint32_t interface_version) :
762768
output([&]() {
763769
void* output = wl_registry_bind(&registry, id, &wl_output_interface, std::min(interface_version, 2u));
@@ -1109,23 +1115,35 @@ struct surface_wrapper {
11091115
wl_surface_set_opaque_region(this->sur, region.reg);
11101116
}
11111117

1112-
uint32_t find_max_scale(const std::map<uint32_t, output_wrapper>& outputs)
1118+
struct scale_and_dpi {
1119+
uint32_t scale = 1;
1120+
uint32_t dpi = 96;
1121+
};
1122+
1123+
scale_and_dpi find_scale_and_dpi(const std::map<uint32_t, output_wrapper>& outputs)
11131124
{
1114-
std::cout << "looking for max scale" << std::endl;
1125+
LOG([](auto& o) {
1126+
o << "looking for max scale" << std::endl;
1127+
})
11151128

11161129
// if surface did not enter any outputs yet, then just take scale of first available output
11171130
if (this->outputs.empty()) {
1118-
std::cout << " surface has not entered any outputs yet" << std::endl;
1119-
return 1;
1131+
LOG([](auto& o) {
1132+
o << " surface has not entered any outputs yet" << std::endl;
1133+
})
1134+
return {};
11201135
}
11211136

1122-
uint32_t max_scale = 1;
1137+
uint32_t max_scale;
1138+
uint32_t dpi;
11231139

11241140
// go through outputs which the surface has entered
11251141
for (auto wlo : this->outputs) {
11261142
auto id = get_output_id(wlo);
11271143

1128-
std::cout << " check output id = " << id << std::endl;
1144+
LOG([&](auto& o) {
1145+
o << " check output id = " << id << std::endl;
1146+
})
11291147

11301148
auto i = outputs.find(id);
11311149
if (i == outputs.end()) {
@@ -1136,12 +1154,20 @@ struct surface_wrapper {
11361154
continue;
11371155
}
11381156

1139-
std::cout << " output found, scale = " << i->second.scale << std::endl;
1157+
auto& output = i->second;
11401158

1141-
max_scale = std::max(i->second.scale, max_scale);
1159+
LOG([&](auto& o) {
1160+
o << " output found, scale = " << output.scale << std::endl;
1161+
})
1162+
1163+
max_scale = std::max(output.scale, max_scale);
1164+
dpi = output.get_dpi();
11421165
}
11431166

1144-
return max_scale;
1167+
return {
1168+
.scale = max_scale, //
1169+
.dpi = dpi
1170+
};
11451171
}
11461172

11471173
private:
@@ -2016,9 +2042,9 @@ struct window_wrapper : public utki::destructable {
20162042
o << "resize window to " << std::dec << dims << std::endl;
20172043
})
20182044

2019-
auto scale = this->surface.find_max_scale(this->registry.outputs);
2045+
auto sd = this->surface.find_scale_and_dpi(this->registry.outputs);
20202046

2021-
dims *= scale;
2047+
dims *= sd.scale;
20222048

20232049
wl_egl_window_resize(this->egl_window.win, int(dims.x()), int(dims.y()), 0, 0);
20242050

@@ -2031,16 +2057,17 @@ struct window_wrapper : public utki::destructable {
20312057
this->surface.commit();
20322058

20332059
LOG([&](auto& o) {
2034-
o << "final window scale = " << scale << std::endl;
2060+
o << "final window scale = " << sd.scale << std::endl;
20352061
})
20362062

2037-
this->scale = ruis::real(scale);
2063+
this->scale = ruis::real(sd.scale);
20382064

20392065
auto& app = ruisapp::inst();
20402066

20412067
// update ruis::context::units
2042-
app.gui.context.get().units.set_dots_per_pp(this->scale);
2043-
// TODO: update dots_per_inch
2068+
auto& units = app.gui.context.get().units;
2069+
units.set_dots_per_pp(this->scale);
2070+
units.set_dots_per_inch(ruis::real(sd.dpi));
20442071

20452072
update_window_rect(app, ruis::rect(0, dims.to<ruis::real>()));
20462073
}

0 commit comments

Comments
 (0)