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