From df5b9359c59f45a8da0f11eede0602da893526b8 Mon Sep 17 00:00:00 2001 From: Luis Geniole Date: Fri, 18 Feb 2022 16:26:25 -0300 Subject: [PATCH 1/3] Add mlx_new_fullscreen_window. --- Makefile.mk | 2 +- mlx.h | 9 ++-- mlx_ext_randr.c | 6 ++- mlx_new_fullscreen_window.c | 72 ++++++++++++++++++++++++++++ test/main.c | 94 ++++++++++++++++++++++++++++++++++--- 5 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 mlx_new_fullscreen_window.c diff --git a/Makefile.mk b/Makefile.mk index 1bae8df8..4cc2613c 100644 --- a/Makefile.mk +++ b/Makefile.mk @@ -32,7 +32,7 @@ SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.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_mouse.c mlx_screen_size.c \ - mlx_destroy_display.c + mlx_new_fullscreen_window.c mlx_destroy_display.c OBJ_DIR = obj OBJ = $(addprefix $(OBJ_DIR)/,$(SRC:%.c=%.o)) diff --git a/mlx.h b/mlx.h index b323412c..e8512210 100644 --- a/mlx.h +++ b/mlx.h @@ -1,9 +1,9 @@ /* -** mlx.h for MinilibX in -** +** mlx.h for MinilibX in +** ** Made by Charlie Root ** Login -** +** ** Started on Mon Jul 31 16:37:50 2000 Charlie Root ** Last update Tue May 15 16:23:28 2007 Olivier Crouzet */ @@ -44,6 +44,9 @@ void *mlx_init(); */ void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); +void *mlx_new_fullscreen_window(void *mlx_ptr, int *size_x, int *size_y, + char *title); + /* ** return void *0 if failed */ diff --git a/mlx_ext_randr.c b/mlx_ext_randr.c index 34ddb919..11543e9b 100644 --- a/mlx_ext_randr.c +++ b/mlx_ext_randr.c @@ -1,5 +1,7 @@ - - +/* +** Requires: sudo apt install libxrandr-dev libxrandr2 +** Compile with flag: -lXrandr +*/ #include "mlx_int.h" diff --git a/mlx_new_fullscreen_window.c b/mlx_new_fullscreen_window.c new file mode 100644 index 00000000..f70769be --- /dev/null +++ b/mlx_new_fullscreen_window.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mlx_new_fullscreen_window.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lpaulo-m +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/18 14:03:21 by lpaulo-m #+# #+# */ +/* Updated: 2022/02/18 16:23:27 by lpaulo-m ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** We do not use White/BlackPixel macro, TrueColor Visual make sure +** 0 is black & -1 is white +** +** With mlx_int_wait_first_expose, no flush is needed. +*/ + +#include "mlx_int.h" + +void *mlx_new_fullscreen_window(t_xvar *xvar, int *size_x, int *size_y, + char *title) +{ + t_win_list *new_win; + XSetWindowAttributes xswa; + XGCValues xgcv; + + xswa.background_pixel = 0; + xswa.border_pixel = -1; + xswa.colormap = xvar->cmap; + xswa.override_redirect = 1; + /* + 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); + mlx_get_screen_size(xvar, size_x, size_y); + new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, *size_x, + *size_y, 0, CopyFromParent, InputOutput, xvar->visual, + CWEventMask | CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect, + &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/test/main.c b/test/main.c index 578eaaeb..07cd18d4 100644 --- a/test/main.c +++ b/test/main.c @@ -11,28 +11,34 @@ void *mlx; void *win1; -void *win2; -void *win3; -void *im1; +void *win2; +void *win3; +void *window_fullsreen; +void *im1; void *im2; void *im3; void *im4; +void *im5; int bpp1; int bpp2; int bpp3; int bpp4; +int bpp5; int sl1; int sl2; int sl3; int sl4; +int sl5; int endian1; int endian2; int endian3; int endian4; +int endian5; char *data1; char *data2; char *data3; char *data4; +char *data5; int xpm1_x; int xpm1_y; @@ -88,8 +94,7 @@ int mouse_win3(int x,int y, void *p) printf("Mouse moving in Win3, at %dx%d.\n",x,y); } - -int main() +static void test_endianess(void) { int a; @@ -100,7 +105,10 @@ int main() else local_endian = 0; printf(" => Local Endian : %d\n",local_endian); +} +static void test_mlx_init(void) +{ printf(" => Connection ..."); if (!(mlx = mlx_init())) { @@ -108,7 +116,10 @@ int main() exit(1); } printf("OK (use_xshm %d pshm_format %d)\n",((t_xvar *)mlx)->use_xshm,((t_xvar *)mlx)->pshm_format); +} +static void test_window(void) +{ printf(" => Window1 %dx%d \"Title 1\" ...",WIN1_SX,WIN1_SY); if (!(win1 = mlx_new_window(mlx,WIN1_SX,WIN1_SY,"Title1"))) { @@ -126,7 +137,10 @@ int main() mlx_clear_window(mlx,win1); printf("OK\n"); sleep(2); +} +static void test_image(void) +{ printf(" => Image1 ZPixmap %dx%d ...",IM1_SX,IM1_SY); if (!(im1 = mlx_new_image(mlx,IM1_SX,IM1_SY))) { @@ -150,7 +164,10 @@ int main() mlx_destroy_image(mlx, im1); printf("OK\n"); sleep(2); +} +static void test_string(void) +{ printf(" => Image3 ZPixmap %dx%d ...",IM3_SX,IM3_SY); if (!(im3 = mlx_new_image(mlx,IM3_SX,IM3_SY))) { @@ -175,7 +192,10 @@ int main() mlx_string_put(mlx,win1,15,WIN1_SY/2+20,0x00FFFF,"MinilibX test"); printf("OK\n"); sleep(2); +} +static void test_xpm(void) +{ printf(" => Xpm from file ..."); if (!(im2 = mlx_xpm_file_to_image(mlx,"open.xpm",&xpm1_x,&xpm1_y))) { @@ -190,9 +210,60 @@ int main() printf(" => Put xpm ..."); mlx_put_image_to_window(mlx,win1,im2,0,0); mlx_put_image_to_window(mlx,win1,im2,100,100); + printf("OK\n"); sleep(2); +} + +static void test_fullscreen_window(void) +{ + int size_x; + int size_y; + + printf(" => Fullscreen Window \"Fullscreen1\" "); + if (!(window_fullsreen = mlx_new_fullscreen_window(mlx,&size_x,&size_y,"Title1"))) + { + printf(" !! KO !!\n"); + exit(1); + } + printf("%dx%d ...", size_x, size_y); + printf("OK\n"); + + printf(" => Image5 ZPixmap %dx%d ...",size_x,size_y); + if (!(im5 = mlx_new_image(mlx,size_x,size_y))) + { + printf(" !! KO !!\n"); + exit(1); + } + data5 = mlx_get_data_addr(im5,&bpp5,&sl5,&endian5); + printf("OK (bpp5: %d, sizeline5: %d endian: %d type: %d)\n",bpp5,sl5,endian5, + ((t_img *)im5)->type); + + printf(" => Fill Image5 ..."); + color_map_2(data5,bpp5,sl5,size_x,size_y,endian5, 5); + printf("OK (pixmap : %d)\n",(int)((t_img *)im5)->pix); + + printf(" => Put Image5 ..."); + mlx_put_image_to_window(mlx,window_fullsreen,im5,20,20); printf("OK\n"); sleep(2); + printf(" => Destroy Image5 ... "); + mlx_destroy_image(mlx, im5); + printf("OK\n"); + sleep(2); + + printf(" => Clear Fullscreen Window ..."); + mlx_clear_window(mlx,window_fullsreen); + printf("OK\n"); + sleep(2); + + printf(" => Destroy Fullscreen Window ..."); + mlx_destroy_window(mlx,window_fullsreen); + printf("OK\n"); + sleep(2); +} + +static void test_hooks(void) +{ printf(" => 2nd window,"); win2 = mlx_new_window(mlx,WIN1_SX,WIN1_SY,"Title2"); if (!(im4 = mlx_new_image(mlx,IM3_SX, IM3_SY))) @@ -216,10 +287,21 @@ int main() mlx_hook(win3, MotionNotify, PointerMotionMask, mouse_win3, 0); printf("OK\nNow in Loop. Just play. Esc in 3 to destroy, 1&2 to quit.\n"); - + mlx_loop(mlx); } +int main() +{ + test_endianess(); + test_mlx_init(); + test_window(); + test_image(); + test_string(); + test_xpm(); + test_fullscreen_window(); + test_hooks(); +} int color_map_1(void *win,int w,int h) { From 4b58aeff2e3e0f711f0da41433fb5c168ec27fb0 Mon Sep 17 00:00:00 2001 From: Luis Geniole Date: Fri, 18 Feb 2022 22:22:39 -0300 Subject: [PATCH 2/3] Reuse mlx_new_window code. --- Makefile.mk | 6 +-- mlx_new_fullscreen_window.c | 72 --------------------------------- mlx_new_window.c | 80 +++++++++++++++++++++++++++---------- 3 files changed, 62 insertions(+), 96 deletions(-) delete mode 100644 mlx_new_fullscreen_window.c diff --git a/Makefile.mk b/Makefile.mk index 4cc2613c..02af928e 100644 --- a/Makefile.mk +++ b/Makefile.mk @@ -1,9 +1,9 @@ ## ## Makefile for MiniLibX in /home/boulon/work/c/raytraceur/minilibx -## +## ## Made by Olivier Crouzet ## Login -## +## ## Started on Tue Oct 5 15:56:43 2004 Olivier Crouzet ## Last update Tue May 15 15:41:20 2007 Olivier Crouzet ## @@ -32,7 +32,7 @@ SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.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_mouse.c mlx_screen_size.c \ - mlx_new_fullscreen_window.c mlx_destroy_display.c + mlx_destroy_display.c OBJ_DIR = obj OBJ = $(addprefix $(OBJ_DIR)/,$(SRC:%.c=%.o)) diff --git a/mlx_new_fullscreen_window.c b/mlx_new_fullscreen_window.c deleted file mode 100644 index f70769be..00000000 --- a/mlx_new_fullscreen_window.c +++ /dev/null @@ -1,72 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* mlx_new_fullscreen_window.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lpaulo-m +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/02/18 14:03:21 by lpaulo-m #+# #+# */ -/* Updated: 2022/02/18 16:23:27 by lpaulo-m ### ########.fr */ -/* */ -/* ************************************************************************** */ - -/* -** We do not use White/BlackPixel macro, TrueColor Visual make sure -** 0 is black & -1 is white -** -** With mlx_int_wait_first_expose, no flush is needed. -*/ - -#include "mlx_int.h" - -void *mlx_new_fullscreen_window(t_xvar *xvar, int *size_x, int *size_y, - char *title) -{ - t_win_list *new_win; - XSetWindowAttributes xswa; - XGCValues xgcv; - - xswa.background_pixel = 0; - xswa.border_pixel = -1; - xswa.colormap = xvar->cmap; - xswa.override_redirect = 1; - /* - 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); - mlx_get_screen_size(xvar, size_x, size_y); - new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, *size_x, - *size_y, 0, CopyFromParent, InputOutput, xvar->visual, - CWEventMask | CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect, - &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/mlx_new_window.c b/mlx_new_window.c index 3f059141..bcebb27d 100644 --- a/mlx_new_window.c +++ b/mlx_new_window.c @@ -1,14 +1,13 @@ /* -** mlx_new_window.c for MiniLibX in -** +** mlx_new_window.c for MiniLibX in +** ** Made by Charlie Root ** Login -** +** ** Started on Mon Jul 31 17:29:02 2000 Charlie Root ** Last update Thu Oct 4 15:44:43 2001 Charlie Root */ - /* ** We do not use White/BlackPixel macro, TrueColor Visual make sure ** 0 is black & -1 is white @@ -16,14 +15,11 @@ ** With mlx_int_wait_first_expose, no flush is needed. */ -#include "mlx_int.h" - +#include "mlx_int.h" -void *mlx_new_window(t_xvar *xvar,int size_x,int size_y,char *title) +static XSetWindowAttributes get_default_attributes(t_xvar *xvar) { - t_win_list *new_win; XSetWindowAttributes xswa; - XGCValues xgcv; xswa.background_pixel = 0; xswa.border_pixel = -1; @@ -33,21 +29,33 @@ void *mlx_new_window(t_xvar *xvar,int size_x,int size_y,char *title) KeyPressMask | KeyReleaseMask | StructureNotifyMask; */ /* xswa.event_mask = ExposureMask; */ - xswa.event_mask = 0xFFFFFF; /* all events */ + xswa.event_mask = 0xFFFFFF; /* all events */ + return (xswa); +} + +static void *allocate_window(void) +{ + t_win_list *new_win; + 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); + return (new_win); +} + +static void *set_configs(t_xvar *xvar, t_win_list *new_win, int size_x, + int size_y, char *title) +{ + XGCValues xgcv; + + 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->gc = XCreateGC(xvar->display, new_win->window, + GCFunction | GCPlaneMask | GCForeground, &xgcv); new_win->next = xvar->win_list; xvar->win_list = new_win; /* @@ -56,7 +64,37 @@ void *mlx_new_window(t_xvar *xvar,int size_x,int size_y,char *title) 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); + XMapRaised(xvar->display, new_win->window); + mlx_int_wait_first_expose(xvar, new_win->window); return (new_win); } + +void *mlx_new_window(t_xvar *xvar, int size_x, int size_y, char *title) +{ + t_win_list *new_win; + XSetWindowAttributes xswa; + + xswa = get_default_attributes(xvar); + new_win = allocate_window(); + new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, size_x, + size_y, 0, CopyFromParent, InputOutput, xvar->visual, + CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &xswa); + return (set_configs(xvar, new_win, size_x, size_y, title)); +} + +void *mlx_new_fullscreen_window(t_xvar *xvar, int *size_x, int *size_y, + char *title) +{ + t_win_list *new_win; + XSetWindowAttributes xswa; + + xswa = get_default_attributes(xvar); + xswa.override_redirect = 1; + new_win = allocate_window(); + mlx_get_screen_size(xvar, size_x, size_y); + new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, *size_x, + *size_y, 0, CopyFromParent, InputOutput, xvar->visual, + CWEventMask | CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect, + &xswa); + return (set_configs(xvar, new_win, *size_x, *size_y, title)); +} From f8ad7b7b3c850733e91146420a204f3516adfb0b Mon Sep 17 00:00:00 2001 From: Luis Geniole Date: Fri, 18 Feb 2022 22:45:55 -0300 Subject: [PATCH 3/3] Fix bad malloc handling. --- mlx_new_window.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/mlx_new_window.c b/mlx_new_window.c index bcebb27d..2162175b 100644 --- a/mlx_new_window.c +++ b/mlx_new_window.c @@ -33,15 +33,6 @@ static XSetWindowAttributes get_default_attributes(t_xvar *xvar) return (xswa); } -static void *allocate_window(void) -{ - t_win_list *new_win; - - if (!(new_win = malloc(sizeof(*new_win)))) - return ((void *)0); - return (new_win); -} - static void *set_configs(t_xvar *xvar, t_win_list *new_win, int size_x, int size_y, char *title) { @@ -74,8 +65,9 @@ void *mlx_new_window(t_xvar *xvar, int size_x, int size_y, char *title) t_win_list *new_win; XSetWindowAttributes xswa; + if (!(new_win = malloc(sizeof(*new_win)))) + return ((void *)0); xswa = get_default_attributes(xvar); - new_win = allocate_window(); new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, size_x, size_y, 0, CopyFromParent, InputOutput, xvar->visual, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &xswa); @@ -88,9 +80,10 @@ void *mlx_new_fullscreen_window(t_xvar *xvar, int *size_x, int *size_y, t_win_list *new_win; XSetWindowAttributes xswa; + if (!(new_win = malloc(sizeof(*new_win)))) + return ((void *)0); xswa = get_default_attributes(xvar); xswa.override_redirect = 1; - new_win = allocate_window(); mlx_get_screen_size(xvar, size_x, size_y); new_win->window = XCreateWindow(xvar->display, xvar->root, 0, 0, *size_x, *size_y, 0, CopyFromParent, InputOutput, xvar->visual,