/*************************************************************************
 *
 * SDL_mouse.c - Mouse module of framebuffer client.
 *
 * $Id$
 *
 * Copyright 2010-2012 Citrix Systems, Inc.  All Rights Reserved.
 *
 *************************************************************************/
#include "citrix.h"

#include "SDL/SDL_events.h"

#include "mainloop.h"
#include "simplified.h"
#include "CTX_SDL_internal.h"

/* Note ordering, ICA thinks the middle button is number 3. */

static unsigned char  buttons[3][2] = {{MOU_STATUS_B1DOWN, MOU_STATUS_B1UP},
                                       {MOU_STATUS_B3DOWN, MOU_STATUS_B3UP},
                                       {MOU_STATUS_B2DOWN, MOU_STATUS_B2UP}};

int handle_SDL_mouse(SDL_Event *ev)
{
    static int          last_x, last_y;
    unsigned int        button;

    switch (ev->type) {
      case SDL_MOUSEMOTION:
        last_x = ev->motion.x;
        last_y = ev->motion.y;
        Mouse_changed(&last_x, &last_y, MOU_STATUS_MOVED);
        break;
      case SDL_MOUSEBUTTONDOWN:
      case SDL_MOUSEBUTTONUP:
        button = ev->button.button - 1;
        if (button > ELEMENTS_IN_ARRAY(buttons))
            break;
        Mouse_changed(&last_x, &last_y,
                      buttons[button][ev->type == SDL_MOUSEBUTTONUP]);
        break;
      default:
        break;
    }
    return 0;   /* No screen update. */
}
