adamdunkels | 355ada1 | 2004-07-04 20:54:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * 3. All advertising materials mentioning features or use of this |
| 15 | * software must display the following acknowledgement: |
| 16 | * This product includes software developed by Adam Dunkels. |
| 17 | * 4. The name of the author may not be used to endorse or promote |
| 18 | * products derived from this software without specific prior |
| 19 | * written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 22 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * This file is part of the "ctk" console GUI toolkit for cc65 |
| 34 | * |
| 35 | * $Id: ctk-gtksim-conio.c,v 1.1 2004/07/04 20:54:13 adamdunkels Exp $ |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | /* This file provides a very simple implementation of CTK using the |
| 40 | GTK (Gimp toolkit) under FreeBSD/Linux. */ |
| 41 | |
| 42 | #include <gtk/gtk.h> |
| 43 | |
| 44 | #include "libconio.h" |
| 45 | |
| 46 | #include "ctk.h" |
| 47 | #include "ctk-draw.h" |
| 48 | |
| 49 | #include <string.h> |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | |
| 53 | #define FONT_HEIGHT 14 |
| 54 | #define FONT_WIDTH 8 |
| 55 | #define FONT_HEIGHT_BASELINE 11 |
| 56 | |
| 57 | #define SCREEN_WIDTH LIBCONIO_CONF_SCREEN_WIDTH |
| 58 | #define SCREEN_HEIGHT LIBCONIO_CONF_SCREEN_HEIGHT |
| 59 | |
| 60 | static GdkPixmap *pixmap = NULL; |
| 61 | static GtkWidget *drawing_area; |
| 62 | static GdkFont *font; |
| 63 | |
| 64 | static int mouse_x, mouse_y, mouse_button; |
| 65 | |
| 66 | /*-----------------------------------------------------------------------------------*/ |
| 67 | void |
| 68 | ctk_arch_draw_char(char c, |
| 69 | unsigned char x, unsigned char y, |
| 70 | unsigned char reversed, |
| 71 | unsigned char color) |
| 72 | { |
| 73 | char str[2]; |
| 74 | |
| 75 | str[0] = c; |
| 76 | str[1] = 0; |
| 77 | |
| 78 | if(reversed) { |
| 79 | gdk_draw_rectangle(pixmap, |
| 80 | drawing_area->style->black_gc, |
| 81 | TRUE, |
| 82 | x * FONT_WIDTH, |
| 83 | y * FONT_HEIGHT, |
| 84 | FONT_WIDTH, FONT_HEIGHT); |
| 85 | |
| 86 | gdk_draw_string(pixmap, |
| 87 | font, |
| 88 | drawing_area->style->white_gc, |
| 89 | x * FONT_WIDTH, FONT_HEIGHT_BASELINE + y * FONT_HEIGHT, |
| 90 | str); |
| 91 | } else { |
| 92 | gdk_draw_rectangle(pixmap, |
| 93 | drawing_area->style->white_gc, |
| 94 | TRUE, |
| 95 | x * FONT_WIDTH, |
| 96 | y * FONT_HEIGHT, |
| 97 | FONT_WIDTH, FONT_HEIGHT); |
| 98 | |
| 99 | gdk_draw_string(pixmap, |
| 100 | font, |
| 101 | drawing_area->style->black_gc, |
| 102 | x * FONT_WIDTH, FONT_HEIGHT_BASELINE + y * FONT_HEIGHT, |
| 103 | str); |
| 104 | } |
| 105 | |
| 106 | gdk_draw_pixmap(drawing_area->window, |
| 107 | drawing_area->style->fg_gc[GTK_WIDGET_STATE (drawing_area)], |
| 108 | pixmap, |
| 109 | x * FONT_WIDTH, |
| 110 | y * FONT_HEIGHT, |
| 111 | x * FONT_WIDTH, |
| 112 | y * FONT_HEIGHT, |
| 113 | FONT_WIDTH, FONT_HEIGHT); |
| 114 | |
| 115 | } |
| 116 | /*-----------------------------------------------------------------------------------*/ |
| 117 | #define NUMKEYS 100 |
| 118 | static ctk_arch_key_t keys[NUMKEYS]; |
| 119 | static int firstkey, lastkey; |
| 120 | |
| 121 | unsigned char |
| 122 | ctk_arch_keyavail(void) |
| 123 | { |
| 124 | return firstkey != lastkey; |
| 125 | } |
| 126 | /*-----------------------------------------------------------------------------------*/ |
| 127 | ctk_arch_key_t |
| 128 | ctk_arch_getkey(void) |
| 129 | { |
| 130 | ctk_arch_key_t key; |
| 131 | key = keys[firstkey]; |
| 132 | |
| 133 | if(firstkey != lastkey) { |
| 134 | ++firstkey; |
| 135 | if(firstkey >= NUMKEYS) { |
| 136 | firstkey = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return key; |
| 141 | } |
| 142 | /*-----------------------------------------------------------------------------------*/ |
| 143 | static gint |
| 144 | configure_event(GtkWidget *widget, GdkEventConfigure *event) |
| 145 | { |
| 146 | if(pixmap != NULL) { |
| 147 | gdk_pixmap_unref(pixmap); |
| 148 | } |
| 149 | |
| 150 | pixmap = gdk_pixmap_new(widget->window, |
| 151 | widget->allocation.width, |
| 152 | widget->allocation.height, |
| 153 | -1); |
| 154 | |
| 155 | if(pixmap == NULL) { |
| 156 | printf("gdk_pixmap_new == NULL\n"); |
| 157 | exit(1); |
| 158 | } |
| 159 | gdk_draw_rectangle(pixmap, |
| 160 | widget->style->white_gc, |
| 161 | TRUE, |
| 162 | 0, 0, |
| 163 | widget->allocation.width, |
| 164 | widget->allocation.height); |
| 165 | |
| 166 | return TRUE; |
| 167 | } |
| 168 | |
| 169 | /* Redraw the screen from the backing pixmap */ |
| 170 | static gint |
| 171 | expose_event (GtkWidget * widget, GdkEventExpose * event) |
| 172 | { |
| 173 | /* draw_screen();*/ |
| 174 | gdk_draw_pixmap(widget->window, |
| 175 | widget->style->fg_gc[GTK_WIDGET_STATE (widget)], |
| 176 | pixmap, |
| 177 | event->area.x, event->area.y, |
| 178 | event->area.x, event->area.y, |
| 179 | event->area.width, event->area.height); |
| 180 | return FALSE; |
| 181 | } |
| 182 | |
| 183 | static gint |
| 184 | key_press_event (GtkWidget * widget, GdkEventKey * event) |
| 185 | { |
| 186 | if(event->keyval == GDK_Shift_L || |
| 187 | event->keyval == GDK_Shift_R) { |
| 188 | return TRUE; |
| 189 | } |
| 190 | keys[lastkey] = event->keyval; |
| 191 | ++lastkey; |
| 192 | if(lastkey >= NUMKEYS) { |
| 193 | lastkey = 0; |
| 194 | } |
| 195 | |
| 196 | return TRUE; |
| 197 | } |
| 198 | |
| 199 | static gint |
| 200 | key_release_event (GtkWidget * widget, GdkEventKey * event) |
| 201 | { |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | static gint |
| 206 | motion_notify_event (GtkWidget * widget, GdkEventMotion * event) |
| 207 | { |
| 208 | mouse_x = event->x; |
| 209 | mouse_y = event->y; |
| 210 | return TRUE; |
| 211 | } |
| 212 | |
| 213 | static gint |
| 214 | button_press_event (GtkWidget * widget, GdkEventButton * event) |
| 215 | { |
| 216 | mouse_button = event->button; |
| 217 | return TRUE; |
| 218 | } |
| 219 | |
| 220 | static gint |
| 221 | button_release_event (GtkWidget * widget, GdkEventButton * event) |
| 222 | { |
| 223 | mouse_button = 0; |
| 224 | return TRUE; |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | quit(void) |
| 229 | { |
| 230 | gtk_exit(0); |
| 231 | } |
| 232 | /*-----------------------------------------------------------------------------------*/ |
| 233 | void |
| 234 | ctk_gtksim_init(int *argc, char **argv[]) |
| 235 | { |
| 236 | GtkWidget *window; |
| 237 | #if 0 |
| 238 | GtkWidget *vbox; |
| 239 | #endif |
| 240 | |
| 241 | gtk_init(argc, argv); |
| 242 | |
| 243 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 244 | gtk_window_set_title(window, "Contiki GTKsim"); |
| 245 | |
| 246 | #if 0 |
| 247 | vbox = gtk_vbox_new(FALSE, 0); |
| 248 | gtk_container_add(GTK_CONTAINER (window), vbox); |
| 249 | gtk_widget_show(vbox); |
| 250 | #endif |
| 251 | gtk_signal_connect(GTK_OBJECT (window), "destroy", |
| 252 | GTK_SIGNAL_FUNC (quit), NULL); |
| 253 | |
| 254 | /* Create the drawing area */ |
| 255 | |
| 256 | drawing_area = gtk_drawing_area_new(); |
| 257 | gtk_drawing_area_size(GTK_DRAWING_AREA (drawing_area), |
| 258 | SCREEN_WIDTH * FONT_WIDTH, |
| 259 | SCREEN_HEIGHT * FONT_HEIGHT); |
| 260 | #if 0 |
| 261 | gtk_box_pack_start(GTK_BOX(vbox), drawing_area, TRUE, TRUE, 0); |
| 262 | #else |
| 263 | gtk_container_add(GTK_CONTAINER(window), drawing_area); |
| 264 | #endif |
| 265 | |
| 266 | gtk_widget_show(drawing_area); |
| 267 | |
| 268 | /* Load a fixed width font. */ |
| 269 | /* font = gdk_font_load("-*-gamow-medium-r-*-*-*-90-*-*-*-*-*-*");*/ |
| 270 | /* font = gdk_font_load("-*-courier-*-r-normal-*-14-*-*-*-m-*-iso8859-1");*/ |
| 271 | font = gdk_font_load("-*-courier-medium-r-*-*-12-*-*-*-*-*-iso8859-1"); |
| 272 | if(font != NULL) { |
| 273 | printf("Font loaded OK\n"); |
| 274 | } else { |
| 275 | printf("Font loading failed\n"); |
| 276 | exit(1); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /* Signals used to handle backing pixmap */ |
| 281 | |
| 282 | gtk_signal_connect(GTK_OBJECT (drawing_area), "expose_event", |
| 283 | (GtkSignalFunc) expose_event, NULL); |
| 284 | gtk_signal_connect(GTK_OBJECT (drawing_area), "configure_event", |
| 285 | (GtkSignalFunc) configure_event, NULL); |
| 286 | |
| 287 | /* Event signals */ |
| 288 | |
| 289 | gtk_signal_connect(GTK_OBJECT (window), "key_press_event", |
| 290 | (GtkSignalFunc) key_press_event, NULL); |
| 291 | gtk_signal_connect(GTK_OBJECT (window), "key_release_event", |
| 292 | (GtkSignalFunc) key_release_event, NULL); |
| 293 | |
| 294 | gtk_signal_connect(GTK_OBJECT (drawing_area), "motion_notify_event", |
| 295 | (GtkSignalFunc) motion_notify_event, NULL); |
| 296 | |
| 297 | gtk_signal_connect(GTK_OBJECT (drawing_area), "button_press_event", |
| 298 | (GtkSignalFunc) button_press_event, NULL); |
| 299 | |
| 300 | gtk_signal_connect(GTK_OBJECT (drawing_area), "button_release_event", |
| 301 | (GtkSignalFunc) button_release_event, NULL); |
| 302 | |
| 303 | gtk_widget_set_events(drawing_area, gtk_widget_get_events (drawing_area) |
| 304 | | GDK_KEY_PRESS_MASK |
| 305 | | GDK_KEY_RELEASE_MASK |
| 306 | | GDK_POINTER_MOTION_MASK |
| 307 | | GDK_BUTTON_PRESS_MASK |
| 308 | | GDK_BUTTON_RELEASE_MASK); |
| 309 | |
| 310 | gtk_widget_show(window); |
| 311 | |
| 312 | } |
| 313 | /*-----------------------------------------------------------------------------------*/ |
| 314 | void |
| 315 | ctk_mouse_init(void) |
| 316 | { |
| 317 | |
| 318 | } |
| 319 | /*-----------------------------------------------------------------------------------*/ |
| 320 | unsigned short |
| 321 | ctk_mouse_x(void) |
| 322 | { |
| 323 | return mouse_x; |
| 324 | } |
| 325 | /*-----------------------------------------------------------------------------------*/ |
| 326 | unsigned short |
| 327 | ctk_mouse_y(void) |
| 328 | { |
| 329 | return mouse_y; |
| 330 | } |
| 331 | /*-----------------------------------------------------------------------------------*/ |
| 332 | unsigned char |
| 333 | ctk_mouse_xtoc(unsigned short x) |
| 334 | { |
| 335 | return x / FONT_WIDTH; |
| 336 | } |
| 337 | /*-----------------------------------------------------------------------------------*/ |
| 338 | unsigned char |
| 339 | ctk_mouse_ytoc(unsigned short y) |
| 340 | { |
| 341 | return y / FONT_HEIGHT; |
| 342 | } |
| 343 | /*-----------------------------------------------------------------------------------*/ |
| 344 | unsigned char |
| 345 | ctk_mouse_button(void) |
| 346 | { |
| 347 | return mouse_button; |
| 348 | } |
| 349 | /*-----------------------------------------------------------------------------------*/ |
| 350 | void |
| 351 | ctk_mouse_hide(void) |
| 352 | { |
| 353 | } |
| 354 | /*-----------------------------------------------------------------------------------*/ |
| 355 | void |
| 356 | ctk_mouse_show(void) |
| 357 | { |
| 358 | } |
| 359 | /*-----------------------------------------------------------------------------------*/ |