src/video/ataricommon/SDL_ataridevmouse.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 1895 c121d94672cb
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
patmandin@1420
     1
/*
patmandin@1420
     2
    SDL - Simple DirectMedia Layer
patmandin@1420
     3
    Copyright (C) 1997-2006 Sam Lantinga
patmandin@1420
     4
patmandin@1420
     5
    This library is free software; you can redistribute it and/or
patmandin@1420
     6
    modify it under the terms of the GNU Lesser General Public
patmandin@1420
     7
    License as published by the Free Software Foundation; either
patmandin@1420
     8
    version 2.1 of the License, or (at your option) any later version.
patmandin@1420
     9
patmandin@1420
    10
    This library is distributed in the hope that it will be useful,
patmandin@1420
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
patmandin@1420
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
patmandin@1420
    13
    Lesser General Public License for more details.
patmandin@1420
    14
patmandin@1420
    15
    You should have received a copy of the GNU Lesser General Public
patmandin@1420
    16
    License along with this library; if not, write to the Free Software
patmandin@1420
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
patmandin@1420
    18
patmandin@1420
    19
    Sam Lantinga
patmandin@1420
    20
    slouken@libsdl.org
patmandin@1420
    21
*/
patmandin@1420
    22
#include "SDL_config.h"
patmandin@1420
    23
patmandin@1420
    24
/*
patmandin@1420
    25
	MiNT /dev/mouse driver
patmandin@1420
    26
patmandin@1420
    27
	Patrice Mandin
patmandin@1420
    28
*/
patmandin@1420
    29
patmandin@1420
    30
#include <fcntl.h>
patmandin@1420
    31
#include <unistd.h>
patmandin@1420
    32
patmandin@1420
    33
#include "../../events/SDL_events_c.h"
patmandin@1420
    34
#include "SDL_ataridevmouse_c.h"
patmandin@1420
    35
patmandin@1420
    36
/* Defines */
patmandin@1420
    37
patmandin@1420
    38
#define DEVICE_NAME	"/dev/mouse"
patmandin@1420
    39
patmandin@1420
    40
/* Local variables */
patmandin@1420
    41
patmandin@1420
    42
static int handle = -1;
patmandin@1420
    43
static int mouseb, prev_mouseb;
patmandin@1420
    44
patmandin@1420
    45
/* Functions */
patmandin@1420
    46
slouken@1895
    47
int
slouken@1895
    48
SDL_AtariDevMouse_Open(void)
patmandin@1420
    49
{
slouken@1895
    50
    int r;
slouken@1895
    51
    const char *mousedev;
patmandin@1420
    52
slouken@1895
    53
    /*
slouken@1895
    54
       TODO: Fix the MiNT device driver, that locks mouse for other
slouken@1895
    55
       applications, so this is disabled till fixed
slouken@1895
    56
     */
slouken@1895
    57
    return 0;
patmandin@1420
    58
slouken@1895
    59
    /* First, try SDL_MOUSEDEV device */
slouken@1895
    60
    mousedev = SDL_getenv("SDL_MOUSEDEV");
slouken@1895
    61
    if (!mousedev) {
slouken@1895
    62
        handle = open(mousedev, 0);
slouken@1895
    63
    }
slouken@1895
    64
slouken@1895
    65
    /* Failed, try default device */
slouken@1895
    66
    if (handle < 0) {
slouken@1895
    67
        handle = open(DEVICE_NAME, 0);
slouken@1895
    68
    }
patmandin@1420
    69
slouken@1895
    70
    if (handle < 0) {
slouken@1895
    71
        handle = -1;
slouken@1895
    72
        return 0;
slouken@1895
    73
    }
patmandin@1420
    74
slouken@1895
    75
    /* Set non blocking mode */
slouken@1895
    76
    r = fcntl(handle, F_GETFL, 0);
slouken@1895
    77
    if (r < 0) {
slouken@1895
    78
        close(handle);
slouken@1895
    79
        handle = -1;
slouken@1895
    80
        return 0;
slouken@1895
    81
    }
patmandin@1420
    82
slouken@1895
    83
    r |= O_NDELAY;
patmandin@1420
    84
slouken@1895
    85
    r = fcntl(handle, F_SETFL, r);
slouken@1895
    86
    if (r < 0) {
slouken@1895
    87
        close(handle);
slouken@1895
    88
        handle = -1;
slouken@1895
    89
        return 0;
slouken@1895
    90
    }
patmandin@1420
    91
slouken@1895
    92
    prev_mouseb = 7;
slouken@1895
    93
    return 1;
patmandin@1420
    94
}
patmandin@1420
    95
