~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/src/play/win/cygapp.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * cygapp.c -- $Id: cygapp.c,v 1.1 2003/03/08 15:26:49 travo Exp $
 
3
 * cygwin (or uwin?) main program stub
 
4
 *
 
5
 * Copyright (c) 2000.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#include "playw.h"
 
9
#include "pstdlib.h"
 
10
 
 
11
extern int cyg_app(int (*on_launch)(int, char **),
 
12
                   HINSTANCE me, LPSTR cmd_line, int show0);
 
13
 
 
14
static int (*w_on_launch)(int ,char **)= 0;
 
15
static void w_get_cmds(LPSTR cmd_line);
 
16
static int w_argc = 0;
 
17
static char **w_argv = 0;
 
18
static char *w_str_cpy(HANDLE heap, const char *text, long len);
 
19
static int w_result = 0;
 
20
 
 
21
static int w_loop(void);
 
22
static BOOL WINAPI w_ctrlc_handler(DWORD type);
 
23
 
 
24
static void cyg_quit(void);
 
25
 
 
26
int
 
27
cyg_app(int (*on_launch)(int, char **),
 
28
        HINSTANCE me, LPSTR cmd_line, int show0)
 
29
{
 
30
  HMODULE hm = me;
 
31
  HWND hw = CreateWindow(w_win_class, "dummy main wnd", 0,
 
32
                        CW_USEDEFAULT, 0, 1, 1, 0, 0, hm, 0);
 
33
 
 
34
  w_initialize(hm, hw, cyg_quit, 0, 0);
 
35
 
 
36
  /* this should not be necessary for windows subsystem */
 
37
  SetConsoleCtrlHandler(w_ctrlc_handler, 1);
 
38
 
 
39
  w_on_launch = on_launch;
 
40
  w_get_cmds(cmd_line);
 
41
  w_protect(w_loop);
 
42
  if (w_result) return w_result;
 
43
 
 
44
  return w_on_quit();
 
45
}
 
46
 
 
47
static void
 
48
cyg_quit(void)
 
49
{
 
50
  PostQuitMessage(0);
 
51
}
 
52
 
 
53
static int
 
54
w_loop(void)
 
55
{
 
56
  MSG msg;
 
57
  if (w_on_launch) {
 
58
    int (*on_launch)(int, char **)= w_on_launch;
 
59
    w_on_launch = 0;
 
60
    p_mminit();
 
61
    w_pollinit();
 
62
    w_result = on_launch(w_argc, w_argv);
 
63
    if (w_result) return w_result;
 
64
  }
 
65
  /* event loop closely matches mfc default CWinApp::Run */
 
66
  for (;;) {
 
67
    while (!PeekMessage(&msg, 0, 0,0, PM_REMOVE))
 
68
      if (!w_work_idle()) {
 
69
        GetMessage(&msg, 0, 0,0);
 
70
        break;
 
71
      }
 
72
    if (msg.message == WM_QUIT) break;
 
73
    if (!w_app_msg(&msg)) {
 
74
      TranslateMessage(&msg);
 
75
      DispatchMessage(&msg);
 
76
    }
 
77
  }
 
78
  return msg.wParam;
 
79
}
 
80
 
 
81
static void
 
82
w_get_cmds(LPSTR cmd_line)
 
83
{
 
84
  HANDLE heap = GetProcessHeap();
 
85
  char module_name[1028];
 
86
  DWORD len = GetModuleFileName(w_app_instance, module_name, 1024);
 
87
  module_name[len] = '\0';
 
88
  w_argc = 0;
 
89
  w_argv = (char**)HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, sizeof(char *)*9);
 
90
  w_argv[w_argc++] = w_str_cpy(heap, w_unixpath(module_name), len);
 
91
  if (cmd_line) {
 
92
    char *c = cmd_line;
 
93
    char delim;
 
94
    for (;;) {
 
95
      while (c[0]==' ' || c[0]=='\t' || c[0]=='\r' || c[0]=='\n') c++;
 
96
      delim = c[0];
 
97
      if (!delim) break;
 
98
      cmd_line = c;
 
99
      if (delim=='"' || delim=='\'') {
 
100
        cmd_line = ++c;
 
101
        while (c[0] && c[0]!=delim) c++;
 
102
      } else {
 
103
        while (c[0] && c[0]!=' ' && c[0]!='\t' &&
 
104
               c[0]!='\r' && c[0]!='\n') c++;
 
105
        delim = 'x';
 
106
      }
 
107
      if (w_argc>1 || cmd_line[0]!='-'||cmd_line[1]!='n'||cmd_line[2]!='o'||
 
108
          cmd_line[3]!='m'||cmd_line[4]!='d'||cmd_line[5]!='i') {
 
109
        if (!(w_argc&7))
 
110
          w_argv = (char **)HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS,
 
111
                                       w_argv, sizeof(char *)*(2*w_argc+1));
 
112
        w_argv[w_argc++] = w_str_cpy(heap, cmd_line, c - cmd_line);
 
113
      } else {
 
114
        w_no_mdi = 1;
 
115
      }
 
116
      if (c[0] == delim) c++;
 
117
      cmd_line = c;
 
118
    }
 
119
  }
 
120
  w_argv[w_argc] = 0;
 
121
}
 
122
 
 
123
static char *
 
124
w_str_cpy(HANDLE heap, const char *text, long len)
 
125
{
 
126
  if (!len) while (text[len]) len++;
 
127
  {
 
128
    char *buf = (char *)HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, len+1);
 
129
    char *buffer = buf;
 
130
    while (len--) *buf++= *text++;
 
131
    buf[0] = '\0';
 
132
    return buffer;
 
133
  }
 
134
}
 
135
 
 
136
static BOOL WINAPI
 
137
w_ctrlc_handler(DWORD type)
 
138
{ /* this function runs in its own thread */
 
139
  return (type==CTRL_C_EVENT)? w_sigint(1) : FALSE;
 
140
}