forked from avs333/Nougat_dlfunctions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
354 lines (301 loc) · 9.56 KB
/
main.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <jni.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <android_native_app_glue.h>
#include <sys/system_properties.h>
#include "main.h"
static void *app_thread(void *arg);
static void stop_app_thread(struct ctx *ctx)
{
ctx->should_run = 0;
pthread_join(ctx->app_thread, 0);
ctx->app_thread = 0;
}
static void done(struct ctx *ctx)
{
ANativeActivity* activity;
if(!ctx) return;
activity = ctx->app->activity;
pthread_mutex_lock(&ctx->mutex);
if(ctx->app_thread) log_error("internal error: thread not stopped in %s", __func__);
ft_quit(ctx);
pthread_mutex_unlock(&ctx->mutex);
pthread_mutex_destroy(&ctx->mutex);
if(ctx->buffer) free(ctx->buffer);
free(ctx);
ANativeActivity_finish(activity);
}
static void draw_frame(struct ctx* ctx)
{
int k, width, stride;
void *src, *dst;
ANativeWindow_Buffer buffer;
if(!ctx->app->window) {
log_error("%s called with null window", __func__);
return;
}
log_info("%s: %d x %d", __func__, ctx->width, ctx->height);
pthread_mutex_lock(&ctx->mutex);
if(ANativeWindow_setBuffersGeometry(ctx->app->window, 0, 0, ctx->fmt) != 0) {
log_error("Failed to set buffer format");
pthread_mutex_unlock(&ctx->mutex);
return;
}
if(ANativeWindow_lock(ctx->app->window, &buffer, 0) < 0) {
log_error("Unable to lock window buffer");
pthread_mutex_unlock(&ctx->mutex);
return;
}
src = ctx->buffer;
dst = buffer.bits;
if(ctx->fmt == WINDOW_FORMAT_RGBA_8888) {
width = ctx->width * 4;
stride = ctx->stride * 4;
} else if(ctx->fmt == WINDOW_FORMAT_RGB_565) {
width = ctx->width * 2;
stride = ctx->stride * 2;
} else {
log_error("%s called for unsupported window format %d", __func__, ctx->fmt);
ANativeWindow_unlockAndPost(ctx->app->window);
pthread_mutex_unlock(&ctx->mutex);
return;
}
for(k = 0; k < ctx->height; k++, src += stride, dst += stride) memcpy(dst, src, width);
ANativeWindow_unlockAndPost(ctx->app->window);
pthread_mutex_unlock(&ctx->mutex);
}
static void init_window(struct ctx *ctx)
{
ANativeWindow_Buffer buffer;
if(ctx->buffer) return;
log_info(__func__);
if(ANativeWindow_setBuffersGeometry(ctx->app->window, 0, 0, ctx->fmt) != 0) {
log_error("Failed to set buffer format");
done(ctx);
return;
}
if(ANativeWindow_lock(ctx->app->window, &buffer, 0) < 0) {
log_error("Unable to lock window buffer");
done(ctx);
return;
}
ctx->height = buffer.height;
ctx->width = buffer.width;
ctx->stride = buffer.stride;
ctx->buffer = calloc(1, ctx->height * ctx->stride * (ctx->fmt == WINDOW_FORMAT_RGBA_8888 ? 4 : 2));
ANativeWindow_unlockAndPost(ctx->app->window);
if(!ctx->buffer) {
log_error("No memory for window buffer");
done(ctx);
}
log_info("window %d x %d stride %d allocated", ctx->width, ctx->height, ctx->stride);
ctx->should_run = 1;
if(pthread_create(&ctx->app_thread, 0, app_thread, ctx) != 0) {
log_error("failed to create app thread");
done(ctx);
}
}
static int32_t handle_input(struct android_app* app, AInputEvent* event)
{
struct ctx* ctx = (struct ctx*) app->userData;
if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
int x = AMotionEvent_getX(event, 0);
int y = AMotionEvent_getY(event, 0);
log_info("AINPUT_EVENT_TYPE_MOTION %d %d", x, y);
pthread_mutex_lock(&ctx->mutex);
if(ft_render_string(ctx, "BRAVO, Mr. T.W.Lewis\nЭто по-русски\n", x, y, 1000) != 0)
log_error("rendering failed");
pthread_mutex_unlock(&ctx->mutex);
draw_frame(ctx);
stop_app_thread(ctx);
return 1;
}
return 0;
}
static void handle_cmd(struct android_app* app, int32_t cmd)
{
struct ctx* ctx = (struct ctx*) app->userData;
switch(cmd) {
case APP_CMD_INIT_WINDOW:
log_info("APP_CMD_INIT_WINDOW");
pthread_mutex_lock(&ctx->mutex);
init_window(ctx);
pthread_mutex_unlock(&ctx->mutex);
draw_frame(ctx);
break;
case APP_CMD_WINDOW_REDRAW_NEEDED:
case APP_CMD_WINDOW_RESIZED:
draw_frame(ctx);
log_info("APP_CMD_WINDOW_RESIZED/REDRAW_NEEDED");
break;
case APP_CMD_TERM_WINDOW:
log_info("APP_CMD_TERM_WINDOW");
break;
default:
log_info("APP_CMD_%d", cmd);
break;
}
}
/* Terrible bug corrected: https://groups.google.com/d/msg/alt.usage.english/4t0PAe9-QGc/H2Jb2SJQBgAJ */
static const char *low_nums[] = { 0, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
static const char *ten_multiples[] = { 0, 0, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
static const char *large_nums[] = { "billion", "million", "thousand", 0 };
static const int large_vals[] = { 1000000000, 1000000, 1000, 0 };
/* Numerals in the English language are spoken in triples each consisting
of the number of hundreds (if any) possibly followed by "and" and the lesser number, e.g.:
909,168,442 is "nine hundred and nine million one hundered and sixty-eight thousand four hundred and forty-two" */
static char *triple(char *out, int num)
{
int hi, rest;
char *c = out;
if(num <= 0 || num >= 1000) return 0; /* can't happen */
hi = num/100;
rest = num % 100;
if(hi) {
sprintf(c, "%s hundred%s", low_nums[hi], rest ? " and " : "");
c += strlen(c);
}
if(!rest) return out;
if(rest <= 19) {
sprintf(c, "%s", low_nums[rest]);
return out;
}
hi = rest/10;
rest = rest % 10;
if(rest) sprintf(c, "%s-%s", ten_multiples[hi], low_nums[rest]);
else sprintf(c, "%s", ten_multiples[hi]);
return out;
}
static char *num2engl(unsigned int num)
{
uint32_t i, hi, rest;
char tmp[512], *c;
if(!num) return strdup("zero");
for(i = 0, rest = num, c = tmp; large_vals[i]; i++) {
hi = rest/large_vals[i];
rest = rest % large_vals[i];
if(hi) {
sprintf(c, "%s %s%s", triple(c, hi), large_nums[i], rest ? " " : "");
c += strlen(c);
}
if(!rest) return strdup(tmp);
}
triple(c, rest);
return strdup(tmp);
}
static void *app_thread(void *arg)
{
struct ctx *ctx = (struct ctx *) arg;
int k = 0, stride_bytes, bytes_per_font_height, pixels_per_font_height,
lines, cur_lines, max_lines, target_width, start_x, start_y;
pixels_per_font_height = ft_get_line_height(ctx) + 10;
if(!pixels_per_font_height || !ctx->height) {
log_error("dimensions unknown");
return 0;
}
start_x = 10;
start_y = 250;
cur_lines = 0;
max_lines = (ctx->height - 2 * start_y)/pixels_per_font_height;
target_width = ctx->width - 2 * start_x;
log_info("%s: width = %d height %d max_lines %d", __func__, target_width, ctx->height, max_lines);
if(ctx->fmt == WINDOW_FORMAT_RGBA_8888) stride_bytes = ctx->stride * 4;
else stride_bytes = ctx->stride * 2;
bytes_per_font_height = stride_bytes * pixels_per_font_height;
while(1) {
char *c = num2engl(k++);
pthread_mutex_lock(&ctx->mutex);
if(!ctx->should_run) {
pthread_mutex_unlock(&ctx->mutex);
break;
}
if(ft_get_string_metrics(ctx, c, target_width, &lines) != 0) {
log_error("ft_get_string_metrics failed");
pthread_mutex_unlock(&ctx->mutex);
break;
}
if(lines + cur_lines >= max_lines) { /* scroll up window buffer by "lines" */
void *ptr = ctx->buffer + start_y * stride_bytes;
log_info("scrolling: %d + %d > %d", lines, cur_lines, max_lines);
if(lines > max_lines) {
log_error("logcat string won't fit on this screen: lines %d max_lines %d", lines, max_lines);
pthread_mutex_unlock(&ctx->mutex);
continue;
}
cur_lines -= lines;
memmove(ptr, ptr + lines * bytes_per_font_height, cur_lines * bytes_per_font_height);
memset(ptr + cur_lines * bytes_per_font_height, 0, (max_lines - cur_lines) * bytes_per_font_height);
}
if(ft_render_string(ctx, c, start_x, start_y + cur_lines * pixels_per_font_height, target_width) != 0) {
pthread_mutex_unlock(&ctx->mutex);
break;
}
cur_lines += lines;
pthread_mutex_unlock(&ctx->mutex);
draw_frame(ctx);
free(c);
sleep(1);
}
return 0;
}
#ifdef TESTCPP
extern int test_cplusplus();
#endif
void android_main(struct android_app* app)
{
struct ctx *ctx = 0;
int ident, events;
struct android_poll_source* source;
char prop_density[PROP_VALUE_MAX];
app_dummy();
#ifdef __arm__
log_info("starting arm code");
#elif defined(__aarch64__)
log_info("starting aarch64 code");
#else
#error "Arch unknown, please port me"
#endif
#ifdef TESTCPP
test_cplusplus();
#endif
ctx = (struct ctx *) calloc(1, sizeof(struct ctx));
if(!ctx) {
log_error("no memory");
ANativeActivity_finish(app->activity);
return;
}
app->onAppCmd = handle_cmd;
app->onInputEvent = handle_input;
app->userData = ctx;
pthread_mutex_init(&ctx->mutex, 0);
ctx->app = app;
if(__system_property_get("ro.sf.lcd_density", prop_density) < 0
|| sscanf(prop_density, "%d", &ctx->dpi) != 1) {
log_info("failed to find screen density, assuming 480");
ctx->dpi = 480;
}
ctx->fmt = WINDOW_FORMAT_RGB_565; // WINDOW_FORMAT_RGBA_8888;
if(ft_init(ctx) != 0) {
done(ctx);
return;
}
while(1) {
while((ident = ALooper_pollAll(-1, NULL, &events, (void**)&source)) >= 0) {
if(source) source->process(app, source);
if(app->destroyRequested != 0) {
stop_app_thread(ctx);
log_info("destroyed.");
done(ctx);
return;
}
}
}
}