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