123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /**
- *
- * Copyright (c) 2002, Marc Bruenink
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the names of his contributors may be
- * used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- #ifndef _XLIB_TEXTFADER_CPP_
- #define _XLIB_TEXTFADER_CPP_
- #include "Xlib_textfader.h"
- #include <time.h> // test
-
- Xlib_textfader::Xlib_textfader(){
-
- dpy = XOpenDisplay(NULL);
- screen = DefaultScreen(dpy);
- screenWidth = XDisplayWidth(dpy, screen);
- screenHeight = XDisplayHeight(dpy, screen);
- screenDepth = DefaultDepth(dpy, screen);
- screenVisual = DefaultVisual(dpy,screen);
- font = NULL;
- attrValueMask = 0;
- gc_valValueMask = 0;
-
- // don't use the window manager
- attr.override_redirect = True;
- attrValueMask |= CWOverrideRedirect;
-
- }
-
- Xlib_textfader::~Xlib_textfader(){
- XFreeFont(dpy, font);
-
- XCloseDisplay(dpy);
- }
-
- int Xlib_textfader::fade(char* text, int iTextLength, unsigned int steps,unsigned long optionmask=0){
- if (font == NULL) return 0;
- Window win;
- XEvent event;
- //int pos_y=0, pos_x=0;
- unsigned long pix;
- int iRand;
- XImage *backImg, *dstImg;
- int iWindowWidth, iWindowHeight;
- int iModCount = 0;
- // init vars
- iWindowWidth = XTextWidth(font, text, iTextLength);
- iWindowHeight = get_font_height();
- XSync(dpy, False);
-
- if (optionmask & (1<<1)) {
- // TF_WINDOW_CENTER
- pos_y = screenHeight / 2;
- pos_x = screenWidth / 2;
- optionmask |= (1<<2);
- pos_y -= iWindowHeight / 2;
- pos_x -= iWindowWidth / 2;
- }
- switch(TF_COORDINATES_GET(optionmask)) {
- case TF_COORDINATES_UPPER_LEFT:
- // nothing to change:
- break;
- case TF_COORDINATES_UPPER_CENTER:
- pos_x -= iWindowWidth /2;
- break;
- case TF_COORDINATES_UPPER_RIGHT:
- pos_x -= iWindowWidth;
- break;
- case TF_COORDINATES_CENTER_LEFT:
- pos_y -= iWindowHeight / 2;
- break;
- case TF_COORDINATES_CENTER_CENTER:
- pos_y -= iWindowHeight / 2;
- pos_x -= iWindowWidth /2;
- break;
- case TF_COORDINATES_CENTER_RIGHT:
- pos_y -= iWindowHeight / 2;
- pos_x -= iWindowWidth;
- break;
- case TF_COORDINATES_LOWER_LEFT:
- pos_y -= iWindowHeight;
- break;
- case TF_COORDINATES_LOWER_CENTER:
- pos_y -= iWindowHeight;
- pos_x -= iWindowWidth /2;
- break;
- case TF_COORDINATES_LOWER_RIGHT:
- pos_y -= iWindowHeight;
- pos_x -= iWindowWidth;
- break;
- }
- // if you want to fade there... ok its your problem, but i have NOT to
- // fade and produce a Bad Match so i say hey i've faded and return 1
- if ((pos_x >= screenWidth) || (pos_y >=screenHeight)) {
- return 1;
- }
- // perhaps i've to code a better solution with half seeable windows
- // in the left and top
- if((pos_x < 0 ) || (pos_y < 0)) {
- return 0;
- }
- if ((pos_x+iWindowWidth) > screenWidth) {
- iWindowWidth = screenWidth - pos_x;
- }
- if ((pos_y + iWindowHeight) > screenHeight) {
- iWindowHeight = screenHeight - pos_y;
- }
-
- win = XCreateWindow(dpy, DefaultRootWindow(dpy), pos_x, pos_y, iWindowWidth, iWindowHeight, 0, screenDepth, InputOutput, screenVisual, attrValueMask, &attr);
- // m o v e your ass!!
-
- XSelectInput(dpy, win, ExposureMask);
-
- GC gc = XCreateGC(dpy, win, gc_valValueMask, &gc_val);
-
- XFlush(dpy);
- XMapWindow(dpy, win);
- XFlush(dpy);
- while(1){
- XNextEvent(dpy,&event);
- switch(event.type){
- case Expose:
- // save background
- // backImg = XGetImage(dpy, win, 0, 0, iWindowWidth, iWindowHeight, AllPlanes, XYPixmap);
- XDrawString(dpy,win,gc,0 ,get_font_height(), text, iTextLength);
- XSync(dpy,False);
- // dstImg = XGetImage(dpy, win, 0, 0, iWindowWidth, iWindowHeight, AllPlanes,XYPixmap);
- sleep(steps/100);
- /*
- for(int iCountSteps = 0;iCountSteps < steps; iCountSteps++) {
- for (int y=0; y< iWindowHeight ;y++) {
- for (int x=0; x< iWindowWidth ; x++) {
- // print pixel??
- pix = XGetPixel(dstImg, x ,y);
- if (pix == gc_val.foreground) {
- if ((rand() % (int)(steps - iCountSteps))==0) {}
- //XPutPixel(dstImg, x, y, XGetPixel(backImg, x, y));
- }
- }
- }
- //XPutImage(dpy, win, gc, dstImg, 0,0,0,0,iTextLength * get_font_width(), get_font_height());
- }
- */
- // XDestroyImage(dstImg);
- // XDestroyImage(backImg);
- XFreeGC(dpy ,gc);
- XDestroyWindow(dpy,win);
- XSync(dpy,False);
- return True;
- }
- }
- // }// end test for
- return True;
- }
- int Xlib_textfader::get_font_height(){
- return font->max_bounds.ascent + font->max_bounds.descent;
- }
- int Xlib_textfader::get_font_width() {
- return font->max_bounds.rbearing + font->max_bounds.lbearing;
- }
- int Xlib_textfader::set_font(char *name){
- // if (font != NULL) XFreeFont(dpy,font);
- font = XLoadQueryFont(dpy, name);
- if (font == NULL)
- return 0;
- gc_val.font = font->fid;
- gc_valValueMask |= GCFont;
- XSync(dpy,False);
- return 1;
- }
- char** Xlib_textfader::get_list_fonts(int* count, char *pattern="*"){
- return XListFonts(dpy, "*", INT_MAX, count);
- }
- int Xlib_textfader::set_text_color(char *name) {
- XColor exactColor;
- int ret;
-
- ret = XAllocNamedColor(dpy, DefaultColormap(dpy, screen), name, &textColor, &exactColor);
- if (ret) {
- gc_val.foreground = textColor.pixel;
- gc_valValueMask |= GCForeground;
- }
- return ret;
- }
- int Xlib_textfader::set_text_color(double red, double green, double blue) {
- XColor col;
- int ret;
- col.red = (unsigned short)(65535 * red);
- col.green = (unsigned short)(65535 * green);
- col.blue = (unsigned short)(65535 * blue);
- ret = XAllocColor(dpy, DefaultColormap(dpy, 0), &col);
- if (ret) {
- gc_val.foreground = col.pixel;
- gc_valValueMask |= GCForeground;
- }
- return ret;
- }
- #endif
|