[4] | 1 | /***************************************************************************************** |
---|
| 2 | * VNCViewer for SAGE |
---|
| 3 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 4 | * University of Illinois at Chicago |
---|
| 5 | * |
---|
| 6 | * All rights reserved. |
---|
| 7 | * |
---|
| 8 | * Redistribution and use in source and binary forms, with or without |
---|
| 9 | * modification, are permitted provided that the following conditions are met: |
---|
| 10 | * |
---|
| 11 | * * Redistributions of source code must retain the above copyright |
---|
| 12 | * notice, this list of conditions and the following disclaimer. |
---|
| 13 | * * Redistributions in binary form must reproduce the above |
---|
| 14 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 15 | * in the documentation and/or other materials provided with the distribution. |
---|
| 16 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 17 | * the names of its contributors may be used to endorse or promote |
---|
| 18 | * products derived from this software without specific prior written permission. |
---|
| 19 | * |
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 31 | * |
---|
| 32 | * Direct questions, comments etc about VNCViewer for SAGE to www.evl.uic.edu/cavern/forum |
---|
| 33 | *****************************************************************************************/ |
---|
| 34 | |
---|
| 35 | #include "sgVNCViewer.h" |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | static int ok = 0; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | void aLog(const char* format,...) |
---|
| 42 | { |
---|
| 43 | va_list vl; |
---|
| 44 | char line[2048]; |
---|
| 45 | |
---|
| 46 | va_start(vl,format); |
---|
| 47 | vsprintf(line,format,vl); |
---|
| 48 | va_end(vl); |
---|
| 49 | |
---|
| 50 | fprintf(stderr, "%s", line); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void aError(const char* format,...) |
---|
| 54 | { |
---|
| 55 | va_list vl; |
---|
| 56 | char line[2048]; |
---|
| 57 | |
---|
| 58 | va_start(vl,format); |
---|
| 59 | vsprintf(line,format,vl); |
---|
| 60 | va_end(vl); |
---|
| 61 | |
---|
| 62 | fprintf(stderr,"ERROR: %s\n",line); |
---|
| 63 | |
---|
| 64 | #if defined(WIN32) |
---|
| 65 | // Open a dialog |
---|
| 66 | MessageBox(0, line, "Log Message", MB_OK); |
---|
| 67 | #endif |
---|
| 68 | // Quit |
---|
| 69 | exit(-1); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | static int |
---|
| 74 | NextPower(int val) |
---|
| 75 | { |
---|
| 76 | int res; |
---|
| 77 | float tmp; |
---|
| 78 | |
---|
| 79 | res = 0; |
---|
| 80 | tmp = (float) val; |
---|
| 81 | while (tmp > 1.0) |
---|
| 82 | { |
---|
| 83 | tmp /= 2.0; |
---|
| 84 | res ++; |
---|
| 85 | } |
---|
| 86 | res = 1 << res; |
---|
| 87 | return res; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | sgVNCViewer::sgVNCViewer(char *host, int display, int _ox, int _oy, int _sw, int _sh, char *passwd) |
---|
| 92 | { |
---|
| 93 | char dest[256]; |
---|
| 94 | VNC = new VNCViewer; |
---|
| 95 | |
---|
| 96 | #if defined(WIN32) |
---|
| 97 | InitWinsock(); |
---|
| 98 | #endif |
---|
| 99 | |
---|
| 100 | hostname = strdup( host ); |
---|
| 101 | password = strdup( passwd ); |
---|
| 102 | displaynumber = display; |
---|
| 103 | sprintf(dest, "%s:%d", hostname, displaynumber); |
---|
| 104 | |
---|
| 105 | int argc = 2; |
---|
| 106 | char *argv[2]; |
---|
| 107 | argv[0] = strdup ( "viewer" ); // program name |
---|
| 108 | argv[1] = strdup ( dest ); // display name |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | // Connection to the server |
---|
| 112 | VNC->GetArgsAndResources(argc, argv); |
---|
| 113 | |
---|
| 114 | if (!VNC->ConnectToRFBServer()) |
---|
| 115 | aError("ConnectToRFBServer\n"); |
---|
| 116 | |
---|
| 117 | if (!VNC->InitialiseRFBConnection(password)) |
---|
| 118 | aError("InitialiseRFBConnection\n"); |
---|
| 119 | |
---|
| 120 | VNC->SetVisualAndCmap(); |
---|
| 121 | VNC->SetFormatAndEncodings(); |
---|
| 122 | aLog("Framebuffer width %d , height %d\n", |
---|
| 123 | VNC->si.framebufferWidth, VNC->si.framebufferHeight); |
---|
| 124 | |
---|
| 125 | // Building the visualization |
---|
| 126 | ox = _ox; |
---|
| 127 | oy = _oy; |
---|
| 128 | sw = _sw; |
---|
| 129 | sh = _sh; |
---|
| 130 | |
---|
| 131 | rectData = 0; |
---|
| 132 | screen_back = (uchar* )malloc(sw * sh * 4); |
---|
| 133 | memset(screen_back, 0, sw * sh * 4); |
---|
| 134 | screen_front = (uchar* )malloc(sw * sh * 4); |
---|
| 135 | memset(screen_front, 0, sw * sh * 4); |
---|
| 136 | |
---|
| 137 | // First update from VNC server |
---|
| 138 | VNC->SendFramebufferUpdateRequest(ox, oy, sw, sh, False); |
---|
| 139 | |
---|
| 140 | rectData = 0; |
---|
| 141 | |
---|
| 142 | focus = 0; // Tracker inside, i.e. the mouse |
---|
| 143 | bstate = 0; // button state |
---|
| 144 | |
---|
| 145 | // No update yet |
---|
| 146 | ok = 0; |
---|
| 147 | |
---|
| 148 | Step(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | sgVNCViewer::~sgVNCViewer() |
---|
| 152 | { |
---|
| 153 | delete VNC; |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | void sgVNCViewer::SendKey(unsigned long key, int down) |
---|
| 160 | { |
---|
| 161 | //aLog("sending key %d (%d)\n", key, down); |
---|
| 162 | |
---|
| 163 | VNC->SendKeyEvent(key, down); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void sgVNCViewer::SendMouse(int x, int y, int button) |
---|
| 167 | { |
---|
| 168 | int mask; |
---|
| 169 | |
---|
| 170 | switch (button) |
---|
| 171 | { |
---|
| 172 | case MK_LEFT: |
---|
| 173 | mask = 1; |
---|
| 174 | break; |
---|
| 175 | case MK_RIGHT: |
---|
| 176 | mask = 2; |
---|
| 177 | break; |
---|
| 178 | case MK_MIDDLE: |
---|
| 179 | mask = 4; |
---|
| 180 | break; |
---|
| 181 | default: |
---|
| 182 | mask = 0; |
---|
| 183 | break; |
---|
| 184 | } |
---|
| 185 | //aLog("sending mouse %d\n", mask); |
---|
| 186 | |
---|
| 187 | VNC->SendPointerEvent(x, y, mask); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | uchar* |
---|
| 192 | sgVNCViewer::Data() |
---|
| 193 | { |
---|
| 194 | return screen_front; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | bool |
---|
| 198 | sgVNCViewer::Step() |
---|
| 199 | { |
---|
| 200 | if (VNC->HandleRFBServerMessage(this)) |
---|
| 201 | { |
---|
| 202 | // Copy into main memory |
---|
| 203 | memcpy(screen_front, screen_back, sw*sh*4); |
---|
| 204 | |
---|
| 205 | rectData = 1; |
---|
| 206 | VNC->SendFramebufferUpdateRequest(ox, oy, sw, sh, True); |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | return false; |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void |
---|
| 214 | sgVNCViewer::CopyDataToScreen(char *buf, int x, int y, int width, int height) |
---|
| 215 | { |
---|
| 216 | x -= ox; |
---|
| 217 | y -= oy; |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | #if defined(WIN32) |
---|
| 221 | if (VNC->rawDelay != 0) Sleep(VNC->rawDelay); |
---|
| 222 | #else |
---|
| 223 | //if (VNC->rawDelay != 0) usleep(VNC->rawDelay*1000); |
---|
| 224 | #endif |
---|
| 225 | |
---|
| 226 | if (!VNC->useBGR233) |
---|
| 227 | { |
---|
| 228 | int h; |
---|
| 229 | uchar *src, *scr; |
---|
| 230 | scr = &screen_back[y*sw*4+x*4]; |
---|
| 231 | src = (uchar*) buf; |
---|
| 232 | for (h = 0; h < height; h++) |
---|
| 233 | { |
---|
| 234 | memcpy(scr, src, width*4); |
---|
| 235 | src += width*4; |
---|
| 236 | scr += sw*4; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | void |
---|
| 242 | sgVNCViewer::FillToScreen(CARD32 pix, int x, int y, int width, int height) |
---|
| 243 | { |
---|
| 244 | x -= ox; |
---|
| 245 | y -= oy; |
---|
| 246 | |
---|
| 247 | #if defined(WIN32) |
---|
| 248 | if (VNC->rawDelay != 0) Sleep(VNC->rawDelay); |
---|
| 249 | #else |
---|
| 250 | //if (VNC->rawDelay != 0) usleep(VNC->rawDelay*1000); |
---|
| 251 | #endif |
---|
| 252 | if (!VNC->useBGR233) |
---|
| 253 | { |
---|
| 254 | int h,i; |
---|
| 255 | uchar *src, *scr; |
---|
| 256 | |
---|
| 257 | scr = &screen_back[y*sw*4+x*4]; |
---|
| 258 | src = (uchar*) pix; |
---|
| 259 | for (h = 0; h < height; h++) |
---|
| 260 | { |
---|
| 261 | //memcpy(scr, src, width*4); |
---|
| 262 | for (i = 0; i<width;i++) |
---|
| 263 | memcpy(&scr[i*4], &pix, 4);; |
---|
| 264 | |
---|
| 265 | src += width*4; |
---|
| 266 | scr += sw*4; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | void |
---|
| 272 | sgVNCViewer::RectDataToScreen(int x, int y, int w, int h, int orix, int oriy) |
---|
| 273 | { |
---|
| 274 | |
---|
| 275 | x -= ox; |
---|
| 276 | y -= oy; |
---|
| 277 | orix -= ox; |
---|
| 278 | oriy -= oy; |
---|
| 279 | if (rectData) |
---|
| 280 | { |
---|
| 281 | // Prepare double buffer |
---|
| 282 | memcpy(screen_front, screen_back, sw*sh*4); |
---|
| 283 | rectData = 0; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | uchar *scr = &screen_back[y*sw*4+x*4]; |
---|
| 287 | uchar *src = &screen_front[oriy*sw*4+orix*4]; |
---|
| 288 | for (int _h = 0; _h < h; _h++) |
---|
| 289 | { |
---|
| 290 | memcpy(scr, src, w*4); |
---|
| 291 | src += sw*4; |
---|
| 292 | scr += sw*4; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|