]> icculus.org git repositories - dana/openbox.git/blob - render2/surface.c
add painting
[dana/openbox.git] / render2 / surface.c
1 #include "surface.h"
2 #include "instance.h"
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 /* doesn't set win or parent */
8 static struct RrSurface *surface_new(enum RrSurfaceType type,
9                                      int numtex)
10 {
11     struct RrSurface *sur;
12
13     sur = malloc(sizeof(struct RrSurface));
14     sur->type = type;
15     sur->ntextures = numtex;
16     if (numtex) {
17         sur->texture = malloc(sizeof(struct RrTexture) * numtex);
18         memset(sur->texture, 0, sizeof(struct RrTexture) * numtex);
19     } else
20         sur->texture = NULL;
21     sur->x = 0;
22     sur->y = 0;
23     sur->w = 1;
24     sur->h = 1;
25     return sur;
26 }
27
28 static Window create_window(struct RrInstance *inst, Window parent)
29 {
30     Window win = XCreateWindow(RrDisplay(inst), parent, 0, 0, 1, 1, 0,
31                                RrDepth(inst), InputOutput, RrVisual(inst),
32                                0, NULL);
33     XMapWindow(RrDisplay(inst), win);
34     return win;
35 }
36
37 struct RrSurface *RrSurfaceNewProto(enum RrSurfaceType type,
38                                     int numtex)
39 {
40     struct RrSurface *sur;
41
42     sur = surface_new(type, numtex);
43     sur->inst = NULL;
44     sur->win = None;
45     sur->parent = NULL;
46     return sur;
47 }
48
49 struct RrSurface *RrSurfaceNew(struct RrInstance *inst,
50                                enum RrSurfaceType type,
51                                Window win,
52                                int numtex)
53 {
54     struct RrSurface *sur;
55
56     sur = surface_new(type, numtex);
57     sur->inst = inst;
58     sur->win = win;
59     sur->parent = NULL;
60     return sur;
61 }
62
63 struct RrSurface *RrSurfaceNewChild(enum RrSurfaceType type,
64                                     struct RrSurface *parent,
65                                     int numtex)
66 {
67     struct RrSurface *sur;
68
69     /* can't be a child of a prototype! */
70     assert(parent->inst);
71     if (!parent->inst) return NULL;
72
73     sur = surface_new(type, numtex);
74     sur->inst = parent->inst;
75     sur->win = create_window(sur->inst, parent->win);
76     sur->parent = parent;
77     return sur;
78 }
79
80 /* doesn't set win or parent */
81 static struct RrSurface *surface_copy(struct RrSurface *orig)
82 {
83     struct RrSurface *sur;
84
85     sur = malloc(sizeof(struct RrSurface));
86     sur->type = orig->type;
87     switch (sur->type) {
88     case RR_SURFACE_PLANAR:
89         sur->data = orig->data;
90         break;
91     case RR_SURFACE_NONPLANAR:
92         assert(0);
93         break;
94     }
95     sur->ntextures = orig->ntextures;
96     sur->texture = malloc(sizeof(struct RrTexture) * sur->ntextures);
97     memcpy(sur->texture, orig->texture,
98            sizeof(struct RrTexture) * sur->ntextures);
99     return sur;
100 }
101
102 struct RrSurface *RrSurfaceCopy(struct RrInstance *inst,
103                                 struct RrSurface *orig,
104                                 Window win)
105 {
106     struct RrSurface *sur;
107
108     sur = surface_copy(orig);
109     sur->inst = inst;
110     sur->win = win;
111     sur->parent = NULL;
112     return sur;
113 }
114
115 struct RrSurface *RrSurfaceCopyChild(struct RrSurface *orig,
116                                      struct RrSurface *parent)
117 {
118     struct RrSurface *sur;
119
120     /* can't be a child of a prototype! */
121     assert(parent->inst);
122     if (!parent->inst) return NULL;
123
124     sur = surface_copy(orig);
125     sur->inst = parent->inst;
126     sur->win = create_window(sur->inst, parent->win);
127     sur->parent = parent;
128     return sur;
129 }
130
131 void RrSurfaceFree(struct RrSurface *sur)
132 {
133     if (sur) {
134         if (sur->ntextures)
135             free(sur->texture);
136         if (sur->parent && sur->win)
137             XDestroyWindow(RrDisplay(sur->inst), sur->win);
138         free(sur);
139     }
140 }
141
142 void RrSurfaceSetArea(struct RrSurface *sur,
143                       int x,
144                       int y,
145                       int w,
146                       int h)
147 {
148     sur->x = x;
149     sur->y = y;
150     sur->w = w;
151     sur->h = h;
152 }
153
154 Window RrSurfaceWindow(struct RrSurface *sur)
155 {
156     /* can't get a window for a prototype */
157     assert(sur->inst);
158     if (!sur->inst) return None;
159
160     return sur->win;
161 }
162
163 struct RrTexture *RrSurfaceTexture(struct RrSurface *sur, int texnum)
164 {
165     assert(texnum < sur->ntextures);
166     return &(sur->texture[texnum]);
167 }