-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
266 lines (231 loc) · 6.95 KB
/
console.c
File metadata and controls
266 lines (231 loc) · 6.95 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
// anthrm - http://mylcd.sourceforge.net/
// An LCD framebuffer and text rendering API
// Michael McElligott
// okio@users.sourceforge.net
// Copyright (c) 2005-2011 Michael McElligott
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU LIBRARY GENERAL PUBLIC LICENSE for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>
#include <inttypes.h>
#include <windows.h>
#include <mylcd.h>
#include "garminhr.h"
#define FONT LFTW_B14
#define my_calloc calloc
#define my_malloc malloc
#define my_free free
#define my_memcpy memcpy
#ifndef _ANSIDECL_H
#undef VA_OPEN
#undef VA_CLOSE
#undef VA_FIXEDARG
#define VA_OPEN(AP, VAR) { va_list AP; va_start(AP, VAR); { struct Qdmy
#define VA_CLOSE(AP) } va_end(AP); }
#define VA_FIXEDARG(AP, T, N) struct Qdmy
#endif
uint64_t getTime ()
{
return (uint64_t)GetTickCount();
}
/*
void shadowTextEnable (THWD *hw, const int colour, const unsigned char trans)
{
lSetRenderEffect(hw, LTR_SHADOW);
// set direction to South-East, shadow thickness to 5, offset by 1 pixel(s) and transparency to trans
lSetFilterAttribute(hw, LTR_SHADOW, 0, LTRA_SHADOW_S|LTRA_SHADOW_E | LTRA_SHADOW_S5 | LTRA_SHADOW_OS(1) | LTRA_SHADOW_TR(trans));
lSetFilterAttribute(hw, LTR_SHADOW, 1, colour);
}
void shadowTextDisable (THWD *hw)
{
lSetRenderEffect(hw, LTR_DEFAULT);
}
void printSingleLineShadow (TFRAME *frame, const int font, const int x, const int y, const int colour, const char *str)
{
TLPRINTR rt;
int w, h;
lSetForegroundColour(frame->hw, colour);
lGetTextMetrics(frame->hw, str, 0, font, &w, &h);
shadowTextEnable(frame->hw, 0x000000, 120);
if (w >= frame->width){
rt.bx1 = 0; rt.by1 = y;
rt.bx2 = frame->width-1;
rt.by2 = frame->height-1;
rt.sx = rt.bx1; rt.sy = y;
lPrintEx(frame, &rt, font, PF_CLIPDRAW|PF_RIGHTJUSTIFY, LPRT_CPY, str);
}else{
rt.bx1 = (frame->width-w)/2.0;
rt.by1 = y;
rt.bx2 = frame->width-1;
rt.by2 = frame->height-1;
rt.sx = rt.bx1; rt.sy = y;
lPrintEx(frame, &rt, font, PF_CLIPWRAP|PF_CLIPDRAW, LPRT_CPY, str);
}
shadowTextDisable(frame->hw);
}
*/
void marqueeDelete (TMARQUEE *marquee)
{
if (marquee){
WaitForSingleObject(marquee->hLock, INFINITE);
CloseHandle(marquee->hLock);
my_free(marquee->entry);
my_free(marquee);
}
}
TMARQUEE *marqueeNew (const int tLines, const unsigned int flags)
{
TMARQUEE *marquee = my_calloc(1, sizeof(TMARQUEE));
if (marquee){
marquee->entry = (TMARQUEELINE*)my_calloc(tLines, sizeof(TMARQUEELINE));
if (marquee->entry){
marquee->total = tLines;
marquee->hLock = CreateMutex(NULL, FALSE, NULL);
marquee->flags = flags;
}else{
my_free(marquee);
marquee = NULL;
}
}
return marquee;
}
void marqueeAdd (THR *hr, TMARQUEE *mq, const char *str, const unsigned int timeout)
{
if (mq && WaitForSingleObject(mq->hLock, INFINITE) == WAIT_OBJECT_0){
my_memcpy(&mq->entry[0], &mq->entry[1], sizeof(TMARQUEELINE) * (mq->total-1));
strncpy(mq->entry[mq->total-1].line, str, MAX_PATH);
mq->entry[mq->total-1].time = timeout;
mq->ready = 1;
ReleaseMutex(mq->hLock);
}
}
int marqueeDraw (THR *hr, TFRAME *frame, TMARQUEE *mq)
{
if (!mq || !mq->ready) return 0;
int x = 2;
int y = 2;
const int flags = PF_CLIPDRAW;
if (WaitForSingleObject(mq->hLock, INFINITE) == WAIT_OBJECT_0){
lSetBackgroundColour(frame->hw, 120<<24 | 0x000000);
lSetForegroundColour(frame->hw, 0xFFFFFFFF);
uint64_t fTime = getTime();
for (int i = 0; i < mq->total; i++){
if (mq->entry[i].time > fTime){
//if (mq->flags&MARQUEE_CENTER){
// printSingleLineShadow(frame, FONT, x, y, 0xFFFFFFFF, mq->entry[i].line);
//}else{
TFRAME *str = lNewString(frame->hw, frame->bpp, flags, FONT, mq->entry[i].line);
if (str){
lDrawLine(frame, x-1, y, x-1, y+str->height-1, 120<<24 | 0x000000);
lDrawImage(str, frame, x, y);
lDeleteFrame(str);
}
//}
y += 16;
}
}
mq->ready = (y > 2);
ReleaseMutex(mq->hLock);
}
return (y > 2);
}
static void addConsoleline (THR *hr, const char *str)
{
marqueeAdd(hr, hr->marquee, str, getTime()+(10*1000));
}
int _vasprintf (char **result, const char *format, va_list *args)
{
const char *p = format;
/* Add one to make sure that it is never zero, which might cause malloc
to return NULL. */
int total_width = strlen(format) + sizeof(char);
va_list ap;
my_memcpy(&ap, args, sizeof(va_list));
while (*p != '\0')
{
if (*p++ == '%')
{
while (strchr("-+ #0", *p))
++p;
if (*p == '*')
{
++p;
total_width += abs(va_arg(ap, int));
}
else
total_width += strtoul(p, (char**)&p, 10);
if (*p == '.')
{
++p;
if (*p == '*')
{
++p;
total_width += abs(va_arg(ap, int));
}
else
total_width += strtoul(p, (char**)&p, 10);
}
while(strchr("hl", *p))
++p;
/* Should be big enough for any format specifier except %s. */
total_width += 30;
switch (*p)
{
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
case 'c':
(void) va_arg(ap, int);
break;
case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
(void) va_arg(ap, double);
break;
case 's':
total_width += strlen(va_arg(ap, char *));
break;
case 'p':
case 'n':
(void) va_arg(ap, char *);
break;
}
}
}
if (!total_width) return 0;
*result = my_calloc(sizeof(char), 1+total_width);
if (*result != NULL)
return vsnprintf(*result, total_width, format, *args);
else
return 0;
}
void dbprintf (THR *hr, const char *str, ...)
{
char *buffer = NULL;
VA_OPEN(ap, str);
_vasprintf(&buffer, str, &ap);
VA_CLOSE(ap);
if (buffer){
addConsoleline(hr, buffer);
my_free(buffer);
}
}