includes: AROS_UFIxx -> AROS_INTxx change
[AROS.git] / arch / all-hosted / hidd / sdl / event.c
blobec68322743555dc18fa21e7094b28e20e2cf69c2
1 /*
2 * sdl.hidd - SDL graphics/sound/keyboard for AROS hosted
3 * Copyright (c) 2007 Robert Norris. All rights reserved.
4 * Copyright (c) 2010 The AROS Development Team. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the same terms as AROS itself.
8 */
10 #include <aros/symbolsets.h>
11 #include <aros/libcall.h>
13 #include <exec/types.h>
14 #include <exec/tasks.h>
15 #include <exec/lists.h>
16 #include <hardware/intbits.h>
17 #include <proto/exec.h>
18 #include <proto/graphics.h>
20 #include "sdl_intern.h"
22 #define DEBUG 0
23 #include <aros/debug.h>
25 #define MAX_EVENTS (64)
27 AROS_INTH1(tick_handler, struct Task *,task)
29 AROS_INTFUNC_INIT
31 Signal(task, SIGBREAKF_CTRL_D);
33 return FALSE;
35 AROS_INTFUNC_EXIT
39 VOID sdl_event_task(struct Task *creator, ULONG sync, LIBBASETYPEPTR LIBBASE) {
40 struct Interrupt tick_int;
41 SDL_Event e[MAX_EVENTS];
42 int nevents, i;
44 D(bug("[sdl] event loop task starting up\n"));
46 tick_int.is_Code = (VOID_FUNC)tick_handler;
47 tick_int.is_Data = FindTask(NULL);
48 tick_int.is_Node.ln_Name = "SDL event tick";
49 tick_int.is_Node.ln_Pri = 0;
50 tick_int.is_Node.ln_Type = NT_INTERRUPT;
51 AddIntServer(INTB_VERTB, &tick_int);
53 D(bug("[sdl] event loop task running, signalling creator\n"));
55 Signal(creator, sync);
57 D(bug("[sdl] entering loop\n"));
59 while (1) {
60 Uint8 active = 1;
62 Wait(SIGBREAKF_CTRL_D);
64 SV(SDL_PumpEvents);
65 if ((nevents = S(SDL_PeepEvents, e, MAX_EVENTS, SDL_GETEVENT, SDL_MOUSEEVENTMASK|SDL_KEYEVENTMASK|SDL_ACTIVEEVENTMASK)) > 0) {
66 D(bug("[sdl] %d events pending\n", nevents));
68 for (i = 0; i < nevents; i++) {
69 switch (e[i].type) {
70 case SDL_ACTIVEEVENT:
71 if (e[i].active.state & SDL_APPINPUTFOCUS) {
72 active = e[i].active.gain;
73 D(bug("[sdl] Window active: %u\n", active));
74 if (active && LIBBASE->cb)
75 LIBBASE->cb(LIBBASE->cbdata, NULL);
77 break;
78 case SDL_MOUSEMOTION:
79 case SDL_MOUSEBUTTONDOWN:
80 case SDL_MOUSEBUTTONUP:
81 /* We report mouse events only if our window is active.
82 Some OSes (MS Windows) otherwise report mouse movements
83 for inactive windows too, this can confuse Intuition */
84 if (active) {
85 D(bug("[sdl] got mouse event, sending to mouse hidd\n"));
87 if (LIBBASE->mousehidd)
88 Hidd_SDLMouse_HandleEvent(LIBBASE->mousehidd, &e[i]);
90 break;
92 case SDL_KEYUP:
93 case SDL_KEYDOWN:
94 D(bug("[sdl] got keyboard event, sending to keyboard hidd\n"));
96 if (LIBBASE->kbdhidd)
97 Hidd_SDLMouse_HandleEvent(LIBBASE->kbdhidd, &e[i]);
99 break;
106 int sdl_event_init(LIBBASETYPEPTR LIBBASE) {
107 struct Task *task;
108 APTR stack;
109 ULONG sync;
111 D(bug("[sdl] creating event loop task\n"));
113 if ((task = AllocMem(sizeof(struct Task), MEMF_PUBLIC | MEMF_CLEAR)) == NULL) {
114 D(bug("[sdl] couldn't allocate task memory\n"));
115 return FALSE;
118 if ((stack = AllocMem(AROS_STACKSIZE, MEMF_PUBLIC)) == NULL) {
119 D(bug("[sdl] couldn't allocate task stack memory\n"));
120 FreeMem(task, sizeof(struct Task));
121 return FALSE;
124 task->tc_Node.ln_Type = NT_TASK;
125 task->tc_Node.ln_Name = "sdl.hidd event task";
126 task->tc_Node.ln_Pri = 50;
128 NEWLIST(&task->tc_MemEntry);
130 task->tc_SPLower = stack;
131 task->tc_SPUpper = (UBYTE *) stack + AROS_STACKSIZE;
133 #if AROS_STACK_GROWS_DOWNWARDS
134 task->tc_SPReg = (UBYTE *) task->tc_SPUpper - SP_OFFSET;
135 #else
136 task->tc_SPReg = (UBYTE *) task->tc_SPLower + SP_OFFSET;
137 #endif
139 sync = SIGF_BLIT;
140 SetSignal(0, sync);
142 if (NewAddTask(task, sdl_event_task, NULL, TAGLIST(TASKTAG_ARG1, (IPTR)FindTask(NULL),
143 TASKTAG_ARG2, (IPTR)sync,
144 TASKTAG_ARG3, (IPTR)LIBBASE)) == NULL) {
145 D(bug("[sdl] new task creation failed\n"));
146 FreeMem(stack, AROS_STACKSIZE);
147 FreeMem(task, sizeof(struct Task));
148 return FALSE;
151 D(bug("[sdl] task created, waiting for it to start up\n"));
153 Wait(sync);
155 D(bug("[sdl] event loop task up and running\n"));
157 LIBBASE->eventtask = task;
159 return TRUE;
162 void sdl_event_expunge(LIBBASETYPEPTR LIBBASE)
164 RemTask(LIBBASE->eventtask);
165 LIBBASE->eventtask = NULL;