slouken@1895
    96
void
slouken@1895
    97
SDL_AtariDevMouse_Close(void)
patmandin@1420
    98
{
slouken@1895
    99
    if (handle > 0) {
slouken@1895
   100
        close(handle);
slouken@1895
   101
        handle = -1;
slouken@1895
   102
    }
patmandin@1420
   103
}
patmandin@1420
   104
slouken@1895
   105
static int
slouken@1895
   106
atari_GetButton(int button)
patmandin@1420
   107
{
slouken@1895
   108
    switch (button) {
slouken@1895
   109
    case 0:
slouken@1895
   110
        return SDL_BUTTON_RIGHT;
slouken@1895
   111
    case 1:
slouken@1895
   112
        return SDL_BUTTON_MIDDLE;
slouken@1895
   113
    default:
slouken@1895
   114
        break;
slouken@1895
   115
    }
patmandin@1420
   116
slouken@1895
   117
    return SDL_BUTTON_LEFT;
patmandin@1420
   118
}
patmandin@1420
   119
slouken@1895
   120
void
slouken@1895
   121
SDL_AtariDevMouse_PostMouseEvents(_THIS, SDL_bool buttonEvents)
patmandin@1420
   122
{
slouken@1895
   123
    unsigned char buffer[3];
slouken@1895
   124
    int mousex, mousey;
patmandin@1420
   125
slouken@1895
   126
    if (handle < 0) {
slouken@1895
   127
        return;
slouken@1895
   128
    }
patmandin@1420
   129
slouken@1895
   130
    mousex = mousey = 0;
slouken@1895
   131
    while (read(handle, buffer, sizeof(buffer)) == sizeof(buffer)) {
slouken@1895
   132
        mouseb = buffer[0] & 7;
slouken@1895
   133
        mousex += (char) buffer[1];
slouken@1895
   134
        mousey += (char) buffer[2];
patmandin@1420
   135
slouken@1895
   136
        /* Mouse button events */
slouken@1895
   137
        if (buttonEvents && (mouseb != prev_mouseb)) {
slouken@1895
   138
            int i;
slouken@1895
   139
slouken@1895
   140
            for (i = 0; i < 3; i++) {
slouken@1895
   141
                int curbutton, prevbutton;
patmandin@1420
   142
slouken@1895
   143
                curbutton = mouseb & (1 << i);
slouken@1895
   144
                prevbutton = prev_mouseb & (1 << i);
patmandin@1420
   145
slouken@1895
   146
                if (curbutton && !prevbutton) {
slouken@1895
   147
                    SDL_PrivateMouseButton(SDL_RELEASED,
slouken@1895
   148
                                           atari_GetButton(i), 0, 0);
slouken@1895
   149
                }
slouken@1895
   150
                if (!curbutton && prevbutton) {
slouken@1895
   151
                    SDL_PrivateMouseButton(SDL_PRESSED,
slouken@1895
   152
                                           atari_GetButton(i), 0, 0);
slouken@1895
   153
                }
slouken@1895
   154
            }
patmandin@1420
   155
slouken@1895
   156
            prev_mouseb = mouseb;
slouken@1895
   157
        }
slouken@1895
   158
    }
patmandin@1420
   159
slouken@1895
   160
    /* Mouse motion event */
slouken@1895
   161
    if (mousex || mousey) {
slouken@1895
   162
        SDL_PrivateMouseMotion(0, 1, mousex, -mousey);
slouken@1895
   163
    }
patmandin@1420
   164
}
slouken@1895
   165
slouken@1895
   166
/* vi: set ts=4 sw=4 expandtab: */