font-server.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 mainline ToaruOS. In theory, with the fonts
  10. * installed, this could be used to provide fonts for legacy
  11. * mainline applications in ToaruOS-NIH, but this is untested.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <unistd.h>
  17. #include <syscall.h>
  18. #include <toaru/trace.h>
  19. #define TRACE_APP_NAME "font-server"
  20. #define FONT_PATH "/usr/share/fonts/"
  21. #define FONT(a,b) {a, FONT_PATH b}
  22. struct font_def {
  23. char * identifier;
  24. char * path;
  25. };
  26. static struct font_def fonts[] = {
  27. FONT("sans-serif", "DejaVuSans.ttf"),
  28. FONT("sans-serif.bold", "DejaVuSans-Bold.ttf"),
  29. FONT("sans-serif.italic", "DejaVuSans-Oblique.ttf"),
  30. FONT("sans-serif.bolditalic", "DejaVuSans-BoldOblique.ttf"),
  31. FONT("monospace", "DejaVuSansMono.ttf"),
  32. FONT("monospace.bold", "DejaVuSansMono-Bold.ttf"),
  33. FONT("monospace.italic", "DejaVuSansMono-Oblique.ttf"),
  34. FONT("monospace.bolditalic", "DejaVuSansMono-BoldOblique.ttf"),
  35. {NULL, NULL}
  36. };
  37. /**
  38. * Preload a font into the font cache.
  39. */
  40. static char * precache_shmfont(char * ident, char * name) {
  41. FILE * f = fopen(name, "r");
  42. if (!f) return NULL;
  43. size_t s = 0;
  44. fseek(f, 0, SEEK_END);
  45. s = ftell(f);
  46. fseek(f, 0, SEEK_SET);
  47. size_t shm_size = s;
  48. char * font = (char *)syscall_shm_obtain(ident, &shm_size);
  49. assert((shm_size >= s) && "shm_obtain returned too little memory to load a font into!");
  50. fread(font, s, 1, f);
  51. fclose(f);
  52. return font;
  53. }
  54. /**
  55. * Load all of the fonts into the cache.
  56. */
  57. static void load_fonts(char * server) {
  58. int i = 0;
  59. while (fonts[i].identifier) {
  60. char tmp[100];
  61. sprintf(tmp, "sys.%s.fonts.%s", server, fonts[i].identifier);
  62. TRACE("Loading font %s -> %s", fonts[i].path, tmp);
  63. if (!precache_shmfont(tmp, fonts[i].path)) {
  64. TRACE(" ... failed.");
  65. }
  66. ++i;
  67. }
  68. }
  69. int main(int argc, char * argv[]) {
  70. load_fonts(getenv("DISPLAY"));
  71. while (1) {
  72. usleep(100000);
  73. }
  74. return 0;
  75. }