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

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