-
Notifications
You must be signed in to change notification settings - Fork 93
/
mlx_init.c
99 lines (87 loc) · 2.5 KB
/
mlx_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
** mlx_init.c for MiniLibX in
**
** Made by Charlie Root
** Login <[email protected]>
**
** Started on Mon Jul 31 16:52:42 2000 Charlie Root
** Last update Fri Jan 28 17:05:09 2005 Olivier Crouzet
*/
#include "mlx_int.h"
void *mlx_init()
{
t_xvar *xvar;
if (!(xvar = malloc(sizeof(*xvar))))
return ((void*)0);
if ((xvar->display = XOpenDisplay("")) == 0)
{
free(xvar);
return ((void*)0);
}
xvar->screen = DefaultScreen(xvar->display);
xvar->root = DefaultRootWindow(xvar->display);
xvar->cmap = DefaultColormap(xvar->display,xvar->screen);
xvar->depth = DefaultDepth(xvar->display,xvar->screen);
if (mlx_int_get_visual(xvar)==-1)
{
printf(ERR_NO_TRUECOLOR);
exit(1);
}
xvar->win_list = 0;
xvar->loop_hook = 0;
xvar->loop_param = (void *)0;
xvar->do_flush = 1;
xvar->wm_delete_window = XInternAtom (xvar->display, "WM_DELETE_WINDOW", False);
xvar->wm_protocols = XInternAtom (xvar->display, "WM_PROTOCOLS", False);
mlx_int_deal_shm(xvar);
if (xvar->private_cmap)
xvar->cmap = XCreateColormap(xvar->display,xvar->root,
xvar->visual,AllocNone);
mlx_int_rgb_conversion(xvar);
xvar->end_loop = 0;
return (xvar);
}
/*
** pshm_format of -1 : Not XYBitmap|XYPixmap|ZPixmap
** alpha libX need a check of the DISPLAY env var, or shm is allowed
** in remote Xserver connections.
*/
int mlx_int_deal_shm(t_xvar *xvar)
{
int use_pshm;
int bidon;
char *dpy;
char buff[33];
xvar->use_xshm = XShmQueryVersion(xvar->display,&bidon,&bidon,&(use_pshm));
if (xvar->use_xshm && use_pshm)
xvar->pshm_format = XShmPixmapFormat(xvar->display);
else
xvar->pshm_format = -1;
gethostname(buff,32);
dpy = getenv(ENV_DISPLAY);
if (dpy && strlen(dpy) && *dpy!=':' && strncmp(dpy,buff,strlen(buff)) &&
strncmp(dpy,LOCALHOST,strlen(LOCALHOST)) )
{
xvar->pshm_format = -1;
xvar->use_xshm = 0;
}
}
/*
** TrueColor Visual is needed to have *_mask correctly set
*/
int mlx_int_rgb_conversion(t_xvar *xvar)
{
bzero(xvar->decrgb,sizeof(int)*6);
while (!(xvar->visual->red_mask&1))
{ xvar->visual->red_mask >>= 1; xvar->decrgb[0] ++; }
while (xvar->visual->red_mask&1)
{ xvar->visual->red_mask >>= 1; xvar->decrgb[1] ++; }
while (!(xvar->visual->green_mask&1))
{ xvar->visual->green_mask >>= 1; xvar->decrgb[2] ++; }
while (xvar->visual->green_mask&1)
{ xvar->visual->green_mask >>= 1; xvar->decrgb[3] ++; }
while (!(xvar->visual->blue_mask&1))
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[4] ++; }
while (xvar->visual->blue_mask&1)
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[5] ++; }
}