/PC/os2emx/pythonpm.c

http://unladen-swallow.googlecode.com/ · C · 124 lines · 75 code · 16 blank · 33 comment · 4 complexity · 0c0deccd5ac8230f754f4dfdb24fc944 MD5 · raw file

  1. /* OS/2 PM main program - creates a hidden window, and starts Python
  2. * interpreter in a separate thread, so that Python scripts can be
  3. * run in PM process space without a console Window. The interpreter
  4. * is incorporated by linking in the Python DLL.
  5. *
  6. * As it stands, I don't think this is adequate for supporting Python
  7. * GUI modules, as the Python thread doesn't have its own message
  8. * queue - which is required of threads that want to create/use
  9. * PM windows.
  10. *
  11. * This code owes a lot to "OS/2 Presentation Manager Programming", by
  12. * Charles Petzold.
  13. *
  14. * Andrew MacIntyre <andymac@bullseye.apana.org.au>, August 2001.
  15. * Released under the terms of the Python 2.1.1 licence - see the LICENCE
  16. * file in the Python v2.1.1 (or later) source distribution.
  17. * Copyright assigned to the Python Software Foundation, 2001.
  18. */
  19. #define INCL_DOS
  20. #define INCL_WIN
  21. #include <os2.h>
  22. #include <process.h>
  23. #include "Python.h"
  24. /* use structure to pass command line to Python thread */
  25. typedef struct
  26. {
  27. int argc;
  28. char **argv;
  29. HWND Frame;
  30. int running;
  31. } arglist;
  32. /* make this a global to simplify access.
  33. * it should only be set from the Python thread, or by the code that
  34. * initiates the Python thread when the thread cannot be created.
  35. */
  36. int PythonRC;
  37. extern DL_EXPORT(int) Py_Main(int, char **);
  38. void PythonThread(void *);
  39. int
  40. main(int argc, char **argv)
  41. {
  42. ULONG FrameFlags = FCF_TITLEBAR |
  43. FCF_SYSMENU |
  44. FCF_SIZEBORDER |
  45. FCF_HIDEBUTTON |
  46. FCF_SHELLPOSITION |
  47. FCF_TASKLIST;
  48. HAB hab;
  49. HMQ hmq;
  50. HWND Client;
  51. QMSG qmsg;
  52. arglist args;
  53. int python_tid;
  54. /* init PM and create message queue */
  55. hab = WinInitialize(0);
  56. hmq = WinCreateMsgQueue(hab, 0);
  57. /* create a (hidden) Window to house the window procedure */
  58. args.Frame = WinCreateStdWindow(HWND_DESKTOP,
  59. 0,
  60. &FrameFlags,
  61. NULL,
  62. "PythonPM",
  63. 0L,
  64. 0,
  65. 0,
  66. &Client);
  67. /* run Python interpreter in a thread */
  68. args.argc = argc;
  69. args.argv = argv;
  70. args.running = 0;
  71. if (-1 == (python_tid = _beginthread(PythonThread, NULL, 1024 * 1024, &args)))
  72. {
  73. /* couldn't start thread */
  74. WinAlarm(HWND_DESKTOP, WA_ERROR);
  75. PythonRC = 1;
  76. }
  77. else
  78. {
  79. /* process PM messages, until Python exits */
  80. while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
  81. WinDispatchMsg(hab, &qmsg);
  82. if (args.running > 0)
  83. DosKillThread(python_tid);
  84. }
  85. /* destroy window, shutdown message queue and PM */
  86. WinDestroyWindow(args.Frame);
  87. WinDestroyMsgQueue(hmq);
  88. WinTerminate(hab);
  89. return PythonRC;
  90. }
  91. void PythonThread(void *argl)
  92. {
  93. HAB hab;
  94. arglist *args;
  95. /* PM initialisation */
  96. hab = WinInitialize(0);
  97. /* start Python */
  98. args = (arglist *)argl;
  99. args->running = 1;
  100. PythonRC = Py_Main(args->argc, args->argv);
  101. /* enter a critical section and send the termination message */
  102. DosEnterCritSec();
  103. args->running = 0;
  104. WinPostMsg(args->Frame, WM_QUIT, NULL, NULL);
  105. /* shutdown PM and terminate thread */
  106. WinTerminate(hab);
  107. _endthread();
  108. }