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