~ubuntu-branches/ubuntu/dapper/xscreensaver/dapper-updates

« back to all changes in this revision

Viewing changes to hacks/xflame.c

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Hildebrandt
  • Date: 2005-04-09 00:06:43 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050409000643-z0abtifbt9s20pcc
Tags: 4.21-3
Patch by Joachim Breitner to check more frequently if DPMS kicked in (closes: #303374, #286664).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* xflame, Copyright (c) 1996-1999 Carsten Haitzler <raster@redhat.com>
 
1
/* xflame, Copyright (c) 1996-2002 Carsten Haitzler <raster@redhat.com>
2
2
 *
3
3
 * Permission to use, copy, modify, distribute, and sell this software and its
4
4
 * documentation for any purpose is hereby granted without fee, provided that
40
40
       - General cleanup and portability tweaks.
41
41
 
42
42
   * 4-Oct-99, jwz: added support for packed-24bpp (versus 32bpp.)
 
43
   * 16-Jan-2002, jwz: added gdk_pixbuf support.
43
44
 
44
45
 */
45
46
 
48
49
 
49
50
 
50
51
#include "screenhack.h"
 
52
#include "xpm-pixmap.h"
51
53
#include <X11/Xutil.h>
52
54
#include <limits.h>
53
55
 
 
56
#undef countof
 
57
#define countof(x) (sizeof((x))/sizeof((*x)))
 
58
 
54
59
#ifdef HAVE_XSHM_EXTENSION
55
60
# include "xshm.h"
56
61
#endif /* HAVE_XSHM_EXTENSION */
57
62
 
58
 
#ifdef HAVE_XPM
59
 
# include <X11/xpm.h>
60
 
# ifndef PIXEL_ALREADY_TYPEDEFED
61
 
#  define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
62
 
# endif
63
 
#endif
64
 
 
65
 
#ifdef HAVE_XMU
66
 
# ifndef VMS
67
 
#  include <X11/Xmu/Drawing.h>
68
 
# else  /* VMS */
69
 
#  include <Xmu/Drawing.h>
70
 
# endif /* VMS */
71
 
#endif /* HAVE_XMU */
72
 
 
73
63
#include "images/bob.xbm"
74
64
 
75
65
#define MAX_VAL             255
81
71
static int             height;
82
72
static Colormap        colormap;
83
73
static Visual          *visual;
 
74
static Screen          *screen;
84
75
static Bool            shared;
85
76
static Bool            bloom;
86
77
static XImage          *xim;
117
108
  colormap = xwa.colormap;
118
109
  depth    = xwa.depth;
119
110
  visual   = xwa.visual;
 
111
  screen   = xwa.screen;
120
112
  width    = xwa.width;
121
113
  height   = xwa.height;
122
114
 
640
632
    }
641
633
  else  /* load a bitmap file */
