TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkinitialization.cpp
Go to the documentation of this file.
1 #include "ttkinitialization.h"
2 #include "ttkdesktopscreen.h"
3 #include "ttklogoutput.h"
4 #include "ttkversion.h"
5 #include "ttkdumper.h"
6 
7 #if !TTK_QT_VERSION_CHECK(5,0,0)
8 # include <QTextCodec>
9 #endif
10 #include <QApplication>
11 #include <QFontDatabase>
12 
13 #ifdef Q_OS_LINUX
14 # include <malloc.h>
15 #endif
16 
17 #if !TTK_QT_VERSION_CHECK(5,0,0)
18 bool qEnvironmentVariableIsSet(const char *v) noexcept
19 {
20 #ifdef Q_CC_MSVC
21  size_t s = 0;
22  (void)getenv_s(&s, 0, 0, v);
23  return s != 0;
24 #else
25  return ::getenv(v) != nullptr;
26 #endif
27 }
28 #endif
29 
30 static QString message(const TTK::Attribute attr)
31 {
32  QStringList data;
33  if(attr & TTK::Attribute::UseLog) data << "UseLog";
34  if(attr & TTK::Attribute::ScaleFactor) data << "ScaleFactor";
35  if(attr & TTK::Attribute::UseXCB) data << "UseXCB";
36  if(attr & TTK::Attribute::DisbaleFFmpeg) data << "DisbaleFFmpeg";
37 
38  return "(" + data.join(",") + ")";
39 }
40 
42 {
43  if(attr & TTK::Attribute::UseLog)
44  {
45  // initiailize log module
48  }
49 
50  TTK_INFO_STREAM("TTK Application start, flag value is" << message(attr));
51 
53  {
54 #if TTK_QT_VERSION_CHECK(6,0,0)
55  // do nothing
56 #elif TTK_QT_VERSION_CHECK(5,4,0)
57 # if TTK_QT_VERSION_CHECK(5,12,0)
58  QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
59  QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
60 # if TTK_QT_VERSION_CHECK(5,14,0)
61  QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
62 # endif
63 # elif TTK_QT_VERSION_CHECK(5,6,0)
64  const qreal dpi = TTKDesktopScreen::currentPixelRatio();
65  qputenv("QT_SCALE_FACTOR", QByteArray::number(dpi < 1.0 ? 1.0 : dpi));
66 # endif
67 #endif
68  }
69 
70 #ifdef Q_OS_UNIX
71  if(attr & TTK::Attribute::UseXCB)
72  {
73  // froce using xcb on XWayland
74  if(qgetenv("XDG_SESSION_TYPE") == "wayland" && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM"))
75  {
76  qputenv("QT_QPA_PLATFORM", "xcb");
77  }
78  }
79 #endif
80 
81 #if TTK_QT_VERSION_CHECK(6,0,0)
83  {
84 # ifdef Q_OS_WIN
85  qputenv("QT_MEDIA_BACKEND", "windows");
86 # elif defined Q_OS_UNIX
87  qputenv("QT_MEDIA_BACKEND", "gstreamer");
88 # elif defined Q_OS_MAC
89  qputenv("QT_MEDIA_BACKEND", "darwin");
90 # else
91  qputenv("QT_MEDIA_BACKEND", "android");
92 # endif
93  }
94 #endif
95 }
96 
97 
99  : TTKInitialization(nullptr)
100 {
101 
102 }
103 
105 {
106  TTK_REGISTER_ORGANIZATION(TTK_APP);
107 
108  m_dumper = new TTKDumper(functor);
109  m_dumper->execute();
110 }
111 
113 {
114  delete m_dumper;
115 }
116 
118 {
119  QFont font = QApplication::font();
120  for(const QString &family : QtFontFamilies())
121  {
122  if(family == "微软雅黑" ||
123  family == QString::fromUtf8("微软雅黑") ||
124  family == QString::fromLocal8Bit("微软雅黑") ||
125  family == "Microsoft YaHei")
126  {
127  font.setFamily(family);
128  QApplication::setFont(font);
129  TTK_INFO_STREAM("TTK application uses 'Microsoft YaHei' font");
130  break;
131  }
132  }
133 }
134 
136 {
137 #if !TTK_QT_VERSION_CHECK(5,0,0)
138  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
139  QTextCodec::setCodecForLocale(codec);
140  QTextCodec::setCodecForCStrings(codec);
141  QTextCodec::setCodecForTr(codec);
142 #endif
143 }
144 
145 bool TTKInitialization::argumentValid(const QString &arg)
146 {
147  return !arg.endsWith("AppImage") &&
148  !arg.endsWith(TTK_APP_NAME) &&
149  !arg.endsWith(TTK_SERVICE_NAME) &&
150  !arg.endsWith(TTK_APP_RUN_NAME) &&
151  !arg.endsWith(TTK_SERVICE_RUN_NAME);
152 }
153 
155 {
156  execute(QCoreApplication::arguments());
157 }
158 
159 void TTKInitialization::execute(const QStringList &arguments)
160 {
161 #ifdef Q_OS_LINUX
162  // memory free
163  mallopt(M_MMAP_THRESHOLD, 1024 * 1024); // 1MB mmap
164  mallopt(M_TRIM_THRESHOLD, 2 * 1024 * 1024); // 2MB brk
165 #endif
166 
167  for(const QString &arg : arguments)
168  {
169  if("-v" == arg || "--version" == arg || "-version" == arg)
170  {
171  QString text = TTK_STR_CAT("\nModule: ", TTK_APP_NAME, " v", TTK_VERSION_STR, "\n");
172  text += TTK_STR_CAT("Build: ", __DATE__, " ", __TIME__, "\n");
173  text += TTK_STR_CAT("Offical web page: https://github.com/Greedysky/", TTK_APP_NAME, "\n");
174  text += "Copyright(C) 2015 - 2026 Greedysky All Rights Reserved\n";
175  TTK_INFO_STREAM(text);
176  exit(EXIT_SUCCESS);
177  }
178  }
179 }
#define TTK_REGISTER_ORGANIZATION(ORG)
static QString message(const TTK::Attribute attr)
The class of the ttk dumper.
Definition: ttkdumper.h:39
static qreal currentPixelRatio()
#define TTK_APP_NAME
Definition: ttkobject.h:25
TTK_MODULE_EXPORT void initiailizeLog(const QString &module)
TTK_MODULE_EXPORT void installLogHandler()
The class of the ttk initialization.
void(* TTKCleanFunctor)(void)
bool qEnvironmentVariableIsSet(const char *v) noexcept
bool argumentValid(const QString &arg)
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
TTK_MODULE_EXPORT void initialize(TTK::Attribute attr)
void execute()
Definition: ttkdumper.cpp:226
#define QtFontFamilies()
FontDatabase families.
Definition: ttkqtcompat.h:206
#define TTK_VERSION_STR
Definition: ttkversion.h:179
#define TTK_SERVICE_NAME
Definition: ttkobject.h:24
#define TTK_STR_CAT(...)
Definition: ttkglobal.h:240
#define TTK_SERVICE_RUN_NAME
Definition: ttkobject.h:35
#define TTK_APP_RUN_NAME
Definition: ttkobject.h:34