5d0181eed51bb34a763a9480e23905d8b8ad6932
[xscreensaver] / jwxyz / jwxyzI.h
1 /* xscreensaver, Copyright (c) 1991-2016 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     const unsigned long *_MASKS = \
38        DefaultVisualOfScreen (DefaultScreenOfDisplay (dpy))->rgba_masks; \
39     for (unsigned i = 0; i != 4; ++i) \
40       (rgba)[i] = ((pixel) & _MASKS[i]) * (mult) / _MASKS[i]; \
41   }
42
43 /* jwxyz.m, jwxyz-gl.c, jwxyz-image.c */
44 extern void jwxyz_window_resized (Display *);
45
46 /* jwxyz-cocoa.m, jwxyz-android.c */
47 extern const XRectangle *jwxyz_frame (Drawable d); /* XGetGeometry sux. */
48 extern unsigned int jwxyz_drawable_depth (Drawable d);
49
50 /* 'scale' is pixels per point, with 72 points per inch. This matches the
51    meaning of -[UIView contentScaleFactor] on iOS.
52  */
53 extern float jwxyz_scale (Window main_window);
54
55 extern const char *jwxyz_default_font_family (int require);
56 extern void *jwxyz_load_native_font (Window main_window,
57                                      int traits_jwxyz, int mask_jwxyz,
58                                      const char *font_name_ptr,
59                                      size_t font_name_length,
60                                      int font_name_type, float size,
61                                      char **family_name_ret,
62                                      int *ascent_ret, int *descent_ret);
63 extern void jwxyz_release_native_font (Display *, void *native_font);
64
65 /* Text metrics for aliased and antialiased text must match. */
66 extern void jwxyz_render_text (Display *, void *native_font,
67                                const char *str, size_t len, Bool utf8_p,
68                                Bool antialias_p, XCharStruct *cs_ret,
69                                char **pixmap_ret);
70 extern void jwxyz_get_pos (Window w, XPoint *vpos, XPoint *p);
71 #ifndef current_device_rotation
72 extern double current_device_rotation (void);
73 extern Bool ignore_rotation_p (Display *);
74 #endif
75
76 /* jwxyz-common.c */
77 extern void jwxyz_validate_pixel (Display *dpy, unsigned long pixel,
78                                   unsigned int depth, Bool alpha_allowed_p);
79 extern Bool jwxyz_dumb_drawing_mode(Display *dpy, Drawable d, GC gc,
80                                     int x, int y,
81                                     unsigned width, unsigned height);
82 extern void jwxyz_blit (const void *src_data, ptrdiff_t src_pitch,
83                         unsigned src_x, unsigned src_y,
84                         void *dst_data, ptrdiff_t dst_pitch,
85                         unsigned dst_x, unsigned dst_y,
86                         unsigned width, unsigned height);
87 extern void jwxyz_fill_rect (Display *, Drawable, GC,
88                              int x, int y,
89                              unsigned int width, unsigned int height,
90                              unsigned long pixel);
91 extern void jwxyz_gcv_defaults (Display *dpy, XGCValues *gcv, int depth);
92 extern int jwxyz_draw_string (Display *dpy, Drawable d, GC gc, int x, int y,
93                               const char *str, size_t len, int utf8_p);
94 extern void *jwxyz_native_font (Font f);
95
96 #define SEEK_XY(dst, dst_pitch, x, y) \
97   ((uint32_t *)((char *)dst + dst_pitch * y + x * 4))
98
99 # ifdef JWXYZ_QUARTZ
100
101 #  include <CoreGraphics/CGGeometry.h>
102 #  include <CoreGraphics/CGContext.h>
103
104 extern Display *jwxyz_quartz_make_display (Window w);
105 extern void jwxyz_quartz_free_display (Display *);
106 extern void jwxyz_flush_context (Display *);
107
108 #  define jwxyz_assert_display(dpy)
109 #  define jwxyz_assert_drawable(main_window, d)
110 #  define jwxyz_assert_gl()
111
112 /* jwxyz-cocoa.m needs these from jwxyz.m for XCopyArea. */
113 extern void invalidate_drawable_cache (Drawable d);
114 extern void set_color (Display *dpy, CGContextRef cgc, unsigned long argb,
115                        unsigned int depth, Bool alpha_allowed_p, Bool fill_p);
116 extern void push_gc (Drawable d, GC gc);
117 extern void push_color_gc (Display *dpy, Drawable d, GC gc,
118                            unsigned long color,
119                            Bool antialias_p, Bool fill_p);
120 extern CGPoint map_point (Drawable d, int x, int y);
121 extern void jwxyz_quartz_copy_area (Display *dpy, Drawable src, Drawable dst,
122                                     GC gc, int src_x, int src_y,
123                                     unsigned int width, unsigned int height,
124                                     int dst_x, int dst_y);
125
126 #define pop_gc(d,gc) CGContextRestoreGState (d->cgc)
127
128 # endif /* JWXYZ_QUARTZ */
129
130 # ifdef JWXYZ_GL
131
132 #  if defined(USE_IPHONE)
133 #   include <OpenGLES/ES1/gl.h>
134 #  elif defined(HAVE_COCOA)
135 #   include <OpenGL/gl.h>
136 #  elif defined(HAVE_ANDROID)
137 #   include <GLES/gl.h>
138 #  else
139 #   include <GL/gl.h>
140 #  endif
141
142 /* utils/jwxyz-gl.c */
143 extern Display *jwxyz_gl_make_display (Window w);
144 extern void jwxyz_gl_free_display (Display *);
145 extern void jwxyz_set_matrices (Display *dpy, unsigned width, unsigned height,
146                                 Bool screen_p);
147 extern void jwxyz_gl_flush (Display *dpy);
148 extern void jwxyz_gl_set_gc (Display *dpy, GC gc);
149 extern void jwxyz_gl_copy_area (Display *dpy, Drawable src, Drawable dst,
150                                 GC gc, int src_x, int src_y,
151                                 unsigned int width, unsigned int height,
152                                 int dst_x, int dst_y);
153
154 /* Only works if both drawables share OpenGL objects. OpenGL context sharing
155    works on OS X, iOS, and some (but not all!) EGL implementations. This would
156    also work with FBOs and one context for everything. Surprisingly slow and
157    unreliable.
158  */
159 extern void jwxyz_gl_copy_area_read_tex_image (Display *dpy,
160                                                unsigned src_height,
161                                                int src_x, int src_y,
162                                                unsigned int width,
163                                                unsigned int height,
164                                                int dst_x, int dst_y);
165 extern void jwxyz_gl_copy_area_write_tex_image (Display *dpy, GC gc,
166                                                 int src_x, int src_y,
167                                                 unsigned int width,
168                                                 unsigned int height,
169                                                 int dst_x, int dst_y);
170 extern void jwxyz_gl_draw_image (GLenum target,
171                                  unsigned int tex_w, unsigned int tex_h,
172                                  int src_x, int src_y,
173                                  unsigned int width, unsigned int height,
174                                  int dst_x, int dst_y);
175
176 /* glReadPixels followed by glTexImage2D. This is terrible, so only use this
177    if nothing else works.
178  */
179 extern void jwxyz_gl_copy_area_read_pixels (Display *dpy, Drawable src,
180                                             Drawable dst, GC gc,
181                                             int src_x, int src_y,
182                                             unsigned int width,
183                                             unsigned int height,
184                                             int dst_x, int dst_y);
185
186 /* Platform-specific */
187 extern void jwxyz_bind_drawable (Display *dpy, Window w, Drawable d);
188 extern void jwxyz_assert_display (Display *);
189 extern void jwxyz_assert_drawable (Window main_window, Drawable d);
190 extern void jwxyz_assert_gl (void);
191
192 # endif /* JWXYZ_GL */
193
194 # ifdef JWXYZ_IMAGE
195
196 extern Display *jwxyz_image_make_display (Window w,
197                                           const unsigned char *rgba_bytes);
198 extern void jwxyz_image_free_display (Display *);
199
200 extern ptrdiff_t jwxyz_image_pitch (Drawable d);
201 extern void *jwxyz_image_data (Drawable d);
202
203 # endif /* JWXYZ_IMAGE */
204
205 #endif