sdl_fb.c 9.2 KB
Newer Older
M
Ming, Bai 已提交
1 2
#include <rtthread.h>

P
prife 已提交
3
#ifdef _WIN32
M
Ming, Bai 已提交
4
#include <sdl.h>
P
prife 已提交
5 6 7
#else
#include <SDL/SDL.h>
#endif
M
Ming, Bai 已提交
8 9 10 11 12 13
#include <rtdevice.h>
#include <rtgui/driver.h>

#define SDL_SCREEN_WIDTH    800
#define SDL_SCREEN_HEIGHT   480

14 15
extern void rt_hw_exit(void);

M
Ming, Bai 已提交
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
struct sdlfb_device
{
    struct rt_device parent;

    SDL_Surface *screen;
    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;
}

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

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

    switch (cmd)
    {
    case RTGRAPHIC_CTRL_GET_INFO:
    {
        struct rt_device_graphic_info *info;

        info = (struct rt_device_graphic_info *) args;
        info->bits_per_pixel = 16;
59
        info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
M
Ming, Bai 已提交
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
        info->framebuffer = device->screen->pixels;
        info->width = device->screen->w;
        info->height = device->screen->h;
    }
    break;
    case RTGRAPHIC_CTRL_RECT_UPDATE:
    {
        struct rt_device_rect_info *rect;
        rect = (struct rt_device_rect_info *)args;

        /* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */
        SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
    }
    break;
    case RTGRAPHIC_CTRL_SET_MODE:
    {
#if 0
        struct rt_device_rect_info *rect;

        rect = (struct rt_device_rect_info *)args;
        if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR;

        _device.width = rect->width;
        _device.height = rect->height;

        if (_device.screen != RT_NULL)
        {
            SDL_FreeSurface(_device.screen);

            /* re-create screen surface */
            _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
            if (_device.screen == NULL)
            {
                fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
                exit(1);
            }

            SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
        }
#endif
    }
    break;
    }
    rt_mutex_release(sdllock);
    return RT_EOK;
}

static void sdlfb_hw_init(void)
{
    /* set video driver for VC++ debug */
    //_putenv("SDL_VIDEODRIVER=windib");

    //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
P
prife 已提交
113
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
M
Ming, Bai 已提交
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
    {
        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;
    _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
    if (_device.screen == NULL)
    {
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
        exit(1);
    }

    SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
    rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);

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

P
prife 已提交
141
#ifdef _WIN32
M
Ming, Bai 已提交
142 143
#include  <windows.h>
#include  <mmsystem.h>
P
prife 已提交
144 145
#else
#include <pthread.h>
P
prife 已提交
146
#include <signal.h>
P
prife 已提交
147 148
#endif

M
Ming, Bai 已提交
149 150 151 152 153 154
#include  <stdio.h>
#include <rtgui/event.h>
#include <rtgui/kbddef.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>

P
prife 已提交
155
#ifdef _WIN32
156
static HANDLE  sdl_ok_event = NULL;
157

M
Ming, Bai 已提交
158
static DWORD WINAPI sdl_loop(LPVOID lpParam)
P
prife 已提交
159
#else
160 161 162
static pthread_mutex_t sdl_ok_mutex;
static pthread_cond_t sdl_ok_event;

P
prife 已提交
163 164
static void *sdl_loop(void *lpParam)
#endif
M
Ming, Bai 已提交
165 166 167 168 169
{
    int quit = 0;
    SDL_Event event;
    int button_state = 0;
    rt_device_t device;
P
prife 已提交
170 171 172 173 174 175

#ifndef _WIN32
    sigset_t  sigmask, oldmask;
    /* set the getchar without buffer */
    sigfillset(&sigmask);
    pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
176
    pthread_mutex_lock(&sdl_ok_mutex);
P
prife 已提交
177 178
#endif

M
Ming, Bai 已提交
179 180 181
    sdlfb_hw_init();

    device = rt_device_find("sdl");
182
    RT_ASSERT(device);
M
Ming, Bai 已提交
183
    rtgui_graphic_set_device(device);
184 185
#ifdef _WIN32
    SetEvent(sdl_ok_event);
186 187 188
#else
    pthread_cond_signal(&sdl_ok_event);
    pthread_mutex_unlock(&sdl_ok_mutex);
189
#endif
M
Ming, Bai 已提交
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
    /* handle SDL event */
    while (!quit)
    {
        SDL_WaitEvent(&event);

        switch (event.type)
        {
        case SDL_MOUSEMOTION:
        {
            struct rtgui_event_mouse emouse;
            emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
            emouse.parent.sender = RT_NULL;
            emouse.wid = RT_NULL;

            emouse.x = ((SDL_MouseMotionEvent *)&event)->x;
            emouse.y = ((SDL_MouseMotionEvent *)&event)->y;

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

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

        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
        {
            struct rtgui_event_mouse emouse;
            SDL_MouseButtonEvent *mb;

            emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
            emouse.parent.sender = RT_NULL;
            emouse.wid = RT_NULL;

            mb = (SDL_MouseButtonEvent *)&event;

            emouse.x = mb->x;
            emouse.y = mb->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;
                button_state = emouse.button;
            }
            else
            {
                emouse.button |= RTGUI_MOUSE_BUTTON_UP;
                button_state = 0;
            }


            /* send event to server */
            rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
        }
        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;
        }

309 310 311 312 313 314
		if (quit)
		{
		 exit(1);
		 break;
		}
            
M
Ming, Bai 已提交
315
    }
316
    rt_hw_exit();
M
Ming, Bai 已提交
317 318 319 320 321 322
    return 0;
}

/* start sdl thread */
void rt_hw_sdl_start(void)
{
P
prife 已提交
323
#ifdef _WIN32
M
Ming, Bai 已提交
324 325
    HANDLE thread;
    DWORD  thread_id;
326 327 328 329 330 331 332 333 334
    sdl_ok_event = CreateEvent(NULL,
        FALSE,
        FALSE,
        NULL);
    if (sdl_ok_event == NULL)
    {
        printf("error, create SDL event failed\n");
        exit(-1);
    }
M
Ming, Bai 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348
    /* 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);
349 350 351

    /* wait until SDL LCD device is registered and seted */
    WaitForSingleObject(sdl_ok_event, INFINITE);
P
prife 已提交
352
#else
P
prife 已提交
353 354
    /* Linux */
    pthread_t pid;
P
prife 已提交
355
    int res;
356 357 358 359

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

P
prife 已提交
360 361 362 363 364
    res = pthread_create(&pid, NULL, &sdl_loop, NULL);
    if (res)
    {
        printf("pthread create sdl thread faild, <%d>\n", res);
        exit(EXIT_FAILURE);
P
prife 已提交
365
    }
366 367 368 369 370
    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);
P
prife 已提交
371
#endif
M
Ming, Bai 已提交
372
}