From http://www.jwz.org/xscreensaver/xscreensaver-5.40.tar.gz
[xscreensaver] / jwxyz / jwxyzI.h
1 /* xscreensaver, Copyright (c) 1991-2018 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #ifndef __JWXYZ_I_H__
13 #define __JWXYZ_I_H__
14
15 #include <inttypes.h>
16 #include <stddef.h>
17
18 #include "jwxyz.h"
19
20 #define Assert(C, ...) do { if (!(C)) jwxyz_abort (__VA_ARGS__); } while(0)
21
22 #undef MAX
23 #undef MIN
24 #define MAX(a,b) ((a)>(b)?(a):(b))
25 #define MIN(a,b) ((a)<(b)?(a):(b))
26
27 #define JWXYZ_FONT_FAMILY       0 /* i.e. -[NSFont familyName] via XLFD */
28 #define JWXYZ_FONT_FACE         1 /*      -[NSFont fontName]   via native */
29 #define JWXYZ_FONT_RANDOM       2
30
31 #define JWXYZ_STYLE_BOLD        1
32 #define JWXYZ_STYLE_ITALIC      2
33 #define JWXYZ_STYLE_MONOSPACE   4
34
35 #define JWXYZ_QUERY_COLOR(dpy, pixel, mult, rgba) \
36   { \
37     Visual *_V = DefaultVisualOfScreen (DefaultScreenOfDisplay (dpy)); \
38     (rgba)[0] = ((pixel) & _V->red_mask)   * (mult) / _V->red_mask;    \
39     (rgba)[1] = ((pixel) & _V->green_mask) * (mult) / _V->green_mask;  \
40     (rgba)[2] = ((pixel) & _V->blue_mask)  * (mult) / _V->blue_mask;   \
41     (rgba)[3] = ((pixel) & _V->alpha_mask) * (mult) / _V->alpha_mask;  \
42   }
43
44 /* jwxyz.m, jwxyz-gl.c, jwxyz-image.c */
45 extern void jwxyz_window_resized (Display *);
46
47 /* jwxyz-cocoa.m, jwxyz-android.c */
48 extern const XRectangle *jwxyz_frame (Drawable d); /* XGetGeometry sux. */
49 extern unsigned int jwxyz_drawable_depth (Drawable d);
50
51 /* 'scale' is pixels per point, with 72 points per inch. This matches the
52    meaning of -[UIView contentScaleFactor] on iOS.
53  */
54 extern float jwxyz_scale (Window main_window);
55
56 extern const char *jwxyz_default_font_family (int require);
57 extern void *jwxyz_load_native_font (Window main_window,
58                                      int traits_jwxyz, int mask_jwxyz,
59                                      const char *font_name_ptr,
60                                      size_t font_name_length,
61                                      int font_name_type, float size,
62                                      char **family_name_ret,
63                                      int *ascent_ret, int *descent_ret);
64 extern void jwxyz_release_native_font (Display *, void *native_font);
65
66 /* Text metrics for aliased and antialiased text must match. */
67 extern void jwxyz_render_text (Display *, void *native_font,
68                                const char *str, size_t len, Bool utf8_p,
69                                Bool antialias_p, XCharStruct *cs_ret,
70                                char **pixmap_ret);
71 extern void jwxyz_get_pos (Window w, XPoint *vpos, XPoint *p);
72 #ifndef current_device_rotation
73 extern double current_device_rotation (void);
74 extern Bool ignore_rotation_p (Display *);
75 #endif
76
77 /* jwxyz-common.c */
78 extern void jwxyz_validate_pixel (Display *dpy, unsigned long pixel,
79                                   unsigned int depth, Bool alpha_allowed_p);
80 extern Bool jwxyz_dumb_drawing_mode(Display *dpy, Drawable d, GC gc,
81                                     int x, int y,
82                                     unsigned width, unsigned height);
83 extern void jwxyz_blit (const void *src_data, ptrdiff_t src_pitch,
84                         unsigned src_x, unsigned src_y,
85                         void *dst_data, ptrdiff_t dst_pitch,
86                         unsigned dst_x, unsigned dst_y,
87                         unsigned width, unsigned height);
88 extern void jwxyz_fill_rect (Display *, Drawable, GC,
89                              int x, int y,
90                              unsigned int width, unsigned int height,
91                              unsigned long pixel);
92 extern void jwxyz_gcv_defaults (Display *dpy, XGCValues *gcv, int depth);
93 extern int jwxyz_draw_string (Display *dpy, Drawable d, GC gc, int x, int y,
94                               const char *str, size_t len, int utf8_p);
95 extern void *jwxyz_native_font (Font f);
96
97 #define SEEK_XY(dst, dst_pitch, x, y) \
98   ((uint32_t *)((char *)dst + dst_pitch * y + x * 4))
99
100 # ifdef JWXYZ_QUARTZ
101
102 #  include <CoreGraphics/CGGeometry.h>
103 #  include <CoreGraphics/CGContext.h>
104
105 extern Display *jwxyz_quartz_make_display (Window w);
106 extern void jwxyz_quartz_free_display (Display *);
107 extern void jwxyz_flush_context (Display *);
108
109 #  define jwxyz_assert_display(dpy)
110 #  define jwxyz_assert_drawable(main_window, d)
111 #  define jwxyz_assert_gl()
112
113 /* jwxyz-cocoa.m needs these from jwxyz.m for XCopyArea. */
114 extern void invalidate_drawable_cache (Drawable d);
115 extern void set_color (Display *dpy, CGContextRef cgc, unsigned long argb,
116                        unsigned int depth, Bool alpha_allowed_p, Bool fill_p);
117 extern void push_gc (Drawable d, GC gc);
118 extern void push_color_gc (Display *dpy, Drawable d, GC gc,
119                            unsigned long color,
120                            Bool antialias_p, Bool fill_p);
121 extern CGPoint map_point (Drawable d, int x, int y);
122 extern void jwxyz_quartz_copy_area (Display *dpy, Drawable src, Drawable dst,
123                                     GC gc, int src_x, int src_y,
124                                     unsigned int width, unsigned int height,
125                                     int dst_x, int dst_y);
126
127 #define pop_gc(d,gc) CGContextRestoreGState (d->cgc)
128
129 # endif /* JWXYZ_QUARTZ */
130
131 # ifdef JWXYZ_GL
132
133 #  if defined(USE_IPHONE)
134 #   include <OpenGLES/ES1/gl.h>
135 #  elif defined(HAVE_COCOA)
136 #   include <OpenGL/gl.h>
137 #  elif defined(HAVE_ANDROID)
138 #   include <GLES/gl.h>
139 #  else
140 #   include <GL/gl.h>
141 #  endif
142
143 /* utils/jwxyz-gl.c */
144 extern Display *jwxyz_gl_make_display (Window w);
145 extern void jwxyz_gl_free_display (Display *);
146 extern void jwxyz_set_matrices (Display *dpy, unsigned width, unsigned height,
147                                 Bool screen_p);
148 extern void jwxyz_gl_flush (Display *dpy);
149 extern void jwxyz_gl_set_gc (Display *dpy, GC gc);
150 extern void jwxyz_gl_copy_area (Display *dpy, Drawable src, Drawable dst,
151                                 GC gc, int src_x, int src_y,
152                                 unsigned int width, unsigned int height,
153                                 int dst_x, int dst_y);
154
155 /* Only works if both drawables share OpenGL objects. OpenGL context sharing
156    works on OS X, iOS, and some (but not all!) EGL implementations. This would
157    also work with FBOs and one context for everything. Surprisingly slow and
158    unreliable.
159  */
160 extern void jwxyz_gl_copy_area_read_tex_image (Display *dpy,
161                                                unsigned src_height,
162                                                int src_x, int src_y,
163                                                unsigned int width,
164                                                unsigned int height,
165                                                int dst_x, int dst_y);
166 extern void jwxyz_gl_copy_area_write_tex_image (Display *dpy, GC gc,
167                                                 int src_x, int src_y,
168                                                 unsigned int width,
169                                                 unsigned int height,
170                                                 int dst_x, int dst_y);
171
172 extern void jwxyz_gl_draw_image (Display *dpy, GC gc,
173                                  GLenum gl_texture_target,
174                                  unsigned int tex_w, unsigned int tex_h,
175                                  int src_x, int src_y, int src_depth,
176                                  unsigned int width, unsigned int height,
177                                  int dst_x, int dst_y, Bool flip_y);
178
179 /* glReadPixels followed by glTexImage2D. This is terrible, so only use this
180    if nothing else works.
181  */
182 extern void jwxyz_gl_copy_area_read_pixels (Display *dpy, Drawable src,
183                                             Drawable dst, GC gc,
184                                             int src_x, int src_y,
185                                             unsigned int width,
186                                             unsigned int height,
187                                             int dst_x, int dst_y);
188
189 /* Platform-specific */
190 extern void jwxyz_bind_drawable (Display *dpy, Window w, Drawable d);
191 extern void jwxyz_assert_display (Display *);
192 extern void jwxyz_assert_drawable (Window main_window, Drawable d);
193 extern void jwxyz_assert_gl (void);
194
195 # endif /* JWXYZ_GL */
196
197 # ifdef JWXYZ_IMAGE
198
199 extern Display *jwxyz_image_make_display (Window w,
200                                           const unsigned char *rgba_bytes);
201 extern void jwxyz_image_free_display (Display *);
202
203 extern ptrdiff_t jwxyz_image_pitch (Drawable d);
204 extern void *jwxyz_image_data (Drawable d);
205
206 # endif /* JWXYZ_IMAGE */
207
208 #endif