2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-07-04 15:33:59 +02:00
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
2019-04-18 13:22:12 +02:00
|
|
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
2014-07-04 15:33:59 +02:00
|
|
|
*
|
|
|
|
* Originally from the Manjaro Installation Framework
|
|
|
|
* by Roland Singer <roland@manjaro.org>
|
|
|
|
* Copyright (C) 2007 Free Software Foundation, Inc.
|
|
|
|
*
|
2019-04-18 13:39:14 +02:00
|
|
|
* Source by Georg Grabler <ggrabler@gmail.com>
|
|
|
|
*
|
2014-07-04 15:33:59 +02:00
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "keyboardglobal.h"
|
|
|
|
|
2019-03-17 21:11:04 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2019-04-18 13:06:50 +02:00
|
|
|
#ifdef Q_OS_FREEBSD
|
|
|
|
static const char XKB_FILE[] = "/usr/local/share/X11/xkb/rules/base.lst";
|
|
|
|
#else
|
|
|
|
static const char XKB_FILE[] = "/usr/share/X11/xkb/rules/base.lst";
|
|
|
|
#endif
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
// The xkb rules file is made of several "sections". Each section
|
|
|
|
// starts with a line "! <sectionname>". The static methods here
|
|
|
|
// handle individual sections.
|
|
|
|
|
|
|
|
/** @brief Scans a file for a named section
|
2019-04-18 13:39:14 +02:00
|
|
|
*
|
2019-04-18 13:36:12 +02:00
|
|
|
* Reads from @p fh incrementally until it finds a section named @p name
|
|
|
|
* or hits end-of-file. Returns true if the section is found. The
|
|
|
|
* @p name must include the "! " section marker as well.
|
|
|
|
*/
|
|
|
|
static bool findSection( QFile& fh, const char* name )
|
|
|
|
{
|
|
|
|
while ( !fh.atEnd() )
|
|
|
|
{
|
|
|
|
QByteArray line = fh.readLine();
|
|
|
|
if ( line.startsWith( name ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
static KeyboardGlobal::ModelsMap parseKeyboardModels( const char* filepath )
|
2014-07-04 15:33:59 +02:00
|
|
|
{
|
2019-04-18 13:22:12 +02:00
|
|
|
KeyboardGlobal::ModelsMap models;
|
2014-07-04 15:33:59 +02:00
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
QFile fh( filepath );
|
|
|
|
fh.open( QIODevice::ReadOnly );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( !fh.isOpen() )
|
|
|
|
{
|
2019-03-17 21:11:04 +01:00
|
|
|
cDebug() << "X11 Keyboard model definitions not found!";
|
2014-07-04 15:33:59 +02:00
|
|
|
return models;
|
|
|
|
}
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
bool modelsFound = findSection( fh, "! model" );
|
2014-07-04 15:33:59 +02:00
|
|
|
// read the file until the end or until we break the loop
|
2019-04-18 13:39:14 +02:00
|
|
|
while ( modelsFound && !fh.atEnd() )
|
|
|
|
{
|
2014-07-04 15:33:59 +02:00
|
|
|
QByteArray line = fh.readLine();
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
// check if we start a new section
|
|
|
|
if ( line.startsWith( '!' ) )
|
2014-07-04 15:33:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// here we are in the model section, otherwhise we would continue or break
|
|
|
|
QRegExp rx;
|
2019-04-18 13:39:14 +02:00
|
|
|
rx.setPattern( "^\\s+(\\S+)\\s+(\\w.*)\n$" );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
|
|
|
// insert into the model map
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( rx.indexIn( line ) != -1 )
|
|
|
|
{
|
|
|
|
QString modelDesc = rx.cap( 2 );
|
|
|
|
QString model = rx.cap( 1 );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( model == "pc105" )
|
|
|
|
modelDesc += " - " + QObject::tr( "Default Keyboard Model" );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
models.insert( modelDesc, model );
|
2014-07-04 15:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return models;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
KeyboardGlobal::LayoutsMap parseKeyboardLayouts( const char* filepath )
|
2014-07-04 15:33:59 +02:00
|
|
|
{
|
2019-04-18 13:22:12 +02:00
|
|
|
KeyboardGlobal::LayoutsMap layouts;
|
2014-07-04 15:33:59 +02:00
|
|
|
|
|
|
|
//### Get Layouts ###//
|
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
QFile fh( filepath );
|
|
|
|
fh.open( QIODevice::ReadOnly );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( !fh.isOpen() )
|
|
|
|
{
|
2019-03-17 21:11:04 +01:00
|
|
|
cDebug() << "X11 Keyboard layout definitions not found!";
|
2014-07-04 15:33:59 +02:00
|
|
|
return layouts;
|
|
|
|
}
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
bool layoutsFound = findSection( fh, "! layout" );
|
2014-07-04 15:33:59 +02:00
|
|
|
// read the file until the end or we break the loop
|
2019-04-18 13:39:14 +02:00
|
|
|
while ( layoutsFound && !fh.atEnd() )
|
|
|
|
{
|
2014-07-04 15:33:59 +02:00
|
|
|
QByteArray line = fh.readLine();
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
if ( line.startsWith( '!' ) )
|
2014-07-04 15:33:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
QRegExp rx;
|
2019-04-18 13:39:14 +02:00
|
|
|
rx.setPattern( "^\\s+(\\S+)\\s+(\\w.*)\n$" );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
|
|
|
// insert into the layout map
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( rx.indexIn( line ) != -1 )
|
|
|
|
{
|
2019-04-18 13:22:12 +02:00
|
|
|
KeyboardGlobal::KeyboardInfo info;
|
2019-04-18 13:39:14 +02:00
|
|
|
info.description = rx.cap( 2 );
|
|
|
|
info.variants.insert( QObject::tr( "Default" ), "" );
|
|
|
|
layouts.insert( rx.cap( 1 ), info );
|
2014-07-04 15:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh.reset();
|
|
|
|
|
|
|
|
|
|
|
|
//### Get Variants ###//
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
bool variantsFound = findSection( fh, "! variant" );
|
2014-07-04 15:33:59 +02:00
|
|
|
// read the file until the end or until we break
|
2019-04-18 13:39:14 +02:00
|
|
|
while ( variantsFound && !fh.atEnd() )
|
|
|
|
{
|
2014-07-04 15:33:59 +02:00
|
|
|
QByteArray line = fh.readLine();
|
|
|
|
|
2019-04-18 13:36:12 +02:00
|
|
|
if ( line.startsWith( '!' ) )
|
2014-07-04 15:33:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
QRegExp rx;
|
2019-04-18 13:39:14 +02:00
|
|
|
rx.setPattern( "^\\s+(\\S+)\\s+(\\S+): (\\w.*)\n$" );
|
2014-07-04 15:33:59 +02:00
|
|
|
|
|
|
|
// insert into the variants multimap, if the pattern matches
|
2019-04-18 13:39:14 +02:00
|
|
|
if ( rx.indexIn( line ) != -1 )
|
|
|
|
{
|
|
|
|
if ( layouts.find( rx.cap( 2 ) ) != layouts.end() )
|
|
|
|
{
|
2014-07-04 15:33:59 +02:00
|
|
|
// in this case we found an entry in the multimap, and add the values to the multimap
|
2019-04-18 13:39:14 +02:00
|
|
|
layouts.find( rx.cap( 2 ) ).value().variants.insert( rx.cap( 3 ), rx.cap( 1 ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-04 15:33:59 +02:00
|
|
|
// create a new map in the multimap - the value was not found.
|
2019-04-18 13:22:12 +02:00
|
|
|
KeyboardGlobal::KeyboardInfo info;
|
2019-04-18 13:39:14 +02:00
|
|
|
info.description = rx.cap( 2 );
|
|
|
|
info.variants.insert( QObject::tr( "Default" ), "" );
|
|
|
|
info.variants.insert( rx.cap( 3 ), rx.cap( 1 ) );
|
|
|
|
layouts.insert( rx.cap( 2 ), info );
|
2014-07-04 15:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layouts;
|
|
|
|
}
|
2019-04-18 13:22:12 +02:00
|
|
|
|
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
KeyboardGlobal::LayoutsMap KeyboardGlobal::getKeyboardLayouts()
|
|
|
|
{
|
2019-04-18 13:22:12 +02:00
|
|
|
return parseKeyboardLayouts( XKB_FILE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-18 13:39:14 +02:00
|
|
|
KeyboardGlobal::ModelsMap KeyboardGlobal::getKeyboardModels()
|
|
|
|
{
|
2019-04-18 13:22:12 +02:00
|
|
|
return parseKeyboardModels( XKB_FILE );
|
|
|
|
}
|
|
|
|
|