1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <stdio.h>
23 
24 #include "brightoninternals.h"
25 #include "brightonX11.h"
26 
27 static int id = 0;
28 brightonWindow *winlist = 0;
29 
30 extern void clearout();
31 extern int brightonCloseDisplay(brightonDisplay *);
32 extern int BGetGeometry(brightonDisplay *, brightonWindow *);
33 
34 brightonWindow *
brightonCreateWindow(brightonDisplay * display,brightonApp * app,int cmapsize,int flags,int quality,int gs,int x,int y)35 brightonCreateWindow(brightonDisplay *display, brightonApp *app,
36 int cmapsize, int flags, int quality, int gs, int x, int y)
37 {
38 	brightonWindow *bwin;
39 
40 	bwin = brightonmalloc(sizeof(brightonWindow));
41 
42 	bwin->cmap_size = cmapsize;
43 	bwin->quality = quality;
44 	bwin->grayscale = gs;
45 	bwin->id = ++id;
46 	display->bwin = bwin;
47 	((brightonWindow *) display->bwin)->display = (brightonDisplay *) display;
48 
49 	printf("display is %i by %i pixels (%i, %i)\n",
50 		display->width, display->height, x, y);
51 
52 	if (BGetGeometry(display, bwin) < 0)
53 		printf("cannot get root window geometry\n");
54 	else
55 		printf("Window is w %i, h %i, d %i, %i %i %i\n",
56 			bwin->width, bwin->height, bwin->depth, bwin->x, bwin->y,
57 			bwin->border);
58 
59 	if ((display->palette = brightonInitColormap(bwin, bwin->cmap_size))
60 		== NULL)
61 		clearout(-1);
62 
63 	bwin->image = brightonReadImage(bwin, app->image);
64 	bwin->surface = brightonReadImage(bwin, app->surface);
65 
66 	if (bwin->image) {
67 		bwin->width = bwin->image->width;
68 		bwin->height = bwin->image->height;
69 	} else {
70 		bwin->width = app->width;
71 		bwin->height = app->height;
72 	}
73 	bwin->aspect = ((float) bwin->width) / bwin->height;
74 	if (x > 0)
75 		bwin->x = x;
76 	else if (x < 0)
77 		bwin->x = display->width + x - app->width;
78 	if (y > 0)
79 		bwin->y = y;
80 	else if (y < 0)
81 		bwin->y = display->height + y - app->height;
82 
83 	if (app->flags & BRIGHTON_POST_WINDOW)
84 		bwin->flags |= _BRIGHTON_POST;
85 
86 	if (BOpenWindow(display, bwin, app->name) == 0)
87 	{
88 		brightonfree(bwin);
89 		bwin = 0;
90 		clearout(-1);
91 		return(0);
92 	}
93 
94 	bwin->flags |= BRIGHTON_ACTIVE;
95 
96 	brightonInitDefHandlers(bwin);
97 
98 	bwin->next = winlist;
99 	winlist = bwin;
100 
101 	/*
102 	 * Force a fake size to ensure the first configure notify is picked up.
103 	 */
104 	bwin->width = bwin->height = 10;
105 
106 	BFlush((brightonDisplay *) display);
107 
108 	return(bwin);
109 }
110 
111 void
brightonDestroyWindow(brightonWindow * bwin)112 brightonDestroyWindow(brightonWindow *bwin)
113 {
114 	brightonBitmap *bitmap;
115 
116 	printf("brightonDestroyWindow()\n");
117 
118 	BFlush((brightonDisplay *) bwin->display);
119 
120 	BCloseWindow((brightonDisplay *) bwin->display, bwin);
121 
122 	if (brightonCloseDisplay((brightonDisplay *) bwin->display))
123 	{
124 		bwin->flags = 0;
125 		brightonfree(bwin);
126 		clearout(0);
127 	}
128 
129 	brightonFreeBitmap(bwin, bwin->image);
130 	brightonFreeBitmap(bwin, bwin->surface);
131 	brightonFreeBitmap(bwin, bwin->canvas);
132 	brightonFreeBitmap(bwin, bwin->dlayer);
133 	brightonFreeBitmap(bwin, bwin->slayer);
134 	brightonFreeBitmap(bwin, bwin->tlayer);
135 	brightonFreeBitmap(bwin, bwin->render);
136 
137 	bitmap = bwin->bitmaps;
138 
139 	while (bitmap) {
140 		bitmap = brightonFreeBitmap(bwin, bitmap);
141 	}
142 
143 	if (bwin->bitmaps)
144 		printf("Bitmap list is not empty on window exit: %s\n",
145 			bwin->bitmaps->name);
146 
147 	bwin->flags = 0;
148 	brightonfree(bwin);
149 }
150 
151