2012-11-14 04:49:34 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* OS X Server Event Handling
|
|
|
|
*
|
|
|
|
* Copyright 2012 Corey Clayton <can.of.tuna@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-11-14 04:49:34 +04:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mf_event.h"
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
#define TAG SERVER_TAG("mac")
|
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
int mf_is_event_set(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
int num_set;
|
2022-10-13 17:25:36 +03:00
|
|
|
struct timeval time = { 0 };
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(event_queue->pipe_fd[0], &rfds);
|
|
|
|
num_set = select(event_queue->pipe_fd[0] + 1, &rfds, 0, 0, &time);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
return (num_set == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_signal_event(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
int length;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
length = write(event_queue->pipe_fd[1], "sig", 4);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (length != 4)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "mf_signal_event: error");
|
2012-11-14 04:49:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void mf_set_event(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
int length;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
length = write(event_queue->pipe_fd[1], "sig", 4);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (length != 4)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "mf_set_event: error");
|
2012-11-14 04:49:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void mf_clear_events(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
int length;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
while (mf_is_event_set(event_queue))
|
|
|
|
{
|
|
|
|
length = read(event_queue->pipe_fd[0], &length, 4);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (length != 4)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "mf_clear_event: error");
|
2012-11-14 04:49:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_clear_event(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
int length;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
length = read(event_queue->pipe_fd[0], &length, 4);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (length != 4)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "mf_clear_event: error");
|
2012-11-14 04:49:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void mf_event_push(mfEventQueue* event_queue, mfEvent* event)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_queue->count >= event_queue->size)
|
|
|
|
{
|
|
|
|
event_queue->size *= 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
event_queue->events =
|
|
|
|
(mfEvent**)realloc((void*)event_queue->events, sizeof(mfEvent*) * event_queue->size);
|
2012-11-14 04:49:34 +04:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
event_queue->events[(event_queue->count)++] = event;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_unlock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
mf_set_event(event_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
mfEvent* mf_event_peek(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
mfEvent* event;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_lock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_queue->count < 1)
|
|
|
|
event = NULL;
|
|
|
|
else
|
|
|
|
event = event_queue->events[0];
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_unlock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfEvent* mf_event_pop(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
mfEvent* event;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_lock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_queue->count < 1)
|
|
|
|
return NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
/* remove event signal */
|
|
|
|
mf_clear_event(event_queue);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
event = event_queue->events[0];
|
|
|
|
(event_queue->count)--;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
memmove(&event_queue->events[0], &event_queue->events[1], event_queue->count * sizeof(void*));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_unlock(&(event_queue->mutex));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfEventRegion* mf_event_region_new(int x, int y, int width, int height)
|
|
|
|
{
|
2013-01-23 21:54:09 +04:00
|
|
|
mfEventRegion* event_region = malloc(sizeof(mfEventRegion));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_region != NULL)
|
|
|
|
{
|
|
|
|
event_region->x = x;
|
|
|
|
event_region->y = y;
|
|
|
|
event_region->width = width;
|
|
|
|
event_region->height = height;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
return event_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_event_region_free(mfEventRegion* event_region)
|
|
|
|
{
|
|
|
|
free(event_region);
|
|
|
|
}
|
|
|
|
|
|
|
|
mfEvent* mf_event_new(int type)
|
|
|
|
{
|
2013-01-23 21:54:09 +04:00
|
|
|
mfEvent* event = malloc(sizeof(mfEvent));
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!event)
|
|
|
|
return NULL;
|
2012-11-14 04:49:34 +04:00
|
|
|
event->type = type;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_event_free(mfEvent* event)
|
|
|
|
{
|
|
|
|
free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mfEventQueue* mf_event_queue_new()
|
|
|
|
{
|
2013-01-23 21:54:09 +04:00
|
|
|
mfEventQueue* event_queue = malloc(sizeof(mfEventQueue));
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_queue != NULL)
|
|
|
|
{
|
|
|
|
event_queue->pipe_fd[0] = -1;
|
|
|
|
event_queue->pipe_fd[1] = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
event_queue->size = 16;
|
|
|
|
event_queue->count = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
event_queue->events = (mfEvent**)malloc(sizeof(mfEvent*) * event_queue->size);
|
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (pipe(event_queue->pipe_fd) < 0)
|
2017-01-25 15:09:25 +03:00
|
|
|
{
|
|
|
|
free(event_queue);
|
2014-09-13 20:33:33 +04:00
|
|
|
return NULL;
|
2017-01-25 15:09:25 +03:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_init(&(event_queue->mutex), NULL);
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
return event_queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_event_queue_free(mfEventQueue* event_queue)
|
|
|
|
{
|
|
|
|
if (event_queue->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(event_queue->pipe_fd[0]);
|
|
|
|
event_queue->pipe_fd[0] = -1;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
if (event_queue->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(event_queue->pipe_fd[1]);
|
|
|
|
event_queue->pipe_fd[1] = -1;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2012-11-14 04:49:34 +04:00
|
|
|
pthread_mutex_destroy(&(event_queue->mutex));
|
2013-03-29 02:06:34 +04:00
|
|
|
}
|