该文主要是windows平台下的代码分析,暂不针对其他平台。
1、相关宏的定义
在openoffice的源代码main\sal\inc\sal有个main.h的文件:其内容大致如下:
#define SAL_MAIN_IMPL \
int SAL_CALL main(int argc, char ** argv) \
{ \
int ret; \
sal_detail_initialize(argc, argv); \
ret = sal_main(); \
sal_detail_deinitialize(); \
return ret; \
}
#define SAL_WIN_WinMain \
int WINAPI WinMain( HINSTANCE _hinst, HINSTANCE _dummy, char* _cmdline, int _nshow ) \
{ \
int argc = __argc; char ** argv = __argv; \
(void) _hinst; (void) _dummy; (void) _cmdline; (void) _nshow; /* unused */ \
return main(argc, argv); \
}
#define SAL_IMPLEMENT_MAIN() \
static int SAL_CALL sal_main(void); \
SAL_MAIN_IMPL \
SAL_WIN_WinMain \
static int SAL_CALL sal_main(void)
/*
"How to use" Examples:
#include <sal/main.h>
SAL_IMPLEMENT_MAIN()
{
DoSomething();
return 0;
}
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
{
DoSomethingWithArgs(argc, argv);
return 0;
}
*/
2、启动代码
在main\desktop\source\app目录下有个Main.c文件,其大致内容如下:
#include "sal/config.h"
#include "sal/main.h"
#include "sofficemain.h"
SAL_IMPLEMENT_MAIN() {
return soffice_main();
}
把前面的宏定义联系起来,其代码大致如此:
static int SAL_CALL sal_main(void);
int SAL_CALL main(int argc, char ** argv)
{
int ret;
sal_detail_initialize(argc, argv);
ret = sal_main();
sal_detail_deinitialize();
return ret;
}
int WINAPI WinMain( HINSTANCE _hinst, HINSTANCE _dummy, char* _cmdline, int _nshow )
{
int argc = __argc; char ** argv = __argv;
(void) _hinst; (void) _dummy; (void) _cmdline; (void) _nshow; /* unused */
return main(argc, argv);
}
static int SAL_CALL sal_main(void)
{
return soffice_main();
}
这样的话,就知道其在windows平台下的功能函数入口点在soffice_main()中。
下面附上一段官方有关openOffice启动的分析:
- Construct the application object (“the desktop”) (C++ constructor).
- Invoke the “int main(int argc, char **argv)“ function of the executable (“soffice.bin”).
- Initialize VCL.
- Initialize the application object (“the desktop”).
- Bootstrap the initial Uno component context (Unos root object).
- Set the Uno service manger for the process.
- Create the configuration manager.
- Detect the locale and customize the configuration manager.
- Create the OOo to OOo IPC (Inter Process Communication) thread (named pipe communication).
- Activate the runtime error handler.
- Initialize the application object (“the desktop”).
- Invoke the “main” method of “the desktop” object.
- Handle bootstrap errors (show possible bootstrap errors to user and exit application).
- Create and expose the “splash screen”.
- Check for “user” configuration data, if not available, then:
- Create “user” configuration data..
- Check command line arguments for requested UNO acceptors, if requested, then:
- Create Uno acceptors.
- Initialize the UCB.
- Create directory for temporary data.
- Set user interface and application locale.
- Create global broadcaster service for application/document events.
- If “user” configuration data has been newly created, start wizard for license agreement/user data input.
- If no command line arguments have been passed, then:
- Create one instance framework desktop service.
- Create startup module service.
- Create frame and connects startup module to frame.
- If “quickstart” option has been passed on command line, then:
- Create “quickstarter” service, if available. (Note: the “quickstarter” is currently available on Windows only.)
- If to-be-loaded documents have been passed on command line, then:
- Post document-load commands into event loop.
- If to-be-printed documents have been passed on command line, then:
- Post document-print commands into event loop.
- If “headless mode” has been passed on command line, then
- Switch to internal dialogs (file open, printer select, etc.).
- Post IPC thread enable into event loop.
- Enter the global application message loop.
- Dispatch GUI and command messages, e.g.
- document creation / loading / printing,
- Uno acceptor creation / deletion,
- window messages (redraw, move, mouse click, keyboard, etc.).
- Leave loop, if
- “terminate” has been called on “the desktop” object, or
- Menu-bar “File – Exit” or
- keyboard shortcut “CTRL-Q” has been invoked.
- Dispatch GUI and command messages, e.g.
- Close all open documents (asks for allowance for modified documents).
- Block all IPC thread requests.
- Remove the global temporary directory.
- Stop all Uno acceptors
- De-initialize the UCB.
- De-initialize VCL.
- De-initialize “the desktop” object.
- Store all configuration changes.
- Dispose the Uno component context.
- Disable the IPC thread.
- Initialize VCL.
- Destruct “the desktop” object (C++ destructor).