2 * FreeRDP: A Remote Desktop Protocol Client
3 * X11 Server Event Handling
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
26 #include <freerdp/utils/memory.h>
30 int xf_is_event_set(xfEventQueue* event_queue)
37 FD_SET(event_queue->pipe_fd[0], &rfds);
38 memset(&time, 0, sizeof(time));
39 num_set = select(event_queue->pipe_fd[0] + 1, &rfds, 0, 0, &time);
41 return (num_set == 1);
44 void xf_signal_event(xfEventQueue* event_queue)
48 length = write(event_queue->pipe_fd[1], "sig", 4);
51 printf("xf_signal_event: error\n");
54 void xf_set_event(xfEventQueue* event_queue)
58 length = write(event_queue->pipe_fd[1], "sig", 4);
61 printf("xf_set_event: error\n");
64 void xf_clear_event(xfEventQueue* event_queue)
68 while (xf_is_event_set(event_queue))
70 length = read(event_queue->pipe_fd[0], &length, 4);
73 printf("xf_clear_event: error\n");
77 void xf_event_push(xfEventQueue* event_queue, xfEvent* event)
79 pthread_mutex_lock(&(event_queue->mutex));
81 if (event_queue->count >= event_queue->size)
83 event_queue->size *= 2;
84 event_queue->events = (xfEvent**) xrealloc((void*) event_queue->events, sizeof(xfEvent*) * event_queue->size);
87 event_queue->events[(event_queue->count)++] = event;
89 xf_set_event(event_queue);
91 pthread_mutex_unlock(&(event_queue->mutex));
94 xfEvent* xf_event_peek(xfEventQueue* event_queue)
98 pthread_mutex_lock(&(event_queue->mutex));
100 if (event_queue->count < 1)
103 event = event_queue->events[0];
105 pthread_mutex_unlock(&(event_queue->mutex));
110 xfEvent* xf_event_pop(xfEventQueue* event_queue)
114 pthread_mutex_lock(&(event_queue->mutex));
116 if (event_queue->count < 1)
119 event = event_queue->events[0];
120 (event_queue->count)--;
122 memmove(&event_queue->events[0], &event_queue->events[1], event_queue->count * sizeof(void*));
124 pthread_mutex_unlock(&(event_queue->mutex));
129 xfEventRegion* xf_event_region_new(int x, int y, int width, int height)
131 xfEventRegion* event_region = xnew(xfEventRegion);
133 if (event_region != NULL)
137 event_region->width = width;
138 event_region->height = height;
144 void xf_event_region_free(xfEventRegion* event_region)
149 xfEvent* xf_event_new(int type)
151 xfEvent* event = xnew(xfEvent);
156 void xf_event_free(xfEvent* event)
161 xfEventQueue* xf_event_queue_new()
163 xfEventQueue* event_queue = xnew(xfEventQueue);
165 if (event_queue != NULL)
167 event_queue->pipe_fd[0] = -1;
168 event_queue->pipe_fd[1] = -1;
170 event_queue->size = 16;
171 event_queue->count = 0;
172 event_queue->events = (xfEvent**) xzalloc(sizeof(xfEvent*) * event_queue->size);
174 if (pipe(event_queue->pipe_fd) < 0)
175 printf("xf_event_queue_new: pipe failed\n");
177 pthread_mutex_init(&(event_queue->mutex), NULL);
183 void xf_event_queue_free(xfEventQueue* event_queue)
185 if (event_queue->pipe_fd[0] != -1)
187 close(event_queue->pipe_fd[0]);
188 event_queue->pipe_fd[0] = -1;
191 if (event_queue->pipe_fd[1] != -1)
193 close(event_queue->pipe_fd[1]);
194 event_queue->pipe_fd[1] = -1;
197 pthread_mutex_destroy(&(event_queue->mutex));