1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "AppParamParser.h"
10 
11 #include "CompileInfo.h"
12 #include "FileItem.h"
13 #include "ServiceBroker.h"
14 #include "settings/AdvancedSettings.h"
15 #include "utils/StringUtils.h"
16 #include "utils/SystemInfo.h"
17 #include "utils/log.h"
18 
19 #include <iostream>
20 #include <stdlib.h>
21 #include <string>
22 #include <vector>
23 
24 #if defined(TARGET_LINUX)
25 namespace
26 {
27 std::vector<std::string> availableWindowSystems = CCompileInfo::GetAvailableWindowSystems();
28 } // namespace
29 #endif
30 
CAppParamParser()31 CAppParamParser::CAppParamParser()
32 : m_logLevel(LOG_LEVEL_NORMAL),
33   m_playlist(new CFileItemList())
34 {
35 }
36 
37 CAppParamParser::~CAppParamParser() = default;
38 
Parse(const char * const * argv,int nArgs)39 void CAppParamParser::Parse(const char* const* argv, int nArgs)
40 {
41   if (nArgs > 1)
42   {
43     for (int i = 1; i < nArgs; i++)
44       ParseArg(argv[i]);
45 
46     // testmode is only valid if at least one item to play was given
47     if (m_playlist->IsEmpty())
48       m_testmode = false;
49   }
50 }
51 
DisplayVersion()52 void CAppParamParser::DisplayVersion()
53 {
54   printf("%s Media Center %s\n", CSysInfo::GetVersion().c_str(), CSysInfo::GetAppName().c_str());
55   printf("Copyright (C) %s Team %s - http://kodi.tv\n",
56          CCompileInfo::GetCopyrightYears(), CSysInfo::GetAppName().c_str());
57   exit(0);
58 }
59 
DisplayHelp()60 void CAppParamParser::DisplayHelp()
61 {
62   std::string lcAppName = CSysInfo::GetAppName();
63   StringUtils::ToLower(lcAppName);
64   printf("Usage: %s [OPTION]... [FILE]...\n\n", lcAppName.c_str());
65   printf("Arguments:\n");
66   printf("  -fs\t\t\tRuns %s in full screen\n", CSysInfo::GetAppName().c_str());
67   printf("  --standalone\t\t%s runs in a stand alone environment without a window \n", CSysInfo::GetAppName().c_str());
68   printf("\t\t\tmanager and supporting applications. For example, that\n");
69   printf("\t\t\tenables network settings.\n");
70   printf("  -p or --portable\t%s will look for configurations in install folder instead of ~/.%s\n", CSysInfo::GetAppName().c_str(), lcAppName.c_str());
71   printf("  --debug\t\tEnable debug logging\n");
72   printf("  --version\t\tPrint version information\n");
73   printf("  --test\t\tEnable test mode. [FILE] required.\n");
74   printf("  --settings=<filename>\t\tLoads specified file after advancedsettings.xml replacing any settings specified\n");
75   printf("  \t\t\t\tspecified file must exist in special://xbmc/system/\n");
76 #if defined(TARGET_LINUX)
77   printf("  --windowing=<system>\tSelect which windowing method to use.\n");
78   printf("  \t\t\t\tAvailable window systems are:");
79   for (const auto& windowSystem : availableWindowSystems)
80     printf(" %s", windowSystem.c_str());
81   printf("\n");
82 #endif
83   exit(0);
84 }
85 
ParseArg(const std::string & arg)86 void CAppParamParser::ParseArg(const std::string &arg)
87 {
88   if (arg == "-fs" || arg == "--fullscreen")
89     m_startFullScreen = true;
90   else if (arg == "-h" || arg == "--help")
91     DisplayHelp();
92   else if (arg == "-v" || arg == "--version")
93     DisplayVersion();
94   else if (arg == "--standalone")
95     m_standAlone = true;
96   else if (arg == "-p" || arg  == "--portable")
97     m_platformDirectories = false;
98   else if (arg == "--debug")
99     m_logLevel = LOG_LEVEL_DEBUG;
100   else if (arg == "--test")
101     m_testmode = true;
102   else if (arg.substr(0, 11) == "--settings=")
103     m_settingsFile = arg.substr(11);
104 #if defined(TARGET_LINUX)
105   else if (arg.substr(0, 12) == "--windowing=")
106   {
107     if (std::find(availableWindowSystems.begin(), availableWindowSystems.end(), arg.substr(12)) !=
108         availableWindowSystems.end())
109       m_windowing = arg.substr(12);
110     else
111     {
112       std::cout << "Selected window system not available: " << arg << std::endl;
113       std::cout << "    Available window systems:";
114       for (const auto& windowSystem : availableWindowSystems)
115         std::cout << " " << windowSystem;
116       std::cout << std::endl;
117       exit(0);
118     }
119   }
120 #endif
121   else if (arg.length() != 0 && arg[0] != '-')
122   {
123     const CFileItemPtr item = std::make_shared<CFileItem>(arg);
124     item->SetPath(arg);
125     m_playlist->Add(item);
126   }
127 }
128 
SetAdvancedSettings(CAdvancedSettings & advancedSettings) const129 void CAppParamParser::SetAdvancedSettings(CAdvancedSettings& advancedSettings) const
130 {
131   if (m_logLevel == LOG_LEVEL_DEBUG)
132   {
133     advancedSettings.m_logLevel = LOG_LEVEL_DEBUG;
134     advancedSettings.m_logLevelHint = LOG_LEVEL_DEBUG;
135     CServiceBroker::GetLogging().SetLogLevel(LOG_LEVEL_DEBUG);
136   }
137 
138   if (!m_settingsFile.empty())
139     advancedSettings.AddSettingsFile(m_settingsFile);
140 
141   if (m_startFullScreen)
142     advancedSettings.m_startFullScreen = true;
143 
144   if (m_standAlone)
145     advancedSettings.m_handleMounting = true;
146 }
147 
GetPlaylist() const148 const CFileItemList& CAppParamParser::GetPlaylist() const
149 {
150   return *m_playlist;
151 }
152