creator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  17. * All rights reserved.
  18. */
  19. /** \file
  20. * \ingroup creator
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef WIN32
  25. # if defined(_MSC_VER) && defined(_M_X64)
  26. # include <math.h> /* needed for _set_FMA3_enable */
  27. # endif
  28. # include <windows.h>
  29. # include "utfconv.h"
  30. #endif
  31. #include "MEM_guardedalloc.h"
  32. #include "CLG_log.h"
  33. #include "DNA_genfile.h"
  34. #include "BLI_args.h"
  35. #include "BLI_threads.h"
  36. #include "BLI_utildefines.h"
  37. #include "BLI_callbacks.h"
  38. #include "BLI_string.h"
  39. #include "BLI_system.h"
  40. /* mostly init functions */
  41. #include "BKE_appdir.h"
  42. #include "BKE_blender.h"
  43. #include "BKE_brush.h"
  44. #include "BKE_cachefile.h"
  45. #include "BKE_context.h"
  46. #include "BKE_font.h"
  47. #include "BKE_global.h"
  48. #include "BKE_material.h"
  49. #include "BKE_modifier.h"
  50. #include "BKE_gpencil_modifier.h"
  51. #include "BKE_node.h"
  52. #include "BKE_shader_fx.h"
  53. #include "BKE_sound.h"
  54. #include "BKE_image.h"
  55. #include "BKE_particle.h"
  56. #include "DEG_depsgraph.h"
  57. #include "IMB_imbuf.h" /* for IMB_init */
  58. #include "RE_engine.h"
  59. #include "RE_render_ext.h"
  60. #include "ED_datafiles.h"
  61. #include "WM_api.h"
  62. #include "WM_toolsystem.h"
  63. #include "RNA_define.h"
  64. #ifdef WITH_FREESTYLE
  65. # include "FRS_freestyle.h"
  66. #endif
  67. #include <signal.h>
  68. #ifdef __FreeBSD__
  69. # include <floatingpoint.h>
  70. #endif
  71. #ifdef WITH_BINRELOC
  72. # include "binreloc.h"
  73. #endif
  74. #ifdef WITH_LIBMV
  75. # include "libmv-capi.h"
  76. #endif
  77. #ifdef WITH_CYCLES_LOGGING
  78. # include "CCL_api.h"
  79. #endif
  80. #ifdef WITH_SDL_DYNLOAD
  81. # include "sdlew.h"
  82. #endif
  83. #include "creator_intern.h" /* own include */
  84. /* Local Function prototypes */
  85. #ifdef WITH_PYTHON_MODULE
  86. int main_python_enter(int argc, const char **argv);
  87. void main_python_exit(void);
  88. #endif
  89. /* written to by 'creator_args.c' */
  90. struct ApplicationState app_state = {
  91. .signal =
  92. {
  93. .use_crash_handler = true,
  94. .use_abort_handler = true,
  95. },
  96. .exit_code_on_error =
  97. {
  98. .python = 0,
  99. },
  100. };
  101. /* -------------------------------------------------------------------- */
  102. /** \name Application Level Callbacks
  103. *
  104. * Initialize callbacks for the modules that need them.
  105. *
  106. * \{ */
  107. static void callback_mem_error(const char *errorStr)
  108. {
  109. fputs(errorStr, stderr);
  110. fflush(stderr);
  111. }
  112. static void main_callback_setup(void)
  113. {
  114. /* Error output from the alloc routines: */
  115. MEM_set_error_callback(callback_mem_error);
  116. }
  117. /* free data on early exit (if Python calls 'sys.exit()' while parsing args for eg). */
  118. struct CreatorAtExitData {
  119. bArgs *ba;
  120. #ifdef WIN32
  121. const char **argv;
  122. int argv_num;
  123. #endif
  124. };
  125. static void callback_main_atexit(void *user_data)
  126. {
  127. struct CreatorAtExitData *app_init_data = user_data;
  128. if (app_init_data->ba) {
  129. BLI_argsFree(app_init_data->ba);
  130. app_init_data->ba = NULL;
  131. }
  132. #ifdef WIN32
  133. if (app_init_data->argv) {
  134. while (app_init_data->argv_num) {
  135. free((void *)app_init_data->argv[--app_init_data->argv_num]);
  136. }
  137. free((void *)app_init_data->argv);
  138. app_init_data->argv = NULL;
  139. }
  140. #endif
  141. }
  142. static void callback_clg_fatal(void *fp)
  143. {
  144. BLI_system_backtrace(fp);
  145. }
  146. /** \} */
  147. /* -------------------------------------------------------------------- */
  148. /** \name Main Function
  149. * \{ */
  150. #ifdef WITH_PYTHON_MODULE
  151. /* allow python module to call main */
  152. # define main main_python_enter
  153. static void *evil_C = NULL;
  154. # ifdef __APPLE__
  155. /* environ is not available in mac shared libraries */
  156. # include <crt_externs.h>
  157. char **environ = NULL;
  158. # endif
  159. #endif
  160. /**
  161. * Blender's main function responsibilities are:
  162. * - setup subsystems.
  163. * - handle arguments.
  164. * - run #WM_main() event loop,
  165. * or exit immediately when running in background mode.
  166. */
  167. int main(int argc,
  168. #ifdef WIN32
  169. const char **UNUSED(argv_c)
  170. #else
  171. const char **argv
  172. #endif
  173. )
  174. {
  175. bContext *C;
  176. #ifndef WITH_PYTHON_MODULE
  177. bArgs *ba;
  178. #endif
  179. #ifdef WIN32
  180. char **argv;
  181. int argv_num;
  182. #endif
  183. /* --- end declarations --- */
  184. /* ensure we free data on early-exit */
  185. struct CreatorAtExitData app_init_data = {NULL};
  186. BKE_blender_atexit_register(callback_main_atexit, &app_init_data);
  187. /* Unbuffered stdout makes stdout and stderr better synchronized, and helps
  188. * when stepping through code in a debugger (prints are immediately
  189. * visible). */
  190. setvbuf(stdout, NULL, _IONBF, 0);
  191. #ifdef WIN32
  192. /* We delay loading of openmp so we can set the policy here. */
  193. # if defined(_MSC_VER)
  194. _putenv_s("OMP_WAIT_POLICY", "PASSIVE");
  195. # endif
  196. /* FMA3 support in the 2013 CRT is broken on Vista and Windows 7 RTM
  197. * (fixed in SP1). Just disable it. */
  198. # if defined(_MSC_VER) && defined(_M_X64)
  199. _set_FMA3_enable(0);
  200. # endif
  201. /* Win32 Unicode Args */
  202. /* NOTE: cannot use guardedalloc malloc here, as it's not yet initialized
  203. * (it depends on the args passed in, which is what we're getting here!)
  204. */
  205. {
  206. wchar_t **argv_16 = CommandLineToArgvW(GetCommandLineW(), &argc);
  207. argv = malloc(argc * sizeof(char *));
  208. for (argv_num = 0; argv_num < argc; argv_num++) {
  209. argv[argv_num] = alloc_utf_8_from_16(argv_16[argv_num], 0);
  210. }
  211. LocalFree(argv_16);
  212. /* free on early-exit */
  213. app_init_data.argv = argv;
  214. app_init_data.argv_num = argv_num;
  215. }
  216. #endif /* WIN32 */
  217. /* NOTE: Special exception for guarded allocator type switch:
  218. * we need to perform switch from lock-free to fully
  219. * guarded allocator before any allocation happened.
  220. */
  221. {
  222. int i;
  223. for (i = 0; i < argc; i++) {
  224. if (STR_ELEM(argv[i], "-d", "--debug", "--debug-memory", "--debug-all")) {
  225. printf("Switching to fully guarded memory allocator.\n");
  226. MEM_use_guarded_allocator();
  227. break;
  228. }
  229. else if (STREQ(argv[i], "--")) {
  230. break;
  231. }
  232. }
  233. }
  234. #ifdef BUILD_DATE
  235. {
  236. time_t temp_time = build_commit_timestamp;
  237. struct tm *tm = gmtime(&temp_time);
  238. if (LIKELY(tm)) {
  239. strftime(build_commit_date, sizeof(build_commit_date), "%Y-%m-%d", tm);
  240. strftime(build_commit_time, sizeof(build_commit_time), "%H:%M", tm);
  241. }
  242. else {
  243. const char *unknown = "date-unknown";
  244. BLI_strncpy(build_commit_date, unknown, sizeof(build_commit_date));
  245. BLI_strncpy(build_commit_time, unknown, sizeof(build_commit_time));
  246. }
  247. }
  248. #endif
  249. #ifdef WITH_SDL_DYNLOAD
  250. sdlewInit();
  251. #endif
  252. /* Initialize logging */
  253. CLG_init();
  254. CLG_fatal_fn_set(callback_clg_fatal);
  255. C = CTX_create();
  256. #ifdef WITH_PYTHON_MODULE
  257. # ifdef __APPLE__
  258. environ = *_NSGetEnviron();
  259. # endif
  260. # undef main
  261. evil_C = C;
  262. #endif
  263. #ifdef WITH_BINRELOC
  264. br_init(NULL);
  265. #endif
  266. #ifdef WITH_LIBMV
  267. libmv_initLogging(argv[0]);
  268. #elif defined(WITH_CYCLES_LOGGING)
  269. CCL_init_logging(argv[0]);
  270. #endif
  271. main_callback_setup();
  272. #if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE)
  273. /* patch to ignore argument finder gives us (pid?) */
  274. if (argc == 2 && STREQLEN(argv[1], "-psn_", 5)) {
  275. extern int GHOST_HACK_getFirstFile(char buf[]);
  276. static char firstfilebuf[512];
  277. argc = 1;
  278. if (GHOST_HACK_getFirstFile(firstfilebuf)) {
  279. argc = 2;
  280. argv[1] = firstfilebuf;
  281. }
  282. }
  283. #endif
  284. #ifdef __FreeBSD__
  285. fpsetmask(0);
  286. #endif
  287. /* initialize path to executable */
  288. BKE_appdir_program_path_init(argv[0]);
  289. BLI_threadapi_init();
  290. BLI_thread_put_process_on_fast_node();
  291. DNA_sdna_current_init();
  292. BKE_blender_globals_init(); /* blender.c */
  293. IMB_init();
  294. BKE_cachefiles_init();
  295. BKE_images_init();
  296. BKE_modifier_init();
  297. BKE_gpencil_modifier_init();
  298. BKE_shaderfx_init();
  299. DEG_register_node_types();
  300. BKE_brush_system_init();
  301. RE_texture_rng_init();
  302. BLI_callback_global_init();
  303. /* first test for background */
  304. #ifndef WITH_PYTHON_MODULE
  305. ba = BLI_argsInit(argc, (const char **)argv); /* skip binary path */
  306. /* ensure we free on early exit */
  307. app_init_data.ba = ba;
  308. main_args_setup(C, ba);
  309. BLI_argsParse(ba, 1, NULL, NULL);
  310. main_signal_setup();
  311. #else
  312. /* using preferences or user startup makes no sense for py-as-module */
  313. G.factory_startup = true;
  314. #endif
  315. #ifdef WITH_FFMPEG
  316. IMB_ffmpeg_init();
  317. #endif
  318. /* after level 1 args, this is so playanim skips RNA init */
  319. RNA_init();
  320. RE_engines_init();
  321. init_nodesystem();
  322. psys_init_rng();
  323. /* end second init */
  324. #if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
  325. G.background = true; /* python module mode ALWAYS runs in background mode (for now) */
  326. #else
  327. if (G.background) {
  328. main_signal_setup_background();
  329. }
  330. #endif
  331. /* background render uses this font too */
  332. BKE_vfont_builtin_register(datatoc_bfont_pfb, datatoc_bfont_pfb_size);
  333. /* Initialize ffmpeg if built in, also needed for bg mode if videos are
  334. * rendered via ffmpeg */
  335. BKE_sound_init_once();
  336. init_def_material();
  337. if (G.background == 0) {
  338. #ifndef WITH_PYTHON_MODULE
  339. BLI_argsParse(ba, 2, NULL, NULL);
  340. BLI_argsParse(ba, 3, NULL, NULL);
  341. #endif
  342. WM_init(C, argc, (const char **)argv);
  343. /* this is properly initialized with user defs, but this is default */
  344. /* call after loading the startup.blend so we can read U.tempdir */
  345. BKE_tempdir_init(U.tempdir);
  346. }
  347. else {
  348. #ifndef WITH_PYTHON_MODULE
  349. BLI_argsParse(ba, 3, NULL, NULL);
  350. #endif
  351. WM_init(C, argc, (const char **)argv);
  352. /* don't use user preferences temp dir */
  353. BKE_tempdir_init(NULL);
  354. }
  355. #ifdef WITH_PYTHON
  356. /**
  357. * NOTE: the U.pythondir string is NULL until WM_init() is executed,
  358. * so we provide the BPY_ function below to append the user defined
  359. * python-dir to Python's sys.path at this point. Simply putting
  360. * WM_init() before #BPY_python_start() crashes Blender at startup.
  361. */
  362. /* TODO - U.pythondir */
  363. #else
  364. printf(
  365. "\n* WARNING * - Blender compiled without Python!\n"
  366. "this is not intended for typical usage\n\n");
  367. #endif
  368. CTX_py_init_set(C, 1);
  369. WM_keyconfig_init(C);
  370. #ifdef WITH_FREESTYLE
  371. /* initialize Freestyle */
  372. FRS_initialize();
  373. FRS_set_context(C);
  374. #endif
  375. /* OK we are ready for it */
  376. #ifndef WITH_PYTHON_MODULE
  377. main_args_setup_post(C, ba);
  378. #endif
  379. /* Explicitly free data allocated for argument parsing:
  380. * - 'ba'
  381. * - 'argv' on WIN32.
  382. */
  383. callback_main_atexit(&app_init_data);
  384. BKE_blender_atexit_unregister(callback_main_atexit, &app_init_data);
  385. /* paranoid, avoid accidental re-use */
  386. #ifndef WITH_PYTHON_MODULE
  387. ba = NULL;
  388. (void)ba;
  389. #endif
  390. #ifdef WIN32
  391. argv = NULL;
  392. (void)argv;
  393. #endif
  394. #ifdef WITH_PYTHON_MODULE
  395. return 0; /* keep blender in background mode running */
  396. #endif
  397. if (G.background) {
  398. /* Using window-manager API in background mode is a bit odd, but works fine. */
  399. WM_exit(C);
  400. }
  401. else {
  402. if (!G.file_loaded) {
  403. WM_init_splash(C);
  404. }
  405. }
  406. WM_main(C);
  407. return 0;
  408. } /* end of int main(argc, argv) */
  409. #ifdef WITH_PYTHON_MODULE
  410. void main_python_exit(void)
  411. {
  412. WM_exit_ext((bContext *)evil_C, true);
  413. evil_C = NULL;
  414. }
  415. #endif
  416. /** \} */