steed/src/steed/input.c

171 lines
3.8 KiB
C
Raw Normal View History

#include "externals.h"
#include "internals.h"
2009-08-26 08:25:53 +03:00
2010-09-13 12:27:39 +03:00
#include <SDL.h>
2009-09-10 17:23:31 +03:00
static int m_focus = 0;
2010-03-12 16:49:30 +02:00
static int m_minimized = 0;
int minimized(void)
{
2010-03-12 20:06:15 +02:00
if (nopause_sw)
return 0;
2010-03-12 16:49:30 +02:00
return m_minimized;
}
2009-09-10 17:23:31 +03:00
2009-09-10 13:54:32 +03:00
int mouse_focus(void)
{
2009-09-10 17:23:31 +03:00
return m_focus;
2009-09-10 13:54:32 +03:00
}
2010-06-07 16:58:16 +03:00
int mouse_cursor(int on)
{
if (on)
SDL_ShowCursor(SDL_ENABLE);
else
2010-06-07 21:59:31 +03:00
SDL_ShowCursor(SDL_DISABLE);
2010-06-07 16:58:16 +03:00
return 0;
}
2009-08-26 08:25:53 +03:00
void push_user_event(void (*p) (void*), void *data)
{
SDL_Event event;
SDL_UserEvent uevent;
uevent.type = SDL_USEREVENT;
uevent.code = 0;
event.type = SDL_USEREVENT;
uevent.data1 = p;
uevent.data2 = data;
event.user = uevent;
SDL_PushEvent(&event);
}
int input_init(void)
{
SDL_EnableKeyRepeat(500, 30);
2009-09-10 17:23:31 +03:00
m_focus = !!(SDL_GetAppState() & SDL_APPMOUSEFOCUS);
2009-08-26 08:25:53 +03:00
return 0;
}
void input_clear(void)
{
SDL_Event event;
while (SDL_PollEvent(&event));
return;
}
2009-02-21 12:52:44 +02:00
int input(struct inp_event *inp, int wait)
{
int rc;
SDL_Event event;
2009-08-26 08:25:53 +03:00
SDL_Event peek;
memset(&event, 0, sizeof(event));
memset(&peek, 0, sizeof(peek));
if (wait) {
2009-02-21 12:52:44 +02:00
rc = SDL_WaitEvent(&event);
2009-08-26 08:25:53 +03:00
} else
2009-02-21 12:52:44 +02:00
rc = SDL_PollEvent(&event);
if (!rc)
return 0;
2011-02-19 15:23:09 +02:00
inp->sym[0] = 0;
2009-02-21 12:52:44 +02:00
inp->type = 0;
2009-08-26 08:25:53 +03:00
inp->count = 1;
2009-02-21 12:52:44 +02:00
switch(event.type){
2009-09-10 17:23:31 +03:00
case SDL_ACTIVEEVENT:
2010-03-12 16:49:30 +02:00
if (event.active.state & SDL_APPACTIVE) {
m_minimized = !event.active.gain;
2010-03-12 20:06:15 +02:00
snd_pause(!nopause_sw && m_minimized);
2010-03-12 16:49:30 +02:00
}
if (event.active.state & (SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS)) {
2010-01-15 20:41:59 +02:00
if (event.active.gain) {
2009-09-10 17:23:31 +03:00
m_focus = 1;
2010-01-15 20:41:59 +02:00
if (opt_fs)
2010-06-07 16:58:16 +03:00
mouse_cursor(0);
2010-01-15 20:41:59 +02:00
} else if (event.active.state & SDL_APPMOUSEFOCUS) {
2009-12-02 08:09:43 +02:00
m_focus = 0;
2010-01-15 20:41:59 +02:00
if (opt_fs)
2010-06-07 16:58:16 +03:00
mouse_cursor(1); /* is it hack?*/
2010-01-15 20:41:59 +02:00
}
2009-09-10 17:23:31 +03:00
}
2011-02-19 15:23:09 +02:00
#if SDL_VERSION_ATLEAST(1,3,0)
if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_ACTIVEEVENT, SDL_ACTIVEEVENT) > 0)
#else
2010-06-04 12:56:33 +03:00
if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_EVENTMASK(SDL_ACTIVEEVENT)) > 0)
2011-02-19 15:23:09 +02:00
#endif
2010-03-17 09:48:13 +02:00
return AGAIN; /* to avoid flickering */
2010-03-02 16:49:35 +02:00
return 0;
2009-11-29 18:00:22 +02:00
case SDL_USEREVENT: {
void (*p) (void*) = event.user.data1;
p(event.user.data2);
return AGAIN;
}
2009-02-21 12:52:44 +02:00
case SDL_QUIT:
2009-09-12 12:10:29 +03:00
game_running = 0;
2009-02-21 12:52:44 +02:00
return -1;
case SDL_KEYDOWN: //A key has been pressed
inp->type = KEY_DOWN;
inp->code = event.key.keysym.scancode;
2011-02-19 15:23:09 +02:00
strncpy(inp->sym, SDL_GetKeyName(event.key.keysym.sym), sizeof(inp->sym));
inp->sym[sizeof(inp->sym) - 1] = 0;
tolow(inp->sym);
2009-02-21 12:52:44 +02:00
break;
case SDL_KEYUP:
inp->type = KEY_UP;
inp->code = event.key.keysym.scancode;
2011-02-19 15:23:09 +02:00
strncpy(inp->sym, SDL_GetKeyName(event.key.keysym.sym), sizeof(inp->sym));
inp->sym[sizeof(inp->sym) - 1] = 0;
tolow(inp->sym);
2009-02-21 12:52:44 +02:00
break;
case SDL_MOUSEMOTION:
m_focus = 1; /* ahhh */
2009-02-21 12:52:44 +02:00
inp->type = MOUSE_MOTION;
inp->x = event.button.x;
inp->y = event.button.y;
2011-02-19 15:23:09 +02:00
#if SDL_VERSION_ATLEAST(1,3,0)
while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
#else
2009-08-26 08:25:53 +03:00
while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)) > 0) {
2011-02-19 15:23:09 +02:00
#endif
2009-08-26 08:25:53 +03:00
inp->x = peek.button.x;
inp->y = peek.button.y;
}
2009-02-21 12:52:44 +02:00
break;
case SDL_MOUSEBUTTONUP:
inp->type = MOUSE_UP;
inp->x = event.button.x;
inp->y = event.button.y;
2009-11-03 18:45:46 +02:00
inp->code = event.button.button;
2009-02-21 12:52:44 +02:00
if (event.button.button == 4)
inp->type = 0;
else if (event.button.button == 5)
2009-11-03 18:45:46 +02:00
inp->type = 0;
2009-02-21 12:52:44 +02:00
break;
case SDL_MOUSEBUTTONDOWN:
2010-06-04 12:56:33 +03:00
m_focus = 1; /* ahhh */
2009-02-21 12:52:44 +02:00
inp->type = MOUSE_DOWN;
inp->x = event.button.x;
inp->y = event.button.y;
2009-11-03 18:45:46 +02:00
inp->code = event.button.button;
2009-02-21 12:52:44 +02:00
if (event.button.button == 4)
inp->type = MOUSE_WHEEL_UP;
else if (event.button.button == 5)
inp->type = MOUSE_WHEEL_DOWN;
2011-02-19 15:23:09 +02:00
#if SDL_VERSION_ATLEAST(1,3,0)
while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN) > 0) {
#else
2009-08-26 08:25:53 +03:00
while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEBUTTONDOWN)) > 0) {
2011-02-19 15:23:09 +02:00
#endif
2009-08-26 08:25:53 +03:00
if (!((event.button.button == 4 &&
inp->type == MOUSE_WHEEL_UP) ||
(event.button.button == 5 &&
inp->type == MOUSE_WHEEL_DOWN)))
break;
inp->count ++;
}
2009-02-21 12:52:44 +02:00
break;
}
return 1;
}