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/cdefs.h> 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/libkern.h> 35 #include <sys/fbio.h> 36 37 #include <dev/vt/colors/vt_termcolors.h> 38 39 static struct { 40 unsigned char r; /* Red percentage value. */ 41 unsigned char g; /* Green percentage value. */ 42 unsigned char b; /* Blue percentage value. */ 43 } color_def[NCOLORS] = { 44 {0, 0, 0}, /* black */ 45 {50, 0, 0}, /* dark red */ 46 {0, 50, 0}, /* dark green */ 47 {77, 63, 0}, /* dark yellow */ 48 {20, 40, 64}, /* dark blue */ 49 {50, 0, 50}, /* dark magenta */ 50 {0, 50, 50}, /* dark cyan */ 51 {75, 75, 75}, /* light gray */ 52 53 {18, 20, 21}, /* dark gray */ 54 {100, 0, 0}, /* light red */ 55 {0, 100, 0}, /* light green */ 56 {100, 100, 0}, /* light yellow */ 57 {45, 62, 81}, /* light blue */ 58 {100, 0, 100}, /* light magenta */ 59 {0, 100, 100}, /* light cyan */ 60 {100, 100, 100}, /* white */ 61 }; 62 63 static int 64 vt_parse_rgb_triplet(const char *rgb, unsigned char *r, 65 unsigned char *g, unsigned char *b) 66 { 67 unsigned long v; 68 const char *ptr; 69 char *endptr; 70 71 ptr = rgb; 72 73 /* Handle #rrggbb case */ 74 if (*ptr == '#') { 75 if (strlen(ptr) != 7) 76 return (-1); 77 v = strtoul(ptr + 1, &endptr, 16); 78 if (*endptr != '\0') 79 return (-1); 80 81 *r = (v >> 16) & 0xff; 82 *g = (v >> 8) & 0xff; 83 *b = v & 0xff; 84 85 return (0); 86 } 87 88 /* "r, g, b" case */ 89 v = strtoul(ptr, &endptr, 10); 90 if (ptr == endptr) 91 return (-1); 92 if (v > 255) 93 return (-1); 94 *r = v & 0xff; 95 ptr = endptr; 96 97 /* skip separator */ 98 while (*ptr == ',' || *ptr == ' ') 99 ptr++; 100 101 v = strtoul(ptr, &endptr, 10); 102 if (ptr == endptr) 103 return (-1); 104 if (v > 255) 105 return (-1); 106 *g = v & 0xff; 107 ptr = endptr; 108 109 /* skip separator */ 110 while (*ptr == ',' || *ptr == ' ') 111 ptr++; 112 113 v = strtoul(ptr, &endptr, 10); 114 if (ptr == endptr) 115 return (-1); 116 if (v > 255) 117 return (-1); 118 *b = v & 0xff; 119 ptr = endptr; 120 121 /* skip trailing spaces */ 122 while (*ptr == ' ') 123 ptr++; 124 125 /* unexpected characters at the end of the string */ 126 if (*ptr != 0) 127 return (-1); 128 129 return (0); 130 } 131 132 static void 133 vt_palette_init(void) 134 { 135 int i; 136 char rgb[32]; 137 char tunable[32]; 138 unsigned char r, g, b; 139 140 for (i = 0; i < NCOLORS; i++) { 141 snprintf(tunable, sizeof(tunable), 142 "kern.vt.color.%d.rgb", i); 143 if (TUNABLE_STR_FETCH(tunable, rgb, sizeof(rgb))) { 144 if (vt_parse_rgb_triplet(rgb, &r, &g, &b) == 0) { 145 /* convert to percentages */ 146 color_def[i].r = r*100/255; 147 color_def[i].g = g*100/255; 148 color_def[i].b = b*100/255; 149 } 150 } 151 } 152 } 153 154 static int 155 vt_generate_cons_palette(uint32_t *palette, int format, uint32_t rmax, 156 int roffset, uint32_t gmax, int goffset, uint32_t bmax, int boffset) 157 { 158 int i; 159 160 switch (format) { 161 case COLOR_FORMAT_VGA: 162 for (i = 0; i < NCOLORS; i++) 163 palette[i] = cons_to_vga_colors[i]; 164 break; 165 case COLOR_FORMAT_RGB: 166 vt_palette_init(); 167 #define CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset) 168 for (i = 0; i < NCOLORS; i++) 169 palette[i] = CF(r, i) | CF(g, i) | CF(b, i); 170 #undef CF 171 break; 172 default: 173 return (ENODEV); 174 } 175 176 return (0); 177 } 178 179 int 180 vt_config_cons_colors(struct fb_info *info, int format, uint32_t rmax, 181 int roffset, uint32_t gmax, int goffset, uint32_t bmax, int boffset) 182 { 183 if (format == COLOR_FORMAT_RGB) { 184 info->fb_rgboffs.red = roffset; 185 info->fb_rgboffs.green = goffset; 186 info->fb_rgboffs.blue = boffset; 187 } else 188 memset(&info->fb_rgboffs, 0, sizeof(info->fb_rgboffs)); 189 190 return (vt_generate_cons_palette(info->fb_cmap, format, rmax, 191 roffset, gmax, goffset, bmax, boffset)); 192 } 193