642
634
    {
643
 
#ifdef HAVE_XPM
644
 
      XpmInfo xpm_info = { 0, };
645
 
      XpmImage xpm_image = { 0, };
646
 
 
647
 
      int result = XpmReadFileToXpmImage (bitmap_name, &xpm_image, &xpm_info);
648
 
      if (result == XpmSuccess)
649
 
        {
650
 
          int x, y;
651
 
          unsigned char *result, *o;
652
 
          unsigned char *grays;
653
 
          XWindowAttributes xgwa;
654
 
 
655
 
          *w = xpm_image.width;
656
 
          *h = xpm_image.height;
657
 
          result = (unsigned char *) malloc ((*w) * (*h));
658
 
          if (!result)
659
 
            {
660
 
              fprintf(stderr, "%s: out of memory loading %s\n",
661
 
                      progname, bitmap_name);
662
 
              exit (1);
663
 
            }
664
 
 
665
 
          XGetWindowAttributes (display, window, &xgwa);
666
 
 
667
 
          grays = (unsigned char *) calloc (xpm_image.ncolors+1, 1);
668
 
          for (x = 0; x < xpm_image.ncolors; x++)
669
 
            {
670
 
              XColor xc;
671
 
              XpmColor *xpmc = &xpm_image.colorTable[x];
672
 
              char *cstring = 0;
673
 
              if (xpmc->g_color && *xpmc->g_color)
674
 
                cstring = xpmc->g_color;
675
 
              else if (xpmc->g4_color && *xpmc->g4_color)
676
 
                cstring = xpmc->g4_color;
677
 
              else if (xpmc->c_color && *xpmc->c_color)
678
 
                cstring = xpmc->c_color;
679
 
              else
680
 
                cstring = xpmc->m_color;
681
 
 
682
 
              memset (&xc, 0, sizeof(xc));
683
 
              if (!cstring ||
684
 
                  !*cstring ||
685
 
                  !XParseColor (display, xgwa.colormap, cstring, &xc))
686
 
                grays[x] = 0;
687
 
              else
688
 
                grays[x] = (int) (((xc.red   * 0.299) +
689
 
                                   (xc.green * 0.587) +
690
 
                                   (xc.blue  * 0.114))
691
 
                                  / 255);
692
 
            }
693
 
 
694
 
          o = result;
695
 
          for (y = 0; y < *h; y++)
696
 
            for (x = 0; x < *w; x++)
697
 
              {
698
 
                int color = xpm_image.data[(y * (*w)) + x];
699
 
                if (color < 0 || color > xpm_image.ncolors) abort();
700
 
                *o++ = grays[color];
701
 
              }
702
 
          return result;
703
 
        }
704
 
      else      /* failed to read XPM -- fall through and try XBM */
705
 
#endif /* HAVE_XPM */
706
 
        {
707
 
#ifdef HAVE_XMU
708
 
          XImage *ximage = 0;
709
 
          int width, height, xh, yh;
710
 
          int x, y;
711
 
          unsigned char *result, *o;
712
 
          Pixmap bitmap =
713
 
            XmuLocateBitmapFile (DefaultScreenOfDisplay (display),
714
 
                                 bitmap_name, 0, 0, &width, &height, &xh, &yh);
715
 
          if (!bitmap)
716
 
            {
717
 
              fprintf(stderr, "%s: unable to load bitmap file %s\n",
718
 
                      progname, bitmap_name);
719
 
              exit (1);
720
 
            }
721
 
          ximage = XGetImage (display, bitmap, 0, 0, width, height,
722
 
                              1L, XYPixmap);
723
 
          XFreePixmap (display, bitmap);
724
 
 
725
 
          if (ximage->depth != 1) abort();
726
 
 
727
 
          *w = ximage->width;
728
 
          *h = ximage->height;
729
 
          result = (unsigned char *) malloc ((*w) * (*h));
730
 
          if (!result)
731
 
            {
732
 
              fprintf(stderr, "%s: out of memory loading %s\n",
733
 
                      progname, bitmap_name);
734
 
              exit (1);
735
 
            }
736
 
 
737
 
          o = result;
738
 
          for (y = 0; y < *h; y++)
739
 
            for (x = 0; x < *w; x++)
740
 
              *o++ = (XGetPixel(ximage, x, y) ? 255 : 0);
741
 
 
742
 
          return result;
743
 
 
744
 
#else  /* !XMU */
745
 
          fprintf (stderr,
746
 
                   "%s: your vendor doesn't ship the standard Xmu library.\n",
747
 
                   progname);
748
 
          fprintf (stderr, "\tWe can't load XBM files without it.\n");
749
 
          exit (1);
750
 
#endif /* !XMU */
751
 
        }
 
635
      Pixmap pixmap =
 
636
        xpm_file_to_pixmap (display, window, bitmap_name, &width, &height, 0);
 
637
      XImage *image;
 
638
      int x, y;
 
639
      unsigned char *result, *o;
 
640
      XColor colors[256];
 
641
      Bool cmap_p = has_writable_cells (screen, visual);
 
642
 
 
643
      if (cmap_p)
 
644
        {
 
645
          int i;
 
646
          for (i = 0; i < countof (colors); i++)
 
647
            colors[i].pixel = i;
 
648
          XQueryColors (display, colormap, colors, countof (colors));
 
649
        }
 
650
 
 
651
      image = XGetImage (display, pixmap, 0, 0, width, height, ~0L, ZPixmap);
 
652
      XFreePixmap(display, pixmap);
 
653
 
 
654
      result = (unsigned char *) malloc (width * height);
 
655
      o = result;
 
656
      for (y = 0; y < height; y++)
 
657
        for (x = 0; x < width; x++)
 
658
          {
 
659
            int rgba = XGetPixel (image, x, y);
 
660
            int gray;
 
661
            if (cmap_p)
 
662
              gray = ((200 - ((((colors[rgba].red   >> 8) & 0xFF) +
 
663
                              ((colors[rgba].green >> 8) & 0xFF) +
 
664
                              ((colors[rgba].blue  >> 8) & 0xFF))
 
665
                             >> 1))
 
666
                      & 0xFF);
 
667
            else
 
668
              /* This is *so* not handling all the cases... */
 
669
              gray = (image->depth > 16
 
670
                      ? ((((rgba >> 24) & 0xFF) +
 
671
                          ((rgba >> 16) & 0xFF) +
 
672
                          ((rgba >>  8) & 0xFF) +
 
673
                          ((rgba      ) & 0xFF)) >> 2)
 
674
                      : ((((rgba >> 12) & 0x0F) +
 
675
                          ((rgba >>  8) & 0x0F) +
 
676
                          ((rgba >>  4) & 0x0F) +
 
677
                          ((rgba      ) & 0x0F)) >> 1));
 
678
 
 
679
            *o++ = 255 - gray;
 
680
          }
 
681
 
 
682
      XFree (image->data);
 
683
      image->data = 0;
 
684
      XDestroyImage (image);
 
685
 
 
686
      *w = width;
 
687
      *h = height;
 
688
      return result;
752
689
    }
753
690
 
754
691
  *w = 0;
781
718
};
782
719
 
783
720
XrmOptionDescRec options [] = {
 
721
  { "-foreground",".foreground",     XrmoptionSepArg, 0 },
 
722
  { "-fg",        ".foreground",     XrmoptionSepArg, 0 },
784
723
  { "-delay",     ".delay",          XrmoptionSepArg, 0 },
785
724
  { "-bitmap",    ".bitmap",         XrmoptionSepArg, 0 },
786
725
  { "-baseline",  ".bitmapBaseline", XrmoptionSepArg, 0 },