LCOV - code coverage report
Current view: top level - libreoffice/vcl/headless - svpinst.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 163 174 93.7 %
Date: 2012-12-17 Functions: 27 31 87.1 %
Legend: Lines: hit not hit

          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             : #include <sys/time.h>
      23             : #include <sys/poll.h>
      24             : 
      25             : #include <sal/types.h>
      26             : 
      27             : #include <vcl/apptypes.hxx>
      28             : 
      29             : #include "headless/svpinst.hxx"
      30             : #include "headless/svpframe.hxx"
      31             : #include "headless/svpdummies.hxx"
      32             : #include "headless/svpvd.hxx"
      33             : #include "headless/svpbmp.hxx"
      34             : 
      35             : #include <salframe.hxx>
      36             : #include <svdata.hxx>
      37             : #include <generic/gendata.hxx>
      38             : #include <basebmp/scanlineformats.hxx>
      39             : #include <vcl/solarmutex.hxx>
      40             : // FIXME: remove when we re-work the svp mainloop
      41             : #include <unx/salunxtime.h>
      42             : 
      43        8268 : bool SvpSalInstance::isFrameAlive( const SalFrame* pFrame ) const
      44             : {
      45      203364 :     for( std::list< SalFrame* >::const_iterator it = m_aFrames.begin();
      46      135576 :          it != m_aFrames.end(); ++it )
      47             :     {
      48       67788 :         if( *it == pFrame )
      49             :         {
      50        8268 :             return true;
      51             :         }
      52             :     }
      53           0 :     return false;
      54             : }
      55             : 
      56             : SvpSalInstance* SvpSalInstance::s_pDefaultInstance = NULL;
      57             : 
      58          98 : SvpSalInstance::SvpSalInstance( SalYieldMutex *pMutex ) :
      59          98 :     SalGenericInstance( pMutex )
      60             : {
      61          98 :     m_aTimeout.tv_sec       = 0;
      62          98 :     m_aTimeout.tv_usec      = 0;
      63          98 :     m_nTimeoutMS            = 0;
      64             : 
      65          98 :     m_pTimeoutFDS[0] = m_pTimeoutFDS[1] = -1;
      66          98 :     if (pipe (m_pTimeoutFDS) != -1)
      67             :     {
      68             :         // initialize 'wakeup' pipe.
      69             :         int flags;
      70             : 
      71             :         // set close-on-exec descriptor flag.
      72          98 :         if ((flags = fcntl (m_pTimeoutFDS[0], F_GETFD)) != -1)
      73             :         {
      74          98 :             flags |= FD_CLOEXEC;
      75          98 :             fcntl (m_pTimeoutFDS[0], F_SETFD, flags);
      76             :         }
      77          98 :         if ((flags = fcntl (m_pTimeoutFDS[1], F_GETFD)) != -1)
      78             :         {
      79          98 :             flags |= FD_CLOEXEC;
      80          98 :             fcntl (m_pTimeoutFDS[1], F_SETFD, flags);
      81             :         }
      82             : 
      83             :         // set non-blocking I/O flag.
      84          98 :         if ((flags = fcntl (m_pTimeoutFDS[0], F_GETFL)) != -1)
      85             :         {
      86          98 :             flags |= O_NONBLOCK;
      87          98 :             fcntl (m_pTimeoutFDS[0], F_SETFL, flags);
      88             :         }
      89          98 :         if ((flags = fcntl (m_pTimeoutFDS[1], F_GETFL)) != -1)
      90             :         {
      91          98 :             flags |= O_NONBLOCK;
      92          98 :             fcntl (m_pTimeoutFDS[1], F_SETFL, flags);
      93             :         }
      94             :     }
      95          98 :     m_aEventGuard = osl_createMutex();
      96          98 :     if( s_pDefaultInstance == NULL )
      97          98 :         s_pDefaultInstance = this;
      98          98 : }
      99             : 
     100          24 : SvpSalInstance::~SvpSalInstance()
     101             : {
     102           8 :     if( s_pDefaultInstance == this )
     103           8 :         s_pDefaultInstance = NULL;
     104             : 
     105             :     // close 'wakeup' pipe.
     106           8 :     close (m_pTimeoutFDS[0]);
     107           8 :     close (m_pTimeoutFDS[1]);
     108           8 :     osl_destroyMutex( m_aEventGuard );
     109          16 : }
     110             : 
     111       10422 : void SvpSalInstance::PostEvent( const SalFrame* pFrame, void* pData, sal_uInt16 nEvent )
     112             : {
     113       10422 :     if( osl_acquireMutex( m_aEventGuard ) )
     114             :     {
     115       10422 :         m_aUserEvents.push_back( SalUserEvent( pFrame, pData, nEvent ) );
     116       10422 :         osl_releaseMutex( m_aEventGuard );
     117             :     }
     118       10422 :     Wakeup();
     119       10422 : }
     120             : 
     121         146 : void SvpSalInstance::deregisterFrame( SalFrame* pFrame )
     122             : {
     123         146 :     m_aFrames.remove( pFrame );
     124             : 
     125         146 :     if( osl_acquireMutex( m_aEventGuard ) )
     126             :     {
     127             :         // cancel outstanding events for this frame
     128         146 :         if( ! m_aUserEvents.empty() )
     129             :         {
     130         135 :             std::list< SalUserEvent >::iterator it = m_aUserEvents.begin();
     131       28988 :             do
     132             :             {
     133       14494 :                 if( it->m_pFrame    == pFrame )
     134             :                 {
     135         548 :                     it = m_aUserEvents.erase( it );
     136             :                 }
     137             :                 else
     138       13946 :                     ++it;
     139       28988 :             } while( it != m_aUserEvents.end() );
     140             :         }
     141         146 :         osl_releaseMutex( m_aEventGuard );
     142             :     }
     143         146 : }
     144             : 
     145       10946 : void SvpSalInstance::Wakeup()
     146             : {
     147       10946 :     OSL_VERIFY(write (m_pTimeoutFDS[1], "", 1) == 1);
     148       10946 : }
     149             : 
     150        3406 : bool SvpSalInstance::CheckTimeout( bool bExecuteTimers )
     151             : {
     152        3406 :     bool bRet = false;
     153        3406 :     if( m_aTimeout.tv_sec ) // timer is started
     154             :     {
     155             :         timeval aTimeOfDay;
     156        3376 :         gettimeofday( &aTimeOfDay, 0 );
     157        3376 :         if( aTimeOfDay >= m_aTimeout )
     158             :         {
     159        1993 :             bRet = true;
     160        1993 :             if( bExecuteTimers )
     161             :             {
     162             :                 // timed out, update timeout
     163        1116 :                 m_aTimeout = aTimeOfDay;
     164        1116 :                 m_aTimeout += m_nTimeoutMS;
     165             : 
     166        1116 :                 osl::SolarGuard aGuard( mpSalYieldMutex );
     167             : 
     168             :                 // notify
     169        1116 :                 ImplSVData* pSVData = ImplGetSVData();
     170        1116 :                 if( pSVData->mpSalTimer )
     171        1116 :                     pSVData->mpSalTimer->CallCallback();
     172             :             }
     173             :         }
     174             :     }
     175        3406 :     return bRet;
     176             : }
     177             : 
     178           0 : SalFrame* SvpSalInstance::CreateChildFrame( SystemParentData* pParent, sal_uLong nStyle )
     179             : {
     180           0 :     return new SvpSalFrame( this, NULL, nStyle, false, SVP_DEFAULT_BITMAP_FORMAT, pParent );
     181             : }
     182             : 
     183         640 : SalFrame* SvpSalInstance::CreateFrame( SalFrame* pParent, sal_uLong nStyle )
     184             : {
     185         640 :     return new SvpSalFrame( this, pParent, nStyle, false, SVP_DEFAULT_BITMAP_FORMAT );
     186             : }
     187             : 
     188         146 : void SvpSalInstance::DestroyFrame( SalFrame* pFrame )
     189             : {
     190         146 :     delete pFrame;
     191         146 : }
     192             : 
     193           0 : SalObject* SvpSalInstance::CreateObject( SalFrame*, SystemWindowData*, sal_Bool )
     194             : {
     195           0 :     return new SvpSalObject();
     196             : }
     197             : 
     198           0 : void SvpSalInstance::DestroyObject( SalObject* pObject )
     199             : {
     200           0 :     delete pObject;
     201           0 : }
     202             : 
     203       15494 : SalVirtualDevice* SvpSalInstance::CreateVirtualDevice( SalGraphics*,
     204             :                                                        long nDX, long nDY,
     205             :                                                        sal_uInt16 nBitCount, const SystemGraphicsData* )
     206             : {
     207       15494 :     SvpSalVirtualDevice* pNew = new SvpSalVirtualDevice( nBitCount );
     208       15494 :     pNew->SetSize( nDX, nDY );
     209       15494 :     return pNew;
     210             : }
     211             : 
     212       12822 : void SvpSalInstance::DestroyVirtualDevice( SalVirtualDevice* pDevice )
     213             : {
     214       12822 :     delete pDevice;
     215       12822 : }
     216             : 
     217          72 : SalTimer* SvpSalInstance::CreateSalTimer()
     218             : {
     219          72 :     return new SvpSalTimer( this );
     220             : }
     221             : 
     222          72 : SalI18NImeStatus* SvpSalInstance::CreateI18NImeStatus()
     223             : {
     224          72 :     return new SvpImeStatus();
     225             : }
     226             : 
     227          29 : SalSystem* SvpSalInstance::CreateSalSystem()
     228             : {
     229          29 :     return new SvpSalSystem();
     230             : }
     231             : 
     232        2156 : SalBitmap* SvpSalInstance::CreateSalBitmap()
     233             : {
     234        2156 :     return new SvpSalBitmap();
     235             : }
     236             : 
     237        2216 : void SvpSalInstance::Yield( bool bWait, bool bHandleAllCurrentEvents )
     238             : {
     239             :     // first, check for already queued events.
     240             : 
     241             :     // release yield mutex
     242        2216 :     std::list< SalUserEvent > aEvents;
     243        2216 :     sal_uLong nAcquireCount = ReleaseYieldMutex();
     244        2216 :     if( osl_acquireMutex( m_aEventGuard ) )
     245             :     {
     246        2216 :         if( ! m_aUserEvents.empty() )
     247             :         {
     248        1105 :             if( bHandleAllCurrentEvents )
     249             :             {
     250        1025 :                 aEvents = m_aUserEvents;
     251        1025 :                 m_aUserEvents.clear();
     252             :             }
     253             :             else
     254             :             {
     255          80 :                 aEvents.push_back( m_aUserEvents.front() );
     256          80 :                 m_aUserEvents.pop_front();
     257             :             }
     258             :         }
     259        2216 :         osl_releaseMutex( m_aEventGuard );
     260             :     }
     261             :     // acquire yield mutex again
     262        2216 :     AcquireYieldMutex( nAcquireCount );
     263             : 
     264        2216 :     bool bEvent = !aEvents.empty();
     265        2216 :     if( bEvent )
     266             :     {
     267        9373 :         for( std::list<SalUserEvent>::const_iterator it = aEvents.begin(); it != aEvents.end(); ++it )
     268             :         {
     269        8268 :             if ( isFrameAlive( it->m_pFrame ) )
     270             :             {
     271        8268 :                 it->m_pFrame->CallCallback( it->m_nEvent, it->m_pData );
     272        8268 :                 if( it->m_nEvent == SALEVENT_RESIZE )
     273             :                 {
     274             :                     // this would be a good time to post a paint
     275         362 :                     const SvpSalFrame* pSvpFrame = static_cast<const SvpSalFrame*>(it->m_pFrame);
     276         362 :                     pSvpFrame->PostPaint(false);
     277             :                 }
     278             :             }
     279             :         }
     280             :     }
     281             : 
     282        2216 :     bEvent = CheckTimeout() || bEvent;
     283             : 
     284        2216 :     if (bWait && ! bEvent )
     285             :     {
     286          52 :         int nTimeoutMS = 0;
     287          52 :         if (m_aTimeout.tv_sec) // Timer is started.
     288             :         {
     289             :             timeval Timeout;
     290             :             // determine remaining timeout.
     291          41 :             gettimeofday (&Timeout, 0);
     292             :             nTimeoutMS = m_aTimeout.tv_sec*1000 + m_aTimeout.tv_usec/1000
     293          41 :                          - Timeout.tv_sec*1000 - Timeout.tv_usec/1000;
     294          41 :             if( nTimeoutMS < 0 )
     295           0 :                 nTimeoutMS = 0;
     296             :         }
     297             :         else
     298          11 :             nTimeoutMS = -1; // wait until something happens
     299             : 
     300          52 :         DoReleaseYield(nTimeoutMS);
     301        2216 :     }
     302        2216 : }
     303             : 
     304          52 : void SvpSalInstance::DoReleaseYield( int nTimeoutMS )
     305             : {
     306             :     // poll
     307             :     struct pollfd aPoll;
     308          52 :     aPoll.fd = m_pTimeoutFDS[0];
     309          52 :     aPoll.events = POLLIN;
     310          52 :     aPoll.revents = 0;
     311             : 
     312             :     // release yield mutex
     313          52 :     sal_uLong nAcquireCount = ReleaseYieldMutex();
     314             : 
     315          52 :     poll( &aPoll, 1, nTimeoutMS );
     316             : 
     317             :     // acquire yield mutex again
     318          52 :     AcquireYieldMutex( nAcquireCount );
     319             : 
     320             :     // clean up pipe
     321          52 :     if( (aPoll.revents & POLLIN) != 0 )
     322             :     {
     323             :         int buffer;
     324         120 :         while (read (m_pTimeoutFDS[0], &buffer, sizeof(buffer)) > 0)
     325          46 :             continue;
     326             :     }
     327          52 : }
     328             : 
     329        4890 : bool SvpSalInstance::AnyInput( sal_uInt16 nType )
     330             : {
     331        4890 :     if( (nType & VCL_INPUT_TIMER) != 0 )
     332        1190 :         return CheckTimeout( false );
     333        3700 :     return false;
     334             : }
     335             : 
     336           0 : SalSession* SvpSalInstance::CreateSalSession()
     337             : {
     338           0 :     return NULL;
     339             : }
     340             : 
     341          46 : void* SvpSalInstance::GetConnectionIdentifier( ConnectionIdentifierType& rReturnedType, int& rReturnedBytes )
     342             : {
     343          46 :     rReturnedBytes  = 1;
     344          46 :     rReturnedType   = AsciiCString;
     345          46 :     return const_cast<char*>("");
     346             : }
     347             : 
     348             : // ---------------
     349             : // - SalTimer -
     350             : // ---------------
     351             : 
     352           6 : void SvpSalInstance::StopTimer()
     353             : {
     354           6 :     m_aTimeout.tv_sec   = 0;
     355           6 :     m_aTimeout.tv_usec  = 0;
     356           6 :     m_nTimeoutMS        = 0;
     357           6 : }
     358             : 
     359        1415 : void SvpSalInstance::StartTimer( sal_uLong nMS )
     360             : {
     361        1415 :     timeval Timeout (m_aTimeout); // previous timeout.
     362        1415 :     gettimeofday (&m_aTimeout, 0);
     363             : 
     364        1415 :     m_nTimeoutMS  = nMS;
     365        1415 :     m_aTimeout    += m_nTimeoutMS;
     366             : 
     367        1415 :     if ((Timeout > m_aTimeout) || (Timeout.tv_sec == 0))
     368             :     {
     369             :         // Wakeup from previous timeout (or stopped timer).
     370         524 :         Wakeup();
     371             :     }
     372        1415 : }
     373             : 
     374         828 : void SvpSalInstance::AddToRecentDocumentList(const rtl::OUString&, const rtl::OUString&)
     375             : {
     376         828 : }
     377             : 
     378          16 : SvpSalTimer::~SvpSalTimer()
     379             : {
     380          16 : }
     381             : 
     382           6 : void SvpSalTimer::Stop()
     383             : {
     384           6 :     m_pInstance->StopTimer();
     385           6 : }
     386             : 
     387        1415 : void SvpSalTimer::Start( sal_uLong nMS )
     388             : {
     389        1415 :     m_pInstance->StartTimer( nMS );
     390        1415 : }
     391             : 
     392             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10