FreeRDP
Loading...
Searching...
No Matches
mf_event.c
1
20#include <freerdp/config.h>
21
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "mf_event.h"
29
30#include <freerdp/log.h>
31#define TAG SERVER_TAG("mac")
32
33static int mf_is_event_set(mfEventQueue* event_queue)
34{
35 fd_set rfds;
36 int num_set;
37 struct timeval time = { 0 };
38
39 FD_ZERO(&rfds);
40 FD_SET(event_queue->pipe_fd[0], &rfds);
41 num_set = select(event_queue->pipe_fd[0] + 1, &rfds, 0, 0, &time);
42
43 return (num_set == 1);
44}
45
46static void mf_signal_event(mfEventQueue* event_queue)
47{
48 int length;
49
50 length = write(event_queue->pipe_fd[1], "sig", 4);
51
52 if (length != 4)
53 WLog_ERR(TAG, "mf_signal_event: error");
54}
55
56static void mf_set_event(mfEventQueue* event_queue)
57{
58 int length;
59
60 length = write(event_queue->pipe_fd[1], "sig", 4);
61
62 if (length != 4)
63 WLog_ERR(TAG, "mf_set_event: error");
64}
65
66static void mf_clear_events(mfEventQueue* event_queue)
67{
68 int length;
69
70 while (mf_is_event_set(event_queue))
71 {
72 length = read(event_queue->pipe_fd[0], &length, 4);
73
74 if (length != 4)
75 WLog_ERR(TAG, "mf_clear_event: error");
76 }
77}
78
79static void mf_clear_event(mfEventQueue* event_queue)
80{
81 int length;
82
83 length = read(event_queue->pipe_fd[0], &length, 4);
84
85 if (length != 4)
86 WLog_ERR(TAG, "mf_clear_event: error");
87}
88
89void mf_event_push(mfEventQueue* event_queue, mfEvent* event)
90{
91 pthread_mutex_lock(&(event_queue->mutex));
92
93 if (event_queue->count >= event_queue->size)
94 {
95 event_queue->size *= 2;
96 event_queue->events =
97 (mfEvent**)realloc((void*)event_queue->events, sizeof(mfEvent*) * event_queue->size);
98 }
99
100 event_queue->events[(event_queue->count)++] = event;
101
102 pthread_mutex_unlock(&(event_queue->mutex));
103
104 mf_set_event(event_queue);
105}
106
107mfEvent* mf_event_peek(mfEventQueue* event_queue)
108{
109 mfEvent* event;
110
111 pthread_mutex_lock(&(event_queue->mutex));
112
113 if (event_queue->count < 1)
114 event = NULL;
115 else
116 event = event_queue->events[0];
117
118 pthread_mutex_unlock(&(event_queue->mutex));
119
120 return event;
121}
122
123mfEvent* mf_event_pop(mfEventQueue* event_queue)
124{
125 mfEvent* event;
126
127 pthread_mutex_lock(&(event_queue->mutex));
128
129 if (event_queue->count < 1)
130 return NULL;
131
132 /* remove event signal */
133 mf_clear_event(event_queue);
134
135 event = event_queue->events[0];
136 (event_queue->count)--;
137
138 memmove(&event_queue->events[0], &event_queue->events[1], event_queue->count * sizeof(void*));
139
140 pthread_mutex_unlock(&(event_queue->mutex));
141
142 return event;
143}
144
145mfEventRegion* mf_event_region_new(int x, int y, int width, int height)
146{
147 mfEventRegion* event_region = malloc(sizeof(mfEventRegion));
148
149 if (event_region != NULL)
150 {
151 event_region->x = x;
152 event_region->y = y;
153 event_region->width = width;
154 event_region->height = height;
155 }
156
157 return event_region;
158}
159
160void mf_event_region_free(mfEventRegion* event_region)
161{
162 free(event_region);
163}
164
165mfEvent* mf_event_new(int type)
166{
167 mfEvent* event = malloc(sizeof(mfEvent));
168 if (!event)
169 return NULL;
170 event->type = type;
171 return event;
172}
173
174void mf_event_free(mfEvent* event)
175{
176 free(event);
177}
178
179mfEventQueue* mf_event_queue_new()
180{
181 mfEventQueue* event_queue = malloc(sizeof(mfEventQueue));
182
183 if (event_queue != NULL)
184 {
185 event_queue->pipe_fd[0] = -1;
186 event_queue->pipe_fd[1] = -1;
187
188 event_queue->size = 16;
189 event_queue->count = 0;
190 event_queue->events = (mfEvent**)malloc(sizeof(mfEvent*) * event_queue->size);
191
192 if (pipe(event_queue->pipe_fd) < 0)
193 {
194 free(event_queue);
195 return NULL;
196 }
197
198 pthread_mutex_init(&(event_queue->mutex), NULL);
199 }
200
201 return event_queue;
202}
203
204void mf_event_queue_free(mfEventQueue* event_queue)
205{
206 if (event_queue->pipe_fd[0] != -1)
207 {
208 close(event_queue->pipe_fd[0]);
209 event_queue->pipe_fd[0] = -1;
210 }
211
212 if (event_queue->pipe_fd[1] != -1)
213 {
214 close(event_queue->pipe_fd[1]);
215 event_queue->pipe_fd[1] = -1;
216 }
217
218 pthread_mutex_destroy(&(event_queue->mutex));
219}