Preliminary mouse support added
diff --git a/contiki/ctk/ctk-mouse.h b/contiki/ctk/ctk-mouse.h
new file mode 100644
index 0000000..b172597
--- /dev/null
+++ b/contiki/ctk/ctk-mouse.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2003, Adam Dunkels.
+ * All rights reserved. 
+ *
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met: 
+ * 1. Redistributions of source code must retain the above copyright 
+ *    notice, this list of conditions and the following disclaimer. 
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials provided
+ *    with the distribution. 
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgement:
+ *        This product includes software developed by Adam Dunkels. 
+ * 4. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.  
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
+ *
+ * This file is part of the "ctk" console GUI toolkit for cc65
+ *
+ * $Id: ctk-mouse.h,v 1.1 2003/04/09 00:30:46 adamdunkels Exp $
+ *
+ */
+#ifndef __CTK_MOUSE_H__
+#define __CTK_MOUSE_H__
+
+#include "ctk-conf.h"
+
+#if CTK_CONF_MOUSE_SUPPORT
+
+void ctk_mouse_init(void);
+
+unsigned short ctk_mouse_x(void);
+unsigned short ctk_mouse_y(void);
+
+unsigned char ctk_mouse_xtoc(unsigned short x);
+unsigned char ctk_mouse_ytoc(unsigned short y);
+
+unsigned char ctk_mouse_button(void);
+
+#else
+
+#define ctk_mouse_init()
+#define ctk_mouse_x() 0
+#define ctk_mouse_y() 0
+#define ctk_mouse_xtoc(x) 0
+#define ctk_mouse_ytoc(y) 0
+
+#define ctk_mouse_button() 0
+
+#endif /* CTK_CONF_MOUSE_SUPPORT */
+
+#endif /* __CTK_MOUSE_H__ */
diff --git a/contiki/ctk/ctk.c b/contiki/ctk/ctk.c
index 9ee32eb..28b6f19 100644
--- a/contiki/ctk/ctk.c
+++ b/contiki/ctk/ctk.c
@@ -32,7 +32,7 @@
  *
  * This file is part of the "ctk" console GUI toolkit for cc65
  *
- * $Id: ctk.c,v 1.6 2003/04/08 19:28:15 adamdunkels Exp $
+ * $Id: ctk.c,v 1.7 2003/04/09 00:30:46 adamdunkels Exp $
  *
  */
 
@@ -42,6 +42,7 @@
 #include "ctk.h"
 #include "ctk-draw.h"
 #include "ctk-conf.h"
+#include "ctk-mouse.h"
 
 static unsigned char height, width;
 
@@ -82,13 +83,12 @@
 #define ICONY_DELTA  5
 #define ICONY_MAX    (height - 4)
 
-static void idle(void);
-static void sighandler(ek_signal_t s, ek_data_t data);
+static void ctk_idle(void);
+static void ctk_sighandler(ek_signal_t s, ek_data_t data);
 static struct dispatcher_proc p =
-  {DISPATCHER_PROC("CTK Contiki GUI", idle, sighandler, NULL)};
+  {DISPATCHER_PROC("CTK Contiki GUI", ctk_idle, ctk_sighandler, NULL)};
 static ek_id_t ctkid;
 
-
 ek_signal_t ctk_signal_keypress,
   ctk_signal_timer,
   ctk_signal_button_activate,
@@ -96,8 +96,12 @@
   ctk_signal_hyperlink_activate,
   ctk_signal_hyperlink_hover,
   ctk_signal_menu_activate,
-  ctk_signal_window_close;
-		       
+  ctk_signal_window_close,
+  ctk_signal_pointer_move,
+  ctk_signal_pointer_down,
+  ctk_signal_pointer_up;
+
+unsigned short mouse_last_x, mouse_last_y;
 
 static unsigned short screensaver_timer;
 #define SCREENSAVER_TIMEOUT (5*60)
@@ -145,6 +149,8 @@
   menus.menus = menus.desktopmenu = &desktopmenu;
 #endif /* CTK_CONF_MENUS */
 
+  ctk_mouse_init();
+  
   ctk_draw_init();
 
   height = ctk_draw_height();
@@ -161,6 +167,10 @@
   ctk_signal_hyperlink_hover = dispatcher_sigalloc();
   ctk_signal_menu_activate = dispatcher_sigalloc();
   ctk_signal_window_close = dispatcher_sigalloc();
+
+  ctk_signal_pointer_move = dispatcher_sigalloc();
+  ctk_signal_pointer_down = dispatcher_sigalloc();
+  ctk_signal_pointer_up = dispatcher_sigalloc();
   
   dispatcher_listen(ctk_signal_timer);
   dispatcher_timer(ctk_signal_timer, NULL, CLK_TCK);
@@ -919,13 +929,13 @@
 #endif /* CTK_CONF_MENUS */
 /*-----------------------------------------------------------------------------------*/
 static void
