diff options
-rw-r--r-- | minilibx-linux/Makefile.gen | 4 | ||||
-rw-r--r-- | minilibx-linux/Makefile.mk | 4 | ||||
-rw-r--r-- | minilibx-linux/mlx.h | 2 | ||||
-rw-r--r-- | minilibx-linux/mlx_init.c | 113 | ||||
-rw-r--r-- | minilibx-linux/mlx_int.h | 166 | ||||
-rw-r--r-- | minilibx-linux/mlx_loop.c | 36 | ||||
-rw-r--r-- | minilibx-linux/mlx_new_window.c | 73 | ||||
-rw-r--r-- | minilibx-linux/mlx_screen_size.c | 13 |
8 files changed, 219 insertions, 192 deletions
diff --git a/minilibx-linux/Makefile.gen b/minilibx-linux/Makefile.gen index 156687c..a96eb91 100644 --- a/minilibx-linux/Makefile.gen +++ b/minilibx-linux/Makefile.gen @@ -28,7 +28,7 @@ SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.c \ mlx_put_image_to_window.c mlx_get_color_value.c mlx_clear_window.c \ mlx_xpm.c mlx_int_str_to_wordtab.c mlx_destroy_window.c \ mlx_int_param_event.c mlx_int_set_win_event_mask.c mlx_hook.c \ - mlx_rgb.c mlx_destroy_image.c + mlx_rgb.c mlx_destroy_image.c mlx_screen_size.c OBJ =$(SRC:.c=.o) CFLAGS = -O3 -I$(INC) @@ -44,4 +44,4 @@ do_cp : clean : - rm -f $(OBJ) $(NAME) *~ core *.core + rm -f $(OBJ) $(NAME) libmlx_$(HT).a *~ core *.core diff --git a/minilibx-linux/Makefile.mk b/minilibx-linux/Makefile.mk index f05fd6c..4ebc26d 100644 --- a/minilibx-linux/Makefile.mk +++ b/minilibx-linux/Makefile.mk @@ -28,7 +28,7 @@ SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.c \ mlx_put_image_to_window.c mlx_get_color_value.c mlx_clear_window.c \ mlx_xpm.c mlx_int_str_to_wordtab.c mlx_destroy_window.c \ mlx_int_param_event.c mlx_int_set_win_event_mask.c mlx_hook.c \ - mlx_rgb.c mlx_destroy_image.c + mlx_rgb.c mlx_destroy_image.c mlx_screen_size.c OBJ =$(SRC:.c=.o) CFLAGS = -O3 -I$(INC) @@ -44,4 +44,4 @@ do_cp : clean : - rm -f $(OBJ) $(NAME) *~ core *.core + rm -f $(OBJ) $(NAME) libmlx_$(HT).a *~ core *.core diff --git a/minilibx-linux/mlx.h b/minilibx-linux/mlx.h index 25991a1..af6af12 100644 --- a/minilibx-linux/mlx.h +++ b/minilibx-linux/mlx.h @@ -126,4 +126,6 @@ int mlx_do_key_autorepeatoff(void *mlx_ptr); int mlx_do_key_autorepeaton(void *mlx_ptr); int mlx_do_sync(void *mlx_ptr); +int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey); + #endif /* MLX_H */ diff --git a/minilibx-linux/mlx_init.c b/minilibx-linux/mlx_init.c index 7a8db8f..a99e46b 100644 --- a/minilibx-linux/mlx_init.c +++ b/minilibx-linux/mlx_init.c @@ -13,79 +13,80 @@ -void *mlx_init() +void *mlx_init() { - t_xvar *xvar; - - if (!(xvar = malloc(sizeof(*xvar))) || (xvar->display = XOpenDisplay(""))==0) - 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; - mlx_int_deal_shm(xvar); - if (xvar->private_cmap) - xvar->cmap = XCreateColormap(xvar->display,xvar->root, + t_xvar *xvar; + + if (!(xvar = malloc(sizeof(*xvar))) || (xvar->display = XOpenDisplay(""))==0) + 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); + mlx_int_deal_shm(xvar); + if (xvar->private_cmap) + xvar->cmap = XCreateColormap(xvar->display,xvar->root, xvar->visual,AllocNone); - mlx_int_rgb_conversion(xvar); - return (xvar); + mlx_int_rgb_conversion(xvar); + return (xvar); } /* -** pshm_format of -1 : Not XYBitmap|XYPixmap|ZPixmap +** 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 mlx_int_deal_shm(t_xvar *xvar) { - int use_pshm; - int bidon; - char *dpy; - char buff[33]; + 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; - } + 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) +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] ++; } + 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] ++; } } diff --git a/minilibx-linux/mlx_int.h b/minilibx-linux/mlx_int.h index 4349f5f..a43fd1b 100644 --- a/minilibx-linux/mlx_int.h +++ b/minilibx-linux/mlx_int.h @@ -14,112 +14,118 @@ ** Internal settings for MiniLibX */ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/mman.h> -#include <X11/Xlib.h> -#include <X11/Xutil.h> -#include <sys/ipc.h> -#include <sys/shm.h> -#include <X11/extensions/XShm.h> +#ifndef MLX_INT_H + +# define MLX_INT_H + +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <unistd.h> +# include <fcntl.h> +# include <sys/mman.h> +# include <X11/Xlib.h> +# include <X11/Xutil.h> +# include <sys/ipc.h> +# include <sys/shm.h> +# include <X11/extensions/XShm.h> /* #include <X11/xpm.h> */ -#define MLX_TYPE_SHM_PIXMAP 3 -#define MLX_TYPE_SHM 2 -#define MLX_TYPE_XIMAGE 1 +# define MLX_TYPE_SHM_PIXMAP 3 +# define MLX_TYPE_SHM 2 +# define MLX_TYPE_XIMAGE 1 -#define MLX_MAX_EVENT LASTEvent +# define MLX_MAX_EVENT LASTEvent -#define ENV_DISPLAY "DISPLAY" -#define LOCALHOST "localhost" -#define ERR_NO_TRUECOLOR "MinilibX Error : No TrueColor Visual available.\n" -#define WARN_SHM_ATTACH "MinilibX Warning : X server can't attach shared memory.\n" +# define ENV_DISPLAY "DISPLAY" +# define LOCALHOST "localhost" +# define ERR_NO_TRUECOLOR "MinilibX Error : No TrueColor Visual available.\n" +# define WARN_SHM_ATTACH "MinilibX Warning : X server can't attach shared memory.\n" typedef struct s_xpm_col { - int name; - int col; -} t_xpm_col; + int name; + int col; +} t_xpm_col; struct s_col_name { - char *name; - int color; + char *name; + int color; }; typedef struct s_event_list { - int mask; - int (*hook)(); - void *param; -} t_event_list; + int mask; + int (*hook)(); + void *param; +} t_event_list; typedef struct s_win_list { - Window window; - GC gc; - struct s_win_list *next; - int (*mouse_hook)(); - int (*key_hook)(); - int (*expose_hook)(); - void *mouse_param; - void *key_param; - void *expose_param; - t_event_list hooks[MLX_MAX_EVENT]; -} t_win_list; + Window window; + GC gc; + struct s_win_list *next; + int (*mouse_hook)(); + int (*key_hook)(); + int (*expose_hook)(); + void *mouse_param; + void *key_param; + void *expose_param; + t_event_list hooks[MLX_MAX_EVENT]; +} t_win_list; typedef struct s_img { - XImage *image; - Pixmap pix; - GC gc; - int size_line; - int bpp; - int width; - int height; - int type; - int format; - char *data; - XShmSegmentInfo shm; -} t_img; + XImage *image; + Pixmap pix; + GC gc; + int size_line; + int bpp; + int width; + int height; + int type; + int format; + char *data; + XShmSegmentInfo shm; +} t_img; typedef struct s_xvar { - Display *display; - Window root; - int screen; - int depth; - Visual *visual; - Colormap cmap; - int private_cmap; - t_win_list *win_list; - int (*loop_hook)(); - void *loop_param; - int use_xshm; - int pshm_format; - int do_flush; - int decrgb[6]; -} t_xvar; - - -int mlx_int_do_nothing(); -int mlx_int_get_good_color(); -int mlx_int_find_in_pcm(); -int mlx_int_anti_resize_win(); -int mlx_int_wait_first_expose(); -int mlx_int_rgb_conversion(); -int mlx_int_deal_shm(); -void *mlx_int_new_xshm_image(); -char **mlx_int_str_to_wordtab(); -void *mlx_new_image(); -int shm_att_pb(); + Display *display; + Window root; + int screen; + int depth; + Visual *visual; + Colormap cmap; + int private_cmap; + t_win_list *win_list; + int (*loop_hook)(); + void *loop_param; + int use_xshm; + int pshm_format; + int do_flush; + int decrgb[6]; + Atom wm_delete_window; +} t_xvar; + + +int mlx_int_do_nothing(); +int mlx_int_get_good_color(); +int mlx_int_find_in_pcm(); +int mlx_int_anti_resize_win(); +int mlx_int_wait_first_expose(); +int mlx_int_rgb_conversion(); +int mlx_int_deal_shm(); +void *mlx_int_new_xshm_image(); +char **mlx_int_str_to_wordtab(); +void *mlx_new_image(); +int shm_att_pb(); + +#endif diff --git a/minilibx-linux/mlx_loop.c b/minilibx-linux/mlx_loop.c index 74af0a5..11b2a41 100644 --- a/minilibx-linux/mlx_loop.c +++ b/minilibx-linux/mlx_loop.c @@ -16,23 +16,27 @@ extern int (*(mlx_int_param_event[]))(); int mlx_loop(t_xvar *xvar) { - XEvent ev; - t_win_list *win; + XEvent ev; + t_win_list *win; - mlx_int_set_win_event_mask(xvar); - xvar->do_flush = 0; - while (42) - { - while (!xvar->loop_hook || XPending(xvar->display)) + mlx_int_set_win_event_mask(xvar); + xvar->do_flush = 0; + while (42) { - XNextEvent(xvar->display,&ev); - win = xvar->win_list; - while (win && (win->window!=ev.xany.window)) - win = win->next; - if (win && ev.type < MLX_MAX_EVENT) - if (win->hooks[ev.type].hook) - mlx_int_param_event[ev.type](xvar, &ev, win); + while (!xvar->loop_hook || XPending(xvar->display)) + { + XNextEvent(xvar->display,&ev); + win = xvar->win_list; + while (win && (win->window!=ev.xany.window)) + win = win->next; + if (win && ev.type < MLX_MAX_EVENT) + { + if (ev.type == ClientMessage && (Atom)ev.xclient.data.l[0] == xvar->wm_delete_window) + XDestroyWindow(xvar->display, win->window); + if (win->hooks[ev.type].hook) + mlx_int_param_event[ev.type](xvar, &ev, win); + } + } + xvar->loop_hook(xvar->loop_param); } - xvar->loop_hook(xvar->loop_param); - } } diff --git a/minilibx-linux/mlx_new_window.c b/minilibx-linux/mlx_new_window.c index 2399cdc..3f05914 100644 --- a/minilibx-linux/mlx_new_window.c +++ b/minilibx-linux/mlx_new_window.c @@ -21,41 +21,42 @@ void *mlx_new_window(t_xvar *xvar,int size_x,int size_y,char *title) { - t_win_list *new_win; - XSetWindowAttributes xswa; - XGCValues xgcv; + t_win_list *new_win; + XSetWindowAttributes xswa; + XGCValues xgcv; - xswa.background_pixel = 0; - xswa.border_pixel = -1; - xswa.colormap = xvar->cmap; - /* - xswa.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask | - KeyPressMask | KeyReleaseMask | StructureNotifyMask; - */ - /* xswa.event_mask = ExposureMask; */ - xswa.event_mask = 0xFFFFFF; /* all events */ - if (!(new_win = malloc(sizeof(*new_win)))) - return ((void *)0); - new_win->window = XCreateWindow(xvar->display,xvar->root,0,0,size_x,size_y, - 0,CopyFromParent,InputOutput,xvar->visual, - CWEventMask|CWBackPixel|CWBorderPixel| - CWColormap,&xswa); - mlx_int_anti_resize_win(xvar,new_win->window,size_x,size_y); - XStoreName(xvar->display,new_win->window,title); - xgcv.foreground = -1; - xgcv.function = GXcopy; - xgcv.plane_mask = AllPlanes; - new_win->gc = XCreateGC(xvar->display,new_win->window, - GCFunction|GCPlaneMask|GCForeground,&xgcv); - new_win->next = xvar->win_list; - xvar->win_list = new_win; - /* - new_win->mouse_hook = mlx_int_do_nothing; - new_win->key_hook = mlx_int_do_nothing; - new_win->expose_hook = mlx_int_do_nothing; - */ - bzero(&(new_win->hooks), sizeof(new_win->hooks)); - XMapRaised(xvar->display,new_win->window); - mlx_int_wait_first_expose(xvar,new_win->window); - return (new_win); + xswa.background_pixel = 0; + xswa.border_pixel = -1; + xswa.colormap = xvar->cmap; + /* + xswa.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask | + KeyPressMask | KeyReleaseMask | StructureNotifyMask; + */ + /* xswa.event_mask = ExposureMask; */ + xswa.event_mask = 0xFFFFFF; /* all events */ + if (!(new_win = malloc(sizeof(*new_win)))) + return ((void *)0); + new_win->window = XCreateWindow(xvar->display,xvar->root,0,0,size_x,size_y, + 0,CopyFromParent,InputOutput,xvar->visual, + CWEventMask|CWBackPixel|CWBorderPixel| + CWColormap,&xswa); + mlx_int_anti_resize_win(xvar,new_win->window,size_x,size_y); + XStoreName(xvar->display,new_win->window,title); + XSetWMProtocols(xvar->display, new_win->window, &(xvar->wm_delete_window), 1); + xgcv.foreground = -1; + xgcv.function = GXcopy; + xgcv.plane_mask = AllPlanes; + new_win->gc = XCreateGC(xvar->display,new_win->window, + GCFunction|GCPlaneMask|GCForeground,&xgcv); + new_win->next = xvar->win_list; + xvar->win_list = new_win; + /* + new_win->mouse_hook = mlx_int_do_nothing; + new_win->key_hook = mlx_int_do_nothing; + new_win->expose_hook = mlx_int_do_nothing; + */ + bzero(&(new_win->hooks), sizeof(new_win->hooks)); + XMapRaised(xvar->display,new_win->window); + mlx_int_wait_first_expose(xvar,new_win->window); + return (new_win); } diff --git a/minilibx-linux/mlx_screen_size.c b/minilibx-linux/mlx_screen_size.c new file mode 100644 index 0000000..835730d --- /dev/null +++ b/minilibx-linux/mlx_screen_size.c @@ -0,0 +1,13 @@ +#include "mlx_int.h" + +int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey) +{ + XWindowAttributes xwAttr; + Status ret; + t_xvar *xvar; + + xvar = mlx_ptr; + ret = XGetWindowAttributes(xvar->display, xvar->root, &xwAttr); + (*sizex) = xwAttr.width; + (*sizey) = xwAttr.height; +} |