summaryrefslogtreecommitdiff
path: root/libwinnie/src/fbdev/event.cc
blob: 18e363cab7d7924b0deea4529a396535864729ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
winnie - an experimental window system

Copyright (C) 2013 Eleni Maria Stea

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Author: Eleni Maria Stea <elene.mst@gmail.com>
*/

#ifdef WINNIE_FBDEV
#include <list>
#include <stdio.h>

#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>

#include "event.h"
#include "wm.h"
#include "keyboard.h"
#include "mouse.h"

struct TimerEvent {
	long time;
	Window *win;
	TimerMode mode;
	long interval;

	bool operator <(const TimerEvent &rhs) const { return time < rhs.time; }
};

static std::list<TimerEvent> timers;

void process_events()
{
	int keyb_fd = get_keyboard_fd();
	int mouse_fd = get_mouse_fd();

	for(;;) {
		wm->process_windows();

		fd_set read_set;

		FD_ZERO(&read_set);
		FD_SET(keyb_fd, &read_set);
		FD_SET(mouse_fd, &read_set);

		int maxfd = keyb_fd > mouse_fd ? keyb_fd : mouse_fd;

		struct timeval *timeout = 0;
		struct timeval tv;
		if(!timers.empty()) {
			long now = winnie_get_time();
			long next_timer = timers.front().time - now;

			tv.tv_sec = next_timer / 1000;
			tv.tv_usec = next_timer % 1000;
			timeout = &tv;
		}

		int numfd;
		while((numfd = select(maxfd + 1, &read_set, 0, 0, timeout)) == -1) {
			if(errno != EINTR) {
				break;
			}
		}

		if(numfd > 0) {
			if(FD_ISSET(keyb_fd, &read_set)) {
				process_keyboard_event();
			}
			if(FD_ISSET(mouse_fd, &read_set)) {
				process_mouse_event();
			}
		}

		bool added_timer = false;
		long now = winnie_get_time();
		while(!timers.empty() && now >= timers.front().time) {
			TimerEvent ev = timers.front();
			timers.pop_front();

			if(ev.mode == TIMER_REPEAT) {
				ev.time = now + ev.interval;
				timers.push_back(ev);
				added_timer = true;
			}

			Window *win = ev.win;
			TimerFuncType func = win->get_timer_callback();
			if(func) {
				func(win);
			} else {
				fprintf(stderr, "timer gone off but window has no timer callback\n");
			}
		}

		if(added_timer) {
			timers.sort();
		}
	}
}

void set_window_timer(Window *win, unsigned int msec, TimerMode mode)
{
	if(!win) {
		fprintf(stderr, "set_window_timer null window\n");
		return;
	}
	if(!win->get_timer_callback()) {
		fprintf(stderr, "trying to start a timer without having a timer callback\n");
		return;
	}

	TimerEvent ev;
	ev.interval = (long)msec;
	ev.time = winnie_get_time() + ev.interval;
	ev.win = win;
	ev.mode = mode;
	timers.push_back(ev);
	timers.sort();
}

#endif // WINNIE_FBDEV