forked from lvgl/lv_drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin_drv.c
More file actions
301 lines (262 loc) · 7.79 KB
/
win_drv.c
File metadata and controls
301 lines (262 loc) · 7.79 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* @file win_drv.c
*
*/
/*********************
* INCLUDES
*********************/
#include "win_drv.h"
#if USE_WINDOWS
#include <windows.h>
#include <windowsx.h>
#include "lvgl/lv_core/lv_vdb.h"
#include "lvgl/lvgl.h"
#include "lvgl/lv_core/lv_refr.h"
#if LV_COLOR_DEPTH < 16
#error Windows driver only supports true RGB colors at this time
#endif
/**********************
* DEFINES
**********************/
#define WINDOW_STYLE (WS_OVERLAPPEDWINDOW & ~(WS_SIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME))
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void win_drv_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p);
static void win_drv_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color);
static void win_drv_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p);
static bool win_drv_read(lv_indev_data_t * data);
static void msg_handler(void *param);
static COLORREF lv_color_to_colorref(const lv_color_t color);
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
/**********************
* GLOBAL VARIABLES
**********************/
bool lv_win_exit_flag = false;
/**********************
* STATIC VARIABLES
**********************/
static HWND hwnd;
static uint32_t *fbp = NULL; /* Raw framebuffer memory */
static bool mouse_pressed;
static int mouse_x, mouse_y;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
const char g_szClassName[] = "LittlevGL";
HWND windrv_init(void)
{
WNDCLASSEX wc;
RECT winrect;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
return NULL;
}
winrect.left = 0;
winrect.right = WINDOW_HOR_RES - 1;
winrect.top = 0;
winrect.bottom = WINDOW_VER_RES - 1;
AdjustWindowRectEx(&winrect, WINDOW_STYLE, FALSE, WS_EX_CLIENTEDGE);
OffsetRect(&winrect, -winrect.left, -winrect.top);
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WINDOW_STYLE,
CW_USEDEFAULT, CW_USEDEFAULT, winrect.right, winrect.bottom,
NULL, NULL, GetModuleHandle(NULL), NULL);
if(hwnd == NULL)
{
return NULL;
}
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.disp_flush = win_drv_flush;
disp_drv.disp_fill = win_drv_fill;
disp_drv.disp_map = win_drv_map;
lv_disp_drv_register(&disp_drv);
lv_task_create(msg_handler, 0, LV_TASK_PRIO_HIGHEST, NULL);
lv_win_exit_flag = false;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void msg_handler(void *param)
{
(void)param;
MSG msg;
BOOL bRet;
while( (bRet = PeekMessage( &msg, NULL, 0, 0, TRUE )) != 0)
{
if (bRet == -1)
{
return;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(msg.message == WM_QUIT)
lv_win_exit_flag = true;
}
static bool win_drv_read(lv_indev_data_t * data)
{
data->state = mouse_pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
data->point.x = mouse_x;
data->point.y = mouse_y;
return false;
}
static void on_paint(void)
{
HBITMAP bmp = CreateBitmap(WINDOW_HOR_RES, WINDOW_VER_RES, 1, 32, fbp);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = SelectObject(hdcMem, bmp);
BitBlt(hdc, 0, 0, WINDOW_HOR_RES, WINDOW_VER_RES, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
DeleteObject(bmp);
}
/**
* Flush a buffer to the marked area
* @param x1 left coordinate
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color_p an array of colors
*/
static void win_drv_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
{
win_drv_map(x1, y1, x2, y2, color_p);
lv_flush_ready();
}
/**
* Fill out the marked area with a color
* @param x1 left coordinate
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color fill color
*/
static void win_drv_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color)
{
#if 0
HBRUSH brush = CreateSolidBrush(lv_color_to_colorref(color));
HPEN pen = SelectObject(hwnd_dc, NULL_PEN);
RECT rect;
rect.top = y1;
rect.bottom = y2;
rect.left = x1;
rect.right = x2;
FillRect(hwnd_dc, &rect, brush);
SelectObject(hwnd_dc, pen);
DeleteObject(brush);
#endif
}
/**
* Put a color map to the marked area
* @param x1 left coordinate
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color_p an array of colors
*/
static void win_drv_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
{
for(int y = y1; y <= y2; y++)
{
for(int x = x1; x <= x2; x++)
{
fbp[y*WINDOW_HOR_RES+x] = lv_color_to32(*color_p);
color_p++;
}
}
InvalidateRect(hwnd, NULL, FALSE);
UpdateWindow(hwnd);
}
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(msg) {
case WM_CREATE:
fbp = malloc(4*WINDOW_HOR_RES*WINDOW_VER_RES);
if(fbp == NULL)
return 1;
SetTimer(hwnd, 0, 10, (TIMERPROC)lv_task_handler);
SetTimer(hwnd, 1, 25, NULL);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.disp_flush = win_drv_flush;
disp_drv.disp_fill = win_drv_fill;
disp_drv.disp_map = win_drv_map;
lv_disp_drv_register(&disp_drv);
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read = win_drv_read;
lv_indev_drv_register(&indev_drv);
return 0;
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
mouse_x = GET_X_LPARAM(lParam);
mouse_y = GET_Y_LPARAM(lParam);
if(msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) {
mouse_pressed = (msg == WM_LBUTTONDOWN);
}
return 0;
case WM_CLOSE:
free(fbp);
fbp = NULL;
DestroyWindow(hwnd);
return 0;
case WM_PAINT:
on_paint();
return 0;
case WM_TIMER:
lv_tick_inc(25);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
static COLORREF lv_color_to_colorref(const lv_color_t color)
{
uint32_t raw_color = lv_color_to32(color);
lv_color32_t tmp;
tmp.full = raw_color;
uint32_t colorref = RGB(tmp.red, tmp.green, tmp.blue);
return colorref;
}
#endif