-idle(void)     
+ctk_idle(void)     
 {
   static ctk_arch_key_t c;
-  static unsigned char i;  
+  static unsigned char i, mxc, myc;  
   register struct ctk_window *window;
-  register struct ctk_widget *widget;
-
+  register struct ctk_widget *widget;  
+  
 #if CTK_CONF_MENUS
   if(menus.open != NULL) {
     maxnitems = menus.open->nitems;
@@ -943,7 +953,55 @@
       ctk_draw_init();
       ctk_redraw();
     }
-  } else if(mode == CTK_MODE_NORMAL) {  
+  } else if(mode == CTK_MODE_NORMAL) {
+
+    /* Check if the mouse pointer has moved, and if so we emit a
+       signal. */
+    if(ctk_mouse_x() != mouse_last_x ||
+       ctk_mouse_y() != mouse_last_y) {
+
+      window = windows;
+      
+      mouse_last_x = ctk_mouse_x();
+      mouse_last_y = ctk_mouse_y();
+      dispatcher_emit(ctk_signal_pointer_move, NULL, window->owner);
+
+      /* Find out which widget currently is under the mouse pointer
+	 and give it focus, unless it already has focus. */
+      mxc = ctk_mouse_xtoc(mouse_last_x);
+      myc = ctk_mouse_ytoc(mouse_last_y);
+
+ 	
+      /* Check if the mouse even is in the current window. */
+      if(mxc >= window->x &&
+	 mxc <= window->x + window->w &&
+	 myc >= window->y &&
+	 myc <= window->y + window->h) {
+	
+	mxc -= window->x;
+	myc -= window->y;
+	
+	redraw |= REDRAW_WIDGETS;
+	add_redrawwidget(window->focused);
+	window->focused = NULL;
+	
+	/* Now find the appropriate widget to assign focus to. */
+	for(widget = window->active; widget != NULL; widget = widget->next) {
+	  if(mxc >= widget->x &&
+	     mxc <= widget->x + widget->w &&
+	     myc - widget->y == 0 /* &&
+	     ((widget->type == CTK_WIDGET_BITMAP ||
+	       widget->type == CTK_WIDGET_TEXTENTRY ||
+	       widget->type == CTK_WIDGET_ICON) &&
+	       (myc <= widget->y + ((struct ctk_bitmap *)widget)->h))*/) {
+	    CTK_WIDGET_FOCUS(window, widget);
+	    add_redrawwidget(widget);
+	    break;
+	  }
+	}
+      }
+    }
+    
     while(ctk_arch_keyavail()) {
       
       screensaver_timer = 0;
@@ -1108,7 +1166,7 @@
 }
 /*-----------------------------------------------------------------------------------*/
 static void
-sighandler(ek_signal_t s, ek_data_t data)
+ctk_sighandler(ek_signal_t s, ek_data_t data)
 {
   if(s == ctk_signal_timer) {
     if(mode == CTK_MODE_NORMAL) {
@@ -1126,4 +1184,3 @@
 }
 /*-----------------------------------------------------------------------------------*/
 
-/*-----------------------------------------------------------------------------------*/
diff --git a/contiki/ctk/ctk.h b/contiki/ctk/ctk.h
index a1fb4e3..da04b47 100644
--- a/contiki/ctk/ctk.h
+++ b/contiki/ctk/ctk.h
@@ -32,7 +32,7 @@
  *
  * This file is part of the "ctk" console GUI toolkit for cc65
  *
- * $Id: ctk.h,v 1.3 2003/03/28 12:17:25 adamdunkels Exp $
+ * $Id: ctk.h,v 1.4 2003/04/09 00:30:46 adamdunkels Exp $
  *
  */
 
@@ -121,7 +121,7 @@
 
 
 #define CTK_ICON(title, bitmap, textmap) \
- NULL, NULL, 0, 0, CTK_WIDGET_ICON, 0, title, 0, bitmap, textmap
+ NULL, NULL, 0, 0, CTK_WIDGET_ICON, 0, title, 4, 0, bitmap, textmap
 struct ctk_icon {
   struct ctk_widget *next;
   struct ctk_window *window;
@@ -129,6 +129,7 @@
   unsigned char type;
   unsigned char w;
   char *title;
+  unsigned char h;
   ek_id_t owner;
   unsigned char *bitmap;
   char *textmap;
@@ -172,6 +173,7 @@
 
 struct ctk_widget_icon {
   char *title;
+  unsigned char h;
   ek_id_t owner;
   unsigned char *bitmap;
   char *textmap;
@@ -362,7 +364,10 @@
   ctk_signal_hyperlink_activate,
   ctk_signal_hyperlink_hover,
   ctk_signal_menu_activate,
-  ctk_signal_window_close;
+  ctk_signal_window_close,
+  ctk_signal_pointer_move,
+  ctk_signal_pointer_down,
+  ctk_signal_pointer_up;
 		       
 /* Focus flags */
 #define CTK_FOCUS_NONE     0