From 17e1027ea2184c6ea6961144b064f2779793311d Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Fri, 18 Mar 2022 08:51:42 +0900 Subject: [PATCH 1/2] [users] Read product from the device tree on DT platforms Non-DMI platforms may have a device tree instead (e.g. many embedded devices, Apple Silicon Macs). If we find a model string in the DT, use that as a fallback when DMI is not available. Signed-off-by: Hector Martin --- src/modules/users/Config.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index 20c9dea6f..72d765f4b 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -339,15 +339,30 @@ guessProductName() if ( !tried ) { QFile dmiFile( QStringLiteral( "/sys/devices/virtual/dmi/id/product_name" ) ); + QFile modelFile( QStringLiteral( "/proc/device-tree/model" ) ); if ( dmiFile.exists() && dmiFile.open( QIODevice::ReadOnly ) ) { dmiProduct = cleanupForHostname( QString::fromLocal8Bit( dmiFile.readAll().simplified().data() ) ); + if ( !dmiProduct.isEmpty() ) + { + tried = true; + return dmiProduct; + } } - if ( dmiProduct.isEmpty() ) + + if ( modelFile.exists() && modelFile.open( QIODevice::ReadOnly ) ) { - dmiProduct = QStringLiteral( "pc" ); + dmiProduct + = cleanupForHostname( QString::fromLocal8Bit( modelFile.readAll().chopped( 1 ).simplified().data() ) ); + if ( !dmiProduct.isEmpty() ) + { + tried = true; + return dmiProduct; + } } + + dmiProduct = QStringLiteral( "pc" ); tried = true; } return dmiProduct; From a899f76da27c9e1212c6dabb1ab63b4db2fc2894 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Sun, 6 Aug 2023 20:00:52 +0900 Subject: [PATCH 2/2] [users] Clean up DMI model more for hostname Remove anything in parentheses, and also drop the "Apple" prefix for Apple machines. This converts: "Apple MacBook Air (13-inch, M2, 2022)" into: "MacBookAir" which is a lot more reasonable. Other vendors could be added as needed (it's inconsistent whether DT platforms prefix the model with the vendor or not). Signed-off-by: Hector Martin --- src/modules/users/Config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index 72d765f4b..8c7316efd 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -321,7 +321,7 @@ Config::hostnameStatus() const static QString cleanupForHostname( const QString& s ) { - QRegExp dmirx( "[^a-zA-Z0-9]", Qt::CaseInsensitive ); + QRegExp dmirx( "(^Apple|\\(.*\\)|[^a-zA-Z0-9])", Qt::CaseInsensitive ); return s.toLower().replace( dmirx, " " ).remove( ' ' ); }