2 * FreeRDP: A Remote Desktop Protocol Client
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.
21 #include <freerdp/utils/sleep.h>
23 #include "xf_encode.h"
25 XImage* xf_snapshot(xfPeerContext* xfp, int x, int y, int width, int height)
28 xfInfo* xfi = xfp->info;
32 pthread_mutex_lock(&(xfp->mutex));
34 XCopyArea(xfi->display, xfi->root_window, xfi->fb_pixmap,
35 xfi->xdamage_gc, x, y, width, height, x, y);
37 XSync(xfi->display, False);
39 image = xfi->fb_image;
41 pthread_mutex_unlock(&(xfp->mutex));
45 pthread_mutex_lock(&(xfp->mutex));
47 image = XGetImage(xfi->display, xfi->root_window,
48 x, y, width, height, AllPlanes, ZPixmap);
50 pthread_mutex_unlock(&(xfp->mutex));
56 void xf_xdamage_subtract_region(xfPeerContext* xfp, int x, int y, int width, int height)
59 xfInfo* xfi = xfp->info;
64 region.height = height;
67 pthread_mutex_lock(&(xfp->mutex));
68 XFixesSetRegion(xfi->display, xfi->xdamage_region, ®ion, 1);
69 XDamageSubtract(xfi->display, xfi->xdamage, xfi->xdamage_region, None);
70 pthread_mutex_unlock(&(xfp->mutex));
74 void* xf_frame_rate_thread(void* param)
82 client = (freerdp_peer*) param;
83 xfp = (xfPeerContext*) client->context;
86 wait_interval = 1000000 / xfp->fps;
90 event = xf_event_new(XF_EVENT_TYPE_FRAME_TICK);
91 xf_event_push(xfp->event_queue, (xfEvent*) event);
92 freerdp_usleep(wait_interval);
96 void* xf_monitor_updates(void* param)
105 freerdp_peer* client;
106 uint32 wait_interval;
107 struct timeval timeout;
108 int x, y, width, height;
109 XDamageNotifyEvent* notify;
110 xfEventRegion* event_region;
112 client = (freerdp_peer*) param;
113 xfp = (xfPeerContext*) client->context;
117 wait_interval = (1000000 / 2500);
118 memset(&timeout, 0, sizeof(struct timeval));
120 pthread_create(&(xfp->frame_rate_thread), 0, xf_frame_rate_thread, (void*) client);
122 pthread_detach(pthread_self());
127 FD_SET(fds, &rfds_set);
130 timeout.tv_usec = wait_interval;
131 select_status = select(fds + 1, &rfds_set, NULL, NULL, &timeout);
133 if (select_status == -1)
135 printf("select failed\n");
137 else if (select_status == 0)
139 //printf("select timeout\n");
142 pthread_mutex_lock(&(xfp->mutex));
143 pending_events = XPending(xfi->display);
144 pthread_mutex_unlock(&(xfp->mutex));
146 if (pending_events > 0)
148 pthread_mutex_lock(&(xfp->mutex));
149 memset(&xevent, 0, sizeof(xevent));
150 XNextEvent(xfi->display, &xevent);
151 pthread_mutex_unlock(&(xfp->mutex));
153 if (xevent.type == xfi->xdamage_notify_event)
155 notify = (XDamageNotifyEvent*) &xevent;
159 width = notify->area.width;
160 height = notify->area.height;
162 xf_xdamage_subtract_region(xfp, x, y, width, height);
164 event_region = xf_event_region_new(x, y, width, height);
165 xf_event_push(xfp->event_queue, (xfEvent*) event_region);