1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
19 #include <rpc/types.h> // MAXHOSTNAMELEN
24 #include <qmessagebox.h>
25 #include <Qt/qsocketnotifier.h>
26 #include <QStackedWidget>
27 #include <QDesktopWidget>
30 #include <ycp/YCPTerm.h>
31 #include <ycp/YCPCode.h>
33 #define y2log_component "qt-ui"
34 #include <ycp/y2log.h>
37 #include "YQApplication.h"
38 #include "YQWidgetFactory.h"
39 #include "YQOptionalWidgetFactory.h"
41 #include "YUISymbols.h"
46 #include "QY2Settings.h"
49 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
53 static void qMessageHandler( QtMsgType type, const char * msg );
55 YQUI::YQUI( int argc, char **argv, bool with_threads, const char * macro_file )
60 , _wm_close_blocked( false )
61 , _auto_activate_dialogs( true )
63 y2milestone( "YQUI constructor start" );
69 _decorate_toplevel_window = true;
70 _usingVisionImpairedPalette = false;
71 _leftHandedMouse = false;
72 _askedForLeftHandedMouse = false;
73 screenShotNameTemplate = "";
75 qInstallMsgHandler( qMessageHandler );
77 // Copy command line arguments for QApplication
79 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
82 for ( int i=0; i < argc; i++ )
83 _ui_argv[i+1] = strdup( argv[i] );
85 _ui_argv[0] = strdup( "YaST2" );
88 topmostConstructorHasFinished();
97 Qt thinks the first QObject defines the main thread, but
98 the main thread in this case is the ycp thread, not UI
100 //extern void qt_set_current_thread_to_main_thread();
101 //qt_set_current_thread_to_main_thread();
105 new QApplication( _ui_argc, _ui_argv);
106 _busy_cursor_timer = new QTimer( qApp );
107 _busy_cursor_timer->setSingleShot( true );
109 _user_input_timer.setSingleShot( true );
111 _normalPalette = qApp->palette();
113 // Qt keeps track to a global QApplication in qApp.
116 qApp->installEventFilter( this );
117 processCommandLineArgs( _ui_argc, _ui_argv );
120 // Event loop object. Required since a YaST2 UI needs to react to commands
121 // from the YCP command stream as well as to X11 / Qt events.
122 _eventLoop = new QEventLoop( qApp );
123 _do_exit_loop = false;
125 // Create main window for `opt(`defaultsize) dialogs.
127 // We have to use something else than QWidgetStack since QWidgetStack
128 // doesn't accept a WFlags arg which we badly need here.
130 Qt::WFlags wflags = Qt::Window;
132 if ( ! _decorate_toplevel_window )
134 y2debug( "Suppressing WM decorations for toplevel window" );
135 wflags |= Qt::FramelessWindowHint;
138 _main_win = new Q3VBox( 0, 0, wflags ); // parent, name, wflags
139 _main_win->setFocusPolicy( Qt::StrongFocus );
141 // Create widget stack for `opt(`defaultsize) dialogs
143 _widget_stack = new QStackedWidget( _main_win );
144 _widget_stack->setFocusPolicy( Qt::StrongFocus );
146 _main_win->installEventFilter( this );
149 _main_win->resize( _default_size );
151 if ( _fullscreen || ! _have_wm )
152 _main_win->move( 0, 0 );
157 QString title( "YaST2" );
158 char hostname[ MAXHOSTNAMELEN+1 ];
160 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
162 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
164 if ( strlen( hostname ) > 0 )
166 if ( ( strcmp( hostname, "(none)" ) != 0 &&
167 strcmp( hostname, "linux" ) != 0 ) )
175 _main_win->setCaption( title );
178 // Hide the main window for now. The first call to UI::OpenDialog() on an
179 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
180 // the main window - there is nothing to display yet.
185 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
186 // if no Qt style defined):
188 // Qt does not seem to be designed for use in plugin libs. It loads some
189 // add-on libs dynamically with dlopen() and unloads them at program exit
190 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
191 // lib (libqt-mt) themselves, when they are unloading the last call to
192 // dlclose() for them causes the last reference to libqt-mt to vanish as
193 // well. Since libqt-mt is already in the process of destruction there is
194 // no more reference from the caller of libqt-mt, and the GLIBC decides
195 // that libqt-mt is not needed any more (zero references) and unmaps
196 // libqt-mt. When the static destructor of libqt-mt that triggered the
197 // cleanup in QGPluginManager returns, the code it is to return to is
198 // already unmapped, causing a segfault.
200 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
201 // and make sure there is no corresponding dlclose().
203 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
204 void * qt_lib = dlopen( qt_lib_name.utf8().constData(), RTLD_GLOBAL );
205 y2milestone( "Forcing %s open %s", qt_lib_name.utf8().constData(),
206 qt_lib ? "successful" : "failed" );
210 qApp->setFont( yqApp()->currentFont() );
213 connect( & _user_input_timer, SIGNAL( timeout() ),
214 this, SLOT ( userInputTimeout() ) );
216 connect( _busy_cursor_timer, SIGNAL( timeout() ),
217 this, SLOT ( busyCursor() ) );
221 // playMacro( macro_file );
223 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
224 qApp->processEvents();
231 return static_cast<YQApplication *>( app() );
235 void YQUI::processCommandLineArgs( int argc, char **argv )
239 for( int i=0; i < argc; i++ )
241 QString opt = argv[i];
243 y2milestone ("Qt argument: %s", argv[i]);
245 // Normalize command line option - accept "--xy" as well as "-xy"
247 if ( opt.startsWith( "--" ) )
250 if ( opt == QString( "-no-wm" ) ) _have_wm = false;
251 else if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
252 else if ( opt == QString( "-noborder" ) ) _decorate_toplevel_window = false;
253 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
254 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
255 // --macro is handled by YUI_component
256 else if ( opt == QString( "-help" ) )
259 "Command line options for the YaST2 Qt UI:\n"
261 "--nothreads run without additional UI threads\n"
262 "--no-wm assume no window manager is running\n"
263 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
264 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
265 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
266 "--help this help text\n"
268 "--macro <macro-file> play a macro right on startup\n"
270 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
271 "to maintain backwards compatibility.\n"
280 // Qt handles command line option "-reverse" for Arabic / Hebrew
287 y2debug("Closing down Qt UI.");
291 // Intentionally NOT calling dlclose() to libqt-mt
292 // (see constructor for explanation)
298 YQUI::createWidgetFactory()
300 YQWidgetFactory * factory = new YQWidgetFactory();
301 YUI_CHECK_NEW( factory );
308 YOptionalWidgetFactory *
309 YQUI::createOptionalWidgetFactory()
311 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
312 YUI_CHECK_NEW( factory );
319 YQUI::createApplication()
321 YQApplication * app = new YQApplication();
322 YUI_CHECK_NEW( app );
328 void YQUI::calcDefaultSize()
330 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
331 QSize availableSize = qApp->desktop()->availableGeometry().size();
335 _default_size = availableSize;
337 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
338 _default_size.width(), _default_size.height() );
342 // Get _default_size via -geometry command line option (if set)
344 QWidget * dummy = new QWidget();
346 qApp->setMainWidget( dummy );
347 _default_size = dummy->size();
350 // Set min defaultsize or figure one out if -geometry was not used
352 if ( _default_size.width() < 800 ||
353 _default_size.height() < 600 )
355 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
357 // Scale down to 70% of screen size
359 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
360 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
364 _default_size = availableSize;
369 y2milestone( "Forced size (via -geometry): %dx%d",
370 _default_size.width(), _default_size.height() );
375 _default_size = primaryScreenSize;
379 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
384 void YQUI::internalError( const char * msg )
387 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
388 QMessageBox::Abort | QMessageBox::Default,
392 if ( button == QMessageBox::Abort )
397 // exit() leaves a process running (WFM?), so this really seems to be
398 // the only way to make sure we are really going down.
402 void YQUI::idleLoop( int fd_ycp )
406 // runs in main thread
407 _eventLoop->wakeUp();
412 FD_SET(fd_ycp, &rfds);
416 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
423 void YQUI::sendEvent( YEvent * event )
427 _event_handler.sendEvent( event );
430 _eventLoop->exit( 1 );
435 YEvent * YQUI::userInput( unsigned long timeout_millisec )
439 // y2milestone( "userInput %ld", timeout_millisec );
442 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
444 if ( _user_input_timer.isActive() )
445 _user_input_timer.stop();
449 if ( timeout_millisec > 0 )
450 _user_input_timer.start( timeout_millisec ); // single shot
452 dialog->activate( true );
454 if ( qApp->focusWidget() )
455 qApp->focusWidget()->setFocus();
458 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
460 while ( ! pendingEvent() )
462 _eventLoop->processEvents(QEventLoop::WaitForMoreEvents);
465 _do_exit_loop = false;
467 event = _event_handler.consumePendingEvent();
468 dialog->activate( false );
470 // Display a busy cursor, but only if there is no other activity within
471 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
473 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
476 if ( _user_input_timer.isActive() )
477 _user_input_timer.stop();
483 YEvent * YQUI::pollInput()
487 if ( _user_input_timer.isActive() )
488 _user_input_timer.stop();
490 if ( ! pendingEvent() )
492 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
496 dialog->activate( true );
497 //qApp->processEvents();
498 event = _event_handler.consumePendingEvent();
499 dialog->activate( false );
503 if ( pendingEvent() )
504 event = _event_handler.consumePendingEvent();
510 void YQUI::userInputTimeout()
512 if ( ! pendingEvent() )
513 sendEvent( new YTimeoutEvent() );
517 YDialog * YQUI::createDialog( YWidgetOpt & opt )
521 y2milestone( "createDialog %ld", QThread::currentThreadId () );
522 bool has_defaultsize = opt.hasDefaultSize.value();
523 QWidget * qt_parent =
524 has_defaultsize ? _widget_stack : _main_win;
526 // FIXME: Probably obsolete
527 if ( ! has_defaultsize && ! _popup_stack.empty() )
528 qt_parent = _popup_stack.back();
530 YQDialog * dialog = new YQDialog( opt, qt_parent, has_defaultsize );
531 Q_CHECK_PTR( dialog );
533 if ( ! has_defaultsize )
534 _popup_stack.push_back( (QWidget *) dialog->widgetRep() );
540 void YQUI::showDialog( YDialog * dialog )
542 QWidget * qw = (QWidget *) dialog->widgetRep();
546 y2error( "No widgetRep() for dialog" );
550 if ( dialog->hasDefaultSize() )
552 _widget_stack->addWidget ( qw );
553 _widget_stack->setCurrentWidget( qw );
555 if ( ! _main_win->isVisible() )
557 // y2milestone( "Showing main window" );
558 _main_win->resize( _default_size );
561 _main_win->move( 0, 0 );
567 else // non-defaultsize dialog
572 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
574 //qApp->processEvents();
578 void YQUI::closeDialog( YDialog * dialog )
580 QWidget * qw = (QWidget *) dialog->widgetRep();
584 y2error( "No widgetRep() for dialog" );
588 if ( dialog->hasDefaultSize() )
590 _widget_stack->removeWidget( qw );
592 else // non-defaultsize dialog
596 // Clean up the popup stack. libyui guarantees that a dialog will be
597 // deleted after closeDialog() so it is safe to pop that dialog from
598 // the popup stack here.
600 if ( ! _popup_stack.empty() && _popup_stack.back() == qw )
601 _popup_stack.pop_back();
603 y2error( "Popup dialog stack corrupted!" );
608 void YQUI::easterEgg()
610 y2milestone( "Starting easter egg..." );
613 YQEasterBunny::layEgg();
614 y2milestone( "Done." );
617 // desktop()->repaint() has no effect - we need to do it the hard way.
618 system( "/usr/X11R6/bin/xrefresh" );
623 QString YQUI::productName() const
625 return fromUTF8( YUI::productName() );
630 YQUI::setTextdomain( const char * domain )
632 bindtextdomain( domain, LOCALEDIR );
633 bind_textdomain_codeset( domain, "utf8" );
634 textdomain( domain );
636 // Make change known.
638 extern int _nl_msg_cat_cntr;
646 qMessageHandler( QtMsgType type, const char * msg )
651 y2milestone ("qt-debug: %s\n", msg);
654 y2warning ("qt-warning: %s\n", msg);
658 y2warning ("qt-critical: %s\n", msg);
661 y2internal ("qt-fatal: %s\n", msg);
662 exit (1); // qt does the same