summaryrefslogtreecommitdiffhomepage
path: root/src/console/ptyc_console_alloc.c
blob: 62829e44140d2ca97d6d21fb8e4c526654b596f0 (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
/*********************************************************/
/*  ptycon: a pty-console bridge                         */
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntcon/ntcon.h>
#include <ntapi/ntapi.h>
#include <gdi/gdi.h>

#include <ptycon/ptycon.h>
#include "ptycon_driver_impl.h"
#include "ptycon_status_impl.h"

int __stdcall ptyc_console_ctrl(uint32_t);

int __stdcall ptyc_console_reader(void *);
int __stdcall ptyc_console_writer(void *);;
int __stdcall ptyc_console_poller(void *);

int __stdcall ptyc_dbg_cat(struct ptyc_driver_ctx *);
int __stdcall ptyc_dbg_event(void *);
int __stdcall ptyc_dbg_oven(void *);
int __stdcall ptyc_dbg_raw(void *);


struct ptyc_loop_thread_ctx {
	nt_thread_start_routine *	entry;
	struct ptyc_driver_ctx_impl * 	ictx;
};

static int __stdcall ptyc_loop_thread_entry(void * ctx)
{
	struct ptyc_loop_thread_ctx *	xctx;

	xctx = (struct ptyc_loop_thread_ctx *)ctx;

	return ntapi->zw_terminate_thread(
		NT_CURRENT_THREAD_HANDLE,
		xctx->entry(xctx->ictx));
}

static int ptyc_create_loop_thread(
	nt_thread_params *		params,
	struct ptyc_driver_ctx_impl * 	ictx,
	nt_thread_start_routine *	entry)
{
	struct ptyc_loop_thread_ctx	xctx;

	xctx.entry = entry;
	xctx.ictx  = ictx;

	/* rapunzel rapunzel */
	params->hprocess	   = NT_CURRENT_PROCESS_HANDLE;
	params->start		   = ptyc_loop_thread_entry;
	params->ext_ctx		   = &xctx;
	params->ext_ctx_size	   = sizeof(xctx);
	params->stack_size_commit  = 64 * 1024;
	params->stack_size_reserve = 64 * 1024;
	params->creation_flags	   = NT_CREATE_LOCAL_THREAD;

	return ntapi->tt_create_thread(
		params);
}

static int ptyc_free_console_impl(
	struct ptyc_driver_ctx_impl *	ictx,
	int32_t				status)
{
	struct ptyc_term_ctx *		tctx;
	struct ptyc_loop_ctx *		lctx;

	tctx = &ictx->tctx;
	lctx = &ictx->lctx;

	if (tctx->hin)
		ntcon->free_console();

	if (lctx->treader.hthread)
		ntapi->zw_close(lctx->treader.hthread);

	if (lctx->twriter.hthread)
		ntapi->zw_close(lctx->twriter.hthread);

	if (lctx->tpoller.hthread)
		ntapi->zw_close(lctx->tpoller.hthread);

	if (lctx->tdbgevent.hthread)
		ntapi->zw_close(lctx->tdbgevent.hthread);

	if (lctx->tdbgoven.hthread)
		ntapi->zw_close(lctx->tdbgoven.hthread);

	if (lctx->tdbgraw.hthread)
		ntapi->zw_close(lctx->tdbgraw.hthread);

	return status;
}

static int ptyc_init_console(struct ptyc_term_ctx * tctx)
{
	/* console input buffer) */
	if (!(tctx->hin = ntcon->get_std_handle(NT_STD_INPUT_HANDLE)))
		return NT_STATUS_UNSUCCESSFUL;

	/* console screen buffer */
	if (!(tctx->hout = ntcon->create_console_screen_buffer(
			NT_GENERIC_READ | NT_GENERIC_WRITE,
			0,0,NT_CONSOLE_TEXTMODE_BUFFER,0)))
		return NT_STATUS_UNSUCCESSFUL;

	if (!(ntcon->set_console_screen_buffer_size(
			tctx->hout,
			tctx->screen_size)))
		return NT_STATUS_UNSUCCESSFUL;

	if (!(ntcon->set_console_active_screen_buffer(tctx->hout)))
		return NT_STATUS_UNSUCCESSFUL;

	/* console text attributes: default colors */
	if (!(ntcon->set_console_text_attribute(
			tctx->hout,
			NT_BACKGROUND_BLACK | NT_FOREGROUND_WHITE)))
		return NT_STATUS_UNSUCCESSFUL;

	/* console code page: utf-8 */
	if (!(ntcon->set_console_code_page(65001)))
		return NT_STATUS_NOT_SUPPORTED;

	if (!(ntcon->set_console_output_code_page(65001)))
		return NT_STATUS_NOT_SUPPORTED;

	/* input events */
	if (!(ntcon->set_console_mode(
			tctx->hin,
			NT_ENABLE_MOUSE_INPUT
			| NT_ENABLE_WINDOW_INPUT)))
		return NT_STATUS_UNSUCCESSFUL;

	/* ctrl events */
	if (!(ntcon->set_console_ctrl_handler(
			ptyc_console_ctrl,
			true)))
		return NT_STATUS_UNSUCCESSFUL;

	/* cursor info */
	if (!(ntcon->get_console_cursor_info(
			tctx->hout,
			&tctx->cursor_info)))
		return NT_STATUS_UNSUCCESSFUL;

	return NT_STATUS_SUCCESS;
}

int  ptyc_alloc_console(struct ptyc_driver_ctx * dctx)
{
	int32_t				status;
	struct ptyc_driver_ctx_impl *	ictx;
	struct ptyc_term_ctx *		tctx;
	struct ptyc_loop_ctx *		lctx;

	if (!(ictx = ptyc_get_driver_ictx(dctx)))
		return NT_STATUS_INVALID_HANDLE;

	tctx = &ictx->tctx;
	lctx = &ictx->lctx;

	tctx->screen_size.x = 512;
	tctx->screen_size.y = 4096;
	tctx->drvflags      = ictx->cctx.drvflags;

	if (!(ntcon->alloc_console()))
		return NT_STATUS_UNSUCCESSFUL;

	if ((status = ptyc_init_console(tctx)))
		return ptyc_set_status(dctx,status);

	if ((status = ptyc_create_loop_thread(
			&lctx->treader,
			ictx,ptyc_console_reader)))
		return ptyc_set_status(dctx,status);

	if ((status = ptyc_create_loop_thread(
			&lctx->twriter,
			ictx,ptyc_console_writer)))
		return ptyc_set_status(dctx,status);

	if ((status = ptyc_create_loop_thread(
			&lctx->tpoller,
			ictx,ptyc_console_poller)))
		return ptyc_set_status(dctx,status);

	if (dctx->cctx->drvflags & PTYC_DRIVER_DBG_EVENT)
		if ((status = ptyc_create_loop_thread(
				&lctx->tdbgevent,
				ictx,ptyc_dbg_event)))
			return ptyc_set_status(dctx,status);

	if (dctx->cctx->drvflags & PTYC_DRIVER_DBG_OVEN)
		if ((status = ptyc_create_loop_thread(
				&lctx->tdbgoven,
				ictx,ptyc_dbg_oven)))
			return ptyc_set_status(dctx,status);

	if (dctx->cctx->drvflags & PTYC_DRIVER_DBG_RAW)
		if ((status = ptyc_create_loop_thread(
				&lctx->tdbgraw,
				ictx,ptyc_dbg_raw)))
			return ptyc_set_status(dctx,status);

	if ((status = ptyc_dbg_cat(dctx)))
		return ptyc_set_status(dctx,status);

	return ptyc_set_status(dctx,NT_STATUS_SUCCESS);
}


int  ptyc_wait_for_console(struct ptyc_driver_ctx * dctx)
{
	struct ptyc_driver_ctx_impl * ictx;

	if (!(ictx = ptyc_get_driver_ictx(dctx)))
		return NT_STATUS_INVALID_HANDLE;

	return ntapi->zw_wait_for_single_object(
		ictx->lctx.tpoller.hthread,
		NT_SYNC_ALERTABLE,0);
}


void ptyc_free_console(struct ptyc_driver_ctx * dctx)
{
	struct ptyc_driver_ctx_impl * ictx;

	if (!(ictx = ptyc_get_driver_ictx(dctx)))
		return;

	ptyc_free_console_impl(ictx,NT_STATUS_SUCCESS);
}