tty: vt, too many commands per line in rgb_foreground
[cascardo/linux.git] / drivers / tty / vt / vt.c
index dc12532..acecd66 100644 (file)
@@ -278,12 +278,7 @@ static void notify_update(struct vc_data *vc)
  */
 
 #define IS_FG(vc)      ((vc)->vc_num == fg_console)
-
-#ifdef VT_BUF_VRAM_ONLY
-#define DO_UPDATE(vc)  0
-#else
 #define DO_UPDATE(vc)  (CON_IS_VISIBLE(vc) && !console_blanked)
-#endif
 
 static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
 {
@@ -349,7 +344,6 @@ static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
 
 static void do_update_region(struct vc_data *vc, unsigned long start, int count)
 {
-#ifndef VT_BUF_VRAM_ONLY
        unsigned int xx, yy, offset;
        u16 *p;
 
@@ -390,7 +384,6 @@ static void do_update_region(struct vc_data *vc, unsigned long start, int count)
                        start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
                }
        }
-#endif
 }
 
 void update_region(struct vc_data *vc, unsigned long start, int count)
@@ -413,7 +406,6 @@ static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
                return vc->vc_sw->con_build_attr(vc, _color, _intensity,
                       _blink, _underline, _reverse, _italic);
 
-#ifndef VT_BUF_VRAM_ONLY
 /*
  * ++roman: I completely changed the attribute format for monochrome
  * mode (!can_do_color). The formerly used MDA (monochrome display
@@ -448,9 +440,6 @@ static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
                a <<= 1;
        return a;
        }
-#else
-       return 0;
-#endif
 }
 
 static void update_attr(struct vc_data *vc)
@@ -470,10 +459,9 @@ void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
 
        count /= 2;
        p = screenpos(vc, offset, viewed);
-       if (vc->vc_sw->con_invert_region)
+       if (vc->vc_sw->con_invert_region) {
                vc->vc_sw->con_invert_region(vc, p, count);
-#ifndef VT_BUF_VRAM_ONLY
-       else {
+       } else {
                u16 *q = p;
                int cnt = count;
                u16 a;
@@ -501,7 +489,7 @@ void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
                        }
                }
        }
-#endif
+
        if (DO_UPDATE(vc))
                do_update_region(vc, (unsigned long) p, count);
        notify_update(vc);
@@ -1102,11 +1090,9 @@ static void gotoxay(struct vc_data *vc, int new_x, int new_y)
        gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
 }
 
-void scrollback(struct vc_data *vc, int lines)
+void scrollback(struct vc_data *vc)
 {
-       if (!lines)
-               lines = vc->vc_rows / 2;
-       scrolldelta(-lines);
+       scrolldelta(-(vc->vc_rows / 2));
 }
 
 void scrollfront(struct vc_data *vc, int lines)
@@ -1254,48 +1240,87 @@ static void default_attr(struct vc_data *vc)
 
 struct rgb { u8 r; u8 g; u8 b; };
 
-static struct rgb rgb_from_256(int i)
+static void rgb_from_256(int i, struct rgb *c)
 {
-       struct rgb c;
        if (i < 8) {            /* Standard colours. */
-               c.r = i&1 ? 0xaa : 0x00;
-               c.g = i&2 ? 0xaa : 0x00;
-               c.b = i&4 ? 0xaa : 0x00;
+               c->r = i&1 ? 0xaa : 0x00;
+               c->g = i&2 ? 0xaa : 0x00;
+               c->b = i&4 ? 0xaa : 0x00;
        } else if (i < 16) {
-               c.r = i&1 ? 0xff : 0x55;
-               c.g = i&2 ? 0xff : 0x55;
-               c.b = i&4 ? 0xff : 0x55;
+               c->r = i&1 ? 0xff : 0x55;
+               c->g = i&2 ? 0xff : 0x55;
+               c->b = i&4 ? 0xff : 0x55;
        } else if (i < 232) {   /* 6x6x6 colour cube. */
-               c.r = (i - 16) / 36 * 85 / 2;
-               c.g = (i - 16) / 6 % 6 * 85 / 2;
-               c.b = (i - 16) % 6 * 85 / 2;
+               c->r = (i - 16) / 36 * 85 / 2;
+               c->g = (i - 16) / 6 % 6 * 85 / 2;
+               c->b = (i - 16) % 6 * 85 / 2;
        } else                  /* Grayscale ramp. */
