1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
19 #include <rpc/types.h> // MAXHOSTNAMELEN
24 #include <QMessageBox>
25 #include <QSocketNotifier>
26 #include <QStackedWidget>
27 #include <QDesktopWidget>
29 #include <QVBoxLayout>
31 #include <ycp/YCPTerm.h>
32 #include <ycp/YCPCode.h>
34 #define y2log_component "qt-ui"
35 #include <ycp/y2log.h>
38 #include "YQApplication.h"
39 #include "YQWidgetFactory.h"
40 #include "YQOptionalWidgetFactory.h"
42 #include "YUISymbols.h"
47 #include "QY2Settings.h"
50 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
54 static void qMessageHandler( QtMsgType type, const char * msg );
56 YQUI::YQUI( int argc, char **argv, bool with_threads, const char * macro_file )
61 , _wm_close_blocked( false )
62 , _auto_activate_dialogs( true )
64 y2milestone( "YQUI constructor start" );
70 _decorate_toplevel_window = true;
71 _usingVisionImpairedPalette = false;
72 _leftHandedMouse = false;
73 _askedForLeftHandedMouse = false;
74 screenShotNameTemplate = "";
77 qInstallMsgHandler( qMessageHandler );
79 // Copy command line arguments for QApplication
81 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
84 for ( int i=0; i < argc; i++ )
85 _ui_argv[i+1] = strdup( argv[i] );
87 _ui_argv[0] = strdup( "YaST2" );
90 topmostConstructorHasFinished();
99 Qt thinks the first QObject defines the main thread, but
100 the main thread in this case is the ycp thread, not UI
102 //extern void qt_set_current_thread_to_main_thread();
103 //qt_set_current_thread_to_main_thread();
107 new QApplication( _ui_argc, _ui_argv);
108 _busy_cursor_timer = new QTimer( qApp );
109 _busy_cursor_timer->setSingleShot( true );
111 _user_input_timer.setSingleShot( true );
113 _normalPalette = qApp->palette();
115 // Qt keeps track to a global QApplication in qApp.
118 qApp->installEventFilter( this );
119 processCommandLineArgs( _ui_argc, _ui_argv );
122 // Event loop object. Required since a YaST2 UI needs to react to commands
123 // from the YCP command stream as well as to X11 / Qt events.
124 _eventLoop = new QEventLoop( qApp );
125 _do_exit_loop = false;
127 // Create main window for `opt(`defaultsize) dialogs.
129 // We have to use something else than QWidgetStack since QWidgetStack
130 // doesn't accept a WFlags arg which we badly need here.
132 Qt::WFlags wflags = Qt::Window;
134 if ( ! _decorate_toplevel_window )
136 y2debug( "Suppressing WM decorations for toplevel window" );
137 wflags |= Qt::FramelessWindowHint;
140 _main_win = new QWidget( 0, wflags ); // parent, wflags
141 _main_win->setFocusPolicy( Qt::StrongFocus );
142 _main_win->setObjectName( "main_window" );
144 // Create widget stack for `opt(`defaultsize) dialogs
145 QVBoxLayout *vbox = new QVBoxLayout( _main_win );
146 vbox->setSpacing( 0 );
147 vbox->setMargin( 0 );
149 _widget_stack = new QStackedWidget( _main_win );
150 _widget_stack->setFocusPolicy( Qt::StrongFocus );
151 vbox->addWidget( _widget_stack );
153 _main_win->installEventFilter( this );
156 //_main_win->resize( _default_size );
157 _main_win->setFixedSize( 1024, 768 );
159 if ( _fullscreen || ! _have_wm )
160 _main_win->move( 0, 0 );
165 QString title( "YaST2" );
166 char hostname[ MAXHOSTNAMELEN+1 ];
168 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
170 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
172 if ( strlen( hostname ) > 0 )
174 if ( ( strcmp( hostname, "(none)" ) != 0 &&
175 strcmp( hostname, "linux" ) != 0 ) )
183 _main_win->setWindowTitle( title );
186 // Hide the main window for now. The first call to UI::OpenDialog() on an
187 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
188 // the main window - there is nothing to display yet.
193 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
194 // if no Qt style defined):
196 // Qt does not seem to be designed for use in plugin libs. It loads some
197 // add-on libs dynamically with dlopen() and unloads them at program exit
198 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
199 // lib (libqt-mt) themselves, when they are unloading the last call to
200 // dlclose() for them causes the last reference to libqt-mt to vanish as
201 // well. Since libqt-mt is already in the process of destruction there is
202 // no more reference from the caller of libqt-mt, and the GLIBC decides
203 // that libqt-mt is not needed any more (zero references) and unmaps
204 // libqt-mt. When the static destructor of libqt-mt that triggered the
205 // cleanup in QGPluginManager returns, the code it is to return to is
206 // already unmapped, causing a segfault.
208 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
209 // and make sure there is no corresponding dlclose().
211 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
212 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
213 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
214 qt_lib ? "successful" : "failed" );
218 qApp->setFont( yqApp()->currentFont() );
221 connect( & _user_input_timer, SIGNAL( timeout() ),
222 this, SLOT ( userInputTimeout() ) );
224 connect( _busy_cursor_timer, SIGNAL( timeout() ),
225 this, SLOT ( busyCursor() ) );
229 // playMacro( macro_file );
231 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
232 qApp->processEvents();
239 return static_cast<YQApplication *>( app() );
243 void YQUI::processCommandLineArgs( int argc, char **argv )
247 for( int i=0; i < argc; i++ )
249 QString opt = argv[i];
251 y2milestone ("Qt argument: %s", argv[i]);
253 // Normalize command line option - accept "--xy" as well as "-xy"
255 if ( opt.startsWith( "--" ) )
258 if ( opt == QString( "-no-wm" ) ) _have_wm = false;
259 else if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
260 else if ( opt == QString( "-noborder" ) ) _decorate_toplevel_window = false;
261 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
262 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
263 // --macro is handled by YUI_component
264 else if ( opt == QString( "-help" ) )
267 "Command line options for the YaST2 Qt UI:\n"
269 "--nothreads run without additional UI threads\n"
270 "--no-wm assume no window manager is running\n"
271 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
272 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
273 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
274 "--help this help text\n"
276 "--macro <macro-file> play a macro right on startup\n"
278 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
279 "to maintain backwards compatibility.\n"
288 // Qt handles command line option "-reverse" for Arabic / Hebrew
295 y2debug("Closing down Qt UI.");
299 // Intentionally NOT calling dlclose() to libqt-mt
300 // (see constructor for explanation)
306 YQUI::createWidgetFactory()
308 YQWidgetFactory * factory = new YQWidgetFactory();
309 YUI_CHECK_NEW( factory );
316 YOptionalWidgetFactory *
317 YQUI::createOptionalWidgetFactory()
319 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
320 YUI_CHECK_NEW( factory );
327 YQUI::createApplication()
329 YQApplication * app = new YQApplication();
330 YUI_CHECK_NEW( app );
336 void YQUI::calcDefaultSize()
338 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
339 QSize availableSize = qApp->desktop()->availableGeometry().size();
343 _default_size = availableSize;
345 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
346 _default_size.width(), _default_size.height() );
350 // Get _default_size via -geometry command line option (if set)
352 // NOTE not needed in qt4
353 // QWidget * dummy = new QWidget();
355 // qApp->setMainWidget( dummy );
356 // _default_size = dummy->size();
359 // Set min defaultsize or figure one out if -geometry was not used
361 if ( _default_size.width() < 800 ||
362 _default_size.height() < 600 )
364 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
366 // Scale down to 70% of screen size
368 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
369 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
373 _default_size = availableSize;
378 y2milestone( "Forced size (via -geometry): %dx%d",
379 _default_size.width(), _default_size.height() );
384 _default_size = primaryScreenSize;
388 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
393 void YQUI::internalError( const char * msg )
396 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
397 QMessageBox::Abort | QMessageBox::Default,
401 if ( button == QMessageBox::Abort )
406 // exit() leaves a process running (WFM?), so this really seems to be
407 // the only way to make sure we are really going down.
411 void YQUI::idleLoop( int fd_ycp )
415 // runs in main thread
416 _eventLoop->wakeUp();
421 FD_SET(fd_ycp, &rfds);
425 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
432 void YQUI::sendEvent( YEvent * event )
436 _event_handler.sendEvent( event );
439 _eventLoop->exit( 1 );
444 YEvent * YQUI::userInput( unsigned long timeout_millisec )
448 _event_handler.blockEvents( false );
449 _eventLoop->wakeUp();
452 //y2milestone( "userInput %ld", timeout_millisec );
455 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
457 if ( _user_input_timer.isActive() )
458 _user_input_timer.stop();
462 if ( timeout_millisec > 0 )
463 _user_input_timer.start( timeout_millisec ); // single shot
465 dialog->activate( true );
467 if ( qApp->focusWidget() )
468 qApp->focusWidget()->setFocus();
471 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
473 _do_exit_loop = false;
475 event = _event_handler.consumePendingEvent();
476 dialog->activate( false );
478 // Display a busy cursor, but only if there is no other activity within
479 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
481 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
484 if ( _user_input_timer.isActive() )
485 _user_input_timer.stop();
491 YEvent * YQUI::pollInput()
495 if ( _user_input_timer.isActive() )
496 _user_input_timer.stop();
498 if ( ! pendingEvent() )
500 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
504 dialog->activate( true );
505 //qApp->processEvents();
506 event = _event_handler.consumePendingEvent();
507 dialog->activate( false );
511 if ( pendingEvent() )
512 event = _event_handler.consumePendingEvent();
518 void YQUI::userInputTimeout()
520 if ( ! pendingEvent() )
521 sendEvent( new YTimeoutEvent() );
527 YDialog * YQUI::createDialog( YWidgetOpt & opt )
531 y2milestone( "createDialog %ld", QThread::currentThreadId () );
532 bool has_defaultsize = opt.hasDefaultSize.value();
533 QWidget * qt_parent =
534 has_defaultsize ? _widget_stack : _main_win;
536 // FIXME: Probably obsolete
537 if ( ! has_defaultsize && ! _popup_stack.empty() )
538 qt_parent = _popup_stack.back();
540 YQDialog * dialog = new YQDialog( opt, qt_parent, has_defaultsize );
541 Q_CHECK_PTR( dialog );
543 if ( ! has_defaultsize )
544 _popup_stack.push_back( (QWidget *) dialog->widgetRep() );
551 void YQUI::showDialog( YDialog * dialog )
553 QWidget * qw = (QWidget *) dialog->widgetRep();
557 y2error( "No widgetRep() for dialog" );
561 if ( dialog->dialogType() == YMainDialog )
563 _widget_stack->addWidget ( qw );
564 _widget_stack->setCurrentWidget( qw );
566 if ( ! _main_win->isVisible() )
568 // y2milestone( "Showing main window" );
569 _main_win->resize( _default_size );
572 _main_win->move( 0, 0 );
578 else // non-defaultsize dialog
583 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
585 //qApp->processEvents();
589 void YQUI::closeDialog( YDialog * dialog )
591 QWidget * qw = (QWidget *) dialog->widgetRep();
595 y2error( "No widgetRep() for dialog" );
599 if ( dialog->dialogType() == YMainDialog )
601 _widget_stack->removeWidget( qw );
603 else // non-defaultsize dialog
609 // Clean up the popup stack. libyui guarantees that a dialog will be
610 // deleted after closeDialog() so it is safe to pop that dialog from
611 // the popup stack here.
613 if ( ! _popup_stack.empty() && _popup_stack.back() == qw )
614 _popup_stack.pop_back();
616 y2error( "Popup dialog stack corrupted!" );
622 void YQUI::easterEgg()
624 y2milestone( "Starting easter egg..." );
627 YQEasterBunny::layEgg();
628 y2milestone( "Done." );
631 // desktop()->repaint() has no effect - we need to do it the hard way.
632 system( "/usr/X11R6/bin/xrefresh" );
637 QString YQUI::productName() const
639 return fromUTF8( YUI::productName() );
644 YQUI::setTextdomain( const char * domain )
646 bindtextdomain( domain, LOCALEDIR );
647 bind_textdomain_codeset( domain, "utf8" );
648 textdomain( domain );
650 // Make change known.
652 extern int _nl_msg_cat_cntr;
658 void YQUI::blockEvents( bool block )
662 if ( ++blocked_level == 1 )
664 _event_handler.blockEvents( true );
670 if ( --blocked_level == 0 )
672 _event_handler.blockEvents( false );
673 _eventLoop->wakeUp();
679 bool YQUI::eventsBlocked() const
681 return _event_handler.eventsBlocked();
685 qMessageHandler( QtMsgType type, const char * msg )
690 y2milestone ("qt-debug: %s\n", msg);
693 y2warning ("qt-warning: %s\n", msg);
697 y2warning ("qt-critical: %s\n", msg);
700 y2internal ("qt-fatal: %s\n", msg);
701 exit (1); // qt does the same