not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / kfrontend / kconsole.cpp
bloba6c34a9c6ee298954671762e40065335c2264750
1 /*
3 xconsole widget for KDM
5 Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "kconsole.h"
25 #include "kdmconfig.h"
26 #include "kdm_greet.h"
28 #include <klocale.h>
29 #include <kpty.h>
31 #include <QSocketNotifier>
32 #include <QScrollBar>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <sys/ioctl.h>
41 #ifdef HAVE_TERMIOS_H
42 /* for HP-UX (some versions) the extern C is needed, and for other
43 platforms it doesn't hurt */
44 extern "C" {
45 #include <termios.h>
47 #endif
48 #if !defined(__osf__)
49 #ifdef HAVE_TERMIO_H
50 /* needed at least on AIX */
51 #include <termio.h>
52 #endif
53 #endif
55 #if defined (_HPUX_SOURCE)
56 #define _TERMIOS_INCLUDED
57 #include <bsdtty.h>
58 #endif
60 KConsole::KConsole( QWidget *_parent )
61 : inherited( _parent )
62 , pty( 0 )
63 , notifier( 0 )
64 , fd( -1 )
66 setReadOnly( true );
67 setLineWrapMode( NoWrap );
69 if (!openConsole())
70 append( i18n("*** Cannot connect to console log ***") );
73 KConsole::~KConsole()
75 closeConsole();
78 int
79 KConsole::openConsole()
81 #ifdef TIOCCONS
82 static const char on = 1;
83 #endif
85 if (*_logSource) {
86 if ((fd = open( _logSource, O_RDONLY | O_NONBLOCK )) >= 0)
87 goto gotcon;
88 logError( "Cannot open log source %s, "
89 "falling back to /dev/console.\n", _logSource );
92 pty = new KPty;
93 if (!pty->open()) {
94 delete pty;
95 pty = 0;
96 return 0;
99 #ifdef TIOCCONS
100 if (ioctl( pty->slaveFd(), TIOCCONS, &on ) < 0) {
101 perror( "ioctl TIOCCONS" );
102 delete pty;
103 pty = 0;
104 return 0;
106 #else
107 int consfd;
108 if ((consfd = open( "/dev/console", O_RDONLY )) < 0) {
109 perror( "opening /dev/console" );
110 delete pty;
111 pty = 0;
112 return 0;
114 if (ioctl( consfd, SRIOCSREDIR, pty->slaveFd() ) < 0) {
115 perror( "ioctl SRIOCSREDIR" );
116 ::close( consfd );
117 delete pty;
118 pty = 0;
119 return 0;
121 ::close( consfd );
122 #endif
123 fd = pty->masterFd();
125 gotcon:
126 notifier = new QSocketNotifier( fd, QSocketNotifier::Read, this );
127 connect( notifier, SIGNAL(activated( int )), SLOT(slotData()) );
128 return 1;
131 void
132 KConsole::closeConsole()
134 delete notifier;
135 notifier = 0;
136 if (pty) {
137 delete pty;
138 pty = 0;
139 } else
140 ::close( fd );
141 fd = -1;
144 void
145 KConsole::slotData()
147 int n;
148 char buffer[1024];
150 if ((n = read( fd, buffer, sizeof(buffer) )) <= 0) {
151 closeConsole();
152 if (n || !openConsole())
153 append( i18n("\n*** Lost connection with console log ***") );
154 } else {
155 QString str( QString::fromLocal8Bit( buffer, n ).remove( '\r' ) );
156 int pos, opos;
157 for (opos = 0; (pos = str.indexOf( '\n', opos )) >= 0; opos = pos + 1) {
158 if (document()->blockCount() == 100) {
159 QTextCursor cur( document() );
160 cur.movePosition( QTextCursor::NextBlock, QTextCursor::KeepAnchor );
161 cur.removeSelectedText();
163 if (!leftover.isEmpty()) {
164 append( leftover + str.mid( opos, pos - opos ) );
165 leftover.clear();
166 } else
167 append( str.mid( opos, pos - opos ) );
169 leftover += str.mid( opos );
173 #include "kconsole.moc"