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 = "";
76 qInstallMsgHandler( qMessageHandler );
78 // Copy command line arguments for QApplication
80 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
83 for ( int i=0; i < argc; i++ )
84 _ui_argv[i+1] = strdup( argv[i] );
86 _ui_argv[0] = strdup( "YaST2" );
89 topmostConstructorHasFinished();
98 Qt thinks the first QObject defines the main thread, but
99 the main thread in this case is the ycp thread, not UI
101 //extern void qt_set_current_thread_to_main_thread();
102 //qt_set_current_thread_to_main_thread();
106 new QApplication( _ui_argc, _ui_argv);
107 _busy_cursor_timer = new QTimer( qApp );
108 _busy_cursor_timer->setSingleShot( true );
110 _user_input_timer.setSingleShot( true );
112 _normalPalette = qApp->palette();
114 // Qt keeps track to a global QApplication in qApp.
117 qApp->installEventFilter( this );
118 processCommandLineArgs( _ui_argc, _ui_argv );
121 // Event loop object. Required since a YaST2 UI needs to react to commands
122 // from the YCP command stream as well as to X11 / Qt events.
123 _eventLoop = new QEventLoop( qApp );
124 _do_exit_loop = false;
126 // Create main window for `opt(`defaultsize) dialogs.
128 // We have to use something else than QWidgetStack since QWidgetStack
129 // doesn't accept a WFlags arg which we badly need here.
131 Qt::WFlags wflags = Qt::Window;
133 if ( ! _decorate_toplevel_window )
135 y2debug( "Suppressing WM decorations for toplevel window" );
136 wflags |= Qt::FramelessWindowHint;
139 _main_win = new QWidget( 0, 0, wflags ); // parent, name, wflags
140 _main_win->setFocusPolicy( Qt::StrongFocus );
142 // Create widget stack for `opt(`defaultsize) dialogs
143 QVBoxLayout *vbox = new QVBoxLayout( _main_win );
144 vbox->setSpacing( 0 );
145 vbox->setMargin( 0 );
147 _widget_stack = new QStackedWidget( _main_win );
148 _widget_stack->setFocusPolicy( Qt::StrongFocus );
149 vbox->addWidget( _widget_stack );
151 _main_win->installEventFilter( this );
154 _main_win->resize( _default_size );
156 if ( _fullscreen || ! _have_wm )
157 _main_win->move( 0, 0 );
162 QString title( "YaST2" );
163 char hostname[ MAXHOSTNAMELEN+1 ];
165 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
167 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
169 if ( strlen( hostname ) > 0 )
171 if ( ( strcmp( hostname, "(none)" ) != 0 &&
172 strcmp( hostname, "linux" ) != 0 ) )
180 _main_win->setWindowTitle( title );
183 // Hide the main window for now. The first call to UI::OpenDialog() on an
184 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
185 // the main window - there is nothing to display yet.
190 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
191 // if no Qt style defined):
193 // Qt does not seem to be designed for use in plugin libs. It loads some
194 // add-on libs dynamically with dlopen() and unloads them at program exit
195 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
196 // lib (libqt-mt) themselves, when they are unloading the last call to
197 // dlclose() for them causes the last reference to libqt-mt to vanish as
198 // well. Since libqt-mt is already in the process of destruction there is
199 // no more reference from the caller of libqt-mt, and the GLIBC decides
200 // that libqt-mt is not needed any more (zero references) and unmaps
201 // libqt-mt. When the static destructor of libqt-mt that triggered the
202 // cleanup in QGPluginManager returns, the code it is to return to is
203 // already unmapped, causing a segfault.
205 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
206 // and make sure there is no corresponding dlclose().
208 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
209 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
210 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
211 qt_lib ? "successful" : "failed" );
215 qApp->setFont( yqApp()->currentFont() );
218 connect( & _user_input_timer, SIGNAL( timeout() ),
219 this, SLOT ( userInputTimeout() ) );
221 connect( _busy_cursor_timer, SIGNAL( timeout() ),
222 this, SLOT ( busyCursor() ) );
226 // playMacro( macro_file );
228 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
229 qApp->processEvents();
236 return static_cast<YQApplication *>( app() );
240 void YQUI::processCommandLineArgs( int argc, char **argv )
244 for( int i=0; i < argc; i++ )
246 QString opt = argv[i];
248 y2milestone ("Qt argument: %s", argv[i]);
250 // Normalize command line option - accept "--xy" as well as "-xy"
252 if ( opt.startsWith( "--" ) )
255 if ( opt == QString( "-no-wm" ) ) _have_wm = false;
256 else if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
257 else if ( opt == QString( "-noborder" ) ) _decorate_toplevel_window = false;
258 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
259 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
260 // --macro is handled by YUI_component
261 else if ( opt == QString( "-help" ) )
264 "Command line options for the YaST2 Qt UI:\n"
266 "--nothreads run without additional UI threads\n"
267 "--no-wm assume no window manager is running\n"
268 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
269 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
270 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
271 "--help this help text\n"
273 "--macro <macro-file> play a macro right on startup\n"
275 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
276 "to maintain backwards compatibility.\n"
285 // Qt handles command line option "-reverse" for Arabic / Hebrew
292 y2debug("Closing down Qt UI.");
296 // Intentionally NOT calling dlclose() to libqt-mt
297 // (see constructor for explanation)
303 YQUI::createWidgetFactory()
305 YQWidgetFactory * factory = new YQWidgetFactory();
306 YUI_CHECK_NEW( factory );
313 YOptionalWidgetFactory *
314 YQUI::createOptionalWidgetFactory()
316 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
317 YUI_CHECK_NEW( factory );
324 YQUI::createApplication()
326 YQApplication * app = new YQApplication();
327 YUI_CHECK_NEW( app );
333 void YQUI::calcDefaultSize()
335 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
336 QSize availableSize = qApp->desktop()->availableGeometry().size();
340 _default_size = availableSize;
342 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
343 _default_size.width(), _default_size.height() );
347 // Get _default_size via -geometry command line option (if set)
349 // NOTE not needed in qt4
350 // QWidget * dummy = new QWidget();
352 // qApp->setMainWidget( dummy );
353 // _default_size = dummy->size();
356 // Set min defaultsize or figure one out if -geometry was not used
358 if ( _default_size.width() < 800 ||
359 _default_size.height() < 600 )
361 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
363 // Scale down to 70% of screen size
365 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
366 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
370 _default_size = availableSize;
375 y2milestone( "Forced size (via -geometry): %dx%d",
376 _default_size.width(), _default_size.height() );
381 _default_size = primaryScreenSize;
385 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
390 void YQUI::internalError( const char * msg )
393 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
394 QMessageBox::Abort | QMessageBox::Default,
398 if ( button == QMessageBox::Abort )
403 // exit() leaves a process running (WFM?), so this really seems to be
404 // the only way to make sure we are really going down.
408 void YQUI::idleLoop( int fd_ycp )
412 // runs in main thread
413 _eventLoop->wakeUp();
418 FD_SET(fd_ycp, &rfds);
422 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
429 void YQUI::sendEvent( YEvent * event )
433 _event_handler.sendEvent( event );
436 _eventLoop->exit( 1 );
441 YEvent * YQUI::userInput( unsigned long timeout_millisec )
445 // y2milestone( "userInput %ld", timeout_millisec );
448 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
450 if ( _user_input_timer.isActive() )
451 _user_input_timer.stop();
455 if ( timeout_millisec > 0 )
456 _user_input_timer.start( timeout_millisec ); // single shot
458 dialog->activate( true );
460 if ( qApp->focusWidget() )
461 qApp->focusWidget()->setFocus();
464 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
466 while ( ! pendingEvent() )
468 _eventLoop->processEvents(QEventLoop::WaitForMoreEvents);
471 _do_exit_loop = false;
473 event = _event_handler.consumePendingEvent();
474 dialog->activate( false );
476 // Display a busy cursor, but only if there is no other activity within
477 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
479 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
482 if ( _user_input_timer.isActive() )
483 _user_input_timer.stop();
489 YEvent * YQUI::pollInput()
493 if ( _user_input_timer.isActive() )
494 _user_input_timer.stop();
496 if ( ! pendingEvent() )
498 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
502 dialog->activate( true );
503 //qApp->processEvents();
504 event = _event_handler.consumePendingEvent();
505 dialog->activate( false );
509 if ( pendingEvent() )
510 event = _event_handler.consumePendingEvent();
516 void YQUI::userInputTimeout()
518 if ( ! pendingEvent() )
519 sendEvent( new YTimeoutEvent() );
525 YDialog * YQUI::createDialog( YWidgetOpt & opt )
529 y2milestone( "createDialog %ld", QThread::currentThreadId () );
530 bool has_defaultsize = opt.hasDefaultSize.value();
531 QWidget * qt_parent =
532 has_defaultsize ? _widget_stack : _main_win;
534 // FIXME: Probably obsolete
535 if ( ! has_defaultsize && ! _popup_stack.empty() )
536 qt_parent = _popup_stack.back();
538 YQDialog * dialog = new YQDialog( opt, qt_parent, has_defaultsize );
539 Q_CHECK_PTR( dialog );
541 if ( ! has_defaultsize )
542 _popup_stack.push_back( (QWidget *) dialog->widgetRep() );
549 void YQUI::showDialog( YDialog * dialog )
551 QWidget * qw = (QWidget *) dialog->widgetRep();
555 y2error( "No widgetRep() for dialog" );
559 if ( dialog->dialogType() == YMainDialog )
561 _widget_stack->addWidget ( qw );
562 _widget_stack->setCurrentWidget( qw );
564 if ( ! _main_win->isVisible() )
566 // y2milestone( "Showing main window" );
567 _main_win->resize( _default_size );
570 _main_win->move( 0, 0 );
576 else // non-defaultsize dialog
581 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
583 //qApp->processEvents();
587 void YQUI::closeDialog( YDialog * dialog )
589 QWidget * qw = (QWidget *) dialog->widgetRep();
593 y2error( "No widgetRep() for dialog" );
597 if ( dialog->dialogType() == YMainDialog )
599 _widget_stack->removeWidget( qw );
601 else // non-defaultsize dialog
607 // Clean up the popup stack. libyui guarantees that a dialog will be
608 // deleted after closeDialog() so it is safe to pop that dialog from
609 // the popup stack here.
611 if ( ! _popup_stack.empty() && _popup_stack.back() == qw )
612 _popup_stack.pop_back();
614 y2error( "Popup dialog stack corrupted!" );
620 void YQUI::easterEgg()
622 y2milestone( "Starting easter egg..." );
625 YQEasterBunny::layEgg();
626 y2milestone( "Done." );
629 // desktop()->repaint() has no effect - we need to do it the hard way.
630 system( "/usr/X11R6/bin/xrefresh" );
635 QString YQUI::productName() const
637 return fromUTF8( YUI::productName() );
642 YQUI::setTextdomain( const char * domain )
644 bindtextdomain( domain, LOCALEDIR );
645 bind_textdomain_codeset( domain, "utf8" );
646 textdomain( domain );
648 // Make change known.
650 extern int _nl_msg_cat_cntr;
658 qMessageHandler( QtMsgType type, const char * msg )
663 y2milestone ("qt-debug: %s\n", msg);
666 y2warning ("qt-warning: %s\n", msg);
670 y2warning ("qt-critical: %s\n", msg);
673 y2internal ("qt-fatal: %s\n", msg);
674 exit (1); // qt does the same