]> icculus.org git repositories - dana/openbox.git/blob - render2/surface.c
watch out for prototypes when making child surfaces
[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     return sur;
22 }
23
24 struct RrSurface *RrSurfaceNewProto(enum RrSurfaceType type,
25                                     int numtex)
26 {
27     struct RrSurface *sur;
28
29     sur = surface_new(type, numtex);
30     sur->inst = NULL;
31     sur->win = None;
32     sur->parent = NULL;
33     return sur;
34 }
35
36 struct RrSurface *RrSurfaceNew(struct RrInstance *inst,
37                                enum RrSurfaceType type,
38                                Window win,
39                                int numtex)
40 {
41     struct RrSurface *sur;
42
43     sur = surface_new(type, numtex);
44     sur->inst = inst;
45     sur->win = win;
46     sur->parent = NULL;
47     return sur;
48 }
49
50 struct RrSurface *RrSurfaceNewChild(enum RrSurfaceType type,
51                                     struct RrSurface *parent,
52                                     int numtex)
53 {
54     struct RrSurface *sur;
55
56     /* cant be a child of a prototype! */
57     assert(parent->inst);
58     if (!parent->inst) return NULL;
59
60     sur = surface_new(type, numtex);
61     sur->inst = parent->inst;
62     sur->win = None; /* XXX XCreateWindow? */
63     sur->parent = parent;
64     sur->parentx = 0;
65     sur->parenty = 0;
66     return sur;
67 }
68
69 /* doesn't set win or parent */
70 static struct RrSurface *surface_copy(struct RrSurface *orig)
71 {
72     struct RrSurface *sur;
73
74     sur = malloc(sizeof(struct RrSurface));
75     sur->type = orig->type;
76     switch (sur->type) {
77     case RR_SURFACE_PLANAR:
78         sur->data = orig->data;
79         break;
80     case RR_SURFACE_NONPLANAR:
81         assert(0);
82         break;
83     }
84     sur->ntextures = orig->ntextures;
85     sur->texture = malloc(sizeof(struct RrTexture) * sur->ntextures);
86     memcpy(sur->texture, orig->texture,
87            sizeof(struct RrTexture) * sur->ntextures);
88     return sur;
89 }
90
91 struct RrSurface *RrSurfaceCopy(struct RrInstance *inst,
92                                 struct RrSurface *orig,
93                                 Window win)
94 {
95     struct RrSurface *sur;
96
97     sur = surface_copy(orig);
98     sur->inst = inst;
99     sur->win = win;
100     sur->parent = NULL;
101     return sur;
102 }
103
104 struct RrSurface *RrSurfaceCopyChild(struct RrSurface *orig,
105                                      struct RrSurface *parent)
106 {
107     struct RrSurface *sur;
108
109     /* cant be a child of a prototype! */
110     assert(parent->inst);
111     if (!parent->inst) return NULL;
112
113     sur = surface_copy(orig);
114     sur->inst = parent->inst;
115     sur->win = None; /* XXX XCreateWindow? */
116     sur->parent = parent;
117     return sur;
118 }
119
120 void RrSurfaceFree(struct RrSurface *sur)
121 {
122     if (sur) {
123         if (sur->ntextures)
124             free(sur->texture);
125         if (sur->parent && sur->win)
126             XDestroyWindow(RrDisplay(sur->inst), sur->win);
127         free(sur);
128     }
129 }
130
131 struct RrTexture *RrSurfaceTexture(struct RrSurface *sur, int texnum)
132 {
133     assert(texnum < sur->ntextures);
134     return &(sur->texture[texnum]);
135 }