Mercurial > sdl-ios-xcode
view src/thread/linux/SDL_syssem.c @ 563:04dcaf3da918
Massive Quartz input enhancements from Darrell Walisser. His email:
Enclosed is a patch that addresses the following:
--Various minor cleanups.
Removed dead/obsolete code, made some style cleanups
--Mouse Events
Now keep track of what button(s) were pressed so we know when to send
the mouse up event. This fixes the case where the mouse is dragged
outside of the game window and released (in which case we want to send
the mouse up event even though the mouse is outside the game window).
--Input Grabbing
Here is my take on the grabbing situation, which is the basis for the
new implementation.
There are 3 grab states, ungrabbed (UG), visible (VG), and invisible
(IG). Both VG and IG keep the mouse constrained to the window and
produce relative motion events. In VG the cursor is visible (duh), in
IG it is not. In VG, absolute motion events also work.
There are 6 actions that can affect grabbing:
1. Set Fullscreen/Window (F/W). In fullscreen, a visible grab should do
nothing. However, a fullscreen visible grab can be treated just like a
windowed visible grab, which is what I have done to help simplify
things.
2. Cursor hide/show (H/S). If the cursor is hidden when grabbing, the
grab is an invisible grab. If the cursor is visible, the grab should
just constrain the mouse to the window.
3. Input grab/ungrab(G/U). If grabbed, the cursor should be confined to
the window as should the keyboard input. On Mac OS X, the keyboard
input is implicitly grabbed by confining the cursor, except for
command-tab which can switch away from the application. Should the
window come to the foreground if the application is deactivated and
grab input is called? This isn't necessary in this implementation
because the grab state will be asserted upon activation.
Using my notation, these are all the cases that need to be handled
(state + action = new state).
UG+U = UG
UG+G = VG or IG, if cursor is visible or not
UG+H = UG
UG+S = UG
VG+U = UG
VG+G = VG
VG+H = IG
VG+S = VG
IG+U = UG
IG+G = IG
IG+H = IG
IG+S = VG
The cases that result in the same state can be ignored in the code,
which cuts it down to just 5 cases.
Another issue is what happens when the app loses/gains input focus from
deactivate/activate or iconify/deiconify. I think that if input focus
is ever lost (outside of SDL's control), the grab state should be
suspended and the cursor should become visible and active again. When
regained, the cursor should reappear in its original location and/or
grab state. This way, when reactivating the cursor is still in the same
position as before so apps shouldn't get confused when the next motion
event comes in. This is what I've done in this patch.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 27 Dec 2002 20:52:41 +0000 |
parents | f6ffac90895c |
children | b8d311d90021 |
line wrap: on
line source
/* SDL - Simple DirectMedia Layer Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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 */