aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-03-06 22:10:16 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-03-06 22:10:16 +0100
commit079a4b6e9389b05db2c672594cfb2f2b197e35dd (patch)
treef4a2bc882dd7667e2c9758e2be38e3484879e225 /src
parentqwe (diff)
parentokok (diff)
download42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.gz
42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.bz2
42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.xz
42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.tar.zst
42-cub3d-079a4b6e9389b05db2c672594cfb2f2b197e35dd.zip
Merge branch 'fork-music'
Diffstat (limited to '')
-rw-r--r--src/ft_exit.c9
-rw-r--r--src/ft_get_music.c11
-rw-r--r--src/ft_init_map.c5
-rw-r--r--src/ft_music.c24
-rw-r--r--src/ft_parse_map.c10
-rw-r--r--src/ft_treat_args.c10
-rw-r--r--src/ft_warp_level.c17
7 files changed, 51 insertions, 35 deletions
diff --git a/src/ft_exit.c b/src/ft_exit.c
index ca5201c..be4d46e 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -15,9 +15,9 @@
#include <mlx.h>
#include <stddef.h>
#include <stdlib.h>
-#include <stdint.h>
#include <signal.h>
-#include <pthread.h>
+#include <sys/wait.h>
+#include <stdint.h>
static void
ft_free_lists(t_cub *clist)
@@ -34,6 +34,7 @@ static void
ft_memdel((void**)&clist->mlist.music_cmd);
ft_memdel((void**)&clist->mlist.mapl);
ft_free_words(clist->mlist.map);
+ ft_free_words(clist->mlist.mcmd_words);
if (!clist->wlist.inited)
ft_memdel((void**)&clist->wlist.winptr);
}
@@ -67,8 +68,8 @@ int
}
if (clist->isoldmus && clist->wlist.inited)
{
- pthread_cancel(clist->tid);
- pthread_join(clist->tid, NULL);
+ kill(clist->mpid, SIGTERM);
+ wait(&clist->mpid);
}
ft_free_lists(clist);
ft_printf("Exiting program\n");
diff --git a/src/ft_get_music.c b/src/ft_get_music.c
index cdd7039..2383e0a 100644
--- a/src/ft_get_music.c
+++ b/src/ft_get_music.c
@@ -15,7 +15,7 @@
#include <stdlib.h>
#include <stdint.h>
-static void
+static int8_t
ft_set_music_cmd(t_map *mlist)
{
uint8_t len;
@@ -24,9 +24,13 @@ static void
len += ft_strlen(FT_MUS_CMD) - 2;
ft_memdel((void**)&mlist->music_cmd);
if (!(mlist->music_cmd = (char *)malloc((len + 1) * sizeof(char))))
- return ;
+ return (-1);
ft_sprintf(mlist->music_cmd, FT_MUS_CMD,
mlist->music_path);
+ ft_free_words(mlist->mcmd_words);
+ if (!(mlist->mcmd_words = ft_split(mlist->music_cmd, ' ')))
+ return (-1);
+ return (0);
}
int8_t
@@ -54,6 +58,5 @@ int8_t
return (-1);
}
clist->mlist.ismusic = 1;
- ft_set_music_cmd(&clist->mlist);
- return (0);
+ return ((ft_set_music_cmd(&clist->mlist) == 0) ? (0) : (-1));
}
diff --git a/src/ft_init_map.c b/src/ft_init_map.c
index 4fa127c..1c12189 100644
--- a/src/ft_init_map.c
+++ b/src/ft_init_map.c
@@ -30,7 +30,9 @@ static int8_t
!(mlist->music_cmd = (char*)ft_calloc(1, sizeof(char))) ||
!(mlist->mapl = (char*)ft_calloc(1, sizeof(char))) ||
!(mlist->map = (char**)ft_calloc(2, sizeof(char*))) ||
- !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char))))
+ !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char))) ||
+ !(mlist->mcmd_words = (char**)ft_calloc(2, sizeof(char*))) ||
+ !(mlist->mcmd_words[0] = (char*)ft_calloc(1, sizeof(char))))
return (-1);
return (0);
}
@@ -41,6 +43,7 @@ int8_t
if (ft_init_map_callocs(mlist) < 0)
return (-1);
mlist->map[1] = 0;
+ mlist->mcmd_words[1] = 0;
mlist->map_w = 0;
mlist->map_h = 0;
mlist->mapl_len = 0;
diff --git a/src/ft_music.c b/src/ft_music.c
index aec5e58..c3b90b1 100644
--- a/src/ft_music.c
+++ b/src/ft_music.c
@@ -10,21 +10,23 @@
/* */
/* ************************************************************************** */
-#include <libft.h>
#include <cub3d.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <pthread.h>
+#include <unistd.h>
+#include <libft.h>
void
- *ft_music_thread(void *vargp)
+ ft_music_fork(char **mcmd_words, char *const envp[])
{
- t_cub *cl;
+ execve(*(mcmd_words + 0), mcmd_words, envp);
+}
- cl = (t_cub *)vargp;
+void
+ ft_enable_music(t_cub *cl)
+{
cl->isoldmus = 1;
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
- while (1)
- system(cl->mlist.music_cmd);
- return (NULL);
+ cl->mpid = fork();
+ if (cl->mpid == 0)
+ {
+ ft_music_fork(cl->mlist.mcmd_words, cl->envp);
+ }
}
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index 3c1fd9c..36d1ec5 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -75,13 +75,13 @@ static int8_t
}
void
- ft_save_name(const char *map_path, t_map *mlist, t_cub *clist)
+ ft_save_name(const char *map_path, t_cub *clist)
{
- ft_memdel((void**)&mlist->filename);
- if (!(mlist->filename =
+ ft_memdel((void**)&clist->mlist.filename);
+ if (!(clist->mlist.filename =
(char*)malloc((ft_strlen(map_path) + 1) * sizeof(char))))
ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist);
- ft_sprintf(mlist->filename, "%s", map_path);
+ ft_sprintf(clist->mlist.filename, "%s", map_path);
}
void
@@ -95,7 +95,7 @@ void
fd = open(map_path, O_RDONLY);
if (fd < 0)
ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist);
- ft_save_name(map_path, &clist->mlist, clist);
+ ft_save_name(map_path, clist);
ret = 1;
while (ret != 12 && ret >= 0)
ret = ft_parse_it(fd, clist);
diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c
index 2571709..a47f9f7 100644
--- a/src/ft_treat_args.c
+++ b/src/ft_treat_args.c
@@ -14,7 +14,6 @@
#include <cub3d.h>
#include <stddef.h>
#include <stdint.h>
-#include <pthread.h>
#include <unistd.h>
uint8_t
@@ -30,15 +29,20 @@ uint8_t
}
uint8_t
- ft_use_args(int argc, const char *argv[], t_cub *clist)
+ ft_use_args(int argc, const char *argv[], char *const envp[], t_cub *clist)
{
+ clist->envp = envp;
if (argc < 3)
{
if (ft_init_winptr(clist) < 0)
+ {
return (ft_exit(FT_RET_FAILED_MLX, clist));
+ }
ft_draw_scene(clist);
if (clist->mlist.ismusic)
- pthread_create(&clist->tid, NULL, ft_music_thread, clist);
+ {
+ ft_enable_music(clist);
+ }
ft_hooks_and_loops(&clist->wlist, clist);
}
else if (argc == 3 && !ft_strncmp("--save", argv[2], 7))
diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c
index 808b346..37789a5 100644
--- a/src/ft_warp_level.c
+++ b/src/ft_warp_level.c
@@ -15,13 +15,15 @@
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
-#include <mlx.h>
+#include <unistd.h>
#include <signal.h>
-#include <pthread.h>
+#include <sys/wait.h>
+#include <mlx.h>
static void
ft_del_map(t_map *ml)
{
+ ft_memdel((void**)&ml->filename);
ft_memdel((void**)&ml->no_tex_path);
ft_memdel((void**)&ml->so_tex_path);
ft_memdel((void**)&ml->ea_tex_path);
@@ -33,6 +35,7 @@ static void
ft_memdel((void**)&ml->music_cmd);
ft_memdel((void**)&ml->mapl);
ft_free_words(ml->map);
+ ft_free_words(ml->mcmd_words);
}
static void
@@ -66,16 +69,16 @@ static void
{
if (isoldmus && !cl->mlist.ismusic)
{
- pthread_cancel(cl->tid);
- pthread_join(cl->tid, NULL);
+ kill(cl->mpid, SIGTERM);
+ wait(&cl->mpid);
cl->isoldmus = 0;
}
else if (isoldmus && cl->mlist.ismusic
&& ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))
{
- pthread_cancel(cl->tid);
- pthread_join(cl->tid, NULL);
- pthread_create(&cl->tid, NULL, ft_music_thread, cl);
+ kill(cl->mpid, SIGTERM);
+ wait(&cl->mpid);
+ ft_enable_music(cl);
}
else if (isoldmus && cl->mlist.ismusic
&& !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))