1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 The FreeBSD Foundation 5 * 6 * This software was developed by Aleksandr Rybalko under sponsorship from the 7 * FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/libkern.h> 34 #include <sys/fbio.h> 35 36 #include <dev/vt/colors/vt_termcolors.h> 37 38 static struct { 39 unsigned char r; /* Red percentage value. */ 40 unsigned char g; /* Green percentage value. */ 41 unsigned char b; /* Blue percentage value. */ 42 } color_def[NCOLORS] = { 43 {0, 0, 0}, /* black */ 44 {50, 0, 0}, /* dark red */ 45 {0, 50, 0}, /* dark green */ 46 {77, 63, 0}, /* dark yellow */ 47 {20, 40, 64}, /* dark blue */ 48 {50, 0, 50}, /* dark magenta */ 49 {0, 50, 50}, /* dark cyan */ 50 {75, 75, 75}, /* light gray */ 51 52 {18, 20, 21}, /* dark gray */ 53 {100, 0, 0}, /* light red */ 54 {0, 100, 0}, /* light green */ 55 {100, 100, 0}, /* light yellow */ 56 {45, 62, 81}, /* light blue */ 57 {100, 0, 100}, /* light magenta */ 58 {0, 100, 100}, /* light cyan */ 59 {100, 100, 100}, /* white */ 60 }; 61 62 static int 63 vt_parse_rgb_triplet(const char *rgb, unsigned char *r, 64 unsigned char *g, unsigned char *b) 65 { 66 unsigned long v; 67 const char *ptr; 68 char *endptr; 69 70 ptr = rgb; 71 72 /* Handle #rrggbb case */ 73 if (*ptr == '#') { 74 if (strlen(ptr) != 7) 75 return (-1); 76 v = strtoul(ptr + 1, &endptr, 16); 77 if (*endptr != '\0') 78 return (-1); 79 80 *r = (v >> 16) & 0xff; 81 *g = (v >> 8) & 0xff; 82 *b = v & 0xff; 83 84 return (0); 85 } 86 87 /* "r, g, b" case */ 88 v = strtoul(ptr, &endptr, 10); 89 if (ptr == endptr) 90 return (-1); 91 if (v > 255) 92 return (-1); 93 *r = v & 0xff; 94 ptr = endptr; 95 96 /* skip separator */ 97 while (*ptr == ',' || *ptr == ' ') 98 ptr++; 99 100 v = strtoul(ptr, &endptr, 10); 101 if (ptr == endptr) 102 return (-1); 103 if (v > 255) 104 return (-1); 105 *g = v & 0xff; 106 ptr = endptr; 107 108 /* skip separator */ 109 while (*ptr == ',' || *ptr == ' ') 110 ptr++; 111 112 v = strtoul(ptr, &endptr, 10); 113 if (ptr == endptr) 114 return (-1); 115 if (v > 255) 116 return (-1); 117 *b = v & 0xff; 118 ptr = endptr; 119 120 /* skip trailing spaces */ 121 while (*ptr == ' ') 122 ptr++; 123 124 /* unexpected characters at the end of the string */ 125 if (*ptr != 0) 126 return (-1); 127 128 return (0); 129 } 130 131 static void 132 vt_palette_init(void) 133 { 134 int i; 135 char rgb[32]; 136 char tunable[32]; 137 unsigned char r, g, b; 138 139 for (i = 0; i < NCOLORS; i++) { 140 snprintf(tunable, sizeof(tunable), 141 "kern.vt.color.%d.rgb", i); 142 if (TUNABLE_STR_FETCH(tunable, rgb, sizeof(rgb))) { 143 if (vt_parse_rgb_triplet(rgb, &r, &g, &b) == 0) { 144 /* convert to percentages */ 145 color_def[i].r = r*100/255; 146 color_def[i].g = g*100/255; 147 color_def[i].b = b*100/255; 148 } 149 } 150 } 151 } 152 153 static int 154 vt_generate_cons_palette(uint32_t *palette, int format, uint32_t rmax, 155 int roffset, uint32_t gmax, int goffset, uint32_t bmax, int boffset) 156 { 157 int i; 158 159 switch (format) { 160 case COLOR_FORMAT_VGA: 161 for (i = 0; i < NCOLORS; i++) 162 palette[i] = cons_to_vga_colors[i]; 163 break; 164 case COLOR_FORMAT_RGB: 165 vt_palette_init(); 166 #define CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset) 167 for (i = 0; i < NCOLORS; i++) 168 palette[i] = CF(r, i) | CF(g, i) | CF(b, i); 169 #undef CF 170 break; 171 default: 172 return (ENODEV); 173 } 174 175 return (0); 176 } 177 178 int 179 vt_config_cons_colors(struct fb_info *info, int format, uint32_t rmax, 180 int roffset, uint32_t gmax, int goffset, uint32_t bmax, int boffset) 181 { 182 if (format == COLOR_FORMAT_RGB) { 183 info->fb_rgboffs.red = roffset; 184 info->fb_rgboffs.green = goffset; 185 info->fb_rgboffs.blue = boffset; 186 } else 187 memset(&info->fb_rgboffs, 0, sizeof(info->fb_rgboffs)); 188 189 return (vt_generate_cons_palette(info->fb_cmap, format, rmax, 190 roffset, gmax, goffset, bmax, boffset)); 191 } 192