-               c.r = c.g = c.b = i * 10 - 2312;
-       return c;
+               c->r = c->g = c->b = i * 10 - 2312;
 }
 
-static void rgb_foreground(struct vc_data *vc, struct rgb c)
+static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
 {
-       u8 hue, max = c.r;
-       if (c.g > max)
-               max = c.g;
-       if (c.b > max)
-               max = c.b;
-       hue = (c.r > max/2 ? 4 : 0)
-           | (c.g > max/2 ? 2 : 0)
-           | (c.b > max/2 ? 1 : 0);
-       if (hue == 7 && max <= 0x55)
-               hue = 0, vc->vc_intensity = 2;
+       u8 hue = 0, max = max3(c->r, c->g, c->b);
+
+       if (c->r > max / 2)
+               hue |= 4;
+       if (c->g > max / 2)
+               hue |= 2;
+       if (c->b > max / 2)
+               hue |= 1;
+
+       if (hue == 7 && max <= 0x55) {
+               hue = 0;
+               vc->vc_intensity = 2;
+       } else if (max > 0xaa)
+               vc->vc_intensity = 2;
        else
-               vc->vc_intensity = (max > 0xaa) + 1;
+               vc->vc_intensity = 1;
+
        vc->vc_color = (vc->vc_color & 0xf0) | hue;
 }
 
-static void rgb_background(struct vc_data *vc, struct rgb c)
+static void rgb_background(struct vc_data *vc, const struct rgb *c)
 {
        /* For backgrounds, err on the dark side. */
        vc->vc_color = (vc->vc_color & 0x0f)
-               | (c.r&0x80) >> 1 | (c.g&0x80) >> 2 | (c.b&0x80) >> 3;
+               | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
+}
+
+/*
+ * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
+ * and thus need to be detected and ignored by hand. Strictly speaking, that
+ * standard also wants : rather than ; as separators, contrary to ECMA-48, but
+ * no one produces such codes and almost no one accepts them.
+ *
+ * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
+ * supporting them.
+ */
+static int vc_t416_color(struct vc_data *vc, int i,
+               void(*set_color)(struct vc_data *vc, const struct rgb *c))
+{
+       struct rgb c;
+
+       i++;
+       if (i > vc->vc_npar)
+               return i;
+
+       if (vc->vc_par[i] == 5 && i < vc->vc_npar) {
+               /* 256 colours -- ubiquitous */
+               i++;
+               rgb_from_256(vc->vc_par[i], &c);
+       } else if (vc->vc_par[i] == 2 && i <= vc->vc_npar + 3) {
+               /* 24 bit -- extremely rare */
+               c.r = vc->vc_par[i + 1];
+               c.g = vc->vc_par[i + 2];
+               c.b = vc->vc_par[i + 3];
+               i += 3;
+       } else
+               return i;
+
+       set_color(vc, &c);
+
+       return i;
 }
 
 /* console_lock is held */
@@ -1369,56 +1394,11 @@ static void csi_m(struct vc_data *vc)
                        case 27:
                                vc->vc_reverse = 0;
                                break;
-                       case 38: /* ITU T.416
-                                 * Higher colour modes.
-                                 * They break the usual properties of SGR codes
-                                 * and thus need to be detected and ignored by
-                                 * hand.  Strictly speaking, that standard also
-                                 * wants : rather than ; as separators, contrary
-                                 * to ECMA-48, but no one produces such codes
-                                 * and almost no one accepts them.
-                                 */
-                               i++;
-                               if (i > vc->vc_npar)
-                                       break;
-                               if (vc->vc_par[i] == 5 &&  /* 256 colours */
-                                   i < vc->vc_npar) {     /* ubiquitous */
-                                       i++;
-                                       rgb_foreground(vc,
-                                               rgb_from_256(vc->vc_par[i]));
-                               } else if (vc->vc_par[i] == 2 &&  /* 24 bit */
-                                          i <= vc->vc_npar + 3) {/* extremely rare */
-                                       struct rgb c = {
-                                               .r = vc->vc_par[i + 1],
-                                               .g = vc->vc_par[i + 2],
-                                               .b = vc->vc_par[i + 3],
-                                       };
-                                       rgb_foreground(vc, c);
-                                       i += 3;
-                               }
-                               /* Subcommands 3 (CMY) and 4 (CMYK) are so insane
-                                * there's no point in supporting them.
-                                */
+                       case 38:
+                               i = vc_t416_color(vc, i, rgb_foreground);
                                break;
                        case 48:
