This repository was archived by the owner on Feb 23, 2018. It is now read-only.
forked from katahiromz/XScreenSaverWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextclient.cpp
More file actions
282 lines (241 loc) · 7.43 KB
/
textclient.cpp
File metadata and controls
282 lines (241 loc) · 7.43 KB
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
/* xscreensaver, Copyright(c) 2012 Jamie Zawinski <[email protected]>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* Running programs under a pipe or pty and returning bytes from them.
* Uses these X resources:
*
* program: What to run. Usually "xscreensaver-text".
* relaunchDelay: secs How long after the command dies before restarting.
* usePty: bool Whether to run the command interactively.
* metaSendsESC: bool Whether to send Alt-x as ESC x in pty-mode.
* swapBSDEL: bool Swap Backspace and Delete in pty-mode.
*/
#include "xws2win.h"
#include <vector>
#include "mzc2mini.h"
#include <cstdio>
using namespace std;
/*#define DEBUG*/
EXTERN_C const char *progname;
EXTERN_C char *program;
EXTERN_C int relaunchDelay;
int tc_fprintf(FILE *fp, char *fmt, ...)
{
CHAR sz[512];
va_list va;
int n;
va_start(va, fmt);
n = wvsprintfA(sz, fmt, va);
va_end(va);
OutputDebugStringA(sz);
return n;
}
#define fprintf tc_fprintf
struct text_data
{
Display *dpy;
char *program;
int pix_w, pix_h, char_w, char_h;
MFile *pipeInput;
MFile *pipeOutput;
MProcessMaker *pmaker;
Bool input_available_p;
DWORD subproc_relaunch_delay;
DWORD dwTick;
const char *out_buffer;
int out_column;
};
static void launch_text_generator(text_data *d)
{
const char *oprogram = d->program;
char program[MAX_PATH * 3], program2[MAX_PATH * 3];
char comspec[MAX_PATH];
if (lstrcmpiA(oprogram, "xscreensaver-text") == 0 ||
lstrcmpiA(oprogram, "xscreensaver-text.exe") == 0)
{
sprintf(program, "xscreensaver-text --cols %d", d->char_w);
}
else
{
strcpy(program, oprogram);
}
fprintf(stderr, "%s\n", program);
d->pmaker->SetShowWindow(SW_HIDE);
d->pmaker->SetCreationFlags(CREATE_NEW_CONSOLE);
if (d->pmaker->PrepareForRedirect(
&d->pipeInput->m_hHandle, &d->pipeOutput->m_hHandle, &d->pipeOutput->m_hHandle))
{
BOOL bOK;
CHAR *p, szDir[MAX_PATH];
# ifdef DEBUG
fprintf(stderr, "%s: textclient: launch pipe: %s\n", progname, program);
# endif
GetModuleFileNameA(NULL, szDir, MAX_PATH);
p = strrchr(szDir, '\\');
*p = '\0';
d->pmaker->SetCurrentDirectory(szDir);
bOK = d->pmaker->CreateProcess(NULL, program);
if (!bOK)
{
// retry
p = strrchr(szDir, '\\');
*p = '\0';
d->pmaker->SetCurrentDirectory(szDir);
bOK = d->pmaker->CreateProcess(NULL, program);
}
if (!bOK)
{
// and retry again
*p = '\0';
GetEnvironmentVariableA("COMSPEC", comspec, MAX_PATH);
wsprintfA(program2, "\"%s\" /C %s", comspec, program);
bOK = d->pmaker->CreateProcess(NULL, program2);
}
if (bOK)
{
# ifdef DEBUG
fprintf(stderr, "%s: textclient: CreateProcess\n", progname);
# endif
d->input_available_p = True;
}
}
d->dwTick = 0;
}
static void relaunch_generator_timer(void *closure)
{
text_data *d = (text_data *)closure;
#ifdef DEBUG
fprintf(stderr, "%s: textclient: launch timer fired\n", progname);
#endif
launch_text_generator(d);
}
EXTERN_C void textclient_reshape(text_data *d,
int pix_w, int pix_h, int char_w, int char_h)
{
/* If we're running xscreensaver-text, then kill and restart it any
time the window is resized so that it gets an updated --cols arg
right away. But if we're running something else, leave it alone.
*/
if (!strcmp(d->program, "xscreensaver-text"))
{
d->pipeInput->CloseHandle();
d->pipeOutput->CloseHandle();
d->pmaker->Close();
d->input_available_p = False;
relaunch_generator_timer(d);
}
}
EXTERN_C text_data *textclient_open(Display *dpy)
{
text_data *d = (text_data *) calloc(1, sizeof(*d));
#ifdef DEBUG
fprintf(stderr, "%s: textclient: init\n", progname);
#endif
d->dpy = dpy;
d->subproc_relaunch_delay = (1000 * relaunchDelay);
//d->program = get_string_resource(dpy, "program", "Program");
d->program = _strdup(program);
d->pipeInput = new MFile;
d->pipeOutput = new MFile;
d->pmaker = new MProcessMaker;
launch_text_generator(d);
return d;
}
EXTERN_C void textclient_close(text_data *d)
{
# ifdef DEBUG
fprintf(stderr, "%s: textclient: free\n", progname);
# endif
if (d->pmaker->IsRunning())
d->pmaker->TerminateProcess(-1);
if (d->program)
free(d->program);
delete d->pipeInput;
delete d->pipeOutput;
delete d->pmaker;
memset(d, 0, sizeof(*d));
free(d);
}
EXTERN_C int textclient_getc(text_data *d)
{
int ret = -1;
if (d->dwTick != 0)
{
if (GetTickCount() - d->dwTick > d->subproc_relaunch_delay)
{
relaunch_generator_timer(d);
d->dwTick = 0;
}
}
if (d->out_buffer && *d->out_buffer)
{
ret = *d->out_buffer;
d->out_buffer++;
}
else if (d->input_available_p)
{
unsigned char s[2];
DWORD n;
//fprintf(stderr, "getc: waiting\n");
d->pipeOutput->PeekNamedPipe(s, 1, &n);
//fprintf(stderr, "getc: done\n");
if (n == 1)
{
d->pipeOutput->ReadFile(s, 1, &n);
ret = s[0];
fprintf(stderr, "getc: '%c'\n", s[0]);
}
else if (!d->pmaker->IsRunning())
{
d->pipeInput->CloseHandle();
d->pipeOutput->CloseHandle();
# ifdef DEBUG
fprintf(stderr, "%s: textclient: pclose\n", progname);
# endif
d->pmaker->TerminateProcess(-1);
d->pmaker->Close();
if (d->out_column > 0)
{
# ifdef DEBUG
fprintf(stderr, "%s: textclient: adding blank line at EOF\n",
progname);
# endif
d->out_buffer = "\r\n\r\n";
}
# ifdef DEBUG
fprintf(stderr, "%s: textclient: relaunching in %d\n", progname,
(int) d->subproc_relaunch_delay);
# endif
d->dwTick = GetTickCount();
d->input_available_p = False;
}
}
if (ret == '\r' || ret == '\n')
d->out_column = 0;
else if (ret > 0)
d->out_column++;
# ifdef DEBUG
if (ret <= 0)
fprintf(stderr, "%s: textclient: getc: %d\n", progname, ret);
else if (ret < ' ')
fprintf(stderr, "%s: textclient: getc: %03o\n", progname, ret);
else
fprintf(stderr, "%s: textclient: getc: '%c'\n", progname, (char) ret);
# endif
return ret;
}
EXTERN_C Bool textclient_putc(text_data *d, int c)
{
DWORD cb;
# ifdef DEBUG
fprintf(stderr, "%s: textclient: putc '%c'\n", progname, (char) c);
# endif
return d->pipeInput->WriteFile(&c, 1, &cb);
}