Mercurial > sdl-ios-xcode
view src/thread/linux/SDL_syssem.c @ 1192:54aa9aa32327
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Fri, 18 Nov 2005 23:39:02 +0100
Subject: [SDL] Mouse position bugs on Mac OS X
The attached patch fixes a few bugs in SDL related to the mouse position
in windowed mode on Mac OS X, reproduced using the attached minimal test
program - at least here on 10.3.9, with SDL CVS from today. Could anyone
test whether the bugs exist and are fixed by the patch on 10.2 and 10.4?
1. When using OpenGL, the vertical mouse positions obtained through
events or SDL_GetMouseState() are off by one.
2. When using OpenGL, SDL_WarpMouse() inverts the y coordinate.
3. Clicks on the topmost pixel row of the window are not recognized.
1 and 2 do not occur in non-OpenGL mode, while 3 does. All three only
occur in windowed mode, not in fullscreen.
The cause for 1 and 3 is that in Cocoa, "the location of the mouse"
seems to be defined as "the location of the top left corner of the mouse
pointer's hot pixel" (this is not documented, it's just what I found out
here), which together with the fact that Cocoa's usual y coordinates
start at the bottom and increase upwards means that the y coordinate of
the mouse runs from 1 to h, not from 0 to h-1, in a window of height h.
If it does work on 10.2 and 10.4 (I'll try to test it as soon as I can,
but at the moment all I have at hand is 10.3.9), can this be applied to
the CVS?
-Christian
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Mon, 28 Nov 2005 10:41:51 +0100
Subject: [SDL] Re: Mouse position bugs on Mac OS X
I wrote:
> I'll try to test it as soon as I can, but at the moment all I have at hand is 10.3.9
So, here are the results of my tests (with patched and unpatched
frameworks compiled with Xcode 1.5 (gcc 3.3) on 10.3.9):
On 10.1.5, my test program doesn't run because of "Undefined symbols:
SDL undefined reference to _CGMainDisplayID expected to be defined in
Carbon". I guess not supporting 10.1 was a deliberate decision then and
that's OK with me.
On 10.2.8, 10.3.9, and 10.4.0, the bugs exist as described in my
original post and are fixed by my patch. That is, there is no difference
between pre/post 10.3 and the patched version works correctly in all
combinations of GL/non-GL and windowed/fullscreen.
I therefore recommend the patch for inclusion.
-Christian
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 28 Nov 2005 13:58:26 +0000 |
parents | b8d311d90021 |
children | fd068ab116ee |
line wrap: on
line source
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2004 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Sam Lantinga slouken@libsdl.org */ #ifdef SAVE_RCSID static char rcsid = "@(#) $Id$"; #endif #include <stdlib.h> #include "SDL_error.h" #include "SDL_thread.h" #include "SDL_timer.h" #ifdef linux /* Look to see if glibc is available, and if so, what version */ #include <features.h> #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) #warning Working around a bug in glibc 2.0 pthreads #undef SDL_USE_PTHREADS /* The bug is actually a problem where threads are suspended, but don't wake up when the thread manager sends them a signal. This is a problem with thread creation too, but it happens less often. :-/ We avoid this by using System V IPC for semaphores. */ #endif /* glibc 2.0 */ #endif /* linux */ #ifdef SDL_USE_PTHREADS #ifdef SDL_NO_PTHREAD_SEMAPHORES #include "generic/SDL_syssem.c" #else #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* For getpid() */ #include <pthread.h> #include <semaphore.h> /* Wrapper around POSIX 1003.1b semaphores */ #ifdef MACOSX #define USE_NAMED_SEMAPHORES /* Broken sem_getvalue() in MacOS X Public Beta */ #define BROKEN_SEMGETVALUE #endif /* MACOSX */ struct SDL_semaphore { sem_t *sem; #ifndef USE_NAMED_SEMAPHORES sem_t sem_data; #endif #ifdef BROKEN_SEMGETVALUE /* This is a little hack for MacOS X - It's not thread-safe, but it's better than nothing */ int sem_value; #endif }; /* Create a semaphore, initialized with value */ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) { SDL_sem *sem = (SDL_sem *) malloc(sizeof(SDL_sem)); if ( sem ) { #ifdef USE_NAMED_SEMAPHORES static int semnum = 0; char name[32]; sprintf(name, "/SDL_sem-%d-%4.4d", getpid(), semnum++); sem->sem = sem_open(name, O_CREAT, 0600, initial_value); if ( sem->sem == (sem_t *)SEM_FAILED ) { SDL_SetError("sem_open(%s) failed", name); free(sem); sem = NULL; } else { sem_unlink(name); } #else if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) { SDL_SetError("sem_init() failed"); free(sem); sem = NULL; } else { sem->sem = &sem->sem_data; } #endif /* USE_NAMED_SEMAPHORES */ #ifdef BROKEN_SEMGETVALUE if ( sem ) { sem->sem_value = initial_value; } #endif /* BROKEN_SEMGETVALUE */ } else { SDL_OutOfMemory(); } return sem; } void SDL_DestroySemaphore(SDL_sem *sem) { if ( sem ) { #ifdef USE_NAMED_SEMAPHORES sem_close(sem->sem); #else sem_destroy(sem->sem); #endif free(sem); } } int SDL_SemTryWait(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } retval = SDL_MUTEX_TIMEDOUT; if ( sem_trywait(sem->sem) == 0 ) { #ifdef BROKEN_SEMGETVALUE --sem->sem_value; #endif retval = 0; } return retval; } int SDL_SemWait(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } #ifdef BROKEN_SEMGETVALUE --sem->sem_value; #endif retval = sem_wait(sem->sem); if ( retval < 0 ) { SDL_SetError("sem_wait() failed"); } return retval; } int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } /* Try the easy cases first */ if ( timeout == 0 ) { return SDL_SemTryWait(sem); } if ( timeout == SDL_MUTEX_MAXWAIT ) { return SDL_SemWait(sem); } /* Ack! We have to busy wait... */ timeout += SDL_GetTicks(); do { retval = SDL_SemTryWait(sem); if ( retval == 0 ) { break; } SDL_Delay(1); } while ( SDL_GetTicks() < timeout ); return retval; } Uint32 SDL_SemValue(SDL_sem *sem) { int ret = 0; if ( sem ) { #ifdef BROKEN_SEMGETVALUE ret = sem->sem_value; #else sem_getvalue(sem->sem, &ret); #endif if ( ret < 0 ) { ret = 0; } } return (Uint32)ret; } int SDL_SemPost(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } #ifdef BROKEN_SEMGETVALUE ++sem->sem_value; #endif retval = sem_post(sem->sem); if ( retval < 0 ) { SDL_SetError("sem_post() failed"); } return retval; } #endif /* NO_PTHREAD_SEMAPHORES */ #else /* System V IPC implementation */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <errno.h> #include "SDL_error.h" #include "SDL_thread.h" struct SDL_semaphore { int id; }; /* Not defined by many operating systems, use configure to detect */ #if !defined(HAVE_SEMUN) union semun { int val; struct semid_ds *buf; ushort *array; }; #endif static struct sembuf op_trywait[2] = { { 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */ }; static struct sembuf op_wait[2] = { { 0, -1, SEM_UNDO } /* Decrement semaphore */ }; static struct sembuf op_post[1] = { { 0, 1, (IPC_NOWAIT|SEM_UNDO) } /* Increment semaphore */ }; /* Create a blockable semaphore */ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) { extern int _creating_thread_lock; /* SDL_threads.c */ SDL_sem *sem; union semun init; key_t key; sem = (SDL_sem *)malloc(sizeof(*sem)); if ( sem == NULL ) { SDL_OutOfMemory(); return(NULL); } /* This flag is true if we are creating the thread manager sem, which is never freed. This allows us to reuse the same sem. */ if ( _creating_thread_lock ) { key = 'S'+'D'+'L'; } else { key = IPC_PRIVATE; } /* Keep trying to create sem while we don't own the requested key */ do { if ( key != IPC_PRIVATE ) { ++key; } sem->id = semget(key, 1, (0600|IPC_CREAT)); } while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES)); /* Report the error if we eventually failed */ if ( sem->id < 0 ) { SDL_SetError("Couldn't create semaphore"); free(sem); return(NULL); } init.val = initial_value; /* Initialize semaphore */ semctl(sem->id, 0, SETVAL, init); return(sem); } void SDL_DestroySemaphore(SDL_sem *sem) { if ( sem ) { #ifdef _SGI_SOURCE semctl(sem->id, 0, IPC_RMID); #else union semun dummy; dummy.val = 0; semctl(sem->id, 0, IPC_RMID, dummy); #endif free(sem); } } int SDL_SemTryWait(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } retval = 0; tryagain: if ( semop(sem->id, op_trywait, 1) < 0 ) { if ( errno == EINTR ) { goto tryagain; } retval = SDL_MUTEX_TIMEDOUT; } return retval; } int SDL_SemWait(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } retval = 0; tryagain: if ( semop(sem->id, op_wait, 1) < 0 ) { if ( errno == EINTR ) { goto tryagain; } SDL_SetError("Semaphore operation error"); retval = -1; } return retval; } int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } /* Try the easy cases first */ if ( timeout == 0 ) { return SDL_SemTryWait(sem); } if ( timeout == SDL_MUTEX_MAXWAIT ) { return SDL_SemWait(sem); } /* Ack! We have to busy wait... */ timeout += SDL_GetTicks(); do { retval = SDL_SemTryWait(sem); if ( retval == 0 ) { break; } SDL_Delay(1); } while ( SDL_GetTicks() < timeout ); return retval; } Uint32 SDL_SemValue(SDL_sem *sem) { int semval; Uint32 value; value = 0; if ( sem ) { tryagain: #ifdef _SGI_SOURCE semval = semctl(sem->id, 0, GETVAL); #else { union semun arg; arg.val = 0; semval = semctl(sem->id, 0, GETVAL, arg); } #endif if ( semval < 0 ) { if ( errno == EINTR ) { goto tryagain; } } else { value = (Uint32)semval; } } return value; } int SDL_SemPost(SDL_sem *sem) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } retval = 0; tryagain: if ( semop(sem->id, op_post, 1) < 0 ) { if ( errno == EINTR ) { goto tryagain; } SDL_SetError("Semaphore operation error"); retval = -1; } return retval; } #endif /* SDL_USE_PTHREADS */