-                               i++;
-                               if (i > vc->vc_npar)
-                                       break;
-                               if (vc->vc_par[i] == 5 &&  /* 256 colours */
-                                   i < vc->vc_npar) {
-                                       i++;
-                                       rgb_background(vc,
-                                               rgb_from_256(vc->vc_par[i]));
-                               } else if (vc->vc_par[i] == 2 && /* 24 bit */
-                                          i <= vc->vc_npar + 3) {
-                                       struct rgb c = {
-                                               .r = vc->vc_par[i + 1],
-                                               .g = vc->vc_par[i + 2],
-                                               .b = vc->vc_par[i + 3],
-                                       };
-                                       rgb_background(vc, c);
-                                       i += 3;
-                               }
+                               i = vc_t416_color(vc, i, rgb_background);
                                break;
                        case 39:
                                vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
@@ -2177,18 +2157,20 @@ static int is_double_width(uint32_t ucs)
        return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
 }
 
+static void con_flush(struct vc_data *vc, unsigned long draw_from,
+               unsigned long draw_to, int *draw_x)
+{
+       if (*draw_x < 0)
+               return;
+
+       vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
+                       (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
+       *draw_x = -1;
+}
+
 /* acquires console_lock */
 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
 {
-#ifdef VT_BUF_VRAM_ONLY
-#define FLUSH do { } while(0);
-#else
-#define FLUSH if (draw_x >= 0) { \
-       vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
-       draw_x = -1; \
-       }
-#endif
-
        int c, tc, ok, n = 0, draw_x = -1;
        unsigned int currcons;
        unsigned long draw_from = 0, draw_to = 0;
@@ -2380,12 +2362,13 @@ rescan_last_byte:
                                } else {
                                        vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
                                }
-                               FLUSH
+                               con_flush(vc, draw_from, draw_to, &draw_x);
                        }
 
                        while (1) {
                                if (vc->vc_need_wrap || vc->vc_decim)
-                                       FLUSH
+                                       con_flush(vc, draw_from, draw_to,
+                                                       &draw_x);
                                if (vc->vc_need_wrap) {
                                        cr(vc);
                                        lf(vc);
@@ -2415,9 +2398,8 @@ rescan_last_byte:
                        }
                        notify_write(vc, c);
 
-                       if (inverse) {
-                               FLUSH
-                       }
+                       if (inverse)
+                               con_flush(vc, draw_from, draw_to, &draw_x);
 
                        if (rescan) {
                                rescan = 0;
@@ -2428,15 +2410,14 @@ rescan_last_byte:
                        }
                        continue;
                }
-               FLUSH
+               con_flush(vc, draw_from, draw_to, &draw_x);
                do_con_trol(tty, vc, orig);
        }
-       FLUSH
+       con_flush(vc, draw_from, draw_to, &draw_x);
        console_conditional_schedule();
        console_unlock();
        notify_update(vc);
        return n;
-#undef FLUSH
 }
 
 /*
@@ -2470,7 +2451,7 @@ static void console_callback(struct work_struct *ignored)
        if (scrollback_delta) {
                struct vc_data *vc = vc_cons[fg_console].d;
                clear_selection();
-               if (vc->vc_mode == KD_TEXT)
+               if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
                        vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
                scrollback_delta = 0;
        }
@@ -3980,7 +3961,7 @@ static void set_palette(struct vc_data *vc)
 {
        WARN_CONSOLE_UNLOCKED();
 
-       if (vc->vc_mode != KD_GRAPHICS)
+       if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
                vc->vc_sw->con_set_palette(vc, color_table);
 }