summaryrefslogtreecommitdiff
path: root/Build/source/texk/windvi/winlog.c
blob: f1f9be5c5e40dd5b932c11a13ff8a2b3d04b1693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "wingui.h"
#include "xdvi-config.h"

/*****************************************************************************
  View Log File Box
  ****************************************************************************/
LRESULT CALLBACK DlgViewLog(HWND, UINT, WPARAM, LPARAM);
LRESULT MsgViewLogInit(HWND, UINT, WPARAM, LPARAM);
LRESULT MsgViewLogCommand(HWND, UINT, WPARAM, LPARAM);
LRESULT CmdViewLogOK(HWND, WORD, WORD, HWND);
/* LRESULT CmdViewLogCancel(HWND, WORD, WORD, HWND); */

/* ViewLog dialog message table definition. */
MSD rgmsdViewLog[] =
{
    {WM_COMMAND,    MsgViewLogCommand},
    {WM_INITDIALOG, MsgViewLogInit}
};

MSDI msdiViewLog =
{
    sizeof(rgmsdViewLog) / sizeof(MSD),
    rgmsdViewLog,
    edwpNone
};

/* ViewLog dialog command table definition. */
CMD rgcmdViewLog[] =
{
    {IDOK,     CmdViewLogOK}
    /*    {IDCANCEL, CmdViewLogCancel} */
};

CMDI cmdiViewLog =
{
    sizeof(rgcmdViewLog) / sizeof(CMD),
    rgcmdViewLog,
    edwpNone
};

/* Global Variables */
HANDLE hLogEdit;
HANDLE hLogEvent;
char *szText;
BOOL abortRequest;		/* Used to wait the user has acknowledged 
				 the log view. */
BOOL somethingToRead;
/*****************************************************************************
  View Log File Box
  ****************************************************************************/
LRESULT CALLBACK DlgViewLog(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
    return DispMessage(&msdiViewLog, hdlg, uMessage, wparam, lparam);
}

BOOL CreateViewLog(HWND hwndParent)
{
  abortRequest = FALSE;
  somethingToRead = FALSE;
  hViewLog = CreateDialog(hInst,
                          "DlgViewLog",
			  hwndParent, 
			  (DLGPROC) DlgViewLog); 

  if (hViewLog == NULL) {
    Win32Error("ViewLog/CreateDialog");
    return FALSE;
  }

  hLogEdit = GetDlgItem(hViewLog, IDC_LOG_EDIT);

  SendDlgItemMessage(hViewLog, IDC_LOG_EDIT, 
			   EM_LIMITTEXT, (WPARAM)0, 0);

  ShowWindow(hViewLog, SW_HIDE);
  bLogShown = False;
  return TRUE;
}

LRESULT MsgViewLogInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
  return 0;
}


LRESULT MsgViewLogCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
  return DispCommand(&cmdiViewLog, hwnd, wparam, lparam);
}

