font-server.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <unistd.h>
  5. #include <syscall.h>
  6. #include <toaru/trace.h>
  7. #define TRACE_APP_NAME "font-server"
  8. #define FONT_PATH "/usr/share/fonts/"
  9. #define FONT(a,b) {a, FONT_PATH b}
  10. struct font_def {
  11. char * identifier;
  12. char * path;
  13. };
  14. static struct font_def fonts[] = {
  15. FONT("sans-serif", "DejaVuSans.ttf"),
  16. FONT("sans-serif.bold", "DejaVuSans-Bold.ttf"),
  17. FONT("sans-serif.italic", "DejaVuSans-Oblique.ttf"),
  18. FONT("sans-serif.bolditalic", "DejaVuSans-BoldOblique.ttf"),
  19. FONT("monospace", "DejaVuSansMono.ttf"),
  20. FONT("monospace.bold", "DejaVuSansMono-Bold.ttf"),
  21. FONT("monospace.italic", "DejaVuSansMono-Oblique.ttf"),
  22. FONT("monospace.bolditalic", "DejaVuSansMono-BoldOblique.ttf"),
  23. {NULL, NULL}
  24. };
  25. /**
  26. * Preload a font into the font cache.
  27. *
  28. * TODO This should probably be moved out of the compositor,
  29. * perhaps into a generic resource cache daemon. This
  30. * is mostly kept this way for legacy reasons - the old
  31. * compositor did it, but it was also using some of the
  32. * fonts for internal rendering. We don't draw any text.
  33. */
  34. static char * precache_shmfont(char * ident, char * name) {
  35. FILE * f = fopen(name, "r");
  36. if (!f) return NULL;
  37. size_t s = 0;
  38. fseek(f, 0, SEEK_END);
  39. s = ftell(f);
  40. fseek(f, 0, SEEK_SET);
  41. size_t shm_size = s;
  42. char * font = (char *)syscall_shm_obtain(ident, &shm_size);
  43. assert((shm_size >= s) && "shm_obtain returned too little memory to load a font into!");
  44. fread(font, s, 1, f);
  45. fclose(f);
  46. return font;
  47. }
  48. /**
  49. * Load all of the fonts into the cache.
  50. */
  51. static void load_fonts(char * server) {
  52. int i = 0;
  53. while (fonts[i].identifier) {
  54. char tmp[100];
  55. sprintf(tmp, "sys.%s.fonts.%s", server, fonts[i].identifier);
  56. TRACE("Loading font %s -> %s", fonts[i].path, tmp);
  57. if (!precache_shmfont(tmp, fonts[i].path)) {
  58. TRACE(" ... failed.");
  59. }
  60. ++i;
  61. }
  62. }
  63. int main(int argc, char * argv[]) {
  64. load_fonts(getenv("DISPLAY"));
  65. while (1) {
  66. usleep(100000);
  67. }
  68. return 0;
  69. }