summaryrefslogtreecommitdiff
path: root/bsp/simulator/drivers/sdl_fb.c
blob: 98da565a2a5b31b44776edc860fcd7c73452c449 (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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <rtthread.h>

#include <stdio.h>

#ifdef _WIN32
#include <sdl.h>
#else
#include <SDL/SDL.h>
#endif
#include <rtdevice.h>
#include <rtgui/driver.h>

#define SDL_SCREEN_WIDTH    480
#define SDL_SCREEN_HEIGHT   320

extern void rt_hw_exit(void);

#define SDL_SCREEN_FORMAT   SDL_PIXELFORMAT_RGB565

struct sdlfb_device
{
    struct rt_device parent;

    SDL_Renderer *renderer;         /* window renderer */
    SDL_Surface  *surface;          /* screen surface  */

    rt_uint16_t width;
    rt_uint16_t height;
};
struct sdlfb_device _device;

/* common device interface */
static rt_err_t  sdlfb_init(rt_device_t dev)
{
    return RT_EOK;
}
static rt_err_t  sdlfb_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}
static rt_err_t  sdlfb_close(rt_device_t dev)
{
    SDL_Quit();
    return RT_EOK;
}

int sdlfb_info(int *format, int *bpp)
{
    int bit_pp;                         /* texture bits per pixel */
    Uint32 Rmask, Gmask, Bmask, Amask;  /* masks for pixel format passed into OpenGL */

                                        /* Grab info about format that will be passed into OpenGL */
    SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bit_pp, &Rmask, &Gmask,
        &Bmask, &Amask);

    *bpp = bit_pp;
    switch (SDL_SCREEN_FORMAT)
    {
    case SDL_PIXELFORMAT_RGB565:
        *format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
        break;
    case SDL_PIXELFORMAT_RGB888:
        *format = RTGRAPHIC_PIXEL_FORMAT_RGB888;
        break;
    case SDL_PIXELFORMAT_ARGB8888:
        *format = RTGRAPHIC_PIXEL_FORMAT_ARGB888;
        break;
    case SDL_PIXELFORMAT_ABGR8888:
        *format = RTGRAPHIC_PIXEL_FORMAT_ABGR888;
    }

    return 0;
}

static rt_mutex_t sdllock;
static rt_err_t  sdlfb_control(rt_device_t dev, int cmd, void *args)
{
    struct sdlfb_device *device;

    rt_mutex_take(sdllock, RT_WAITING_FOREVER);
    device = (struct sdlfb_device *)dev;
    RT_ASSERT(device != RT_NULL);

    switch (cmd)
    {
    case RTGRAPHIC_CTRL_GET_INFO:
    {
        int format, bpp;
        struct rt_device_graphic_info *info;

        sdlfb_info(&format, &bpp);
        info = (struct rt_device_graphic_info *) args;
        info->bits_per_pixel = bpp;
        info->pixel_format = format;
        info->framebuffer = (rt_uint8_t*)device->surface->pixels;
        info->width = device->width;
        info->height = device->height;
    }
    break;

    case RTGRAPHIC_CTRL_RECT_UPDATE:
    {
        SDL_Texture * texture;
        struct rt_device_rect_info *rect;
        SDL_Rect _rect = { 0, 0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT };

        rect = (struct rt_device_rect_info *)args;

        SDL_RenderClear(_device.renderer);

        texture = SDL_CreateTextureFromSurface(_device.renderer, _device.surface);
        SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
        SDL_RenderCopy(_device.renderer, texture, NULL, &_rect);

        SDL_RenderPresent(_device.renderer);

        SDL_DestroyTexture(texture);
    }
    break;
    case RTGRAPHIC_CTRL_SET_MODE:
    {
        break;
    }
    break;
    }
    rt_mutex_release(sdllock);
    return RT_EOK;
}