LRESULT CmdViewLogOK(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
{
  somethingToRead = FALSE;	/* The user has read */
#if 0
  if (abortRequest) {
    DestroyWindow(hViewLog);
    hViewLog = NULL;
  }
  else {
#endif
    ShowWindow(hViewLog, SW_HIDE);
    bLogShown = False;
    if (hWndMain) SetForegroundWindow(hWndMain);
#if 0
  }
#endif
  return 0;
}

#if 0
LRESULT CmdViewLogCancel(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
{
  /* If there is nothing to read, destroy the window immediately */
  if (somethingToRead) {
    abortRequest = TRUE;
    ShowWindow(hViewLog, SW_SHOW);
    bLogShown=TRUE;
  }
  else {
    DestroyWindow(hViewLog);
    hViewLog = NULL;
  }
    
  return 0;
}
#endif

/*
  Log Loop function
  */

DWORD WINAPI LogLoop(LPVOID lpParam)
{
  MSG msg;
  int rc;

  if (CreateViewLog(hWndMain) == FALSE) {
    return 1;
  }

  if (SetEvent(hLogEvent) == 0) {
    return 1;
  }

  while (TRUE) {
    rc = GetMessage(&msg, hViewLog, 0, 0);
    switch (rc) {
    case -1:
      break;
    case 0:
      goto end1;
    default:
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      break;
    }
  }
 end1:
  if (hViewLog) DestroyWindow(hViewLog);
  hViewLog = NULL;

#if 0
  MessageBox(NULL, "Exit ViewLog thread", NULL, MB_APPLMODAL | MB_ICONHAND | MB_OK);
#endif
  ExitThread(0);
  /* unreachable */
  return 0;
}

/*
  Sentinel function. It runs in its own thread and sends messages 
  to the dialog box.
*/

DWORD WINAPI ViewLogSentinel(LPVOID lpParam)
{
  char data[64];
  char line[128];
  int nbRead, count, text_count;
  BOOL bResult;
  DWORD idLoopThread;
  SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };

  count = 0;			/* count chars in line */
  ZeroMemory(line, sizeof(line));
#if 0
  if ((szText = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1)) == NULL) {
      MessageBox(NULL, "HeapAlloc failed", "ViewLog", 
		 MB_OK|MB_ICONERROR );
      return 1;
  }
#endif
  text_count = 1;

  if ((hLogEvent = CreateEvent(&sa, /* security attributes */
			       FALSE, /* Automatic reset */
			       FALSE, /* Initial state is reset */
			       "WinLogEvent")) == NULL) {
    Win32Error("ViewLogSentinel/WinLogEvent");
    return 0;
  }

  if ((hLogLoopThread = CreateThread(&sa, /* security attributes */
				  0,	/* default stack size */
				  LogLoop, /* start address of thread */
				  0,	/* parameter */
				  0,	/* creation flags */
				  &idLoopThread /* thread id */
				  )) == NULL) {
    Win32Error("ViewLogSentinel/LogLoopThread");
    return 0;
  }

  if (WaitForSingleObject(hLogEvent, 4000) != WAIT_OBJECT_0) {
    Win32Error("Log Window not created !");
    return 0;
  }
  
  CloseHandleAndClear(&hLogEvent);

  do {
    nbRead = 0;
    bResult = ReadFile(hLogIn, data, 1, &nbRead, NULL /* &gOverLapped */);
    if (!bResult || !nbRead) {
      /* EOF on hCrtOut */
      goto exit_loop;
    }

    if (!bResult) {	
      /* deal with the error code */
      switch (GetLastError()) {
      case ERROR_BROKEN_PIPE:
	/* program exiting */
#if 1
	MessageBox(NULL, "Broken Pipe", NULL, MB_APPLMODAL | MB_ICONHAND | MB_OK);
#endif
	goto exit_loop;
      default:
	Win32Error("ViewLogSentinel/ReadFile");
      }
    }

    /* first, accumulate into line[]
s       until 80 chars or eol */

    if (data[0] == '\n') {
      line[count++] = '\r';
      line[count++] = '\n';
    }
    else
      line[count++] = data[0];

    if (data[0] == '\n' || count >= 80) {
      if (hViewLog) {
	/* remove any selection */
		SendDlgItemMessage(hViewLog, IDC_LOG_EDIT, EM_SETSEL,
						   -1, 0);
		SendDlgItemMessage(hViewLog, IDC_LOG_EDIT, EM_REPLACESEL,
						   FALSE, (LPARAM)line);
		if (! bLogShown) {
		  /* FIXME : really needed ? */
		  GdiFlush();
		  somethingToRead = TRUE;
		  if (resource.log_flag) {
		    ShowWindow(hViewLog, SW_SHOW);
		    /* SetFocus(hViewLog); */
		    SetForegroundWindow(hViewLog);
		    /* There might be some fight to get the focus. */
		    SetForegroundWindow(hWndMain);
		    if (bMagDisp) {
		      SetForegroundWindow(hWndMagnify);
		    }
		    bLogShown = TRUE;
		  }
		}
		UpdateWindow(hViewLog);
      }
      ZeroMemory(line, sizeof(line));
      count = 0;
    }
	
  }  while(1);

exit_loop:
  if (PostMessage(hViewLog, WM_QUIT, 0, 0) == 0)
    Win32Error("ViewLogSentinel/PostMessage/WM_QUIT");
  if (WaitForSingleObject(hLogLoopThread, INFINITE) == WAIT_FAILED) {
    // MessageBox( NULL, "LogLoop thread does not want to shut down...", "", MB_OK|MB_ICONINFORMATION );
    Win32Error("ViewLogSentinel/WaitForSingleObject/hLogLoopThread");
  }
  CloseHandleAndClear(&hLogLoopThread);

  return 0;
}