tty: vt, separate T.416 high colors handler
authorJiri Slaby <jslaby@suse.cz>
Thu, 23 Jun 2016 11:34:31 +0000 (13:34 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 25 Jun 2016 16:04:48 +0000 (09:04 -0700)
The code with T.416 high colors handling is flushed to the right and
hard to read. Move the code to a separate function and remove code
duplication for foreground & background colors.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/vt/vt.c

index 6e12d9c..da49f5c 100644 (file)
@@ -1284,6 +1284,40 @@ static void rgb_background(struct vc_data *vc, struct rgb c)
                | (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, 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++;
+               set_color(vc, rgb_from_256(vc->vc_par[i]));
+       } else if (vc->vc_par[i] == 2 && i <= vc->vc_npar + 3) {
+               /* 24 bit -- extremely rare */
+               struct rgb c = {
+                       .r = vc->vc_par[i + 1],
+                       .g = vc->vc_par[i + 2],
+                       .b = vc->vc_par[i + 3],
+               };
+               set_color(vc, c);
+               i += 3;
+       }
+
+       return i;
+}
+
 /* console_lock is held */
 static void csi_m(struct vc_data *vc)
 {
@@ -1355,56 +1389,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);