Uint16 pixels[16 * 16] = {  // ...or with raw pixel data:
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0aab, 0x0789, 0x0bcc, 0x0eee, 0x09aa, 0x099a, 0x0ddd,
    0x0fff, 0x0eee, 0x0899, 0x0fff, 0x0fff, 0x1fff, 0x0dde, 0x0dee,
    0x0fff, 0xabbc, 0xf779, 0x8cdd, 0x3fff, 0x9bbc, 0xaaab, 0x6fff,
    0x0fff, 0x3fff, 0xbaab, 0x0fff, 0x0fff, 0x6689, 0x6fff, 0x0dee,
    0xe678, 0xf134, 0x8abb, 0xf235, 0xf678, 0xf013, 0xf568, 0xf001,
    0xd889, 0x7abc, 0xf001, 0x0fff, 0x0fff, 0x0bcc, 0x9124, 0x5fff,
    0xf124, 0xf356, 0x3eee, 0x0fff, 0x7bbc, 0xf124, 0x0789, 0x2fff,
    0xf002, 0xd789, 0xf024, 0x0fff, 0x0fff, 0x0002, 0x0134, 0xd79a,
    0x1fff, 0xf023, 0xf000, 0xf124, 0xc99a, 0xf024, 0x0567, 0x0fff,
    0xf002, 0xe678, 0xf013, 0x0fff, 0x0ddd, 0x0fff, 0x0fff, 0xb689,
    0x8abb, 0x0fff, 0x0fff, 0xf001, 0xf235, 0xf013, 0x0fff, 0xd789,
    0xf002, 0x9899, 0xf001, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0xe789,
    0xf023, 0xf000, 0xf001, 0xe456, 0x8bcc, 0xf013, 0xf002, 0xf012,
    0x1767, 0x5aaa, 0xf013, 0xf001, 0xf000, 0x0fff, 0x7fff, 0xf124,
    0x0fff, 0x089a, 0x0578, 0x0fff, 0x089a, 0x0013, 0x0245, 0x0eff,
    0x0223, 0x0dde, 0x0135, 0x0789, 0x0ddd, 0xbbbc, 0xf346, 0x0467,
    0x0fff, 0x4eee, 0x3ddd, 0x0edd, 0x0dee, 0x0fff, 0x0fff, 0x0dee,
    0x0def, 0x08ab, 0x0fff, 0x7fff, 0xfabc, 0xf356, 0x0457, 0x0467,
    0x0fff, 0x0bcd, 0x4bde, 0x9bcc, 0x8dee, 0x8eff, 0x8fff, 0x9fff,
    0xadee, 0xeccd, 0xf689, 0xc357, 0x2356, 0x0356, 0x0467, 0x0467,
    0x0fff, 0x0ccd, 0x0bdd, 0x0cdd, 0x0aaa, 0x2234, 0x4135, 0x4346,
    0x5356, 0x2246, 0x0346, 0x0356, 0x0467, 0x0356, 0x0467, 0x0467,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
    0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff
};

static void sdlfb_hw_init(void)
{
    SDL_Window *win;

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
    {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    _device.parent.init = sdlfb_init;
    _device.parent.open = sdlfb_open;
    _device.parent.close = sdlfb_close;
    _device.parent.read = RT_NULL;
    _device.parent.write = RT_NULL;
    _device.parent.control = sdlfb_control;

    _device.width  = SDL_SCREEN_WIDTH;
    _device.height = SDL_SCREEN_HEIGHT;

    {
        int bpp;                            /* texture bits per pixel */
        Uint32 Rmask, Gmask, Bmask, Amask;  /* masks for pixel format passed into OpenGL */

        /* Grab info about format that will be passed into OpenGL */
        SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bpp, &Rmask, &Gmask,
            &Bmask, &Amask);

        _device.surface = SDL_CreateRGBSurface(0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT, 
            bpp, Rmask, Gmask, Bmask, Amask);
    }

    win = SDL_CreateWindow("RT-Thread/Persimmon",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
    if (win == NULL)
    {
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
        exit(1);
    }

    /* set the icon of Window */
    {
        SDL_Surface *surface;
        surface = SDL_CreateRGBSurfaceFrom(pixels, 16, 16, 16, 16 * 2, 0x0f00, 0x00f0, 0x000f, 0xf000);
        SDL_SetWindowIcon(win, surface);
    }

    _device.renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

    rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);

    sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO);
}

