]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/console.cpp
Merge branch 'flake' into 'master'
[xonotic/netradiant.git] / radiant / console.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant 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 General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "console.h"
23
24 #include <time.h>
25 #include <uilib/uilib.h>
26 #include <gtk/gtk.h>
27 #include <mutex>
28
29 #include "gtkutil/accelerator.h"
30 #include "gtkutil/messagebox.h"
31 #include "gtkutil/container.h"
32 #include "gtkutil/menu.h"
33 #include "gtkutil/nonmodal.h"
34 #include "stream/stringstream.h"
35 #include "convert.h"
36
37 #include "aboutmsg.h"
38 #include "gtkmisc.h"
39 #include "mainframe.h"
40
41 // handle to the console log file
42 namespace
43 {
44 FILE* g_hLogFile;
45 }
46
47 bool g_Console_enableLogging = false;
48
49 struct Gtk_Idle_Print_Data {
50         int level;
51         char *buf;
52         std::size_t length;
53         bool contains_newline;
54 };
55
56 // called whenever we need to open/close/check the console log file
57 void Sys_EnableLogFile( bool enable ){
58         if ( enable && !g_hLogFile ) {
59                 // settings say we should be logging and we don't have a log file .. so create it
60                 if ( !SettingsPath_get()[0] ) {
61                         return; // cannot open a log file yet
62                 }
63                 // open a file to log the console (if user prefs say so)
64                 // the file handle is g_hLogFile
65                 // the log file is erased
66                 StringOutputStream name( 256 );
67                 name << SettingsPath_get() << "radiant.log";
68                 g_hLogFile = fopen( name.c_str(), "w" );
69                 if ( g_hLogFile != 0 ) {
70                         globalOutputStream() << "Started logging to " << name.c_str() << "\n";
71                         time_t localtime;
72                         time( &localtime );
73                         globalOutputStream() << "Today is: " << ctime( &localtime )
74                                                                  << "This is " RADIANT_NAME " " RADIANT_VERSION " compiled " __DATE__ "\n" RADIANT_ABOUTMSG "\n";
75                 }
76                 else{
77                         ui::alert( ui::root, "Failed to create log file, check write permissions in " RADIANT_NAME " directory.\n",
78                                                         "Console logging", ui::alert_type::OK, ui::alert_icon::Error );
79                 }
80         }
81         else if ( !enable && g_hLogFile != 0 ) {
82                 // settings say we should not be logging but still we have an active logfile .. close it
83                 time_t localtime;
84                 time( &localtime );
85                 globalOutputStream() << "Closing log file at " << ctime( &localtime ) << "\n";
86                 fclose( g_hLogFile );
87                 g_hLogFile = 0;
88         }
89 }
90
91 ui::TextView g_console{ui::null};
92
93 void console_clear(){
94         g_console.text("");
95 }
96
97 void console_populate_popup( ui::TextView textview, ui::Menu menu, gpointer user_data ){
98         menu_separator( menu );
99
100         ui::Widget item(ui::MenuItem( "Clear" ));
101         item.connect( "activate", G_CALLBACK( console_clear ), 0 );
102         item.show();
103         menu.add(item);
104 }
105
106 gboolean destroy_set_null( ui::Window widget, ui::Widget* p ){
107         *p = ui::Widget{ui::null};
108         return FALSE;
109 }
110
111 WidgetFocusPrinter g_consoleWidgetFocusPrinter( "console" );
112
113 ui::Widget Console_constructWindow( ui::Window toplevel ){
114         auto scr = ui::ScrolledWindow(ui::New);
115         scr.overflow(ui::Policy::AUTOMATIC, ui::Policy::AUTOMATIC);
116         gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
117         scr.show();
118
119         {
120                 auto text = ui::TextView(ui::New);
121                 text.dimensions(0, -1); // allow shrinking
122                 gtk_text_view_set_wrap_mode( text, GTK_WRAP_WORD );
123                 gtk_text_view_set_editable( text, FALSE );
124                 scr.add(text);
125                 text.show();
126                 g_console = text;
127
128                 //globalExtendedASCIICharacterSet().print();
129
130                 widget_connect_escape_clear_focus_widget( g_console );
131
132                 //g_consoleWidgetFocusPrinter.connect(g_console);
133
134                 g_console.connect( "populate-popup", G_CALLBACK( console_populate_popup ), 0 );
135                 g_console.connect( "destroy", G_CALLBACK( destroy_set_null ), &g_console );
136         }
137
138         gtk_container_set_focus_chain( GTK_CONTAINER( scr ), NULL );
139
140         return scr;
141 }
142
143 class GtkTextBufferOutputStream : public TextOutputStream
144 {
145 GtkTextBuffer* textBuffer;
146 GtkTextIter* iter;
147 GtkTextTag* tag;
148 public:
149 GtkTextBufferOutputStream( GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag ) : textBuffer( textBuffer ), iter( iter ), tag( tag ){
150 }
151 std::size_t write( const char* buffer, std::size_t length ){
152         gtk_text_buffer_insert_with_tags( textBuffer, iter, buffer, gint( length ), tag, NULL );
153         return length;
154 }
155 };
156
157 // This function is meant to be used with gtk_idle_add. It will free its argument.
158 static gboolean Gtk_Idle_Print( gpointer data ){
159         Gtk_Idle_Print_Data *args = reinterpret_cast<Gtk_Idle_Print_Data *>(data);
160         g_assert(g_console);
161
162         auto buffer = gtk_text_view_get_buffer( g_console );
163
164         GtkTextIter iter;
165         gtk_text_buffer_get_end_iter( buffer, &iter );
166
167         static auto end = gtk_text_buffer_create_mark( buffer, "end", &iter, FALSE );
168
169         const GdkColor yellow = { 0, 0xb0ff, 0xb0ff, 0x0000 };
170         const GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
171
172         static auto error_tag = gtk_text_buffer_create_tag( buffer, "red_foreground", "foreground-gdk", &red, NULL );
173         static auto warning_tag = gtk_text_buffer_create_tag( buffer, "yellow_foreground", "foreground-gdk", &yellow, NULL );
174         static auto standard_tag = gtk_text_buffer_create_tag( buffer, "black_foreground", NULL );
175         GtkTextTag* tag;
176         switch ( args->level )
177         {
178         case SYS_WRN:
179                 tag = warning_tag;
180                 break;
181         case SYS_ERR:
182                 tag = error_tag;
183                 break;
184         case SYS_STD:
185         case SYS_VRB:
186         default:
187                 tag = standard_tag;
188                 break;
189         }
190
191         {
192                 GtkTextBufferOutputStream textBuffer( buffer, &iter, tag );
193                 if ( !globalCharacterSet().isUTF8() ) {
194                         BufferedTextOutputStream<GtkTextBufferOutputStream> buffered( textBuffer );
195                         buffered << StringRange( args->buf, args->buf + args->length );
196                 }
197                 else
198                 {
199                         textBuffer << StringRange( args->buf, args->buf + args->length );
200                 }
201         }
202
203         // update console widget immediatly if we're doing something time-consuming
204         if ( args->contains_newline ) {
205                 gtk_text_view_scroll_mark_onscreen( g_console, end );
206
207                 if ( !ScreenUpdates_Enabled() && gtk_widget_get_realized( g_console ) ) {
208                         ScreenUpdates_process();
209                 }
210         }
211
212         free( args->buf );
213         free( args );
214
215         return FALSE; // call this once, not repeatedly
216 }
217
218 // Print logs to the in-game console and/or to the log file.
219 // This function is thread safe.
220 std::size_t Sys_Print( int level, const char* buf, std::size_t length ){
221         bool contains_newline = std::find( buf, buf + length, '\n' ) != buf + length;
222
223         if ( level == SYS_ERR ) {
224                 Sys_EnableLogFile( true );
225         }
226
227         if ( g_hLogFile != 0 ) {
228                 // prevent parallel write
229                 static std::mutex log_file_mutex;
230                 std::lock_guard<std::mutex> guard(log_file_mutex);
231
232                 fwrite( buf, 1, length, g_hLogFile );
233                 if ( contains_newline ) {
234                         fflush( g_hLogFile );
235                 }
236         }
237
238         if ( level != SYS_NOCON && g_console ) {
239                 auto data = reinterpret_cast<Gtk_Idle_Print_Data *>( malloc( sizeof(struct Gtk_Idle_Print_Data) ) );
240                 if (data != nullptr) {
241                         *data = { level, g_strndup(buf, length), length, contains_newline };
242                         gdk_threads_add_idle(Gtk_Idle_Print, (gpointer)data);
243                 }
244         }
245
246         return length;
247 }
248
249
250 class SysPrintOutputStream : public TextOutputStream
251 {
252 public:
253 std::size_t write( const char* buffer, std::size_t length ){
254         return Sys_Print( SYS_STD, buffer, length );
255 }
256 };
257
258 class SysPrintErrorStream : public TextOutputStream
259 {
260 public:
261 std::size_t write( const char* buffer, std::size_t length ){
262         return Sys_Print( SYS_ERR, buffer, length );
263 }
264 };
265
266 SysPrintOutputStream g_outputStream;
267
268 TextOutputStream& getSysPrintOutputStream(){
269         return g_outputStream;
270 }
271
272 SysPrintErrorStream g_errorStream;
273
274 TextOutputStream& getSysPrintErrorStream(){
275         return g_errorStream;
276 }