| version 1.1, 2003/10/16 17:59:19 | version 1.10, 2004/03/09 16:31:18 | 
| Line 2 | Line 2 | 
 | #include        <stdarg.h> | #include        <stdarg.h> | 
 | #include        "strres.h" | #include        "strres.h" | 
 | #include        "dosio.h" | #include        "dosio.h" | 
 |  |  | 
 |  |  | 
 | // #define      WRITE_INI |  | 
 | #if defined(WRITE_INI) |  | 
 | #include        "ini.h" | #include        "ini.h" | 
| #endif |  | 
 |  |  | 
 | #ifdef TRACE | #ifdef TRACE | 
 |  |  | 
 |  | #define FILEBUFSIZE                     (1 << 20) | 
 |  | #define FILELASTBUFONLY | 
 |  |  | 
 | #ifdef STRICT | #ifdef STRICT | 
 | #define SUBCLASSPROC    WNDPROC | #define SUBCLASSPROC    WNDPROC | 
 | #else | #else | 
| Line 25 | Line 24 | 
 | #define VIEW_SIZE               12 | #define VIEW_SIZE               12 | 
 |  |  | 
 | typedef struct { | typedef struct { | 
 |  | BYTE    en; | 
 |  | FILEH   fh; | 
 |  | HWND    hwnd; | 
 |  | } TRACEWIN; | 
 |  |  | 
 |  | typedef struct { | 
 | int             posx; | int             posx; | 
 | int             posy; | int             posy; | 
 | int             width; | int             width; | 
| Line 34  typedef struct { | Line 39  typedef struct { | 
 | extern  HINSTANCE       hInst; | extern  HINSTANCE       hInst; | 
 | extern  HINSTANCE       hPrev; | extern  HINSTANCE       hPrev; | 
 |  |  | 
 |  | enum { | 
 |  | IDM_TRACEEN             = 3300, | 
 |  | IDM_TRACEFH, | 
 |  | IDM_TRACECL | 
 |  | }; | 
 |  |  | 
 | static const char       ProgTitle[] = "console"; | static const char       ProgTitle[] = "console"; | 
 | static const char       ClassName[] = "TRACE-console"; | static const char       ClassName[] = "TRACE-console"; | 
 | static const char       ClassEdit[] = "EDIT"; | static const char       ClassEdit[] = "EDIT"; | 
 |  | static const char       traceen[] = "Enable"; | 
 |  | static const char       tracefh[] = "File out"; | 
 |  | static const char       tracecl[] = "Clear"; | 
 | static const char       crlf[] = "\r\n"; | static const char       crlf[] = "\r\n"; | 
 |  |  | 
| static  HWND            hWndConsole = NULL; | static  TRACEWIN        tracewin; | 
 | static  HWND            hView = NULL; | static  HWND            hView = NULL; | 
 | static  HFONT           hfView = NULL; | static  HFONT           hfView = NULL; | 
 | static  HBRUSH          hBrush = NULL; | static  HBRUSH          hBrush = NULL; | 
 | static  char            szView[VIEW_BUFFERSIZE]; | static  char            szView[VIEW_BUFFERSIZE]; | 
 | static  TRACECFG        tracecfg; | static  TRACECFG        tracecfg; | 
 |  |  | 
| #if defined(WRITE_INI) | static const char       np2trace[] = "np2trace.ini"; | 
 | static const char       inititle[] = "TRACE"; | static const char       inititle[] = "TRACE"; | 
 | static const INITBL     initbl[4] = { | static const INITBL     initbl[4] = { | 
| {str_posx,              INITYPE_SINT32, &tracecfg.posx,         0}, | {"posx",        INITYPE_SINT32, &tracecfg.posx,         0}, | 
| {str_posy,              INITYPE_SINT32, &tracecfg.posy,         0}, | {"posy",        INITYPE_SINT32, &tracecfg.posy,         0}, | 
| {str_width,             INITYPE_SINT32, &tracecfg.width,        0}, | {"width",       INITYPE_SINT32, &tracecfg.width,        0}, | 
| {str_height,    INITYPE_SINT32, &tracecfg.height,       0}}; | {"height",      INITYPE_SINT32, &tracecfg.height,       0}}; | 
| #endif |  | 
|  |  | 
 |  |  | 
 | static void View_ScrollToBottom(HWND hWnd) { | static void View_ScrollToBottom(HWND hWnd) { | 
 |  |  | 
| Line 65  static void View_ScrollToBottom(HWND hWn | Line 77  static void View_ScrollToBottom(HWND hWn | 
 | PostMessage(hWnd, EM_LINESCROLL, 0, MaxPos); | PostMessage(hWnd, EM_LINESCROLL, 0, MaxPos); | 
 | } | } | 
 |  |  | 
| static void View_AddString(char *lpszString) { | static void View_ClrString(void) { | 
|  |  | 
|  | szView[0] = '\0'; | 
|  | SetWindowText(hView, szView); | 
|  | } | 
|  |  | 
|  | static void View_AddString(const char *lpszString) { | 
 |  |  | 
 | int             len, vlen; | int             len, vlen; | 
 | char    *p; | char    *p; | 
| Line 91  static void View_AddString(char *lpszStr | Line 109  static void View_AddString(char *lpszStr | 
 | View_ScrollToBottom(hView); | View_ScrollToBottom(hView); | 
 | } | } | 
 |  |  | 
 |  |  | 
 |  | // ---- | 
 |  |  | 
 |  | static  char    filebuf[FILEBUFSIZE]; | 
 |  | static  UINT32  filebufpos; | 
 |  |  | 
 |  | static void trfh_close(void) { | 
 |  |  | 
 |  | FILEH   fh; | 
 |  | UINT    size; | 
 |  |  | 
 |  | fh = tracewin.fh; | 
 |  | tracewin.fh = FILEH_INVALID; | 
 |  | if (fh != FILEH_INVALID) { | 
 |  | size = filebufpos & (FILEBUFSIZE - 1); | 
 |  | #if defined(FILELASTBUFONLY) | 
 |  | if (filebufpos >= FILEBUFSIZE) { | 
 |  | file_write(fh, filebuf + size, FILEBUFSIZE - size); | 
 |  | } | 
 |  | #endif | 
 |  | if (size) { | 
 |  | file_write(fh, filebuf, size); | 
 |  | } | 
 |  | file_close(fh); | 
 |  | } | 
 |  | } | 
 |  |  | 
 |  | static void trfh_open(const char *fname) { | 
 |  |  | 
 |  | trfh_close(); | 
 |  | tracewin.fh = file_create(fname); | 
 |  | filebufpos = 0; | 
 |  | } | 
 |  |  | 
 |  | static void trfh_add(const char *buf) { | 
 |  |  | 
 |  | UINT    size; | 
 |  | UINT    pos; | 
 |  | UINT    rem; | 
 |  |  | 
 |  | size = strlen(buf); | 
 |  | while(size) { | 
 |  | pos = filebufpos & (FILEBUFSIZE - 1); | 
 |  | rem = FILEBUFSIZE - pos; | 
 |  | if (size >= rem) { | 
 |  | CopyMemory(filebuf + pos, buf, rem); | 
 |  | filebufpos += rem; | 
 |  | buf += rem; | 
 |  | size -= rem; | 
 |  | #if !defined(FILELASTBUFONLY) | 
 |  | file_write(tracewin.fh, buf, strlen(buf)); | 
 |  | #endif | 
 |  | } | 
 |  | else { | 
 |  | CopyMemory(filebuf + pos, buf, size); | 
 |  | filebufpos += size; | 
 |  | break; | 
 |  | } | 
 |  | } | 
 |  | } | 
 |  |  | 
 |  |  | 
 |  | // ---- | 
 |  |  | 
 | static LRESULT CALLBACK traceproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { | static LRESULT CALLBACK traceproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { | 
 |  |  | 
 | RECT    rc; | RECT    rc; | 
 |  | HMENU   hmenu; | 
 |  |  | 
 | switch (msg) { | switch (msg) { | 
 | case WM_CREATE: | case WM_CREATE: | 
 |  | hmenu = GetSystemMenu(hWnd, FALSE); | 
 |  | InsertMenu(hmenu, 0, MF_BYPOSITION | MF_STRING, | 
 |  | IDM_TRACEEN, traceen); | 
 |  | InsertMenu(hmenu, 1, MF_BYPOSITION | MF_STRING, | 
 |  | IDM_TRACEFH, tracefh); | 
 |  | InsertMenu(hmenu, 2, MF_BYPOSITION | MF_STRING, | 
 |  | IDM_TRACECL, tracecl); | 
 |  | InsertMenu(hmenu, 3, MF_BYPOSITION | MF_SEPARATOR, 0, NULL); | 
 |  |  | 
 |  | CheckMenuItem(hmenu, IDM_TRACEEN, | 
 |  | (tracewin.en & 1)?MF_CHECKED:MF_UNCHECKED); | 
 |  |  | 
 | GetClientRect(hWnd, &rc); | GetClientRect(hWnd, &rc); | 
 | hView = CreateWindowEx(WS_EX_CLIENTEDGE, | hView = CreateWindowEx(WS_EX_CLIENTEDGE, | 
 | ClassEdit, NULL, | ClassEdit, NULL, | 
| Line 121  static LRESULT CALLBACK traceproc(HWND h | Line 216  static LRESULT CALLBACK traceproc(HWND h | 
 | SetFocus(hView); | SetFocus(hView); | 
 | return(TRUE); | return(TRUE); | 
 |  |  | 
 |  | case WM_SYSCOMMAND: | 
 |  | switch(wp) { | 
 |  | case IDM_TRACEEN: | 
 |  | tracewin.en ^= 1; | 
 |  | hmenu = GetSystemMenu(hWnd, FALSE); | 
 |  | CheckMenuItem(hmenu, IDM_TRACEEN, | 
 |  | (tracewin.en & 1)?MF_CHECKED:MF_UNCHECKED); | 
 |  | break; | 
 |  |  | 
 |  | case IDM_TRACEFH: | 
 |  | if (tracewin.fh != FILEH_INVALID) { | 
 |  | trfh_close(); | 
 |  | } | 
 |  | else { | 
 |  | trfh_open("traceout.txt"); | 
 |  | } | 
 |  | hmenu = GetSystemMenu(hWnd, FALSE); | 
 |  | CheckMenuItem(hmenu, IDM_TRACEFH, | 
 |  | (tracewin.fh != FILEH_INVALID)? | 
 |  | MF_CHECKED:MF_UNCHECKED); | 
 |  | break; | 
 |  |  | 
 |  | case IDM_TRACECL: | 
 |  | View_ClrString(); | 
 |  | break; | 
 |  |  | 
 |  | default: | 
 |  | return(DefWindowProc(hWnd, msg, wp, lp)); | 
 |  | } | 
 |  | break; | 
 |  |  | 
 | case WM_MOVE: | case WM_MOVE: | 
 | if (!(GetWindowLong(hWnd, GWL_STYLE) & | if (!(GetWindowLong(hWnd, GWL_STYLE) & | 
 | (WS_MAXIMIZE | WS_MINIMIZE))) { | (WS_MAXIMIZE | WS_MINIMIZE))) { | 
| Line 185  static LRESULT CALLBACK traceproc(HWND h | Line 311  static LRESULT CALLBACK traceproc(HWND h | 
 |  |  | 
 | void trace_init(void) { | void trace_init(void) { | 
 |  |  | 
 |  | HWND    hwnd; | 
 |  |  | 
 | if (!hPrev) { | if (!hPrev) { | 
 | WNDCLASS wc; | WNDCLASS wc; | 
 | wc.style = CS_HREDRAW | CS_VREDRAW; | wc.style = CS_HREDRAW | CS_VREDRAW; | 
| Line 202  void trace_init(void) { | Line 330  void trace_init(void) { | 
 | } | } | 
 | } | } | 
 |  |  | 
 |  | #if 1 | 
 |  | tracewin.en = 0; | 
 |  | tracewin.fh = FILEH_INVALID; | 
 |  | #else | 
 |  | tracewin.en = 0; | 
 |  | tracewin.fh = trfh_open("traces.txt"); | 
 |  | #endif | 
 |  |  | 
 | tracecfg.posx = CW_USEDEFAULT; | tracecfg.posx = CW_USEDEFAULT; | 
 | tracecfg.posy = CW_USEDEFAULT; | tracecfg.posy = CW_USEDEFAULT; | 
 | tracecfg.width = CW_USEDEFAULT; | tracecfg.width = CW_USEDEFAULT; | 
 | tracecfg.height = CW_USEDEFAULT; | tracecfg.height = CW_USEDEFAULT; | 
| #if defined(WRITE_INI) | ini_read(file_getcd(np2trace), inititle, initbl, 4); | 
| ini_read(NULL, inititle, initbl, 4); |  | 
| #endif |  | 
 |  |  | 
| hWndConsole = CreateWindowEx(WS_EX_CONTROLPARENT, | hwnd = CreateWindowEx(WS_EX_CONTROLPARENT, | 
 | ClassName, ProgTitle, | ClassName, ProgTitle, | 
 | WS_OVERLAPPEDWINDOW, | WS_OVERLAPPEDWINDOW, | 
 | tracecfg.posx, tracecfg.posy, | tracecfg.posx, tracecfg.posy, | 
 | tracecfg.width, tracecfg.height, | tracecfg.width, tracecfg.height, | 
 | NULL, NULL, hInst, NULL); | NULL, NULL, hInst, NULL); | 
| if (!hWndConsole) { | tracewin.hwnd = hwnd; | 
|  | if (hwnd == NULL) { | 
 | return; | return; | 
 | } | } | 
| ShowWindow(hWndConsole, SW_SHOW); | ShowWindow(hwnd, SW_SHOW); | 
| UpdateWindow(hWndConsole); | UpdateWindow(hwnd); | 
 | } | } | 
 |  |  | 
 | void trace_term(void) { | void trace_term(void) { | 
 |  |  | 
| if (hWndConsole) { | if (tracewin.fh != FILEH_INVALID) { | 
| DestroyWindow(hWndConsole); | trfh_close(); | 
| hWndConsole = NULL; | } | 
| #if defined(WRITE_INI) | if (tracewin.hwnd) { | 
| ini_write(NULL, inititle, initbl, 4); | DestroyWindow(tracewin.hwnd); | 
| #endif | tracewin.hwnd = NULL; | 
|  | ini_write(file_getcd(np2trace), inititle, initbl, 4); | 
 | } | } | 
 | } | } | 
 |  |  | 
 | void trace_fmt(const char *fmt, ...) { | void trace_fmt(const char *fmt, ...) { | 
 |  |  | 
| char    buf[0x1000]; | BOOL    en; | 
 | va_list ap; | va_list ap; | 
 |  | char    buf[0x1000]; | 
 |  |  | 
 |  | va_start(ap, fmt); | 
 |  | vsprintf(buf, fmt, ap); | 
 |  | va_end(ap); | 
 | if (hView) { | if (hView) { | 
 |  | View_AddString(buf); | 
 |  | } | 
 |  | if (tracewin.fh != FILEH_INVALID) { | 
 |  | trfh_add(buf); | 
 |  | trfh_add(crlf); | 
 |  | } | 
 |  | } | 
 |  |  | 
 |  | void trace_fmt2(const char *fmt, ...) { | 
 |  |  | 
 |  | BOOL    en; | 
 |  | va_list ap; | 
 |  | char    buf[0x1000]; | 
 |  |  | 
 |  | en = (tracewin.en & 1) || (tracewin.fh != FILEH_INVALID); | 
 |  | if (en) { | 
 | va_start(ap, fmt); | va_start(ap, fmt); | 
 | vsprintf(buf, fmt, ap); | vsprintf(buf, fmt, ap); | 
 | va_end(ap); | va_end(ap); | 
| View_AddString(buf); | if ((tracewin.en & 1) && (hView)) { | 
|  | View_AddString(buf); | 
|  | } | 
|  | if (tracewin.fh != FILEH_INVALID) { | 
|  | trfh_add(buf); | 
|  | trfh_add(crlf); | 
|  | } | 
 | } | } | 
 | } | } | 
 |  |  | 
 |  | #if 0 | 
 |  | void trace_fileout(const char *fname) { | 
 |  |  | 
 |  | if (tracewin.fh != FILEH_INVALID) { | 
 |  | file_close(tracewin.fh); | 
 |  | tracewin.fh = FILEH_INVALID; | 
 |  | } | 
 |  | if (fname) { | 
 |  | tracewin.fh = file_create_c(fname); | 
 |  | } | 
 |  | } | 
 |  | #endif | 
 |  |  | 
 | #endif | #endif | 
 |  |  |