xref: /aoo41x/main/vcl/source/app/svmainhook.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 #include <tools/tools.h>
31 
32 #ifndef MACOSX
33 
34 sal_Bool ImplSVMainHook( sal_Bool * )
35 {
36     return sal_False;   // indicate that ImplSVMainHook is not implemented
37 }
38 
39 #else
40 // MACOSX cocoa implementation of ImplSVMainHook is in aqua/source/app/salinst.cxx
41 #ifndef QUARTZ  // MACOSX (X11) needs the CFRunLoop()
42 #include <osl/thread.h>
43 #include <premac.h>
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <postmac.h>
46 #include <unistd.h>
47 
48 extern sal_Bool ImplSVMain();
49 
50 // ============================================================================
51 
52 
53 static void SourceContextCallBack( void *pInfo )
54 {
55 }
56 
57 struct ThreadContext
58 {
59     sal_Bool* pRet;
60     CFRunLoopRef* pRunLoopRef;
61 };
62 
63 static void RunSVMain(void *pData)
64 {
65     ThreadContext* tcx = reinterpret_cast<ThreadContext*>(pData);
66 
67     // busy waiting (ok in this case) until the run loop is
68     // running
69     while (!CFRunLoopIsWaiting(*tcx->pRunLoopRef))
70         usleep(100);
71 
72     *tcx->pRet = ImplSVMain();
73 
74     // Force exit since some JVMs won't shutdown when only exit() is invoked
75     _exit( 0 );
76 }
77 
78 sal_Bool ImplSVMainHook( sal_Bool *pbInit )
79 {
80     // Mac OS X requires that any Cocoa code have a CFRunLoop started in the
81     // primordial thread. Since all of the AWT classes in Java 1.4 and higher
82     // are written in Cocoa, we need to start the CFRunLoop here and run
83     // ImplSVMain() in a secondary thread.
84     // See http://developer.apple.com/samplecode/simpleJavaLauncher/listing3.html
85     // for further details and an example
86 
87     CFRunLoopRef runLoopRef = CFRunLoopGetCurrent();
88     ThreadContext tcx;
89     tcx.pRet = pbInit;  // the return value
90     tcx.pRunLoopRef = &runLoopRef;
91     oslThread hThreadID = osl_createThread(RunSVMain, &tcx);
92 
93     // Start the CFRunLoop
94     CFRunLoopSourceContext aSourceContext;
95     aSourceContext.version = 0;
96     aSourceContext.info = NULL;
97     aSourceContext.retain = NULL;
98     aSourceContext.release = NULL;
99     aSourceContext.copyDescription = NULL;
100     aSourceContext.equal = NULL;
101     aSourceContext.hash = NULL;
102     aSourceContext.schedule = NULL;
103     aSourceContext.cancel = NULL;
104     aSourceContext.perform = &SourceContextCallBack;
105     CFRunLoopSourceRef aSourceRef = CFRunLoopSourceCreate(NULL, 0, &aSourceContext);
106     CFRunLoopAddSource(runLoopRef, aSourceRef, kCFRunLoopCommonModes);
107     CFRunLoopRun();
108 
109     osl_joinWithThread( hThreadID );
110     osl_destroyThread( hThreadID );
111 
112     return sal_True;    // indicate that ImplSVMainHook is implemented
113 }
114 
115 #endif // MACOSX
116 #endif
117