wip prep commit in lieu of gfx subsystem update changes.
[AROS.git] / arch / all-hosted / hidd / sdl / event.c
blobc71f4c3817b6390fcee1aeb94db4a134095dc154
1 /*
2 * sdl.hidd - SDL graphics/sound/keyboard for AROS hosted
3 * Copyright (c) 2007 Robert Norris. All rights reserved.
4 * Copyright (c) 2010-2017 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 #ifdef __THROW
21 #undef __THROW
22 #endif
23 #ifdef __CONCAT
24 #undef __CONCAT
25 #endif
27 #include "sdl_intern.h"
29 #define DEBUG 0
30 #include <aros/debug.h>
32 #define MAX_EVENTS (64)
34 AROS_INTH1(tick_handler, struct Task *,task)
36 AROS_INTFUNC_INIT
38 Signal(task, SIGBREAKF_CTRL_D);
40 return FALSE;
42 AROS_INTFUNC_EXIT
46 VOID sdl_event_task(struct Task *creator, ULONG sync, LIBBASETYPEPTR LIBBASE) {
47 struct Interrupt tick_int;
48 SDL_Event e[MAX_EVENTS];
49 int nevents, i;
51 D(bug("[sdl] event loop task starting up\n"));
53 tick_int.is_Code = (VOID_FUNC)tick_handler;
54 tick_int.is_Data = FindTask(NULL);
55 tick_int.is_Node.ln_Name = "SDL event tick";
56 tick_int.is_Node.ln_Pri = 0;
57 tick_int.is_Node.ln_Type = NT_INTERRUPT;
58 AddIntServer(INTB_VERTB, &tick_int);
60 D(bug("[sdl] event loop task running, signalling creator\n"));
62 Signal(creator, sync);
64 D(bug("[sdl] entering loop\n"));
66 while (1) {
67 Uint8 active = 1;
69 Wait(SIGBREAKF_CTRL_D);
71 SV(SDL_PumpEvents);
72 if ((nevents = S(SDL_PeepEvents, e, MAX_EVENTS, SDL_GETEVENT, SDL_MOUSEEVENTMASK|SDL_KEYEVENTMASK|SDL_ACTIVEEVENTMASK)) > 0) {
73 D(bug("[sdl] %d events pending\n", nevents));
75 for (i = 0; i < nevents; i++) {
76 switch (e[i].type) {
77 case SDL_ACTIVEEVENT:
78 if (e[i].active.state & SDL_APPINPUTFOCUS) {
79 active = e[i].active.gain;
80 D(bug("[sdl] Window active: %u\n", active));
81 if (active && LIBBASE->cb)
82 LIBBASE->cb(LIBBASE->cbdata, NULL);
84 break;
85 case SDL_MOUSEMOTION:
86 case SDL_MOUSEBUTTONDOWN:
87 case SDL_MOUSEBUTTONUP:
88 /* We report mouse events only if our window is active.
89 Some OSes (MS Windows) otherwise report mouse movements
90 for inactive windows too, this can confuse Intuition */
91 if (active) {
92 D(bug("[sdl] got mouse event, sending to mouse hidd\n"));
94 if (LIBBASE->mousehidd)
95 Hidd_Mouse_SDL_HandleEvent(LIBBASE->mousehidd, &e[i]);
97 break;
99 case SDL_KEYUP:
100 case SDL_KEYDOWN:
101 D(bug("[sdl] got keyboard event, sending to keyboard hidd\n"));
103 if (LIBBASE->kbdhidd)
104 Hidd_Kbd_SDL_HandleEvent(LIBBASE->kbdhidd, &e[i]);
106 break;
113 int sdl_event_init(LIBBASETYPEPTR LIBBASE) {
114 struct Task *task;
115 ULONG sync;
117 D(bug("[sdl] creating event loop task\n"));
119 sync = SIGF_BLIT;
120 SetSignal(0, sync);
122 if ((task = NewCreateTask(TASKTAG_PC, sdl_event_task,
123 TASKTAG_STACKSIZE, AROS_STACKSIZE,
124 TASKTAG_NAME, "sdl.hidd event task",
125 TASKTAG_PRI, 50,
126 TASKTAG_ARG1, (IPTR)FindTask(NULL),
127 TASKTAG_ARG2, (IPTR)sync,
128 TASKTAG_ARG3, (IPTR)LIBBASE,
129 TAG_DONE)) == NULL)
131 D(bug("[sdl] new task creation failed\n"));
132 return FALSE;
135 D(bug("[sdl] task created, waiting for it to start up\n"));
137 Wait(sync);
139 D(bug("[sdl] event loop task up and running\n"));
141 LIBBASE->eventtask = task;
143 return TRUE;
146 void sdl_event_expunge(LIBBASETYPEPTR LIBBASE)
148 RemTask(LIBBASE->eventtask);
149 LIBBASE->eventtask = NULL;