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.
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"
23 #include <aros/debug.h>
25 #define MAX_EVENTS (64)
27 AROS_INTH1(tick_handler
, struct Task
*,task
)
31 Signal(task
, SIGBREAKF_CTRL_D
);
39 VOID
sdl_event_task(struct Task
*creator
, ULONG sync
, LIBBASETYPEPTR LIBBASE
) {
40 struct Interrupt tick_int
;
41 SDL_Event e
[MAX_EVENTS
];
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"));
62 Wait(SIGBREAKF_CTRL_D
);
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
++) {
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
);
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 */
85 D(bug("[sdl] got mouse event, sending to mouse hidd\n"));
87 if (LIBBASE
->mousehidd
)
88 Hidd_SDLMouse_HandleEvent(LIBBASE
->mousehidd
, &e
[i
]);
94 D(bug("[sdl] got keyboard event, sending to keyboard hidd\n"));
97 Hidd_SDLMouse_HandleEvent(LIBBASE
->kbdhidd
, &e
[i
]);
106 int sdl_event_init(LIBBASETYPEPTR LIBBASE
) {
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"));
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
));
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
;
136 task
->tc_SPReg
= (UBYTE
*) task
->tc_SPLower
+ SP_OFFSET
;
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
));
151 D(bug("[sdl] task created, waiting for it to start up\n"));
155 D(bug("[sdl] event loop task up and running\n"));
157 LIBBASE
->eventtask
= task
;
162 void sdl_event_expunge(LIBBASETYPEPTR LIBBASE
)
164 RemTask(LIBBASE
->eventtask
);
165 LIBBASE
->eventtask
= NULL
;