xref: /trunk/main/vcl/inc/aqua/salinst.h (revision 2a4643a67e92c4338a0f7b947e78b04714015a0d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_SALINST_H
25 #define _SV_SALINST_H
26 
27 #include "vos/mutex.hxx"
28 #include "vos/thread.hxx"
29 #include "osl/conditn.h"
30 
31 #include "aqua/aquavcltypes.h"
32 
33 #include "salinst.hxx"
34 
35 #include <boost/function.hpp>
36 #include <boost/bind.hpp>
37 #include <boost/ref.hpp>
38 
39 #include <exception>
40 #include <list>
41 #include <pthread.h>
42 
43 class AquaSalFrame;
44 class ApplicationEvent;
45 class Image;
46 
47 // -----------------
48 // - SalYieldMutex -
49 // -----------------
50 
51 class SalYieldMutex : public vos::OMutex
52 {
53     sal_uLong                                       mnCount;
54     vos::OThread::TThreadIdentifier             mnThreadId;
55     vos::OThread::TThreadIdentifier             mnMainThreadId;
56 
57     // state for running code on the main thread on behalf of the owner
58     pthread_mutex_t                             maMainThreadMutex;
59     pthread_cond_t                              maMainThreadCond;
60     pthread_cond_t                              maResultCond;
61     bool                                        mbWakeUpMain;
62     bool                                        mbMainWaiting;
63     bool                                        mbResultReady;
64     const boost::function0<void>*               mpMainThreadFunc;
65     std::exception_ptr                          maMainThreadException;
66     bool                                        mbRunningForOwner;  // main thread only
67 
68     bool                                        isMainThread() const;
69     void                                        acquireOnMainThread();
70     void                                        runForOwner( const boost::function0<void>* pFunc );
71 
72 public:
73                                                 SalYieldMutex();
74                                                 ~SalYieldMutex();
75     virtual void                                acquire();
76     virtual void                                release();
77     virtual sal_Bool                            tryToAcquire();
GetAcquireCount()78     sal_uLong                                       GetAcquireCount() const { return mnCount; }
GetThreadId()79     vos::OThread::TThreadIdentifier             GetThreadId() const { return mnThreadId; }
80 
setMainThread(vos::OThread::TThreadIdentifier nId)81     void                                        setMainThread( vos::OThread::TThreadIdentifier nId ) { mnMainThreadId = nId; }
82     // runs rFunc on the main thread while the calling thread keeps owning the mutex
83     void                                        runOnMainThread( const boost::function0<void>& rFunc );
84     // true while the main thread runs code for the owner; the main thread then counts as owner
85     bool                                        isRunningForOwner() const;
86     // runs a function handed over by runOnMainThread(), if any; main thread only
87     void                                        runPendingOnMainThread();
88 };
89 
90 #define YIELD_GUARD vos::OGuard aGuard( GetSalData()->mpFirstInstance->GetYieldMutex() )
91 
92 
93 // -------------------
94 // - SalInstanceData -
95 // -------------------
96 
97 //struct SalInstanceData
98 //{
99 //public:
100 //};
101 
102 // ------------------
103 // - AquaSalInstance -
104 // ------------------
105 
106 class AquaSalInstance : public SalInstance
107 {
108     struct SalUserEvent
109     {
110         AquaSalFrame*   mpFrame;
111         void*           mpData;
112         sal_uInt16          mnType;
113 
SalUserEventSalUserEvent114         SalUserEvent( AquaSalFrame* pFrame, void* pData, sal_uInt16 nType ) :
115             mpFrame( pFrame ), mpData( pData ), mnType( nType )
116         {}
117     };
118 
119 public:
120     SalYieldMutex*                          mpSalYieldMutex;        // Sal-Yield-Mutex
121     rtl::OUString                           maDefaultPrinter;
122     vos::OThread::TThreadIdentifier         maMainThread;
123     bool                                    mbWaitingYield;
124     int                                     mnActivePrintJobs;
125     std::list< SalUserEvent >               maUserEvents;
126     oslMutex                                maUserEventListMutex;
127     oslCondition                            maWaitingYieldCond;
128 
129     typedef std::list<const ApplicationEvent*> AppEventList;
130     static AppEventList aAppEventList;
131 
132 public:
133     AquaSalInstance();
134     virtual ~AquaSalInstance();
135 
136     virtual SalSystem*      CreateSystem();
137     virtual void            DestroySystem(SalSystem*);
138     virtual SalFrame*       CreateChildFrame( SystemParentData* pParent, sal_uLong nStyle );
139     virtual SalFrame*       CreateFrame( SalFrame* pParent, sal_uLong nStyle );
140     virtual void            DestroyFrame( SalFrame* pFrame );
141     virtual SalObject*      CreateObject( SalFrame* pParent, SystemWindowData* pWindowData, sal_Bool bShow = sal_True );
142     virtual void            DestroyObject( SalObject* pObject );
143     virtual SalVirtualDevice*   CreateVirtualDevice( SalGraphics* pGraphics,
144                                                      long nDX, long nDY,
145                                                      sal_uInt16 nBitCount, const SystemGraphicsData *pData );
146     virtual void            DestroyVirtualDevice( SalVirtualDevice* pDevice );
147 
148     virtual SalInfoPrinter* CreateInfoPrinter( SalPrinterQueueInfo* pQueueInfo,
149                                                ImplJobSetup* pSetupData );
150     virtual void            DestroyInfoPrinter( SalInfoPrinter* pPrinter );
151     virtual SalPrinter*     CreatePrinter( SalInfoPrinter* pInfoPrinter );
152     virtual void            DestroyPrinter( SalPrinter* pPrinter );
153     virtual void            GetPrinterQueueInfo( ImplPrnQueueList* pList );
154     virtual void            GetPrinterQueueState( SalPrinterQueueInfo* pInfo );
155     virtual void            DeletePrinterQueueInfo( SalPrinterQueueInfo* pInfo );
156     virtual String             GetDefaultPrinter();
157     virtual SalTimer*           CreateSalTimer();
158     virtual SalI18NImeStatus*   CreateI18NImeStatus();
159     virtual SalSystem*          CreateSalSystem();
160     virtual SalBitmap*          CreateSalBitmap();
161     virtual vos::IMutex*        GetYieldMutex();
162     virtual sal_uLong               ReleaseYieldMutex();
163     virtual void                AcquireYieldMutex( sal_uLong nCount );
164     virtual bool                CheckYieldMutex();
165     virtual void                Yield( bool bWait, bool bHandleAllCurrentEvents );
166     virtual bool                AnyInput( sal_uInt16 nType );
167     virtual SalMenu*            CreateMenu( sal_Bool bMenuBar, Menu* pVCLMenu );
168     virtual void                DestroyMenu( SalMenu* );
169     virtual SalMenuItem*        CreateMenuItem( const SalItemParams* pItemData );
170     virtual void                DestroyMenuItem( SalMenuItem* );
171     virtual SalSession*         CreateSalSession();
172     virtual void*               GetConnectionIdentifier( ConnectionIdentifierType& rReturnedType, int& rReturnedBytes );
173     virtual void                AddToRecentDocumentList(const rtl::OUString& rFileUrl, const rtl::OUString& rMimeType);
174     virtual void            SetEventCallback( void* pInstance, bool(*pCallback)(void*,void*,int) );
175     virtual void            SetErrorEventCallback( void* pInstance, bool(*pCallback)(void*,void*,int) );
176 
177     // dtrans implementation
178     virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
179         CreateClipboard( const com::sun::star::uno::Sequence< com::sun::star::uno::Any >& i_rArguments );
180     virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface > CreateDragSource();
181     virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface > CreateDropTarget();
182 
183     static void handleAppDefinedEvent( NSEvent* pEvent );
184 
185     // check whether a particular string is passed on the command line
186     // this is needed to avoid duplicate open events through a) command line and b) NSApp's openFile
187     static bool isOnCommandLine( const rtl::OUString& );
188 
189     void wakeupYield();
190 
assignResult(R * pResult,const boost::function0<R> & rFunc)191     template< typename R > static void assignResult( R* pResult, const boost::function0<R>& rFunc )
192     {
193         *pResult = rFunc();
194     }
195 
196  public:
197     friend class AquaSalFrame;
198 
199     void PostUserEvent( AquaSalFrame* pFrame, sal_uInt16 nType, void* pData );
200     void delayedSettingsChanged( bool bInvalidate );
201 
202     bool isNSAppThread() const;
203 
204     // AppKit raises or deadlocks when used off the main thread (e.g. by remote UNO calls):
205     // off the main thread these run rFunc there and return true, on it they return false
206     bool forwardToMainThread( const boost::function0<void>& rFunc );
callOnMainThread(const boost::function0<R> & rFunc)207     template< typename R > R callOnMainThread( const boost::function0<R>& rFunc )
208     {
209         R aResult = R();
210         mpSalYieldMutex->runOnMainThread( boost::bind( &AquaSalInstance::assignResult<R>, &aResult, boost::cref( rFunc ) ) );
211         return aResult;
212     }
213 
startedPrintJob()214     void startedPrintJob() { mnActivePrintJobs++; }
endedPrintJob()215     void endedPrintJob() { mnActivePrintJobs--; }
216 
217     // event subtypes for NSApplicationDefined events
218     static const short AppExecuteSVMain   = 0x7fff;
219     static const short AppEndLoopEvent    = 1;
220     static const short AppStartTimerEvent = 10;
221     static const short AppleRemoteEvent   = 15;
222     static const short YieldWakeupEvent   = 20;
223 
224     static NSMenu* GetDynamicDockMenu();
225 };
226 
227 // helper class: inverted solar guard
228 class YieldMutexReleaser
229 {
230     sal_uLong mnCount;
231     public:
232     YieldMutexReleaser();
233     ~YieldMutexReleaser();
234 };
235 
236 // helper class
237 rtl::OUString GetOUString( CFStringRef );
238 rtl::OUString GetOUString( NSString* );
239 CFStringRef CreateCFString( const rtl::OUString& );
240 NSString* CreateNSString( const rtl::OUString& );
241 
242 CGImageRef CreateCGImage( const Image& );
243 NSImage*   CreateNSImage( const Image& );
244 
245 #endif // _SV_SALINST_H
246