printer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (C) 2014 Olivier Marty <olivier.marty.m@gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "printer.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include <string.h>
  21. struct printerEnv printerOpenWindow(int width, int height, int padding_bottom)
  22. {
  23. struct printerEnv env;
  24. // open connection
  25. env.d = XOpenDisplay(NULL);
  26. if (env.d == NULL) {
  27. fprintf(stderr, "Unable to open display\n");
  28. exit(1);
  29. }
  30. env.s = DefaultScreen(env.d);
  31. XVisualInfo vinfo;
  32. XMatchVisualInfo(env.d, env.s, 32, TrueColor, &vinfo);
  33. // size of the screen
  34. XWindowAttributes RootAttr;
  35. XGetWindowAttributes(env.d, RootWindow(env.d, env.s), &RootAttr);
  36. // set window attributes
  37. XSetWindowAttributes attr;
  38. attr.colormap = XCreateColormap(env.d, RootWindow(env.d, env.s),
  39. vinfo.visual, AllocNone);
  40. attr.border_pixel = 0;
  41. attr.background_pixel = 0;
  42. attr.override_redirect = 1; // no window manager border
  43. // set colors
  44. env.color_text = XWhitePixel(env.d, env.s);
  45. XColor tmp;
  46. XParseColor(env.d, attr.colormap, "#222222", &tmp);
  47. XAllocColor(env.d, attr.colormap, &tmp);
  48. env.color_background = tmp.pixel;
  49. //env.color_text = -1;
  50. //env.color_background = (100 * 256 + 100) * 256 + 100;
  51. // create the window
  52. env.width = (width < 0) ? RootAttr.width : width;
  53. env.height = height;
  54. env.w = XCreateWindow(env.d, RootWindow(env.d, env.s),
  55. (RootAttr.width - env.width)/2,
  56. RootAttr.height - env.height - padding_bottom, env.width, env.height, 0,
  57. vinfo.depth, InputOutput, vinfo.visual,
  58. CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attr);
  59. // set the font in a new GC
  60. XGCValues gr_values;
  61. env.fontinfo = XLoadQueryFont(env.d, "*x24");
  62. gr_values.font = env.fontinfo->fid;
  63. gr_values.foreground = XWhitePixel(env.d, env.s);
  64. gr_values.background = XBlackPixel(env.d, env.s);
  65. env.gc = XCreateGC(env.d, env.w, GCFont | GCForeground, &gr_values);
  66. // event to be listened
  67. //XSelectInput(env.d, env.w, ExposureMask | KeyPressMask);
  68. // display the window
  69. XMapWindow(env.d, env.w);
  70. XFlush(env.d);
  71. return env;
  72. }
  73. void printerCloseWindow(struct printerEnv env)
  74. {
  75. XCloseDisplay(env.d); // window is destroyed
  76. }
  77. int printLines(struct printerEnv env, char *text, int gap,
  78. int width, int height, int padding) // for the frame
  79. {
  80. if(*text == '\0')
  81. {
  82. XSetForeground(env.d, env.gc, env.color_background);
  83. XFillRectangle(env.d, env.w, env.gc, (env.width - width)/2 - padding,
  84. env.height - height - padding, width + 2*padding, height + 2*padding);
  85. XSetForeground(env.d, env.gc, env.color_text);
  86. return env.height - padding;
  87. }
  88. char *next;
  89. int size;
  90. next = strchr(text, '\n');
  91. if(next == NULL)
  92. {
  93. size = strlen(text);
  94. next = text + size;
  95. }
  96. else
  97. {
  98. size = next-text;
  99. if(*(next-1) == '\r')
  100. size--; // hide this character
  101. next++; // forget \n
  102. }
  103. // compute the size of the text
  104. int font_direction, font_ascent, font_descent, text_y;
  105. XCharStruct text_structure;
  106. XTextExtents(env.fontinfo, text, size,
  107. &font_direction, &font_ascent, &font_descent,
  108. &text_structure);
  109. // print next line, and this one above
  110. if(text_structure.width > width)
  111. width = text_structure.width;
  112. text_y = printLines(env, next, gap, width,
  113. height + text_structure.ascent + text_structure.descent + gap, padding);
  114. text_y -= text_structure.descent;
  115. XDrawString(env.d, env.w, env.gc, (env.width - text_structure.width)/2,
  116. text_y, text, size);
  117. return text_y - text_structure.ascent - gap;
  118. }
  119. void printerShow(struct printerEnv env, char* text)
  120. {
  121. XClearWindow(env.d, env.w);
  122. printLines(env, text, 5, 0, 0, 5);
  123. XFlush(env.d);
  124. }
  125. void printerClean(struct printerEnv env)
  126. {
  127. XClearWindow(env.d, env.w);
  128. XFlush(env.d);
  129. }