Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / misc / window_displayer.cpp
blob39e5ff35d965096e3292339f86579fc1f57409ea
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2016 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdmisc.h"
22 #include "nel/misc/path.h"
23 #include "nel/misc/command.h"
24 #include "nel/misc/thread.h"
26 #include "nel/misc/window_displayer.h"
28 using namespace std;
30 #ifdef DEBUG_NEW
31 #define new DEBUG_NEW
32 #endif
34 namespace NLMISC {
36 class CUpdateThread : public IRunnable
38 CWindowDisplayer *Disp;
39 string WindowNameEx;
40 sint X, Y, W, H, HS;
41 bool Iconified;
42 uint32 FS;
43 string FN;
44 bool WW;
45 CLog *Log;
47 public:
48 CUpdateThread (CWindowDisplayer *disp, string windowNameEx, bool iconified, sint x, sint y, sint w, sint h, sint hs, sint fs, const std::string &fn, bool ww, CLog *log) :
49 Disp(disp), WindowNameEx(windowNameEx), X(x), Y(y), W(w), H(h), HS(hs), Iconified(iconified), FS(fs), FN(fn), WW(ww), Log(log)
53 void run()
55 Disp->open (WindowNameEx, Iconified, X, Y, W, H, HS, FS, FN, WW, Log);
56 Disp->display_main ();
60 CWindowDisplayer::~CWindowDisplayer ()
62 // we have to wait the exit of the thread
63 _Continue = false;
64 nlassert (_Thread != NULL);
65 _Thread->wait();
66 delete _Thread;
69 bool CWindowDisplayer::update ()
71 vector<string> copy;
73 CSynchronized<std::vector<std::string> >::CAccessor access (&_CommandsToExecute);
74 copy = access.value();
75 access.value().clear ();
78 // execute all commands in the main thread
79 for (uint i = 0; i < copy.size(); i++)
81 nlassert (Log != NULL);
82 ICommand::execute (copy[i], *Log);
85 return _Continue;
88 uint CWindowDisplayer::createLabel (const char *value)
90 uint pos;
92 CSynchronized<std::vector<CLabelEntry> >::CAccessor access (&_Labels);
93 access.value().push_back (CLabelEntry(value));
94 pos = (uint)access.value().size()-1;
96 return pos;
99 void CWindowDisplayer::setLabel (uint label, const string &value)
102 CSynchronized<std::vector<CLabelEntry> >::CAccessor access (&_Labels);
103 nlassert (label < access.value().size());
104 if (access.value()[label].Value != value)
106 access.value()[label].Value = value;
107 access.value()[label].NeedUpdate = true;
112 void CWindowDisplayer::create (string windowNameEx, bool iconified, sint x, sint y, sint w, sint h, sint hs, sint fs, const std::string &fn, bool ww, CLog *log)
114 nlassert (_Thread == NULL);
115 _Thread = IThread::create (new CUpdateThread(this, windowNameEx, iconified, x, y, w, h, hs, fs, fn, ww, log));
117 Log = log;
119 _Thread->start ();
122 std::string CWindowDisplayer::stringifyMessage(const NLMISC::CLog::TDisplayInfo &args, const char *message, bool needSlashR)
124 bool needSpace = false;
125 //stringstream ss;
126 string str;
128 if (args.LogType != CLog::LOG_NO)
130 str += CWindowDisplayer::logTypeToString(args.LogType);
131 needSpace = true;
134 // Write thread identifier
135 if (args.ThreadId != 0)
137 if (needSpace) { str += " "; needSpace = false; }
138 #ifdef NL_OS_WINDOWS
139 str += NLMISC::toString("%4x", args.ThreadId);
140 #else
141 str += NLMISC::toString("%08x", args.ThreadId);
142 #endif
143 needSpace = true;
146 if (args.FileName != NULL)
148 if (needSpace) { str += " "; needSpace = false; }
149 str += NLMISC::toString("%20s", CFile::getFilename(args.FileName).c_str());
150 needSpace = true;
153 if (args.Line != -1)
155 if (needSpace) { str += " "; needSpace = false; }
156 str += NLMISC::toString("%4u", args.Line);
157 //ss << setw(4) << args.Line;
158 needSpace = true;
161 if (args.FuncName != NULL)
163 if (needSpace) { str += " "; needSpace = false; }
164 str += NLMISC::toString("%20s", args.FuncName);
165 needSpace = true;
168 if (needSpace) { str += ": "; needSpace = false; }
170 uint nbl = 1;
172 char *npos, *pos = const_cast<char *>(message);
173 while ((npos = strchr(pos, '\n')))
175 *npos = '\0';
176 str += pos;
177 if (needSlashR)
178 str += "\r";
179 str += "\n";
180 *npos = '\n';
181 pos = npos + 1;
182 nbl++;
184 str += pos;
186 pos = const_cast<char *>(args.CallstackAndLog.c_str());
187 while ((npos = strchr(pos, '\n')))
189 *npos = '\0';
190 str += pos;
191 if (needSlashR)
192 str += "\r";
193 str += "\n";
194 *npos = '\n';
195 pos = npos + 1;
196 nbl++;
198 str += pos;
200 return str;
203 void CWindowDisplayer::doDisplay (const NLMISC::CLog::TDisplayInfo &args, const char *message)
205 uint32 color = 0xFF000000;
207 if (args.LogType != CLog::LOG_NO)
209 if (args.LogType == CLog::LOG_ERROR || args.LogType == CLog::LOG_ASSERT) color = 0x00FF0000;
210 else if (args.LogType == CLog::LOG_WARNING) color = 0x00800000;
211 else if (args.LogType == CLog::LOG_DEBUG) color = 0x00808080;
212 else color = 0;
215 std::string str = stringifyMessage(args, message, needSlashR);
218 CSynchronized<std::list<std::pair<uint32, std::string> > >::CAccessor access (&_Buffer);
219 if (_HistorySize > 0 && access.value().size() >= (uint)_HistorySize)
221 access.value().erase (access.value().begin());
223 access.value().push_back (make_pair (color, str));
227 } // NLMISC