2 * FreeRDP: A Remote Desktop Protocol Implementation
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 <sys/select.h>
27 #include <sys/signal.h>
29 #include <freerdp/utils/sleep.h>
31 #include "xf_encode.h"
33 XImage* xf_snapshot(xfPeerContext* xfp, int x, int y, int width, int height)
36 xfInfo* xfi = xfp->info;
40 pthread_mutex_lock(&(xfp->mutex));
42 XCopyArea(xfi->display, xfi->root_window, xfi->fb_pixmap,
43 xfi->xdamage_gc, x, y, width, height, x, y);
45 XSync(xfi->display, False);
47 image = xfi->fb_image;
49 pthread_mutex_unlock(&(xfp->mutex));
53 pthread_mutex_lock(&(xfp->mutex));
55 image = XGetImage(xfi->display, xfi->root_window,
56 x, y, width, height, AllPlanes, ZPixmap);
58 pthread_mutex_unlock(&(xfp->mutex));
64 void xf_xdamage_subtract_region(xfPeerContext* xfp, int x, int y, int width, int height)
67 xfInfo* xfi = xfp->info;
72 region.height = height;
75 pthread_mutex_lock(&(xfp->mutex));
76 XFixesSetRegion(xfi->display, xfi->xdamage_region, ®ion, 1);
77 XDamageSubtract(xfi->display, xfi->xdamage, xfi->xdamage_region, None);
78 pthread_mutex_unlock(&(xfp->mutex));
82 void* xf_frame_rate_thread(void* param)
90 client = (freerdp_peer*) param;
91 xfp = (xfPeerContext*) client->context;
94 wait_interval = 1000000 / xfp->fps;
98 // check if we should terminate
101 event = xf_event_new(XF_EVENT_TYPE_FRAME_TICK);
102 xf_event_push(xfp->event_queue, (xfEvent*) event);
103 freerdp_usleep(wait_interval);
107 void* xf_monitor_updates(void* param)
116 freerdp_peer* client;
117 UINT32 wait_interval;
118 struct timeval timeout;
119 int x, y, width, height;
120 XDamageNotifyEvent* notify;
121 xfEventRegion* event_region;
123 client = (freerdp_peer*) param;
124 xfp = (xfPeerContext*) client->context;
128 wait_interval = (1000000 / 2500);
129 memset(&timeout, 0, sizeof(struct timeval));
131 pthread_create(&(xfp->frame_rate_thread), 0, xf_frame_rate_thread, (void*) client);
135 // check if we should terminate
136 pthread_testcancel();
139 FD_SET(fds, &rfds_set);
142 timeout.tv_usec = wait_interval;
143 select_status = select(fds + 1, &rfds_set, NULL, NULL, &timeout);
145 if (select_status == -1)
147 printf("select failed\n");
149 else if (select_status == 0)
151 //printf("select timeout\n");
154 pthread_mutex_lock(&(xfp->mutex));
155 pending_events = XPending(xfi->display);
156 pthread_mutex_unlock(&(xfp->mutex));
158 if (pending_events > 0)
160 pthread_mutex_lock(&(xfp->mutex));
161 memset(&xevent, 0, sizeof(xevent));
162 XNextEvent(xfi->display, &xevent);
163 pthread_mutex_unlock(&(xfp->mutex));
165 if (xevent.type == xfi->xdamage_notify_event)
167 notify = (XDamageNotifyEvent*) &xevent;
171 width = notify->area.width;
172 height = notify->area.height;
174 xf_xdamage_subtract_region(xfp, x, y, width, height);
176 event_region = xf_event_region_new(x, y, width, height);
177 xf_event_push(xfp->event_queue, (xfEvent*) event_region);