Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <unistd.h>
21 : #include <fcntl.h>
22 :
23 : #include <cstdio>
24 : #include <cstring>
25 : #include <cstdlib>
26 : #include <stdio.h> // snprintf, seems not to be in namespace std on every platform
27 : #include <limits.h>
28 : #include <errno.h>
29 : #include <pthread.h>
30 : #include <sys/resource.h>
31 : #ifdef SUN
32 : #include <sys/systeminfo.h>
33 : #endif
34 : #ifdef AIX
35 : #include <strings.h>
36 : #endif
37 : #ifdef FREEBSD
38 : #include <sys/types.h>
39 : #include <sys/time.h>
40 : #endif
41 :
42 : #include <prex.h>
43 : #include <X11/Xproto.h>
44 : #include <postx.h>
45 :
46 : #include <osl/process.h>
47 : #include <osl/mutex.hxx>
48 :
49 : #include "unx/saldisp.hxx"
50 : #include "unx/saldata.hxx"
51 : #include "unx/salframe.h"
52 : #include "unx/sm.hxx"
53 : #include "unx/i18n_im.hxx"
54 : #include "unx/i18n_xkb.hxx"
55 : #include "salinst.hxx"
56 :
57 : #include <osl/signal.h>
58 : #include <osl/thread.h>
59 : #include <rtl/strbuf.hxx>
60 : #include <rtl/bootstrap.hxx>
61 :
62 : #include <tools/debug.hxx>
63 : #include <vcl/svapp.hxx>
64 :
65 : #ifndef UNX
66 : #ifndef SIGBUS
67 : #define SIGBUS 10
68 : #endif
69 : #ifndef SIGSEGV
70 : #define SIGSEGV 11
71 : #endif
72 : #ifndef SIGIOT
73 : #define SIGIOT SIGABRT
74 : #endif
75 : #endif
76 :
77 0 : X11SalData* GetX11SalData()
78 : {
79 0 : SalData * p1 = ImplGetSVData()->mpSalData;
80 : OSL_ASSERT(p1 != 0);
81 0 : X11SalData * p2 = dynamic_cast< X11SalData * >(p1);
82 : OSL_ASSERT(p2 != 0);
83 0 : return p2;
84 : }
85 :
86 : static const struct timeval noyield__ = { 0, 0 };
87 : static const struct timeval yield__ = { 0, 10000 };
88 :
89 : static const char* XRequest[] = {
90 : // see /usr/lib/X11/XErrorDB, /usr/openwin/lib/XErrorDB ...
91 : NULL,
92 : "X_CreateWindow",
93 : "X_ChangeWindowAttributes",
94 : "X_GetWindowAttributes",
95 : "X_DestroyWindow",
96 : "X_DestroySubwindows",
97 : "X_ChangeSaveSet",
98 : "X_ReparentWindow",
99 : "X_MapWindow",
100 : "X_MapSubwindows",
101 : "X_UnmapWindow",
102 : "X_UnmapSubwindows",
103 : "X_ConfigureWindow",
104 : "X_CirculateWindow",
105 : "X_GetGeometry",
106 : "X_QueryTree",
107 : "X_InternAtom",
108 : "X_GetAtomName",
109 : "X_ChangeProperty",
110 : "X_DeleteProperty",
111 : "X_GetProperty",
112 : "X_ListProperties",
113 : "X_SetSelectionOwner",
114 : "X_GetSelectionOwner",
115 : "X_ConvertSelection",
116 : "X_SendEvent",
117 : "X_GrabPointer",
118 : "X_UngrabPointer",
119 : "X_GrabButton",
120 : "X_UngrabButton",
121 : "X_ChangeActivePointerGrab",
122 : "X_GrabKeyboard",
123 : "X_UngrabKeyboard",
124 : "X_GrabKey",
125 : "X_UngrabKey",
126 : "X_AllowEvents",
127 : "X_GrabServer",
128 : "X_UngrabServer",
129 : "X_QueryPointer",
130 : "X_GetMotionEvents",
131 : "X_TranslateCoords",
132 : "X_WarpPointer",
133 : "X_SetInputFocus",
134 : "X_GetInputFocus",
135 : "X_QueryKeymap",
136 : "X_OpenFont",
137 : "X_CloseFont",
138 : "X_QueryFont",
139 : "X_QueryTextExtents",
140 : "X_ListFonts",
141 : "X_ListFontsWithInfo",
142 : "X_SetFontPath",
143 : "X_GetFontPath",
144 : "X_CreatePixmap",
145 : "X_FreePixmap",
146 : "X_CreateGC",
147 : "X_ChangeGC",
148 : "X_CopyGC",
149 : "X_SetDashes",
150 : "X_SetClipRectangles",
151 : "X_FreeGC",
152 : "X_ClearArea",
153 : "X_CopyArea",
154 : "X_CopyPlane",
155 : "X_PolyPoint",
156 : "X_PolyLine",
157 : "X_PolySegment",
158 : "X_PolyRectangle",
159 : "X_PolyArc",
160 : "X_FillPoly",
161 : "X_PolyFillRectangle",
162 : "X_PolyFillArc",
163 : "X_PutImage",
164 : "X_GetImage",
165 : "X_PolyText8",
166 : "X_PolyText16",
167 : "X_ImageText8",
168 : "X_ImageText16",
169 : "X_CreateColormap",
170 : "X_FreeColormap",
171 : "X_CopyColormapAndFree",
172 : "X_InstallColormap",
173 : "X_UninstallColormap",
174 : "X_ListInstalledColormaps",
175 : "X_AllocColor",
176 : "X_AllocNamedColor",
177 : "X_AllocColorCells",
178 : "X_AllocColorPlanes",
179 : "X_FreeColors",
180 : "X_StoreColors",
181 : "X_StoreNamedColor",
182 : "X_QueryColors",
183 : "X_LookupColor",
184 : "X_CreateCursor",
185 : "X_CreateGlyphCursor",
186 : "X_FreeCursor",
187 : "X_RecolorCursor",
188 : "X_QueryBestSize",
189 : "X_QueryExtension",
190 : "X_ListExtensions",
191 : "X_ChangeKeyboardMapping",
192 : "X_GetKeyboardMapping",
193 : "X_ChangeKeyboardControl",
194 : "X_GetKeyboardControl",
195 : "X_Bell",
196 : "X_ChangePointerControl",
197 : "X_GetPointerControl",
198 : "X_SetScreenSaver",
199 : "X_GetScreenSaver",
200 : "X_ChangeHosts",
201 : "X_ListHosts",
202 : "X_SetAccessControl",
203 : "X_SetCloseDownMode",
204 : "X_KillClient",
205 : "X_RotateProperties",
206 : "X_ForceScreenSaver",
207 : "X_SetPointerMapping",
208 : "X_GetPointerMapping",
209 : "X_SetModifierMapping",
210 : "X_GetModifierMapping",
211 : NULL,
212 : NULL,
213 : NULL,
214 : NULL,
215 : NULL,
216 : NULL,
217 : NULL,
218 : "X_NoOperation"
219 : };
220 :
221 0 : X11SalData::X11SalData( SalGenericDataType t, SalInstance *pInstance )
222 0 : : SalGenericData( t, pInstance )
223 : {
224 0 : pXLib_ = NULL;
225 0 : m_pPlugin = NULL;
226 :
227 0 : m_aOrigXIOErrorHandler = XSetIOErrorHandler ( (XIOErrorHandler)XIOErrorHdl );
228 0 : PushXErrorLevel( !!getenv( "SAL_IGNOREXERRORS" ) );
229 0 : }
230 :
231 0 : X11SalData::~X11SalData()
232 : {
233 0 : DeleteDisplay();
234 0 : PopXErrorLevel();
235 0 : XSetIOErrorHandler (m_aOrigXIOErrorHandler);
236 0 : }
237 :
238 0 : void X11SalData::Dispose()
239 : {
240 0 : deInitNWF();
241 0 : delete GetDisplay();
242 0 : SetSalData( NULL );
243 0 : }
244 :
245 0 : void X11SalData::DeleteDisplay()
246 : {
247 0 : delete GetDisplay();
248 0 : SetDisplay( NULL );
249 0 : delete pXLib_;
250 0 : pXLib_ = NULL;
251 0 : }
252 :
253 0 : void X11SalData::Init()
254 : {
255 0 : pXLib_ = new SalXLib();
256 0 : pXLib_->Init();
257 0 : }
258 :
259 0 : void X11SalData::initNWF( void )
260 : {
261 0 : }
262 :
263 0 : void X11SalData::deInitNWF( void )
264 : {
265 0 : }
266 :
267 0 : void X11SalData::ErrorTrapPush()
268 : {
269 0 : PushXErrorLevel( true );
270 0 : }
271 :
272 0 : bool X11SalData::ErrorTrapPop( bool bIgnoreError )
273 : {
274 0 : bool err = false;
275 0 : if( !bIgnoreError )
276 0 : err = HasXErrorOccurred();
277 0 : ResetXErrorOccurred();
278 0 : PopXErrorLevel();
279 0 : return err;
280 : }
281 :
282 :
283 0 : void X11SalData::PushXErrorLevel( bool bIgnore )
284 : {
285 0 : m_aXErrorHandlerStack.push_back( XErrorStackEntry() );
286 0 : XErrorStackEntry& rEnt = m_aXErrorHandlerStack.back();
287 0 : rEnt.m_bWas = false;
288 0 : rEnt.m_bIgnore = bIgnore;
289 0 : rEnt.m_nLastErrorRequest = 0;
290 0 : rEnt.m_aHandler = XSetErrorHandler( (XErrorHandler)XErrorHdl );
291 0 : }
292 :
293 0 : void X11SalData::PopXErrorLevel()
294 : {
295 0 : if( m_aXErrorHandlerStack.size() )
296 : {
297 0 : XSetErrorHandler( m_aXErrorHandlerStack.back().m_aHandler );
298 0 : m_aXErrorHandlerStack.pop_back();
299 : }
300 0 : }
301 :
302 0 : int X11SalData::XErrorHdl( Display *pDisplay, XErrorEvent *pEvent )
303 : {
304 0 : GetX11SalData()->XError( pDisplay, pEvent );
305 0 : return 0;
306 : }
307 :
308 0 : int X11SalData::XIOErrorHdl( Display * )
309 : {
310 0 : if (::osl::Thread::getCurrentIdentifier() != Application::GetMainThreadIdentifier())
311 : {
312 0 : pthread_exit(NULL);
313 : return 0;
314 : }
315 :
316 : /* #106197# hack: until a real shutdown procedure exists
317 : * _exit ASAP
318 : */
319 0 : if( ImplGetSVData()->maAppData.mbAppQuit )
320 0 : _exit(1);
321 :
322 : // really bad hack
323 0 : if( ! SessionManagerClient::checkDocumentsSaved() )
324 0 : /* oslSignalAction eToDo = */ osl_raiseSignal (OSL_SIGNAL_USER_X11SUBSYSTEMERROR, NULL);
325 :
326 0 : std::fprintf( stderr, "X IO Error\n" );
327 0 : std::fflush( stdout );
328 0 : std::fflush( stderr );
329 :
330 : /* #106197# the same reasons to use _exit instead of exit in salmain
331 : * do apply here. Since there is nothing to be done after an XIO
332 : * error we have to _exit immediately.
333 : */
334 0 : _exit(0);
335 : return 0;
336 : }
337 :
338 :
339 0 : SalXLib::SalXLib()
340 : {
341 0 : m_aTimeout.tv_sec = 0;
342 0 : m_aTimeout.tv_usec = 0;
343 0 : m_nTimeoutMS = 0;
344 :
345 0 : nFDs_ = 0;
346 0 : FD_ZERO( &aReadFDS_ );
347 0 : FD_ZERO( &aExceptionFDS_ );
348 :
349 0 : m_pTimeoutFDS[0] = m_pTimeoutFDS[1] = -1;
350 0 : if (pipe (m_pTimeoutFDS) != -1)
351 : {
352 : // initialize 'wakeup' pipe.
353 : int flags;
354 :
355 : // set close-on-exec descriptor flag.
356 0 : if ((flags = fcntl (m_pTimeoutFDS[0], F_GETFD)) != -1)
357 : {
358 0 : flags |= FD_CLOEXEC;
359 0 : fcntl (m_pTimeoutFDS[0], F_SETFD, flags);
360 : }
361 0 : if ((flags = fcntl (m_pTimeoutFDS[1], F_GETFD)) != -1)
362 : {
363 0 : flags |= FD_CLOEXEC;
364 0 : fcntl (m_pTimeoutFDS[1], F_SETFD, flags);
365 : }
366 :
367 : // set non-blocking I/O flag.
368 0 : if ((flags = fcntl (m_pTimeoutFDS[0], F_GETFL)) != -1)
369 : {
370 0 : flags |= O_NONBLOCK;
371 0 : fcntl (m_pTimeoutFDS[0], F_SETFL, flags);
372 : }
373 0 : if ((flags = fcntl (m_pTimeoutFDS[1], F_GETFL)) != -1)
374 : {
375 0 : flags |= O_NONBLOCK;
376 0 : fcntl (m_pTimeoutFDS[1], F_SETFL, flags);
377 : }
378 :
379 : // insert [0] into read descriptor set.
380 0 : FD_SET( m_pTimeoutFDS[0], &aReadFDS_ );
381 0 : nFDs_ = m_pTimeoutFDS[0] + 1;
382 : }
383 0 : }
384 :
385 0 : SalXLib::~SalXLib()
386 : {
387 : // close 'wakeup' pipe.
388 0 : close (m_pTimeoutFDS[0]);
389 0 : close (m_pTimeoutFDS[1]);
390 0 : }
391 :
392 0 : void SalXLib::Init()
393 : {
394 0 : SalI18N_InputMethod* pInputMethod = new SalI18N_InputMethod;
395 0 : pInputMethod->SetLocale();
396 0 : XrmInitialize();
397 :
398 : /*
399 : * open connection to X11 Display
400 : * try in this order:
401 : * o -display command line parameter,
402 : * o $DISPLAY environment variable
403 : * o default display
404 : */
405 :
406 0 : Display *pDisp = NULL;
407 :
408 : // is there a -display command line parameter?
409 :
410 0 : sal_uInt32 nParams = osl_getCommandArgCount();
411 0 : OUString aParam;
412 0 : OString aDisplay;
413 0 : for (sal_uInt16 i=0; i<nParams; i++)
414 : {
415 0 : osl_getCommandArg(i, &aParam.pData);
416 0 : if ( aParam == "-display" )
417 : {
418 0 : osl_getCommandArg(i+1, &aParam.pData);
419 0 : aDisplay = OUStringToOString(
420 0 : aParam, osl_getThreadTextEncoding());
421 :
422 0 : if ((pDisp = XOpenDisplay(aDisplay.getStr()))!=NULL)
423 : {
424 : /*
425 : * if a -display switch was used, we need
426 : * to set the environment accoringly since
427 : * the clipboard build another connection
428 : * to the xserver using $DISPLAY
429 : */
430 0 : OUString envVar("DISPLAY");
431 0 : osl_setEnvironment(envVar.pData, aParam.pData);
432 : }
433 0 : break;
434 : }
435 : }
436 :
437 0 : if (!pDisp && aDisplay.isEmpty())
438 : {
439 : // Open $DISPLAY or default...
440 0 : char *pDisplay = getenv("DISPLAY");
441 0 : if (pDisplay != NULL)
442 0 : aDisplay = OString(pDisplay);
443 0 : pDisp = XOpenDisplay(pDisplay);
444 : }
445 :
446 0 : if ( !pDisp )
447 : {
448 0 : OUString aProgramFileURL;
449 0 : osl_getExecutableFile( &aProgramFileURL.pData );
450 0 : OUString aProgramSystemPath;
451 0 : osl_getSystemPathFromFileURL (aProgramFileURL.pData, &aProgramSystemPath.pData);
452 : OString aProgramName = OUStringToOString(
453 : aProgramSystemPath,
454 0 : osl_getThreadTextEncoding() );
455 : std::fprintf( stderr, "%s X11 error: Can't open display: %s\n",
456 0 : aProgramName.getStr(), aDisplay.getStr());
457 0 : std::fprintf( stderr, " Set DISPLAY environment variable, use -display option\n");
458 0 : std::fprintf( stderr, " or check permissions of your X-Server\n");
459 0 : std::fprintf( stderr, " (See \"man X\" resp. \"man xhost\" for details)\n");
460 0 : std::fflush( stderr );
461 0 : exit(0);
462 : }
463 :
464 0 : SalX11Display *pSalDisplay = new SalX11Display( pDisp );
465 :
466 0 : pInputMethod->CreateMethod( pDisp );
467 0 : pSalDisplay->SetupInput( pInputMethod );
468 0 : }
469 :
470 : extern "C" {
471 0 : void EmitFontpathWarning( void )
472 : {
473 : static Bool bOnce = False;
474 0 : if ( !bOnce )
475 : {
476 0 : bOnce = True;
477 : std::fprintf( stderr, "Please verify your fontpath settings\n"
478 : "\t(See \"man xset\" for details"
479 0 : " or ask your system administrator)\n" );
480 : }
481 0 : }
482 :
483 : } /* extern "C" */
484 :
485 0 : static void PrintXError( Display *pDisplay, XErrorEvent *pEvent )
486 : {
487 0 : char msg[ 120 ] = "";
488 : #if ! ( defined LINUX && defined PPC )
489 0 : XGetErrorText( pDisplay, pEvent->error_code, msg, sizeof( msg ) );
490 : #endif
491 0 : std::fprintf( stderr, "X-Error: %s\n", msg );
492 0 : if( pEvent->request_code < SAL_N_ELEMENTS( XRequest ) )
493 : {
494 0 : const char* pName = XRequest[pEvent->request_code];
495 0 : if( !pName )
496 0 : pName = "BadRequest?";
497 0 : std::fprintf( stderr, "\tMajor opcode: %d (%s)\n", pEvent->request_code, pName );
498 : }
499 : else
500 : {
501 0 : std::fprintf( stderr, "\tMajor opcode: %d\n", pEvent->request_code );
502 : // TODO: also display extension name?
503 0 : std::fprintf( stderr, "\tMinor opcode: %d\n", pEvent->minor_code );
504 : }
505 :
506 : std::fprintf( stderr, "\tResource ID: 0x%lx\n",
507 0 : pEvent->resourceid );
508 : std::fprintf( stderr, "\tSerial No: %ld (%ld)\n",
509 0 : pEvent->serial, LastKnownRequestProcessed(pDisplay) );
510 :
511 0 : if( !getenv( "SAL_SYNCHRONIZE" ) )
512 : {
513 0 : std::fprintf( stderr, "These errors are reported asynchronously,\n");
514 0 : std::fprintf( stderr, "set environment variable SAL_SYNCHRONIZE to 1 to help debugging\n");
515 : }
516 :
517 0 : std::fflush( stdout );
518 0 : std::fflush( stderr );
519 0 : }
520 :
521 0 : void X11SalData::XError( Display *pDisplay, XErrorEvent *pEvent )
522 : {
523 0 : if( ! m_aXErrorHandlerStack.back().m_bIgnore )
524 : {
525 0 : if ( (pEvent->error_code == BadAlloc)
526 0 : && (pEvent->request_code == X_OpenFont) )
527 : {
528 : static Bool bOnce = False;
529 0 : if ( !bOnce )
530 : {
531 0 : std::fprintf(stderr, "X-Error occurred in a request for X_OpenFont\n");
532 0 : EmitFontpathWarning();
533 :
534 0 : bOnce = True ;
535 : }
536 0 : return;
537 : }
538 : /* ignore
539 : * X_SetInputFocus: it's a hint only anyway
540 : * X_GetProperty: this is part of the XGetWindowProperty call and will
541 : * be handled by the return value of that function
542 : */
543 0 : else if( pEvent->request_code == X_SetInputFocus ||
544 0 : pEvent->request_code == X_GetProperty
545 : )
546 0 : return;
547 :
548 :
549 0 : if( pDisplay != GetGenericData()->GetSalDisplay()->GetDisplay() )
550 0 : return;
551 :
552 0 : PrintXError( pDisplay, pEvent );
553 :
554 0 : oslSignalAction eToDo = osl_raiseSignal (OSL_SIGNAL_USER_X11SUBSYSTEMERROR, NULL);
555 0 : switch (eToDo)
556 : {
557 : case osl_Signal_ActIgnore :
558 0 : return;
559 : case osl_Signal_ActAbortApp :
560 0 : abort();
561 : case osl_Signal_ActKillApp :
562 0 : exit(0);
563 : case osl_Signal_ActCallNextHdl :
564 0 : break;
565 : default :
566 0 : break;
567 : }
568 :
569 : }
570 :
571 0 : m_aXErrorHandlerStack.back().m_bWas = true;
572 : }
573 :
574 : struct YieldEntry
575 : {
576 : YieldEntry* next; // pointer to next entry
577 : int fd; // file descriptor for reading
578 : void* data; // data for predicate and callback
579 : YieldFunc pending; // predicate (determins pending events)
580 : YieldFunc queued; // read and queue up events
581 : YieldFunc handle; // handle pending events
582 :
583 0 : inline int HasPendingEvent() const { return pending( fd, data ); }
584 0 : inline int IsEventQueued() const { return queued( fd, data ); }
585 0 : inline void HandleNextEvent() const { handle( fd, data ); }
586 : };
587 :
588 : #define MAX_NUM_DESCRIPTORS 128
589 :
590 : static YieldEntry yieldTable[ MAX_NUM_DESCRIPTORS ];
591 :
592 0 : void SalXLib::Insert( int nFD, void* data,
593 : YieldFunc pending,
594 : YieldFunc queued,
595 : YieldFunc handle )
596 : {
597 : DBG_ASSERT( nFD, "can not insert stdin descriptor" );
598 : DBG_ASSERT( !yieldTable[nFD].fd, "SalXLib::Insert fd twice" );
599 :
600 0 : yieldTable[nFD].fd = nFD;
601 0 : yieldTable[nFD].data = data;
602 0 : yieldTable[nFD].pending = pending;
603 0 : yieldTable[nFD].queued = queued;
604 0 : yieldTable[nFD].handle = handle;
605 :
606 0 : FD_SET( nFD, &aReadFDS_ );
607 0 : FD_SET( nFD, &aExceptionFDS_ );
608 :
609 0 : if( nFD >= nFDs_ )
610 0 : nFDs_ = nFD + 1;
611 0 : }
612 :
613 0 : void SalXLib::Remove( int nFD )
614 : {
615 0 : FD_CLR( nFD, &aReadFDS_ );
616 0 : FD_CLR( nFD, &aExceptionFDS_ );
617 :
618 0 : yieldTable[nFD].fd = 0;
619 :
620 0 : if ( nFD == nFDs_ )
621 : {
622 0 : for ( nFD = nFDs_ - 1;
623 0 : nFD >= 0 && !yieldTable[nFD].fd;
624 : nFD-- ) ;
625 :
626 0 : nFDs_ = nFD + 1;
627 : }
628 0 : }
629 :
630 0 : bool SalXLib::CheckTimeout( bool bExecuteTimers )
631 : {
632 0 : bool bRet = false;
633 0 : if( m_aTimeout.tv_sec ) // timer is started
634 : {
635 : timeval aTimeOfDay;
636 0 : gettimeofday( &aTimeOfDay, 0 );
637 0 : if( aTimeOfDay >= m_aTimeout )
638 : {
639 0 : bRet = true;
640 0 : if( bExecuteTimers )
641 : {
642 : // timed out, update timeout
643 0 : m_aTimeout = aTimeOfDay;
644 : /*
645 : * #107827# autorestart immediately, will be stopped (or set
646 : * to different value in notify hdl if necessary;
647 : * CheckTimeout should return false while
648 : * timers are being dispatched.
649 : */
650 0 : m_aTimeout += m_nTimeoutMS;
651 : // notify
652 0 : GetX11SalData()->Timeout();
653 : }
654 : }
655 : }
656 0 : return bRet;
657 : }
658 :
659 0 : void SalXLib::Yield( bool bWait, bool bHandleAllCurrentEvents )
660 : {
661 : // check for timeouts here if you want to make screenshots
662 0 : static char* p_prioritize_timer = getenv ("SAL_HIGHPRIORITY_REPAINT");
663 0 : if (p_prioritize_timer != NULL)
664 0 : CheckTimeout();
665 :
666 0 : const int nMaxEvents = bHandleAllCurrentEvents ? 100 : 1;
667 :
668 : // first, check for already queued events.
669 0 : for ( int nFD = 0; nFD < nFDs_; nFD++ )
670 : {
671 0 : YieldEntry* pEntry = &(yieldTable[nFD]);
672 0 : if ( pEntry->fd )
673 : {
674 : DBG_ASSERT( nFD == pEntry->fd, "wrong fd in Yield()" );
675 0 : for( int i = 0; i < nMaxEvents && pEntry->HasPendingEvent(); i++ )
676 : {
677 0 : pEntry->HandleNextEvent();
678 0 : if( ! bHandleAllCurrentEvents )
679 0 : return;
680 : }
681 : }
682 : }
683 :
684 : // next, select with or without timeout according to bWait.
685 0 : int nFDs = nFDs_;
686 0 : fd_set ReadFDS = aReadFDS_;
687 0 : fd_set ExceptionFDS = aExceptionFDS_;
688 0 : int nFound = 0;
689 :
690 0 : timeval Timeout = noyield__;
691 0 : timeval *pTimeout = &Timeout;
692 :
693 0 : if (bWait)
694 : {
695 0 : pTimeout = 0;
696 0 : if (m_aTimeout.tv_sec) // Timer is started.
697 : {
698 : // determine remaining timeout.
699 0 : gettimeofday (&Timeout, 0);
700 0 : Timeout = m_aTimeout - Timeout;
701 0 : if (yield__ >= Timeout)
702 : {
703 : // guard against micro timeout.
704 0 : Timeout = yield__;
705 : }
706 0 : pTimeout = &Timeout;
707 : }
708 : }
709 :
710 : {
711 : // release YieldMutex (and re-acquire at block end)
712 0 : SalYieldMutexReleaser aReleaser;
713 0 : nFound = select( nFDs, &ReadFDS, NULL, &ExceptionFDS, pTimeout );
714 : }
715 0 : if( nFound < 0 ) // error
716 : {
717 : #ifdef DBG_UTIL
718 : std::fprintf( stderr, "SalXLib::Yield e=%d f=%d\n", errno, nFound );
719 : #endif
720 0 : if( EINTR == errno )
721 : {
722 0 : errno = 0;
723 : }
724 : }
725 :
726 : // usually handle timeouts here (as in 5.2)
727 0 : if (p_prioritize_timer == NULL)
728 0 : CheckTimeout();
729 :
730 : // handle wakeup events.
731 0 : if ((nFound > 0) && (FD_ISSET(m_pTimeoutFDS[0], &ReadFDS)))
732 : {
733 : int buffer;
734 0 : while (read (m_pTimeoutFDS[0], &buffer, sizeof(buffer)) > 0)
735 0 : continue;
736 0 : nFound -= 1;
737 : }
738 :
739 : // handle other events.
740 0 : if( nFound > 0 )
741 : {
742 : // now we are in the protected section !
743 : // recall select if we have acquired fd's, ready for reading,
744 :
745 0 : struct timeval noTimeout = { 0, 0 };
746 : nFound = select( nFDs_, &ReadFDS, NULL,
747 0 : &ExceptionFDS, &noTimeout );
748 :
749 : // someone-else has done the job for us
750 0 : if (nFound == 0)
751 0 : return;
752 :
753 0 : for ( int nFD = 0; nFD < nFDs_; nFD++ )
754 : {
755 0 : YieldEntry* pEntry = &(yieldTable[nFD]);
756 0 : if ( pEntry->fd )
757 : {
758 0 : if ( FD_ISSET( nFD, &ExceptionFDS ) ) {
759 : #if OSL_DEBUG_LEVEL > 1
760 : std::fprintf( stderr, "SalXLib::Yield exception\n" );
761 : #endif
762 0 : nFound--;
763 : }
764 0 : if ( FD_ISSET( nFD, &ReadFDS ) )
765 : {
766 0 : for( int i = 0; pEntry->IsEventQueued() && i < nMaxEvents; i++ )
767 : {
768 0 : pEntry->HandleNextEvent();
769 : // if a recursive call has done the job
770 : // so abort here
771 : }
772 0 : nFound--;
773 : }
774 : }
775 : }
776 : }
777 : }
778 :
779 0 : void SalXLib::Wakeup()
780 : {
781 0 : OSL_VERIFY(write (m_pTimeoutFDS[1], "", 1) == 1);
782 0 : }
783 :
784 0 : void SalXLib::PostUserEvent()
785 : {
786 0 : Wakeup();
787 0 : }
788 :
789 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|