Contiki 2.5
ctk-vncserver.c
Go to the documentation of this file.
1 /**
2  * \file
3  * The ctk-draw implementation for the CTK VNC server.
4  * \author Adam Dunkels <adam@dunkels.com>
5  *
6  */
7 
8 /**
9  * \defgroup CTKVNCServer The CTK VNC server
10  * @{
11  */
12 
13 /*
14  * Copyright (c) 2003, Adam Dunkels.
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  * 3. The name of the author may not be used to endorse or promote
27  * products derived from this software without specific prior
28  * written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
31  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
34  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
36  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * This file is part of the "ctk" console GUI toolkit for cc65
43  *
44  * $Id: ctk-vncserver.c,v 1.3 2010/10/19 18:29:04 adamdunkels Exp $
45  *
46  */
47 
48 #include <string.h> /* For strlen() */
49 
50 #include "ctk/ctk.h"
51 #include "ctk/ctk-draw.h"
52 
53 #include "contiki-net.h"
54 
55 #include "ctk/vnc-server.h"
56 #include "ctk/vnc-out.h"
57 
58 #include "ctk/ctk-vncfont.h"
59 #include "ctk/ctk-vncserver.h"
60 
61 
62 static unsigned char sizex, sizey;
63 
64 #define CH_ULCORNER 0x00
65 #define CH_TITLEBAR 0x01
66 #define CH_URCORNER 0x02
67 #define CH_WINDOWRBORDER 0x03
68 #define CH_LRCORNER 0x04
69 #define CH_WINDOWLOWERBORDER 0x05
70 #define CH_LLCORNER 0x06
71 #define CH_WINDOWLBORDER 0x07
72 
73 #define CH_DIALOG_ULCORNER 0x12
74 #define CH_DIALOGUPPERBORDER 0x09
75 #define CH_DIALOG_URCORNER 0x0a
76 #define CH_DIALOGRBORDER 0x0b
77 #define CH_DIALOG_LRCORNER 0x0c
78 #define CH_DIALOGLOWERBORDER 0x0d
79 #define CH_DIALOG_LLCORNER 0x0e
80 #define CH_DIALOGLBORDER 0x0f
81 
82 #define CH_BUTTONLEFT 0x10
83 #define CH_BUTTONRIGHT 0x11
84 
85 #define CH_SEPARATOR 0x13
86 
87 #include "lib/libconio.h"
88 
89 #define SCREENCOLOR 0
90 #define BORDERCOLOR 1
91 
92 #define WIDGETCOLOR 2
93 #define WIDGETCOLOR_FWIN 3
94 #define WIDGETCOLOR_FOCUS 4
95 #define WIDGETCOLOR_DIALOG 5
96 #define WIDGETCOLOR_HLINK 6
97 #define WIDGETCOLOR_HLINK_FOCUS 7
98 
99 #define WINDOWCOLOR 8
100 #define WINDOWCOLOR_FOCUS 9
101 
102 #define WINDOWBORDER 10
103 #define WINDOWBORDER_FOCUS 11
104 
105 #define DIALOGCOLOR 12
106 
107 #define OPENMENUCOLOR 13
108 
109 #define ACTIVEMENUITEMCOLOR 14
110 
111 #define MENUCOLOR 15
112 
113 
114 PROCESS(ctk_vncserver_process, "CTK VNC server");
115 
116 static struct vnc_server_state conns[CTK_VNCSERVER_CONF_NUMCONNS];
117 
118 #define PRINTF(x)
119 
120 #define revers(x)
121 
122 unsigned char ctk_draw_windowborder_height = 1;
123 unsigned char ctk_draw_windowborder_width = 1;
124 unsigned char ctk_draw_windowtitle_height = 1;
125 
126 
127 /*-----------------------------------------------------------------------------------*/
128 /**
129  * Add an update request from a client to the list of pending updates
130  * for the connection.
131  *
132  * This function is called from the vnc-out module.
133  *
134  * \param vs The VNC connection state.
135  * \param a The area that is requested to be updated.
136  */
137 /*-----------------------------------------------------------------------------------*/
138 void
139 vnc_server_update_add(struct vnc_server_state *vs,
140  struct vnc_server_update *a)
141 {
142  /* XXX: test both head and tail placement!*/
143  a->next = vs->updates_pending;
144  vs->updates_pending = a;
145 }
146 /*-----------------------------------------------------------------------------------*/
147 /**
148  * Allocate an update request from the VNC connection state.
149  *
150  * This function is called from the vnc-out module.
151  *
152  * \param vs The VNC connection state.
153  *
154  * \return Memory for an update structure, or NULL if no update could
155  * be allocated.
156  */
157 /*-----------------------------------------------------------------------------------*/
158 struct vnc_server_update *
159 vnc_server_update_alloc(struct vnc_server_state *vs)
160 {
161  struct vnc_server_update *a;
162 
163  a = vs->updates_free;
164  if(a == NULL) {
165  return NULL;
166  }
167  vs->updates_free = a->next;
168  a->next = NULL;
169  return a;
170 }
171 /*-----------------------------------------------------------------------------------*/
172 /**
173  * Deallocate an update request from the VNC connection state.
174  *
175  * This function is called from the vnc-out module.
176  *
177  * \param vs The VNC connection state.
178  *
179  * \param a The update structure to be deallocated.
180  */
181 /*-----------------------------------------------------------------------------------*/
182 void
183 vnc_server_update_free(struct vnc_server_state *vs,
184  struct vnc_server_update *a)
185 {
186  a->next = vs->updates_free;
187  vs->updates_free = a;
188 }
189 /*-----------------------------------------------------------------------------------*/
190 /**
191  * Dequeue the first update on the queue of updates.
192  *
193  * This function is called from the vnc-out module.
194  *
195  * \param vs The VNC connection state.
196  *
197  * \return The first update on the queue, or NULL if the queue is empty.
198  */
199 /*-----------------------------------------------------------------------------------*/
200 struct vnc_server_update *
201 vnc_server_update_dequeue(struct vnc_server_state *vs)
202 {
203  struct vnc_server_update *a;
204 
205  a = vs->updates_pending;
206  if(a == NULL) {
207  return a;
208  }
209  vs->updates_pending = a->next;
210  a->next = NULL;
211  return a;
212 }
213 /*-----------------------------------------------------------------------------------*/
214 /**
215  * Remove a specific update on the queue of updates.
216  *
217  * \param vs The VNC connection state.
218  * \param a The update to be removed.
219  */
220 /*-----------------------------------------------------------------------------------*/
221 void
222 vnc_server_update_remove(struct vnc_server_state *vs,
223  struct vnc_server_update *a)
224 {
225  struct vnc_server_update *b, *c;
226 
227  if(a == vs->updates_pending) {
228  vs->updates_pending = a->next;
229  } else {
230  b = vs->updates_pending;
231  for(c = vs->updates_pending; c != a; b = c, c = c->next);
232 
233  b->next = a->next;
234  }
235 }
236 /*-----------------------------------------------------------------------------------*/
237 /** \internal
238  * Flag an area to be updated for all open VNC server connections.
239  *
240  */
241 /*-----------------------------------------------------------------------------------*/
242 static void
243 update_area(u8_t x, u8_t y, u8_t w, u8_t h)
244 {
245  u8_t i;
246 
247  if(h == 0 || w == 0) {
248  return;
249  }
250 
251  /* Update for all active VNC connections. */
252  for(i = 0; i < CTK_VNCSERVER_CONF_NUMCONNS; ++i) {
253  if(conns[i].state != VNC_DEALLOCATED) {
254  vnc_out_update_area(&conns[i],
255  x, y, w, h);
256  }
257  }
258 
259 }
260 /*-----------------------------------------------------------------------------------*/
261 /** \internal
262  * Allocate a VNC server connection state from the array of available
263  * VNC connection states.
264  */
265 /*-----------------------------------------------------------------------------------*/
266 static struct vnc_server_state *
267 alloc_state(void)
268 {
269  u8_t i;
270  for(i = 0; i < CTK_VNCSERVER_CONF_NUMCONNS; ++i) {
271  if(conns[i].state == VNC_DEALLOCATED) {
272  return &conns[i];
273  }
274  }
275 
276  /* We are overloaded! XXX: we'll just kick all other connections! */
277  for(i = 0; i < CTK_VNCSERVER_CONF_NUMCONNS; ++i) {
278  conns[i].state = VNC_DEALLOCATED;
279  }
280 
281  return NULL;
282 }
283 /*-----------------------------------------------------------------------------------*/
284 /** \internal
285  * Deallocate a VNC connection state.
286  */
287 /*-----------------------------------------------------------------------------------*/
288 static void
289 dealloc_state(struct vnc_server_state *s)
290 {
291  s->state = VNC_DEALLOCATED;
292 }
293 /*-----------------------------------------------------------------------------------*/
294 static void
295 cputsn(char *str, unsigned char len)
296 {
297  char c;
298 
299  while(len > 0) {
300  --len;
301  c = *str;
302  if(c == 0) {
303  break;
304  }
305  cputc(c);
306  ++str;
307  }
308 }
309 /*-----------------------------------------------------------------------------------*/
310 /**
311  * Initialize the VNC ctk-draw module. Called by the CTK module.
312  *
313  */
314 /*-----------------------------------------------------------------------------------*/
315 void
317 {
318  bgcolor(SCREENCOLOR);
319  bordercolor(BORDERCOLOR);
320  screensize(&sizex, &sizey);
321  ctk_draw_clear(0, sizey);
322 }
323 /*-----------------------------------------------------------------------------------*/
324 static void
325 draw_widget(struct ctk_widget *w,
326  unsigned char x, unsigned char y,
327  unsigned char clipx,
328  unsigned char clipy,
329  unsigned char clipy1, unsigned char clipy2,
330  unsigned char focus)
331 {
332  unsigned char xpos, ypos, xscroll;
333  unsigned char i, j;
334  unsigned char iconnum;
335  char c, *text;
336  unsigned char len;
337 
338  /*
339  if(focus & CTK_FOCUS_WINDOW) {
340  textcolor(WIDGETCOLOR_FWIN);
341  if(focus & CTK_FOCUS_WIDGET) {
342  textcolor(WIDGETCOLOR_FOCUS);
343  }
344  } else if(focus & CTK_FOCUS_DIALOG) {
345  textcolor(WIDGETCOLOR_DIALOG);
346  if(focus & CTK_FOCUS_WIDGET) {
347  textcolor(WIDGETCOLOR_FOCUS);
348  }
349  } else {
350  textcolor(WIDGETCOLOR);
351  }
352 */
353  xpos = x + w->x;
354  ypos = y + w->y;
355 
356  switch(w->type) {
358  textcolor(VNC_OUT_SEPARATORCOLOR + focus);
359  if(ypos >= clipy1 && ypos < clipy2) {
360  /* chlinexy(xpos, ypos, w->w);*/
361  gotoxy(xpos, ypos);
362  for(i = 0; i < w->w; ++i) {
363  cputc(CH_SEPARATOR);
364  }
365  }
366  break;
367  case CTK_WIDGET_LABEL:
368  textcolor(VNC_OUT_LABELCOLOR + focus);
369  text = w->widget.label.text;
370  for(i = 0; i < w->h; ++i) {
371  if(ypos >= clipy1 && ypos < clipy2) {
372  gotoxy(xpos, ypos);
373  cputsn(text, w->w);
374  if(w->w - (wherex() - xpos) > 0) {
375  cclear(w->w - (wherex() - xpos));
376  }
377  }
378  ++ypos;
379  text += w->w;
380  }
381  break;
382  case CTK_WIDGET_BUTTON:
383  textcolor(VNC_OUT_BUTTONCOLOR + focus);
384  if(ypos >= clipy1 && ypos < clipy2) {
385  if(focus & CTK_FOCUS_WIDGET) {
386  revers(1);
387  } else {
388  revers(0);
389  }
390  cputcxy(xpos, ypos, CH_BUTTONLEFT);
391  cputsn(w->widget.button.text, w->w);
392  cputc(CH_BUTTONRIGHT);
393  revers(0);
394  }
395  break;
397  textcolor(VNC_OUT_HYPERLINKCOLOR + focus);
398  if(ypos >= clipy1 && ypos < clipy2) {
399  /* if(focus & CTK_FOCUS_WIDGET) {
400  textcolor(WIDGETCOLOR_HLINK_FOCUS);
401  revers(0);
402  } else {
403  textcolor(WIDGETCOLOR_HLINK);
404  revers(1);
405  }*/
406  gotoxy(xpos, ypos);
407  cputsn(w->widget.button.text, w->w);
408  revers(0);
409  }
410  break;
412  textcolor(VNC_OUT_TEXTENTRYCOLOR + focus);
413  text = w->widget.textentry.text;
414  xscroll = 0;
415  if(w->widget.textentry.xpos >= w->w - 1) {
416  xscroll = w->widget.textentry.xpos - w->w + 1;
417  }
418  for(j = 0; j < w->h; ++j) {
419  if(ypos >= clipy1 && ypos < clipy2) {
420  if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
421  w->widget.textentry.ypos == j) {
422  revers(0);
423  cputcxy(xpos, ypos, '>');
424  c = 1;
425  for(i = 0; i < w->w; ++i) {
426  if(c != 0) {
427  c = text[i + xscroll];
428  }
429  if(i == w->widget.textentry.xpos - xscroll) {
430  textcolor(VNC_OUT_TEXTENTRYCOLOR + (focus ^ 0x01));
431  revers(1);
432  } else {
433  revers(0);
434  }
435  if(c == 0) {
436  cputc(' ');
437  } else {
438  cputc(c);
439  }
440  revers(0);
441  textcolor(VNC_OUT_TEXTENTRYCOLOR + focus);
442  }
443  cputc('<');
444  } else {
445  if(focus & CTK_FOCUS_WIDGET && j == w->widget.textentry.ypos) {
446  revers(1);
447  } else {
448  revers(0);
449  }
450  cvlinexy(xpos, ypos, 1);
451  gotoxy(xpos + 1, ypos);
452  cputsn(text, w->w);
453  i = wherex();
454  if(i - xpos - 1 < w->w) {
455  cclear(w->w - (i - xpos) + 1);
456  }
457  cvline(1);
458  }
459  }
460  ++ypos;
461  text += w->widget.textentry.len + 1;
462  }
463  revers(0);
464  break;
465 #if CTK_CONF_ICONS
466  case CTK_WIDGET_ICON:
467  if(ypos >= clipy1 && ypos < clipy2) {
468  textcolor(VNC_OUT_ICONCOLOR + focus);
469  if(focus & 1) {
470  revers(1);
471  } else {
472  revers(0);
473  }
474 
475  x = xpos;
476  len = strlen(w->widget.icon.title);
477  if(x + len >= sizex) {
478  x = sizex - len;
479  }
480 
481  gotoxy(x, ypos + 3);
482  if(ypos >= clipy1 && ypos < clipy2) {
483  cputs(w->widget.icon.title);
484  }
485 
486 #if CTK_CONF_ICON_BITMAPS
487  if(w->widget.icon.bitmap != NULL) {
488  iconnum = vnc_out_add_icon((struct ctk_icon *)w);
489  textcolor(iconnum | (focus << 6));
490  gotoxy(xpos, ypos);
491  cputc(0x80);
492  cputc(0x81);
493  cputc(0x82);
494  cputc(0x83);
495  ++ypos;
496  gotoxy(xpos, ypos);
497  cputc(0x90);
498  cputc(0x91);
499  cputc(0x92);
500  cputc(0x93);
501  ++ypos;
502  gotoxy(xpos, ypos);
503  cputc(0xa0);
504  cputc(0xa1);
505  cputc(0xa2);
506  cputc(0xa3);
507  ++ypos;
508  textcolor(0);
509  /* for(i = 0; i < 3; ++i) {
510 
511  if(ypos >= clipy1 && ypos < clipy2) {
512  cputc(w->widget.icon.textmap[0 + 3 * i]);
513  cputc(w->widget.icon.textmap[1 + 3 * i]);
514  cputc(w->widget.icon.textmap[2 + 3 * i]);
515  }
516  ++ypos;
517  }*/
518  }
519 #endif /* CTK_CONF_ICON_BITMAPS */
520 
521  revers(0);
522  }
523  break;
524 #endif /* CTK_CONF_ICONS */
525 
526  default:
527  break;
528  }
529 }
530 /*-----------------------------------------------------------------------------------*/
531 /**
532  * Draw a widget on the VNC screen. Called by the CTK module.
533  *
534  * \param w The widget to be drawn.
535  * \param focus The focus of the widget.
536  * \param clipy1 The lower y coordinate bound.
537  * \param clipy2 The upper y coordinate bound.
538  */
539 /*-----------------------------------------------------------------------------------*/
540 void
542  unsigned char focus,
543  unsigned char clipy1,
544  unsigned char clipy2)
545 {
546  struct ctk_window *win = w->window;
547  struct ctk_icon *icon;
548  unsigned char posx, posy, x, len;
549 
550  posx = win->x + 1;
551  posy = win->y + 2;
552 
553  if(w == win->focused) {
554  focus |= CTK_FOCUS_WIDGET;
555  }
556 
557  draw_widget(w, posx, posy,
558  posx + win->w,
559  posy + win->h,
560  clipy1, clipy2,
561  focus);
562 
563  if(w->type != CTK_WIDGET_ICON) {
564  update_area(posx + w->x,
565  posy + w->y, w->w + 2, w->h);
566  } else {
567  icon = (struct ctk_icon *)w;
568 
569  len = strlen(icon->title);
570  x = posx + w->x;
571  if(x + len >= sizex) {
572  x = sizex - len;
573  }
574 
575  update_area(x, posy + w->y, len > 4? len: 4, w->h);
576  }
577 
578 #ifdef CTK_CONIO_CONF_UPDATE
579  CTK_CONIO_CONF_UPDATE();
580 #endif /* CTK_CONIO_CONF_UPDATE */
581 }
582 /*-----------------------------------------------------------------------------------*/
583 /**
584  * Clear a window on the VNC screen. Called by the CTK module.
585  *
586  * \param window The window to be cleared.
587  * \param focus The focus of the window.
588  * \param clipy1 The lower y coordinate bound.
589  * \param clipy2 The upper y coordinate bound.
590  */
591 /*-----------------------------------------------------------------------------------*/
592 void
594  unsigned char focus,
595  unsigned char clipy1,
596  unsigned char clipy2)
597 {
598  unsigned char i;
599  unsigned char h;
600  /*
601  if(focus & CTK_FOCUS_WINDOW){
602  textcolor(WINDOWCOLOR_FOCUS);
603  } else {
604  textcolor(WINDOWCOLOR);
605  }*/
606  textcolor(VNC_OUT_WINDOWCOLOR + focus);
607 
608  h = window->y + 2 + window->h;
609  /* Clear window contents. */
610  for(i = window->y + 2; i < h; ++i) {
611  if(i >= clipy1 && i < clipy2) {
612  cclearxy(window->x + 1, i, window->w);
613  }
614  }
615 
616  update_area(window->x + 1, window->y + 2, window->w, window->h);
617 }
618 /*-----------------------------------------------------------------------------------*/
619 static void
620 draw_window_contents(struct ctk_window *window, unsigned char focus,
621  unsigned char clipy1, unsigned char clipy2,
622  unsigned char x1, unsigned char x2,
623  unsigned char y1, unsigned char y2)
624 {
625  struct ctk_widget *w;
626  unsigned char wfocus;
627 
628  /* Draw inactive widgets. */
629  for(w = window->inactive; w != NULL; w = w->next) {
630  draw_widget(w, x1, y1, x2, y2,
631  clipy1, clipy2,
632  focus);
633  }
634 
635  /* Draw active widgets. */
636  for(w = window->active; w != NULL; w = w->next) {
637  wfocus = focus;
638  if(w == window->focused) {
639  wfocus |= CTK_FOCUS_WIDGET;
640  }
641 
642  draw_widget(w, x1, y1, x2, y2,
643  clipy1, clipy2,
644  wfocus);
645  }
646 
647 #ifdef CTK_CONIO_CONF_UPDATE
648  CTK_CONIO_CONF_UPDATE();
649 #endif /* CTK_CONIO_CONF_UPDATE */
650 
651 }
652 /*-----------------------------------------------------------------------------------*/
653 /**
654  * Draw a window on the VNC screen. Called by the CTK module.
655  *
656  * \param window The window to be drawn.
657  * \param focus The focus of the window.
658  * \param clipy1 The lower y coordinate bound.
659  * \param clipy2 The upper y coordinate bound.
660  * \param draw_borders The flag for border drawing.
661  */
662 /*-----------------------------------------------------------------------------------*/
663 void
664 ctk_draw_window(struct ctk_window *window, unsigned char focus,
665  unsigned char clipy1, unsigned char clipy2,
666  unsigned char draw_borders)
667 {
668  unsigned char x, y;
669  unsigned char h;
670  unsigned char x1, y1, x2, y2;
671  unsigned char i;
672 
673 
674  if(window->y + 1 >= clipy2) {
675  return;
676  }
677 
678  x = window->x;
679  y = window->y + 1;
680  x1 = x + 1;
681  y1 = y + 1;
682  x2 = x1 + window->w;
683  y2 = y1 + window->h;
684 
685  if(draw_borders) {
686 
687  /* Draw window frame. */
688  textcolor(VNC_OUT_WINDOWCOLOR + focus);
689  /* if(focus & CTK_FOCUS_WINDOW) {
690  textcolor(WINDOWCOLOR_FOCUS);
691  } else {
692  textcolor(WINDOWCOLOR);
693  }*/
694 
695  if(y >= clipy1) {
696  cputcxy(x, y, CH_ULCORNER);
697  for(i = wherex() + window->titlelen + CTK_CONF_WINDOWMOVE * 2; i < x2; ++i) {
698  cputcxy(i, y, CH_TITLEBAR);
699  }
700  cputcxy(x2, y, CH_URCORNER);
701  }
702 
703  h = window->h;
704 
705  if(clipy1 > y1) {
706  if(clipy1 - y1 < h) {
707  h = clipy1 - y1;
708  y1 = clipy1;
709  } else {
710  h = 0;
711  }
712  }
713 
714  if(clipy2 < y1 + h) {
715  if(y1 >= clipy2) {
716  h = 0;
717  } else {
718  h = clipy2 - y1;
719  }
720  }
721 
722  for(i = y1; i < y1 + h; ++i) {
723  cputcxy(x, i, CH_WINDOWLBORDER);
724  cputcxy(x2, i, CH_WINDOWRBORDER);
725  }
726 
727  /* cvlinexy(x, y1, h);
728  cvlinexy(x2, y1, h); */
729 
730  if(y + window->h >= clipy1 &&
731  y + window->h < clipy2) {
732  cputcxy(x, y2, CH_LLCORNER);
733  for(i = x1; i < x2; ++i) {
734  cputcxy(i, y2, CH_WINDOWLOWERBORDER);
735  }
736  /* chlinexy(x1, y2, window->w);*/
737  cputcxy(x2, y2, CH_LRCORNER);
738  }
739  }
740 
741  draw_window_contents(window, focus, clipy1, clipy2,
742  x1, x2, y + 1, y2);
743 
744  update_area(window->x, window->y, window->w + 2, window->h + 2);
745 }
746 /*-----------------------------------------------------------------------------------*/
747 /**
748  * Draw a dialog on the VNC screen. Called by the CTK module.
749  *
750  * \param dialog The dialog to be drawn.
751  */
752 /*-----------------------------------------------------------------------------------*/
753 void
755 {
756  unsigned char x, y;
757  unsigned char i;
758  unsigned char x1, y1, x2, y2;
759 
760  /* textcolor(DIALOGCOLOR);*/
761  textcolor(VNC_OUT_WINDOWCOLOR + CTK_FOCUS_DIALOG);
762 
763  x = dialog->x;
764  y = dialog->y + 1;
765 
766 
767  x1 = x + 1;
768  y1 = y + 1;
769  x2 = x1 + dialog->w;
770  y2 = y1 + dialog->h;
771 
772 
773  /* Draw dialog frame. */
774 
775  for(i = y1; i < y1 + dialog->h; ++i) {
776  cputcxy(x, i, CH_DIALOGLBORDER);
777  cputcxy(x2, i, CH_DIALOGRBORDER);
778  }
779  /* cvlinexy(x, y1,
780  dialog->h);
781  cvlinexy(x2, y1,
782  dialog->h);*/
783 
784 
785  for(i = x1; i < x2; ++i) {
786  cputcxy(i, y, CH_DIALOGUPPERBORDER);
787  cputcxy(i, y2, CH_DIALOGLOWERBORDER);
788  }
789  /* chlinexy(x1, y,
790  dialog->w);
791  chlinexy(x1, y2,
792  dialog->w);*/
793 
794  cputcxy(x, y, CH_DIALOG_ULCORNER);
795  cputcxy(x, y2, CH_DIALOG_LLCORNER);
796  cputcxy(x2, y, CH_DIALOG_URCORNER);
797  cputcxy(x2, y2, CH_DIALOG_LRCORNER);
798 
799 
800  /* Clear dialog contents. */
801  for(i = y1; i < y2; ++i) {
802  cclearxy(x1, i, dialog->w);
803  }
804 
805  draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
806  x1, x2, y1, y2);
807 
808  update_area(dialog->x, dialog->y, dialog->w + 4, dialog->h + 4);
809 }
810 /*-----------------------------------------------------------------------------------*/
811 /**
812  * Clear parts of the VNC desktop. Called by the CTK module.
813  *
814  * \param y1 The lower y coordinate bound.
815  * \param y2 The upped y coordinate bound.
816  */
817 /*-----------------------------------------------------------------------------------*/
818 void
819 ctk_draw_clear(unsigned char y1, unsigned char y2)
820 {
821  unsigned char i;
822 
823  textcolor(VNC_OUT_BACKGROUNDCOLOR);
824  for(i = y1; i < y2; ++i) {
825  cclearxy(0, i, sizex);
826  }
827 
828  update_area(0, y1, sizex, y2 - y1);
829 }
830 /*-----------------------------------------------------------------------------------*/
831 /** \internal
832  * Draw one menu on the VNC desktop.
833  *
834  * \param m The CTK menu to be drawn.
835  */
836 /*-----------------------------------------------------------------------------------*/
837 static void
838 draw_menu(struct ctk_menu *m)
839 {
840  unsigned char x, x2, y;
841 
842  textcolor(VNC_OUT_MENUCOLOR);
843  x = wherex();
844  cputs(m->title);
845  cputc(' ');
846  x2 = wherex();
847  if(x + CTK_CONF_MENUWIDTH > sizex) {
848  x = sizex - CTK_CONF_MENUWIDTH;
849  }
850 
851 
852  for(y = 0; y < m->nitems; ++y) {
853  if(y == m->active) {
854  textcolor(VNC_OUT_ACTIVEMENUCOLOR);
855  revers(0);
856  } else {
857  textcolor(VNC_OUT_MENUCOLOR);
858  }
859  gotoxy(x, y + 1);
860  if(m->items[y].title[0] == '-') {
861  chline(CTK_CONF_MENUWIDTH);
862  } else {
863  cputs(m->items[y].title);
864  }
865  if(x + CTK_CONF_MENUWIDTH > wherex()) {
866  cclear(x + CTK_CONF_MENUWIDTH - wherex());
867  }
868  revers(1);
869  }
870 
871  gotoxy(x2, 0);
872  textcolor(VNC_OUT_MENUCOLOR);
873 
874  update_area(x, 0, CTK_CONF_MENUWIDTH, m->nitems + 1);
875 }
876 /*-----------------------------------------------------------------------------------*/
877 /**
878  * Draw the menus on the virtual VNC desktop. Called by the CTK module.
879  *
880  * \param menus The CTK menubar.
881  */
882 /*-----------------------------------------------------------------------------------*/
883 void
884 ctk_draw_menus(struct ctk_menus *menus)
885 {
886  struct ctk_menu *m;
887 
888 
889  /* Draw menus */
890  textcolor(VNC_OUT_MENUCOLOR);
891  gotoxy(0, 0);
892  revers(1);
893  cputc(' ');
894  for(m = menus->menus->next; m != NULL; m = m->next) {
895  if(m != menus->open) {
896  update_area(wherex(), 0, strlen(m->title) + 1, 1);
897  cputs(m->title);
898  cputc(' ');
899  } else {
900  draw_menu(m);
901  }
902  }
903 
904 
905  if(wherex() + strlen(menus->desktopmenu->title) + 1>= sizex) {
906  gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
907  } else {
908  cclear(sizex - wherex() -
909  strlen(menus->desktopmenu->title) - 1);
910  update_area(wherex(), 0, sizex - wherex() -
911  strlen(menus->desktopmenu->title) - 1, 1);
912  }
913 
914  /* Draw desktopmenu */
915  if(menus->desktopmenu != menus->open) {
916  update_area(wherex(), 0, strlen(menus->desktopmenu->title) + 1, 1);
917  cputs(menus->desktopmenu->title);
918  cputc(' ');
919  } else {
920  draw_menu(menus->desktopmenu);
921  }
922 
923  revers(0);
924 
925 
926 
927 }
928 /*-----------------------------------------------------------------------------------*/
929 /**
930  * Obtain the height of the VNC desktop. Called by the CTK module.
931  *
932  * \return The height of the VNC desktop, in characters.
933  */
934 /*-----------------------------------------------------------------------------------*/
935 unsigned char
936 ctk_draw_height(void)
937 {
938  return sizey;
939 }
940 /*-----------------------------------------------------------------------------------*/
941 /**
942  * Obtain the height of the VNC desktop. Called by the CTK module.
943  *
944  * \return The height of the VNC desktop, in characters.
945  */
946 /*-----------------------------------------------------------------------------------*/
947 unsigned char
948 ctk_draw_width(void)
949 {
950  return sizex;
951 }
952 /*-----------------------------------------------------------------------------------*/
953 unsigned char
954 ctk_mouse_xtoc(unsigned short x)
955 {
956  return x / CTK_VNCFONT_WIDTH;
957 }
958 /*-----------------------------------------------------------------------------------*/
959 unsigned char
960 ctk_mouse_ytoc(unsigned short y)
961 {
962  return y / CTK_VNCFONT_HEIGHT;
963 }
964 /*-----------------------------------------------------------------------------------*/
965 /** \internal
966  * Converts between ASCII and the VNC screen character encoding.
967  */
968 /*-----------------------------------------------------------------------------------*/
969 static unsigned char
970 ascii2screen(unsigned char c)
971 {
972  if(c == '|') {
973  return 0x68;
974  }
975  if(c < 0x20) {
976  return c + 0x60;
977  }
978  if(c > 0x20 && c < 0x40) {
979  return c;
980  }
981  if(c >= 0x40 && c < 0x60) {
982  return c;
983  }
984  if(c >= 0x60 && c < 0x80) {
985  return c - 0x60;
986  }
987  if(c >= 0x80) {
988  return c;
989  }
990 
991  return 32;
992 }
993 /*-----------------------------------------------------------------------------------*/
994 /**
995  * Draws a character on the virtual VNC screen. Called by the libconio module.
996  *
997  * \param c The character to be drawn.
998  * \param xpos The x position of the character.
999  * \param ypos The y position of the character.
1000  * \param reversedflag Determines if the character should be reversed or not.
1001  * \param color The color of the character.
1002  */
1003 /*-----------------------------------------------------------------------------------*/
1004 void
1006  unsigned char xpos,
1007  unsigned char ypos,
1008  unsigned char reversedflag,
1009  unsigned char color)
1010 {
1011 
1012  vnc_out_update_screen(xpos, ypos, ascii2screen(c),
1013  color);
1014  /* vnc_out_update_screen(xpos, ypos, c |
1015  (reversedflag? 0x80: 0));*/
1016 }
1017 /*-----------------------------------------------------------------------------------*/
1018 /**
1019  * Checks the key press input queue to see if there are pending
1020  * keys. Called by the CTK module.
1021  *
1022  * \return Zero if no key presses are in buffer, non-zero if there are
1023  * key presses in input buffer.
1024  */
1025 /*-----------------------------------------------------------------------------------*/
1026 unsigned char
1028 {
1029  return vnc_out_keyavail();
1030 }
1031 /*-----------------------------------------------------------------------------------*/
1032 /**
1033  * Retrieves key presses from the VNC client. Called by the CTK
1034  * module.
1035  *
1036  * \return The next key in the input queue.
1037  */
1038 /*-----------------------------------------------------------------------------------*/
1041 {
1042  return vnc_out_getkey() & 0x7f;
1043 }
1044 /*-----------------------------------------------------------------------------------*/
1045 /** \internal
1046  * The uIP event handler.
1047  */
1048 /*-----------------------------------------------------------------------------------*/
1049 void
1050 ctk_vncserver_appcall(void *state)
1051 {
1052  static struct vnc_server_state *vs;
1053 
1054  vs = (struct vnc_server_state *)(state);
1055 
1056  if(uip_connected()) {
1057 
1058  /* Since we've just been connected, the state pointer should be
1059  NULL and we need to allocate a new state object. If we have run
1060  out of memory for state objects, we'll have to abort the
1061  connection and return. */
1062  if(vs == NULL) {
1063  vs = alloc_state();
1064  if(vs == NULL) {
1065  uip_close();
1066  return;
1067  }
1068  tcp_markconn(uip_conn, (void *)vs);
1069  }
1070  } else if(uip_closed() || uip_aborted()) {
1071  if(vs != NULL) {
1072  dealloc_state(vs);
1073  }
1074  return;
1075  }
1076  vnc_server_appcall(vs);
1077 }
1078 /*-----------------------------------------------------------------------------------*/
1079 PROCESS_THREAD(ctk_vncserver_process, ev, data)
1080 {
1081  int i;
1082 
1083  PROCESS_BEGIN();
1084 
1085  tcp_listen(UIP_HTONS(5900));
1086 
1087  for(i = 0; i < CTK_VNCSERVER_CONF_NUMCONNS; ++i) {
1088  conns[i].state = VNC_DEALLOCATED;
1089  }
1090  while(1) {
1092  if(ev == tcpip_event) {
1093  ctk_vncserver_appcall(data);
1094  }
1095  }
1096  PROCESS_END();
1097 }
1098 /*-----------------------------------------------------------------------------------*/
1099 /** @} */