#ifdef _WIN32
#include  <windows.h>
#include  <mmsystem.h>
#else
#include <pthread.h>
#include <signal.h>
#endif

#include  <stdio.h>
#include <rtgui/event.h>
#include <rtgui/kbddef.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>

#ifdef _WIN32
static HANDLE  sdl_ok_event = NULL;

static DWORD WINAPI sdl_loop(LPVOID lpParam)
#else
static pthread_mutex_t sdl_ok_mutex;
static pthread_cond_t sdl_ok_event;

static void *sdl_loop(void *lpParam)
#endif
{
    int quit = 0;
    SDL_Event event;
    int state = 0;
    rt_device_t device;
    int timeout = 1000000;

    int last_x = -1, last_y = -1;

    int motion_x, motion_y;
    int motion_button = 0;
    int motion_tick = 50;

    int mouse_id = 1;
 
#ifndef _WIN32
    sigset_t  sigmask, oldmask;
    /* set the getchar without buffer */
    sigfillset(&sigmask);
    pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
    pthread_mutex_lock(&sdl_ok_mutex);
#endif

    sdlfb_hw_init();

    device = rt_device_find("sdl");
    RT_ASSERT(device);
    rtgui_graphic_set_device(device);
#ifdef _WIN32
    SetEvent(sdl_ok_event);
#else
    pthread_cond_signal(&sdl_ok_event);
    pthread_mutex_unlock(&sdl_ok_mutex);
#endif
    /* handle SDL event */
    while (!quit)
    {
        if (motion_button & RTGUI_MOUSE_BUTTON_DOWN)
        {
            timeout = motion_tick - SDL_GetTicks();
        }
        else
        {
            timeout = 1000 * 1000;
        }

        int tick = SDL_GetTicks();
        SDL_WaitEventTimeout(&event, timeout);
        if (SDL_GetTicks() >= motion_tick && (motion_button & RTGUI_MOUSE_BUTTON_DOWN)) /* whether timeout */
        {
            /* sendout motion event */
            struct rtgui_event_mouse emouse;

            emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
            emouse.parent.sender = RT_NULL;
            emouse.wid = RT_NULL;
            emouse.id = mouse_id;
            emouse.ts = rt_tick_get();

            if ((motion_x > 0 && motion_x < SDL_SCREEN_WIDTH) &&
                (motion_y > 0 && motion_y < SDL_SCREEN_HEIGHT))
            {
                emouse.x = motion_x;
                emouse.y = motion_y;

                /* init mouse button */
                emouse.button = motion_button;

                /* send event to server */
                rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
            }

            /* reset motion tick */
            motion_tick = 50 + SDL_GetTicks();
        }

        switch (event.type)
        {
        case SDL_MOUSEMOTION:
        {
            /* save to (x,y) in the motion */
            motion_x = ((SDL_MouseMotionEvent *)&event)->x;
            motion_y = ((SDL_MouseMotionEvent *)&event)->y;
        }
        break;

        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
        {
            int x, y;
            struct rtgui_event_mouse emouse;
            SDL_MouseButtonEvent *mb;

            emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
            emouse.parent.sender = RT_NULL;
            emouse.wid = RT_NULL;
            if (event.type == SDL_MOUSEBUTTONDOWN && state == 0)
                mouse_id = rt_tick_get();

            emouse.id = mouse_id;
            emouse.ts = rt_tick_get();

            mb = (SDL_MouseButtonEvent *)&event;

            x = mb->x;
            y = mb->y;
            if ((x > 0 && x < SDL_SCREEN_WIDTH) &&
                (y > 0 && y < SDL_SCREEN_HEIGHT))
            {
                emouse.x = x;
                emouse.y = y;

                /* init mouse button */
                emouse.button = 0;

                /* set emouse button */
                if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
                {
                    emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
                }
                else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
                {
                    emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
                }
                else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
                {
                    emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
                }

                if (mb->type == SDL_MOUSEBUTTONDOWN)
                {
                    emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
                    motion_button = emouse.button;

                    /* set motion timeout tick */
                    motion_tick = 50 + SDL_GetTicks();

                    if (state == 0)
                    {
                        /* send event to server */
                        rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                        last_x = -1; last_y = -1;

                        state = 1;
                    }
                }
                else
                {
                    emouse.button |= RTGUI_MOUSE_BUTTON_UP;
                    motion_button = 0;

                    if (state == 1)
                    {
                        /* send event to server */
                        rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                        last_x = -1; last_y = -1;

                        state = 0;
                    }
                }
            }
        }
        break;

        case SDL_KEYUP:
        {
            struct rtgui_event_kbd ekbd;
            ekbd.parent.type    = RTGUI_EVENT_KBD;
            ekbd.parent.sender  = RT_NULL;
            ekbd.type = RTGUI_KEYUP;
            ekbd.wid = RT_NULL;
            ekbd.mod = event.key.keysym.mod;
            ekbd.key = event.key.keysym.sym;

            /* FIXME: unicode */
            ekbd.unicode = 0;

            /* send event to server */
            rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
        }
        break;

        case SDL_KEYDOWN:
        {
            struct rtgui_event_kbd ekbd;
            ekbd.parent.type    = RTGUI_EVENT_KBD;
            ekbd.parent.sender  = RT_NULL;
            ekbd.type = RTGUI_KEYDOWN;
            ekbd.wid = RT_NULL;
            ekbd.mod = event.key.keysym.mod;
            ekbd.key = event.key.keysym.sym;

            /* FIXME: unicode */
            ekbd.unicode = 0;

            /* send event to server */
            rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
        }
        break;

        case SDL_QUIT:
            SDL_Quit();
            quit = 1;
            break;

        default:
            break;
        }

        if (quit)
        {
            exit(1);
            break;
        }
            
    }
    rt_hw_exit();
    return 0;
}

