font-server.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * font-server - Provides shared-memory fonts.
  7. *
  8. * This is an implementation of the shared memory font server
  9. * from Yutani in ToaruOS 1.2.x. It allows applications to
  10. * make use of the Freetype font rendering backend by providing
  11. * a common set of font files.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <unistd.h>
  17. #include <sys/shm.h>
  18. #if 0
  19. #include <toaru/trace.h>
  20. #define TRACE_APP_NAME "font-server"
  21. #else
  22. #define TRACE(...)
  23. #endif
  24. #define FONT_PATH "/usr/share/fonts/"
  25. #define FONT(a,b) {a, FONT_PATH b}
  26. struct font_def {
  27. char * identifier;
  28. char * path;
  29. };
  30. static struct font_def fonts[] = {
  31. FONT("sans-serif", "DejaVuSans.ttf"),
  32. FONT("sans-serif.bold", "DejaVuSans-Bold.ttf"),
  33. FONT("sans-serif.italic", "DejaVuSans-Oblique.ttf"),
  34. FONT("sans-serif.bolditalic", "DejaVuSans-BoldOblique.ttf"),
  35. FONT("monospace", "DejaVuSansMono.ttf"),
  36. FONT("monospace.bold", "DejaVuSansMono-Bold.ttf"),
  37. FONT("monospace.italic", "DejaVuSansMono-Oblique.ttf"),
  38. FONT("monospace.bolditalic", "DejaVuSansMono-BoldOblique.ttf"),
  39. {NULL, NULL}
  40. };
  41. /**
  42. * Preload a font into the font cache.
  43. */
  44. static char * precache_shmfont(char * ident, char * name) {
  45. FILE * f = fopen(name, "r");
  46. if (!f) return NULL;
  47. size_t s = 0;
  48. fseek(f, 0, SEEK_END);
  49. s = ftell(f);
  50. fseek(f, 0, SEEK_SET);
  51. size_t shm_size = s;
  52. char * font = shm_obtain(ident, &shm_size);
  53. assert((shm_size >= s) && "shm_obtain returned too little memory to load a font into!");
  54. fread(font, s, 1, f);
  55. fclose(f);
  56. return font;
  57. }
  58. /**
  59. * Load all of the fonts into the cache.
  60. */
  61. static void load_fonts(char * server) {
  62. int i = 0;
  63. while (fonts[i].identifier) {
  64. char tmp[100];
  65. sprintf(tmp, "sys.%s.fonts.%s", server, fonts[i].identifier);
  66. TRACE("Loading font %s -> %s", fonts[i].path, tmp);
  67. if (!precache_shmfont(tmp, fonts[i].path)) {
  68. TRACE(" ... failed.");
  69. }
  70. ++i;
  71. }
  72. }
  73. int main(int argc, char * argv[]) {
  74. /* Daemonize */
  75. if (!fork()) {
  76. load_fonts("fonts");
  77. while (1) {
  78. usleep(100000);
  79. }
  80. return 0;
  81. }
  82. return 0;
  83. }