Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / surface.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Surface Commands
4  *
5  * Copyright 2011 Vic Lee
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #include <freerdp/utils/pcap.h>
21
22 #include "surface.h"
23
24 static int update_recv_surfcmd_surface_bits(rdpUpdate* update, STREAM* s)
25 {
26         int pos;
27         SURFACE_BITS_COMMAND* cmd = &update->surface_bits_command;
28
29         stream_read_uint16(s, cmd->destLeft);
30         stream_read_uint16(s, cmd->destTop);
31         stream_read_uint16(s, cmd->destRight);
32         stream_read_uint16(s, cmd->destBottom);
33         stream_read_uint8(s, cmd->bpp);
34         stream_seek(s, 2); /* reserved1, reserved2 */
35         stream_read_uint8(s, cmd->codecID);
36         stream_read_uint16(s, cmd->width);
37         stream_read_uint16(s, cmd->height);
38         stream_read_uint32(s, cmd->bitmapDataLength);
39         pos = stream_get_pos(s) + cmd->bitmapDataLength;
40         cmd->bitmapData = stream_get_tail(s);
41
42         IFCALL(update->SurfaceBits, update->context, cmd);
43
44         stream_set_pos(s, pos);
45
46         return 20 + cmd->bitmapDataLength;
47 }
48
49 static int update_recv_surfcmd_frame_marker(rdpUpdate* update, STREAM* s)
50 {
51         SURFACE_FRAME_MARKER* marker = &update->surface_frame_marker;
52
53         stream_read_uint16(s, marker->frameAction);
54         stream_read_uint32(s, marker->frameId);
55
56         IFCALL(update->SurfaceFrameMarker, update->context, marker);
57
58         return 6;
59 }
60
61 boolean update_recv_surfcmds(rdpUpdate* update, uint32 size, STREAM* s)
62 {
63         uint8* mark;
64         uint16 cmdType;
65         uint32 cmdLength;
66
67         while (size > 2)
68         {
69                 stream_get_mark(s, mark);
70
71                 stream_read_uint16(s, cmdType);
72                 size -= 2;
73
74                 switch (cmdType)
75                 {
76                         case CMDTYPE_SET_SURFACE_BITS:
77                         case CMDTYPE_STREAM_SURFACE_BITS:
78                                 cmdLength = update_recv_surfcmd_surface_bits(update, s);
79                                 break;
80
81                         case CMDTYPE_FRAME_MARKER:
82                                 cmdLength = update_recv_surfcmd_frame_marker(update, s);
83                                 break;
84
85                         default:
86                                 DEBUG_WARN("unknown cmdType 0x%X", cmdType);
87                                 return false;
88                 }
89
90                 size -= cmdLength;
91
92                 if (update->dump_rfx)
93                 {
94                         pcap_add_record(update->pcap_rfx, mark, cmdLength + 2);
95                         pcap_flush(update->pcap_rfx);
96                 }
97         }
98         return true;
99 }
100
101 void update_write_surfcmd_surface_bits_header(STREAM* s, SURFACE_BITS_COMMAND* cmd)
102 {
103         stream_check_size(s, SURFCMD_SURFACE_BITS_HEADER_LENGTH);
104
105         stream_write_uint16(s, CMDTYPE_STREAM_SURFACE_BITS);
106
107         stream_write_uint16(s, cmd->destLeft);
108         stream_write_uint16(s, cmd->destTop);
109         stream_write_uint16(s, cmd->destRight);
110         stream_write_uint16(s, cmd->destBottom);
111         stream_write_uint8(s, cmd->bpp);
112         stream_write_uint16(s, 0); /* reserved1, reserved2 */
113         stream_write_uint8(s, cmd->codecID);
114         stream_write_uint16(s, cmd->width);
115         stream_write_uint16(s, cmd->height);
116         stream_write_uint32(s, cmd->bitmapDataLength);
117 }
118
119 void update_write_surfcmd_frame_marker(STREAM* s, uint16 frameAction, uint32 frameId)
120 {
121         stream_check_size(s, SURFCMD_FRAME_MARKER_LENGTH);
122
123         stream_write_uint16(s, CMDTYPE_FRAME_MARKER);
124
125         stream_write_uint16(s, frameAction);
126         stream_write_uint32(s, frameId);
127 }
128