Example #1
0
File: main.c Project: Larhard/hurd
int main (int argc, char **argv)
{
  struct ps_context *pc;
  mach_port_t bootstrap;
  error_t err;

  opt_clk_tck = OPT_CLK_TCK;
  opt_stat_mode = OPT_STAT_MODE;
  opt_fake_self = OPT_FAKE_SELF;
  opt_kernel_pid = OPT_KERNEL_PID;
  opt_anon_owner = OPT_ANON_OWNER;
  err = argp_parse (&argp, argc, argv, 0, 0, 0);
  if (err)
    error (1, err, "Could not parse command line");

  err = ps_context_create (getproc (), &pc);
  if (err)
    error (1, err, "Could not create libps context");

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap == MACH_PORT_NULL)
    error (1, 0, "Must be started as a translator");

  netfs_init ();
  err = root_make_node (pc, &netfs_root_node);
  if (err)
    error (1, err, "Could not create the root node");

  netfs_startup (bootstrap, 0);
  netfs_server_loop ();

  assert (0 /* netfs_server_loop returned after all */);
}
/**
 * Initialize an context for the current display.
 */
static int engine_init_display(struct engine* engine)
{
    int w, h, f, s, b;
    ps_color_format fmt;    

    ps_initialize();

    w = ANativeWindow_getWidth(engine->app->window);
    h = ANativeWindow_getHeight(engine->app->window);
    f = ANativeWindow_getFormat(engine->app->window);

    
    if (f == WINDOW_FORMAT_RGBA_8888 || f == WINDOW_FORMAT_RGBX_8888) {
        b = 4;
        fmt = COLOR_FORMAT_ARGB;
    } else {
        b = 2;
        fmt = COLOR_FORMAT_RGB565;
    }

    ANativeWindow_Buffer buffer;
    ANativeWindow_lock(engine->app->window, &buffer, 0);
    s = buffer.stride * b;
    // create canvas with fake buffer bits !
    engine->canvas = ps_canvas_create_with_data((uint8_t*)buffer.bits, fmt, w, h, s);
    engine->context = ps_context_create(engine->canvas, 0);
    init_context(engine->context, engine->canvas, (unsigned char*)buffer.bits);    
    set_image_data(selt2565.bits, COLOR_FORMAT_RGB565, selt2565.width, selt2565.height, selt2565.pitch);
    set_pattern_data(pat565.bits, COLOR_FORMAT_RGB565, pat565.width, pat565.height, pat565.pitch);
   // ps_scale(engine->context, 4, 4); // zoom 

    engine->width = w;
    engine->height = h;
    engine->bpp = b;
    engine->fmt = fmt;
    ANativeWindow_unlockAndPost(engine->app->window);

    return 0;

}
static int PlatformProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
        case MSG_CREATE:
            {
                if (!hmWnd)
                    hmWnd = hWnd;

                unsigned char* pdr = 0;
                int w,h,p;
                RECT r;
                key_down = FALSE;
                GetClientRect(hWnd, &r);
#if RGB555
            sdc = CreateMemDC(width, height, 16, MEMDC_FLAG_SWSURFACE, 0x7C00, 0x3E0, 0x1F, 0x8000);    
#else
            sdc = CreateMemDC(width, height, 16, MEMDC_FLAG_SWSURFACE, 0xF800, 0x7E0, 0x1F, 0x00);    
#endif
                pdr = LockDC(sdc, &r, &w, &h, &p);
                UnlockDC(sdc);
#if RGB555
                fmt = COLOR_FORMAT_RGB555;
#else
                fmt = COLOR_FORMAT_RGB565;
#endif
                ps_initialize();

                canvas = ps_canvas_create_with_data(pdr, fmt, w, h, p);
                context = ps_context_create(canvas, 0);
                on_init(context, width, height);    
            }
            break;
        case MSG_TIMER:
            on_timer();
            break;
        case MSG_PAINT:
            {
                HDC hdc = BeginPaint(hWnd);
                on_draw(context);
                BitBlt(sdc, 0, 0, width, height, hdc, 0, 0, 0);
                EndPaint(hWnd, hdc);
            }
            return 0;
        case MSG_ERASEBKGND:
            return 0;
        case MSG_LBUTTONDOWN:
            {
                key_down = TRUE;
                on_mouse_event(LEFT_BUTTON_DOWN, key_conv(wParam), LOSWORD(lParam), HISWORD(lParam));
            }
            break;
        case MSG_LBUTTONUP:
            {
                key_down = FALSE;
                on_mouse_event(LEFT_BUTTON_UP, key_conv(wParam), LOSWORD(lParam), HISWORD(lParam));
            }
            break;
        case MSG_MOUSEMOVE:
            {
                DWORD k = key_conv(wParam);
                if (key_down)
                    k |= EVT_LBUTTON;
                on_mouse_event(MOUSE_MOVE, k, LOSWORD(lParam), HISWORD(lParam));
            }
            break;
        case MSG_KEYDOWN:
            on_key_event(KEY_EVENT_DOWN, get_virtual_key(wParam));
            break;
        case MSG_KEYUP:
            on_key_event(KEY_EVENT_UP, get_virtual_key(wParam));
            break;
        case MSG_SIZECHANGED:
            {
                RECT r;
                int w,h,p;
                unsigned char* pdr = 0;
                ps_canvas* old_canvas = 0;
                RECT* rc = (RECT*)lParam;
                width = RECTWP(rc);
                height = RECTHP(rc);
                if (sdc)
                    DeleteMemDC(sdc);

                if (width < 1)
                    width = 1;
                if (height < 1)
                    height = 1;
#if RGB555
            sdc = CreateMemDC(width, height, 16, MEMDC_FLAG_SWSURFACE, 0x7C00, 0x3E0, 0x1F, 0x8000);    
#else
            sdc = CreateMemDC(width, height, 16, MEMDC_FLAG_SWSURFACE, 0xF800, 0x7E0, 0x1F, 0x00);    
#endif
                GetClientRect(hWnd, &r);
                pdr = LockDC(sdc, &r, &w, &h, &p);
                UnlockDC(sdc);
                canvas = ps_canvas_create_with_data(pdr, fmt, w, h, p);
                old_canvas = ps_context_set_canvas(context, canvas);
                ps_canvas_unref(old_canvas);
                on_size(width, height);
            }
            break;
        case MSG_CLOSE:
            on_term(context);
            ps_context_unref(context);
            ps_canvas_unref(canvas);
            ps_shutdown();
            DeleteMemDC(sdc);
            DestroyMainWindow (hWnd);
            PostQuitMessage (hWnd);
            return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
    PAINTSTRUCT ps;
    HDC hdc;
    
    switch (message) 
    {
    case WM_CREATE:
        {
            if (!hmWnd)
                hmWnd = hWnd;

            hdc = GetDC(hWnd);
            CBIT = GetDeviceCaps(hdc, BITSPIXEL);    
            CBYTE = CBIT/8;
            ReleaseDC(hWnd, hdc);
            
            buffer = malloc(width*height*CBYTE);
            bmp.bmType = 0;
            bmp.bmWidth = width;
            bmp.bmHeight = height;
            bmp.bmWidthBytes = width*CBYTE;
            bmp.bmPlanes = 1;
            bmp.bmBitsPixel = CBIT;
            bmp.bmBits = buffer;
            
            if (CBYTE == 4)
                fmt = COLOR_FORMAT_BGRA;
            else if (CBYTE == 3)
                fmt = COLOR_FORMAT_BGR;
            else if (CBYTE == 2)
                fmt = COLOR_FORMAT_RGB565;

            ps_initialize();

            canvas = ps_canvas_create_with_data(buffer, fmt, width, height, width*CBYTE);
            context = ps_context_create(canvas, 0);
            on_init(context, width, height);    
        }
        break;
    case WM_PAINT:
        {
            HDC mdc;
            HGDIOBJ h;
            hdc = BeginPaint(hWnd, &ps);
            
            on_draw(context);
            
            mdc = CreateCompatibleDC(hdc);
            hbmp = CreateBitmapIndirect(&bmp);
            h = SelectObject(mdc, hbmp);
            BitBlt(hdc, 0, 0, width, height, mdc, 0, 0, SRCCOPY);
            SelectObject(mdc, h);
            DeleteObject(hbmp);
            DeleteDC(mdc);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_TIMER:
        on_timer();
        break;
    case WM_SIZE:
        {
            ps_canvas* old_canvas = 0;
            width = LOWORD(lParam);
            height = HIWORD(lParam);
            free(buffer);

            if (width < 1)
                width = 1;
            if (height < 1)
                height = 1;

            buffer = malloc(width*height*CBYTE);
            bmp.bmType = 0;
            bmp.bmWidth = width;
            bmp.bmHeight = height;
            bmp.bmWidthBytes = width*CBYTE;
            bmp.bmPlanes = 1;
            bmp.bmBitsPixel = CBIT;
            bmp.bmBits = buffer;

            canvas = ps_canvas_create_with_data(buffer, fmt, width, height, width*CBYTE);
            old_canvas = ps_context_set_canvas(context, canvas);
            ps_canvas_unref(old_canvas);
            on_size(width, height);
        }
        break;
    case WM_ERASEBKGND:
        break;
    case WM_LBUTTONDOWN:
            on_mouse_event(LEFT_BUTTON_DOWN, wParam, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_MOUSEMOVE:
            on_mouse_event(MOUSE_MOVE, wParam, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_KEYDOWN:
            on_key_event(KEY_EVENT_DOWN, wParam);
        break;
    case WM_KEYUP:
            on_key_event(KEY_EVENT_UP, wParam);
        break;
    case WM_DESTROY:
        {
            on_term(context);
            ps_context_unref(context);
            ps_canvas_unref(canvas);
            ps_shutdown();
            free(buffer);
            PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}