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