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