Xlib_textfader.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. *
  3. * Copyright (c) 2002, Marc Bruenink
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of the author nor the names of his contributors may be
  16. * used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _XLIB_TEXTFADER_CPP_
  32. #define _XLIB_TEXTFADER_CPP_
  33. #include "Xlib_textfader.h"
  34. #include <time.h> // test
  35. Xlib_textfader::Xlib_textfader(){
  36. dpy = XOpenDisplay(NULL);
  37. screen = DefaultScreen(dpy);
  38. screenWidth = XDisplayWidth(dpy, screen);
  39. screenHeight = XDisplayHeight(dpy, screen);
  40. screenDepth = DefaultDepth(dpy, screen);
  41. screenVisual = DefaultVisual(dpy,screen);
  42. font = NULL;
  43. attrValueMask = 0;
  44. gc_valValueMask = 0;
  45. // don't use the window manager
  46. attr.override_redirect = True;
  47. attrValueMask |= CWOverrideRedirect;
  48. }
  49. Xlib_textfader::~Xlib_textfader(){
  50. XFreeFont(dpy, font);
  51. XCloseDisplay(dpy);
  52. }
  53. int Xlib_textfader::fade(char* text, int iTextLength, unsigned int steps,unsigned long optionmask=0){
  54. if (font == NULL) return 0;
  55. Window win;
  56. XEvent event;
  57. //int pos_y=0, pos_x=0;
  58. unsigned long pix;
  59. int iRand;
  60. XImage *backImg, *dstImg;
  61. int iWindowWidth, iWindowHeight;
  62. int iModCount = 0;
  63. // init vars
  64. iWindowWidth = XTextWidth(font, text, iTextLength);
  65. iWindowHeight = get_font_height();
  66. XSync(dpy, False);
  67. if (optionmask & (1<<1)) {
  68. // TF_WINDOW_CENTER
  69. pos_y = screenHeight / 2;
  70. pos_x = screenWidth / 2;
  71. optionmask |= (1<<2);
  72. pos_y -= iWindowHeight / 2;
  73. pos_x -= iWindowWidth / 2;
  74. }
  75. switch(TF_COORDINATES_GET(optionmask)) {
  76. case TF_COORDINATES_UPPER_LEFT:
  77. // nothing to change:
  78. break;
  79. case TF_COORDINATES_UPPER_CENTER:
  80. pos_x -= iWindowWidth /2;
  81. break;
  82. case TF_COORDINATES_UPPER_RIGHT:
  83. pos_x -= iWindowWidth;
  84. break;
  85. case TF_COORDINATES_CENTER_LEFT:
  86. pos_y -= iWindowHeight / 2;
  87. break;
  88. case TF_COORDINATES_CENTER_CENTER:
  89. pos_y -= iWindowHeight / 2;
  90. pos_x -= iWindowWidth /2;
  91. break;
  92. case TF_COORDINATES_CENTER_RIGHT:
  93. pos_y -= iWindowHeight / 2;
  94. pos_x -= iWindowWidth;
  95. break;
  96. case TF_COORDINATES_LOWER_LEFT:
  97. pos_y -= iWindowHeight;
  98. break;
  99. case TF_COORDINATES_LOWER_CENTER:
  100. pos_y -= iWindowHeight;
  101. pos_x -= iWindowWidth /2;
  102. break;
  103. case TF_COORDINATES_LOWER_RIGHT:
  104. pos_y -= iWindowHeight;
  105. pos_x -= iWindowWidth;
  106. break;
  107. }
  108. // if you want to fade there... ok its your problem, but i have NOT to
  109. // fade and produce a Bad Match so i say hey i've faded and return 1
  110. if ((pos_x >= screenWidth) || (pos_y >=screenHeight)) {
  111. return 1;
  112. }
  113. // perhaps i've to code a better solution with half seeable windows
  114. // in the left and top
  115. if((pos_x < 0 ) || (pos_y < 0)) {
  116. return 0;
  117. }
  118. if ((pos_x+iWindowWidth) > screenWidth) {
  119. iWindowWidth = screenWidth - pos_x;
  120. }
  121. if ((pos_y + iWindowHeight) > screenHeight) {
  122. iWindowHeight = screenHeight - pos_y;
  123. }
  124. win = XCreateWindow(dpy, DefaultRootWindow(dpy), pos_x, pos_y, iWindowWidth, iWindowHeight, 0, screenDepth, InputOutput, screenVisual, attrValueMask, &attr);
  125. // m o v e your ass!!
  126. XSelectInput(dpy, win, ExposureMask);
  127. GC gc = XCreateGC(dpy, win, gc_valValueMask, &gc_val);
  128. XFlush(dpy);
  129. XMapWindow(dpy, win);
  130. XFlush(dpy);
  131. while(1){
  132. XNextEvent(dpy,&event);
  133. switch(event.type){
  134. case Expose:
  135. // save background
  136. // backImg = XGetImage(dpy, win, 0, 0, iWindowWidth, iWindowHeight, AllPlanes, XYPixmap);
  137. XDrawString(dpy,win,gc,0 ,get_font_height(), text, iTextLength);
  138. XSync(dpy,False);
  139. // dstImg = XGetImage(dpy, win, 0, 0, iWindowWidth, iWindowHeight, AllPlanes,XYPixmap);
  140. sleep(steps/100);
  141. /*
  142. for(int iCountSteps = 0;iCountSteps < steps; iCountSteps++) {
  143. for (int y=0; y< iWindowHeight ;y++) {
  144. for (int x=0; x< iWindowWidth ; x++) {
  145. // print pixel??
  146. pix = XGetPixel(dstImg, x ,y);
  147. if (pix == gc_val.foreground) {
  148. if ((rand() % (int)(steps - iCountSteps))==0) {}
  149. //XPutPixel(dstImg, x, y, XGetPixel(backImg, x, y));
  150. }
  151. }
  152. }
  153. //XPutImage(dpy, win, gc, dstImg, 0,0,0,0,iTextLength * get_font_width(), get_font_height());
  154. }
  155. */
  156. // XDestroyImage(dstImg);
  157. // XDestroyImage(backImg);
  158. XFreeGC(dpy ,gc);
  159. XDestroyWindow(dpy,win);
  160. XSync(dpy,False);
  161. return True;
  162. }
  163. }
  164. // }// end test for
  165. return True;
  166. }
  167. int Xlib_textfader::get_font_height(){
  168. return font->max_bounds.ascent + font->max_bounds.descent;
  169. }
  170. int Xlib_textfader::get_font_width() {
  171. return font->max_bounds.rbearing + font->max_bounds.lbearing;
  172. }
  173. int Xlib_textfader::set_font(char *name){
  174. // if (font != NULL) XFreeFont(dpy,font);
  175. font = XLoadQueryFont(dpy, name);
  176. if (font == NULL)
  177. return 0;
  178. gc_val.font = font->fid;
  179. gc_valValueMask |= GCFont;
  180. XSync(dpy,False);
  181. return 1;
  182. }
  183. char** Xlib_textfader::get_list_fonts(int* count, char *pattern="*"){
  184. return XListFonts(dpy, "*", INT_MAX, count);
  185. }
  186. int Xlib_textfader::set_text_color(char *name) {
  187. XColor exactColor;
  188. int ret;
  189. ret = XAllocNamedColor(dpy, DefaultColormap(dpy, screen), name, &textColor, &exactColor);
  190. if (ret) {
  191. gc_val.foreground = textColor.pixel;
  192. gc_valValueMask |= GCForeground;
  193. }
  194. return ret;
  195. }
  196. int Xlib_textfader::set_text_color(double red, double green, double blue) {
  197. XColor col;
  198. int ret;
  199. col.red = (unsigned short)(65535 * red);
  200. col.green = (unsigned short)(65535 * green);
  201. col.blue = (unsigned short)(65535 * blue);
  202. ret = XAllocColor(dpy, DefaultColormap(dpy, 0), &col);
  203. if (ret) {
  204. gc_val.foreground = col.pixel;
  205. gc_valValueMask |= GCForeground;
  206. }
  207. return ret;
  208. }
  209. #endif