p3dPythonMain.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Filename: p3dPythonMain.cxx
  2. // Created by: drose (29Aug09)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "run_p3dpython.h"
  15. #include <iostream>
  16. #include <sstream>
  17. #include <string.h> // strrchr
  18. using namespace std;
  19. #ifdef __APPLE__
  20. #include <Carbon/Carbon.h>
  21. extern "C" { void CPSEnableForegroundOperation(ProcessSerialNumber* psn); }
  22. #endif
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: main
  25. // Description: This is a trivial main() function that invokes
  26. // P3DPythonRun. It's used to build p3dpython.exe,
  27. // which is the preferred way to run Python in a child
  28. // process, as a separate executable.
  29. ////////////////////////////////////////////////////////////////////
  30. int
  31. main(int argc, char *argv[]) {
  32. const char *program_name = argv[0];
  33. const char *archive_file = NULL;
  34. const char *input_handle_str = NULL;
  35. const char *output_handle_str = NULL;
  36. const char *interactive_console_str = NULL;
  37. if (argc > 1) {
  38. archive_file = argv[1];
  39. }
  40. if (argc > 2) {
  41. input_handle_str = argv[2];
  42. }
  43. if (argc > 3) {
  44. output_handle_str = argv[3];
  45. }
  46. if (argc > 4) {
  47. interactive_console_str = argv[4];
  48. }
  49. if (archive_file == NULL || *archive_file == '\0') {
  50. cerr << "No archive filename specified on command line.\n";
  51. return 1;
  52. }
  53. FHandle input_handle = invalid_fhandle;
  54. if (input_handle_str != NULL && *input_handle_str) {
  55. stringstream stream(input_handle_str);
  56. stream >> input_handle;
  57. if (!stream) {
  58. input_handle = invalid_fhandle;
  59. }
  60. }
  61. FHandle output_handle = invalid_fhandle;
  62. if (output_handle_str != NULL && *output_handle_str) {
  63. stringstream stream(output_handle_str);
  64. stream >> output_handle;
  65. if (!stream) {
  66. output_handle = invalid_fhandle;
  67. }
  68. }
  69. bool interactive_console = false;
  70. if (interactive_console_str != NULL && *interactive_console_str) {
  71. stringstream stream(interactive_console_str);
  72. int flag;
  73. stream >> flag;
  74. if (!stream.fail()) {
  75. interactive_console = (flag != 0);
  76. }
  77. }
  78. #ifdef __APPLE__
  79. // In case the application is going to run a wx app, allow it to
  80. // have access to the desktop.
  81. ProcessSerialNumber psn;
  82. GetCurrentProcess(&psn);
  83. CPSEnableForegroundOperation(&psn);
  84. SetFrontProcess(&psn);
  85. #endif
  86. if (!run_p3dpython(program_name, archive_file, input_handle, output_handle,
  87. NULL, interactive_console)) {
  88. cerr << "Failure on startup.\n";
  89. return 1;
  90. }
  91. return 0;
  92. }