root/core/console.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. console_init
  2. console_is_inited
  3. console_ensure_inited
  4. console_start_line
  5. console_close
  6. console_clear
  7. console_draw
  8. console_add_line
  9. console_set_layout
  10. console_set_autoredraw
  11. console_redraw
  12. display_console
  13. gui_console_draw
  14. gui_console_kbd_process

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "conf.h"
   4 #include "console.h"
   5 #include "clock.h"
   6 #include "keyboard.h"
   7 #include "gui.h"
   8 #include "gui_draw.h"
   9 
  10 #define MAX_CONSOLE_LINES       (camera_screen.height/FONT_HEIGHT-1)
  11 #define MAX_CONSOLE_HISTORY     30
  12 #define MAX_CONSOLE_LINE_LENGTH 44
  13 
  14 // Static buffer for console output
  15 // Latest entry is always console_buf[console_cur_line]
  16 static char console_buf[MAX_CONSOLE_HISTORY][MAX_CONSOLE_LINE_LENGTH+1];
  17 
  18 static int console_max_lines;           // total number of lines in console
  19 static int console_num_lines;           // number of valid lines
  20 static int console_cur_line = 0;        // current line for new text
  21 static int console_line_length = 0;     // current console width
  22 static int console_x;                   // screen X position
  23 static int console_y;                   // screen Y position
  24 static int console_autoredraw = 1;
  25 static long console_last_modified;
  26 int console_displayed = 0;
  27 
  28 static void console_init(int num_lines, int line_length, int x_pos, int y_pos)
  29 {
  30     console_max_lines = num_lines;
  31     console_line_length = line_length;
  32     console_y = y_pos;
  33     console_x = x_pos;
  34 
  35     console_num_lines = 0;
  36     console_last_modified = 0;
  37 }
  38 
  39 static int console_is_inited()
  40 {
  41     return (console_line_length > 0);
  42 }
  43 
  44 static void console_ensure_inited()
  45 {
  46     if (!console_is_inited())
  47         console_init(5, 25, 0, MAX_CONSOLE_LINES - 5);
  48 }
  49 
  50 static void console_start_line()
  51 {
  52     // increase line count
  53     if (console_num_lines < console_max_lines)
  54         console_num_lines++;
  55     // move current line and clear content
  56     if (++console_cur_line >= MAX_CONSOLE_HISTORY)
  57         console_cur_line = 0;
  58     console_buf[console_cur_line][0] = 0;
  59 }
  60 
  61 void console_close()
  62 {
  63     console_line_length = 0;
  64     console_autoredraw = 1;
  65 }
  66 
  67 void console_clear()
  68 {
  69     console_num_lines = 0;
  70     
  71     if (console_autoredraw)
  72         console_redraw();
  73 }
  74 
  75 void console_draw(int force_redraw)
  76 {
  77     char buf[MAX_CONSOLE_LINE_LENGTH+1];
  78 
  79     console_ensure_inited();
  80 
  81     twoColors col = user_color(conf.osd_color);
  82 
  83     long t = get_tick_count();
  84     if (t <= console_last_modified + (conf.console_timeout*1000))               // Redraw if changed
  85     {
  86         if ((console_displayed == 0) || force_redraw)
  87         {
  88             int y = (console_y + console_max_lines - 1) * FONT_HEIGHT;
  89             int x = console_x * FONT_WIDTH + camera_screen.disp_left;
  90 
  91             int c, i;
  92             for (c = 0, i = console_cur_line; c < console_num_lines; ++c, --i)
  93             {
  94                 if (i < 0) i = MAX_CONSOLE_HISTORY-1;
  95                 strncpy(buf,console_buf[i],console_line_length);
  96                 buf[console_line_length] = 0;
  97                 draw_string_justified(x, y - c * FONT_HEIGHT, buf, col, 0, console_line_length * FONT_WIDTH, TEXT_LEFT|TEXT_FILL);
  98 
  99                 console_displayed = 1;
 100             }
 101         }
 102     }
 103     else if (console_displayed && !camera_info.state.state_kbd_script_run)      // Erase if drawn and script not running
 104     {
 105         gui_set_need_restore();
 106         console_displayed = 0;
 107     }
 108 }
 109 
 110 void console_add_line(const char *str)
 111 {
 112         // Set console_autoredraw to -1 to stop script print statements from being displayed on screen
 113         // Can be used to send script output to a file (using print_screen function); but not have it displayed
 114         // on the LCD.
 115     if (console_autoredraw == -1)
 116             return;
 117 
 118     console_ensure_inited();
 119     console_start_line();
 120     
 121     do
 122     {
 123         char *cur = console_buf[console_cur_line];
 124         int curlen = strlen(cur);
 125         int left = console_line_length - curlen;
 126         if (strlen(str) > left)
 127         {
 128             strncpy(cur + curlen, str, left);
 129             cur[console_line_length] = 0;
 130             console_start_line();
 131             str += left;
 132         }
 133         else
 134         {
 135             strcat(cur, str);
 136             break;
 137         }
 138     } while(1);
 139 
 140     // ToDo: this should probably only be done if console_autoredraw == 0; but that breaks existing scripts (e.g. EDI.lua)
 141     console_displayed = 0;
 142     console_last_modified = get_tick_count();
 143 }
 144 
 145 void console_set_layout(int x1, int y1, int x2, int y2) //untere linke Ecke(x1,y1), obere reche Ecke(x2,y2) - lower left corner (x1,y1), upper right corner(x2,y2)
 146 {
 147     int i;
 148 
 149     // Swap co-ords so x1 < x2 & y1 < y2
 150     if (x1 > x2) { i = x1; x1 = x2; x2 = i; }
 151     if (y1 > y2) { i = y1; y1 = y2; y2 = i; }
 152 
 153     // Limit values to screen range (TODO: add error message if out of range)
 154     if (x1 < 0) x1 = 0;
 155     if (x1 > MAX_CONSOLE_LINE_LENGTH) x1 = MAX_CONSOLE_LINE_LENGTH;
 156     if (x2 < 0) x2 = 0;
 157     if (x2 > MAX_CONSOLE_LINE_LENGTH) x2 = MAX_CONSOLE_LINE_LENGTH;
 158 
 159     if (y1 < 0) y1 = 0;
 160     if (y1 > MAX_CONSOLE_LINES) y1 = MAX_CONSOLE_LINES;
 161     if (y2 < 0) y2 = 0;
 162     if (y2 > MAX_CONSOLE_LINES) y2 = MAX_CONSOLE_LINES;
 163 
 164     //Adjust for new size if needed
 165     console_line_length = x2 - x1;
 166     console_max_lines = y2 - y1;
 167 
 168     // If number of lines in console has reduced and old console has too many lines
 169     if (console_num_lines > console_max_lines)
 170         console_num_lines = console_max_lines;
 171         
 172     console_x = x1;
 173     console_y = MAX_CONSOLE_LINES - y2;
 174 
 175     if (console_autoredraw)
 176         console_redraw();
 177 }
 178 
 179 void console_set_autoredraw(int val)
 180 {
 181         console_autoredraw = val;
 182 }
 183 
 184 void console_redraw()
 185 {
 186     gui_set_need_restore();
 187     console_displayed = 0;
 188     console_last_modified = get_tick_count();
 189 }
 190 
 191 //-------------------------------------------------------------------
 192 
 193 static void gui_console_draw();
 194 static int gui_console_kbd_process();
 195 
 196 static gui_handler mboxGuiHandler = { GUI_MODE_MBOX, gui_console_draw, gui_console_kbd_process, 0, 0, 0 };
 197 
 198 static gui_handler          *gui_console_mode_old;
 199 
 200 static int  console_redraw_flag;
 201 static int  console_scroll;
 202 
 203 #define MAX_CONSOLE_DISP_LINES  12
 204 
 205 //-------------------------------------------------------------------
 206 void display_console()
 207 {
 208     console_redraw_flag = 1;
 209     console_scroll = 0;
 210     gui_console_mode_old = gui_set_mode(&mboxGuiHandler);
 211 }
 212 
 213 //-------------------------------------------------------------------
 214 static void gui_console_draw()
 215 {
 216     if (console_redraw_flag)
 217     {
 218         twoColors col = user_color(conf.menu_color);
 219 
 220         int w = MAX_CONSOLE_LINE_LENGTH;
 221         int h = MAX_CONSOLE_DISP_LINES;
 222 
 223         coord x = (camera_screen.width - w * FONT_WIDTH) >> 1;
 224         coord y = (camera_screen.height - (h+1) * FONT_HEIGHT) >> 1;
 225 
 226         draw_rectangle(x-3, y-3, x+w*FONT_WIDTH+3, y+(h+1)*FONT_HEIGHT+2, col, RECT_BORDER1|DRAW_FILLED); // main box
 227         draw_rectangle(x-2, y-2, x+w*FONT_WIDTH+2, y+FONT_HEIGHT+1, col, RECT_BORDER1|DRAW_FILLED); //title
 228 
 229         draw_string_justified(x, y, "Console - press SET to close", col, 0, w*FONT_WIDTH, TEXT_CENTER); //title text
 230         y += FONT_HEIGHT + 2;
 231 
 232         int c, i;
 233         for (c = h-1, i = console_cur_line-console_scroll; c >= 0; --c, --i)
 234         {
 235             if (i < 0) i += MAX_CONSOLE_HISTORY;
 236             draw_string_justified(x-1, y + c * FONT_HEIGHT, console_buf[i], col, 0, w * FONT_WIDTH, TEXT_LEFT|TEXT_FILL);
 237         }
 238 
 239         // Scrollbar
 240         draw_rectangle(x+w*FONT_WIDTH, y+((MAX_CONSOLE_HISTORY-console_scroll-h)*(h*FONT_HEIGHT))/MAX_CONSOLE_HISTORY,
 241                        x+w*FONT_WIDTH+2, y+((MAX_CONSOLE_HISTORY-console_scroll)*(h*FONT_HEIGHT))/MAX_CONSOLE_HISTORY-1,
 242                        MAKE_COLOR(COLOR_RED, COLOR_RED), RECT_BORDER0|DRAW_FILLED);
 243 
 244         console_redraw_flag = 0;
 245     }
 246 }
 247 
 248 //-------------------------------------------------------------------
 249 static int gui_console_kbd_process()
 250 {
 251     switch (kbd_get_clicked_key() | get_jogdial_direction())
 252     {
 253     case JOGDIAL_LEFT:
 254     case KEY_UP:
 255         if (console_scroll < (MAX_CONSOLE_HISTORY-MAX_CONSOLE_DISP_LINES))
 256         {
 257             console_scroll++;
 258             console_redraw_flag = 1;
 259         }
 260         break;
 261     case JOGDIAL_RIGHT:
 262     case KEY_DOWN:
 263         if (console_scroll > 0)
 264         {
 265             console_scroll--;
 266             console_redraw_flag = 1;
 267         }
 268         break;
 269     case KEY_SET:
 270         gui_set_mode(gui_console_mode_old);
 271         break;
 272     }
 273     return 0;
 274 }

/* [<][>][^][v][top][bottom][index][help] */