/* start sdl thread */
void rt_hw_sdl_start(void)
{
#ifdef _WIN32
    HANDLE thread;
    DWORD  thread_id;
    sdl_ok_event = CreateEvent(NULL,
        FALSE,
        FALSE,
        NULL);
    if (sdl_ok_event == NULL)
    {
        printf("error, create SDL event failed\n");
        exit(-1);
    }
    /* create thread that loop sdl event */
    thread = CreateThread(NULL,
                          0,
                          (LPTHREAD_START_ROUTINE)sdl_loop,
                          0,
                          CREATE_SUSPENDED,
                          &thread_id);
    if (thread == NULL)
    {
        //Display Error Message

        return;
    }
    ResumeThread(thread);

    /* wait until SDL LCD device is registered and seted */
    WaitForSingleObject(sdl_ok_event, INFINITE);
#else
    /* Linux */
    pthread_t pid;
    int res;

    pthread_mutex_init(&sdl_ok_mutex, NULL);
    pthread_cond_init(&sdl_ok_event, NULL);

    res = pthread_create(&pid, NULL, &sdl_loop, NULL);
    if (res)
    {
        printf("pthread create sdl thread faild, <%d>\n", res);
        exit(EXIT_FAILURE);
    }
    pthread_mutex_lock(&sdl_ok_mutex);
    pthread_cond_wait(&sdl_ok_event, &sdl_ok_mutex);

    pthread_mutex_destroy(&sdl_ok_mutex);
    pthread_cond_destroy(&sdl_ok_event);
#endif
}