]> git.sesse.net Git - vlc/blob - modules/video_filter/puzzle.c
avcodec: unused variable
[vlc] / modules / video_filter / puzzle.c
1 /*****************************************************************************
2  * puzzle.c : Puzzle game
3  *****************************************************************************
4  * Copyright (C) 2005-2009 VLC authors and VideoLAN
5  * Copyright (C) 2013      Vianney Boyer
6  * $Id$
7  *
8  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9  *          Vianney Boyer <vlcvboyer -at- gmail -dot- com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_rand.h>
39
40 #include "filter_picture.h"
41
42 #include "puzzle.h"
43 #include "puzzle_bezier.h"
44 #include "puzzle_lib.h"
45 #include "puzzle_pce.h"
46 #include "puzzle_mgt.h"
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 #define ROWS_TEXT N_("Number of puzzle rows")
52 #define ROWS_LONGTEXT N_("Number of puzzle rows")
53 #define COLS_TEXT N_("Number of puzzle columns")
54 #define COLS_LONGTEXT N_("Number of puzzle columns")
55 #define MODE_TEXT N_("Game mode")
56 #define MODE_LONGTEXT N_("Select game mode variation from jigsaw puzzle to sliding puzzle.")
57 #define BORDER_TEXT N_("Border")
58 #define BORDER_LONGTEXT N_("Unshuffled Border width.")
59 #define PREVIEW_TEXT N_("Small preview")
60 #define PREVIEW_LONGTEXT N_("Show small preview.")
61 #define PREVIEWSIZE_TEXT N_("Small preview size")
62 #define PREVIEWSIZE_LONGTEXT N_("Show small preview size (percent of source).")
63 #define SHAPE_SIZE_TEXT N_("Piece edge shape size")
64 #define SHAPE_SIZE_LONGTEXT N_("Size of the curve along the piece's edge")
65 #define AUTO_SHUFFLE_TEXT N_("Auto shuffle")
66 #define AUTO_SHUFFLE_LONGTEXT N_("Auto shuffle delay during game")
67 #define AUTO_SOLVE_TEXT N_("Auto solve")
68 #define AUTO_SOLVE_LONGTEXT N_("Auto solve delay during game")
69 #define ROTATION_TEXT N_("Rotation")
70 #define ROTATION_LONGTEXT N_("Rotation parameter: none;180;90-270;mirror")
71
72 const int pi_mode_values[] = { (int) 0, (int) 1, (int) 2, (int) 3 };
73 const char *const ppsz_mode_descriptions[] = { N_("jigsaw puzzle"), N_("sliding puzzle"), N_("swap puzzle"), N_("exchange puzzle") };
74 const int pi_rotation_values[] = { (int) 0, (int) 1, (int) 2, (int) 3 };
75 const char *const ppsz_rotation_descriptions[] = { N_("0"), N_("0/180"), N_("0/90/180/270"), N_("0/90/180/270/mirror") };
76
77 #define CFG_PREFIX "puzzle-"
78
79 int  Open ( vlc_object_t * );
80 void Close( vlc_object_t * );
81
82 vlc_module_begin()
83     set_description( N_("Puzzle interactive game video filter") )
84     set_shortname( N_( "Puzzle" ))
85     set_capability( "video filter2", 0 )
86     set_category( CAT_VIDEO )
87     set_subcategory( SUBCAT_VIDEO_VFILTER )
88
89     add_integer_with_range( CFG_PREFIX "rows", 4, 2, 16,
90                             ROWS_TEXT, ROWS_LONGTEXT, false )
91     add_integer_with_range( CFG_PREFIX "cols", 4, 2, 16,
92                             COLS_TEXT, COLS_LONGTEXT, false )
93     add_integer_with_range( CFG_PREFIX "border", 3, 0, 40,
94               BORDER_TEXT, BORDER_LONGTEXT, false )
95     add_bool( CFG_PREFIX "preview", false,
96               PREVIEW_TEXT, PREVIEW_LONGTEXT, false )
97     add_integer_with_range( CFG_PREFIX "preview-size", 15, 0, 100,
98               PREVIEWSIZE_TEXT, PREVIEWSIZE_LONGTEXT, false )
99     add_integer_with_range( CFG_PREFIX "shape-size", 90, 0, 100,
100               SHAPE_SIZE_TEXT, SHAPE_SIZE_LONGTEXT, false )
101     add_integer_with_range( CFG_PREFIX "auto-shuffle", 0, 0, 30000,
102               AUTO_SHUFFLE_TEXT, AUTO_SHUFFLE_LONGTEXT, false )
103     add_integer_with_range( CFG_PREFIX "auto-solve", 0, 0, 30000,
104               AUTO_SOLVE_TEXT, AUTO_SOLVE_LONGTEXT, false )
105     add_integer( CFG_PREFIX "rotation", 0,
106               ROTATION_TEXT, ROTATION_LONGTEXT, false )
107         change_integer_list(pi_rotation_values, ppsz_rotation_descriptions )
108     add_integer( CFG_PREFIX "mode", 0,
109               MODE_TEXT, MODE_LONGTEXT, false )
110         change_integer_list(pi_mode_values, ppsz_mode_descriptions )
111
112     set_callbacks( Open, Close )
113 vlc_module_end()
114
115 /*****************************************************************************
116  * Local prototypes
117  *****************************************************************************/
118
119 const char *const ppsz_filter_options[] = {
120     "rows", "cols","border", "preview", "preview-size", "mode", "shape-size", "auto-shuffle", "auto-solve",  "rotation", NULL
121 };
122
123 /**
124  * Open the filter
125  */
126 int Open( vlc_object_t *p_this )
127 {
128     filter_t *p_filter = (filter_t *)p_this;
129     filter_sys_t *p_sys;
130
131     /* Assert video in match with video out */
132     if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) ) {
133         msg_Err( p_filter, "Input and output format does not match" );
134         return VLC_EGENERIC;
135     }
136
137     /* Allocate structure */
138     p_filter->p_sys = p_sys = calloc(1, sizeof( *p_sys ) );
139     if( !p_sys )
140         return VLC_ENOMEM;
141
142     /* init some values */
143     p_sys->b_shuffle_rqst    = true;
144     p_sys->b_change_param    = true;
145     p_sys->i_mouse_drag_pce  = NO_PCE;
146     p_sys->i_pointed_pce     = NO_PCE;
147     p_sys->i_magnet_accuracy = 3;
148
149     /* Generate values of random bezier shapes */
150     p_sys->ps_bezier_pts_H = calloc( SHAPES_QTY, sizeof( point_t *) );
151     if( !p_sys->ps_bezier_pts_H )
152     {
153         free(p_filter->p_sys);
154         p_filter->p_sys = NULL;
155         return VLC_ENOMEM;
156     }
157     for (int32_t i_shape = 0; i_shape<SHAPES_QTY; i_shape++)
158         p_sys->ps_bezier_pts_H[i_shape] = puzzle_rand_bezier(7);
159
160
161     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
162                        p_filter->p_cfg );
163
164     vlc_mutex_init( &p_sys->lock );
165     vlc_mutex_init( &p_sys->pce_lock );
166
167     p_sys->s_new_param.i_rows =
168         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rows" );
169     p_sys->s_new_param.i_cols =
170         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "cols" );
171     p_sys->s_new_param.i_border =
172         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "border" );
173     p_sys->s_new_param.b_preview =
174         var_CreateGetBoolCommand( p_filter, CFG_PREFIX "preview" );
175     p_sys->s_new_param.i_preview_size =
176         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "preview-size" );
177     p_sys->s_new_param.i_shape_size =
178         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "shape-size" );
179     p_sys->s_new_param.i_auto_shuffle_speed =
180         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "auto-shuffle" );
181     p_sys->s_new_param.i_auto_solve_speed =
182         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "auto-solve" );
183     p_sys->s_new_param.i_rotate =
184         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rotation" );
185     p_sys->s_new_param.i_mode =
186         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "mode" );
187
188     var_AddCallback( p_filter, CFG_PREFIX "rows",         puzzle_Callback, p_sys );
189     var_AddCallback( p_filter, CFG_PREFIX "cols",         puzzle_Callback, p_sys );
190     var_AddCallback( p_filter, CFG_PREFIX "border",       puzzle_Callback, p_sys );
191     var_AddCallback( p_filter, CFG_PREFIX "preview",      puzzle_Callback, p_sys );
192     var_AddCallback( p_filter, CFG_PREFIX "preview-size", puzzle_Callback, p_sys );
193     var_AddCallback( p_filter, CFG_PREFIX "shape-size",   puzzle_Callback, p_sys );
194     var_AddCallback( p_filter, CFG_PREFIX "auto-shuffle", puzzle_Callback, p_sys );
195     var_AddCallback( p_filter, CFG_PREFIX "auto-solve",   puzzle_Callback, p_sys );
196     var_AddCallback( p_filter, CFG_PREFIX "rotation",     puzzle_Callback, p_sys );
197     var_AddCallback( p_filter, CFG_PREFIX "mode",     puzzle_Callback, p_sys );
198
199     p_filter->pf_video_filter = Filter;
200     p_filter->pf_video_mouse = puzzle_mouse;
201
202     return VLC_SUCCESS;
203 }
204
205 /**
206  * Close the filter
207  */
208 void Close( vlc_object_t *p_this ) {
209     filter_t *p_filter = (filter_t *)p_this;
210     filter_sys_t *p_sys = p_filter->p_sys;
211
212     var_DelCallback( p_filter, CFG_PREFIX "rows",          puzzle_Callback, p_sys );
213     var_DelCallback( p_filter, CFG_PREFIX "cols",          puzzle_Callback, p_sys );
214     var_DelCallback( p_filter, CFG_PREFIX "border",        puzzle_Callback, p_sys );
215     var_DelCallback( p_filter, CFG_PREFIX "preview",       puzzle_Callback, p_sys );
216     var_DelCallback( p_filter, CFG_PREFIX "preview-size",  puzzle_Callback, p_sys );
217     var_DelCallback( p_filter, CFG_PREFIX "shape-size",    puzzle_Callback, p_sys );
218     var_DelCallback( p_filter, CFG_PREFIX "auto-shuffle",  puzzle_Callback, p_sys );
219     var_DelCallback( p_filter, CFG_PREFIX "auto-solve",    puzzle_Callback, p_sys );
220     var_DelCallback( p_filter, CFG_PREFIX "rotation",      puzzle_Callback, p_sys );
221     var_DelCallback( p_filter, CFG_PREFIX "mode",          puzzle_Callback, p_sys );
222
223     vlc_mutex_destroy( &p_sys->lock );
224     vlc_mutex_destroy( &p_sys->pce_lock );
225
226     /* Free allocated memory */
227     puzzle_free_ps_puzzle_array ( p_filter );
228     puzzle_free_ps_pieces_shapes ( p_filter);
229     puzzle_free_ps_pieces ( p_filter );
230     free(p_sys->ps_desk_planes);
231     free(p_sys->ps_pict_planes);
232     free( p_sys->pi_order );
233
234     for (int32_t i_shape = 0; i_shape<SHAPES_QTY; i_shape++)
235         free(p_sys->ps_bezier_pts_H[i_shape]);
236     free(p_sys->ps_bezier_pts_H);
237
238     free( p_sys );
239 }
240
241 /**
242  * Filter a picture
243  */
244 picture_t *Filter( filter_t *p_filter, picture_t *p_pic_in ) {
245     if( !p_pic_in || !p_filter) return NULL;
246
247     const video_format_t  *p_fmt_in = &p_filter->fmt_in.video;
248     filter_sys_t *p_sys = p_filter->p_sys;
249
250     picture_t *p_pic_out = filter_NewPicture( p_filter );
251     if( !p_pic_out ) {
252         picture_Release( p_pic_in );
253         return NULL;
254     }
255
256     int i_ret = 0;
257     p_sys->b_bake_request = false;
258
259     if ((p_sys->pi_order == NULL) || (p_sys->ps_desk_planes == NULL) || (p_sys->ps_pict_planes == NULL)  || (p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL))
260         p_sys->b_init = false;
261
262     if ((p_sys->ps_pieces_shapes == NULL) && p_sys->s_current_param.b_advanced && (p_sys->s_current_param.i_shape_size != 0))
263         p_sys->b_init = false;
264
265     /* assert initialized & allocated data match with current frame characteristics */
266     if ( p_sys->s_allocated.i_planes != p_pic_out->i_planes)
267         p_sys->b_init = false;
268     p_sys->s_current_param.i_planes = p_pic_out->i_planes;
269     if (p_sys->ps_pict_planes != NULL) {
270         for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
271             if ( (p_sys->ps_pict_planes[i_plane].i_lines != p_pic_in->p[i_plane].i_visible_lines)
272                     || (p_sys->ps_pict_planes[i_plane].i_width != p_pic_in->p[i_plane].i_visible_pitch / p_pic_in->p[i_plane].i_pixel_pitch)
273                     || (p_sys->ps_desk_planes[i_plane].i_lines != p_pic_out->p[i_plane].i_visible_lines)
274                     || (p_sys->ps_desk_planes[i_plane].i_width != p_pic_out->p[i_plane].i_visible_pitch / p_pic_out->p[i_plane].i_pixel_pitch) )
275                 p_sys->b_init = false;
276         }
277     }
278
279     p_sys->s_current_param.i_pict_width  = (int) p_pic_in->p[0].i_visible_pitch / p_pic_in->p[0].i_pixel_pitch;
280     p_sys->s_current_param.i_pict_height = (int) p_pic_in->p[0].i_visible_lines;
281     p_sys->s_current_param.i_desk_width  = (int) p_pic_out->p[0].i_visible_pitch / p_pic_out->p[0].i_pixel_pitch;
282     p_sys->s_current_param.i_desk_height = (int) p_pic_out->p[0].i_visible_lines;
283
284     /* assert no mismatch between sizes */
285     if (    p_sys->s_current_param.i_pict_width  != p_sys->s_current_param.i_desk_width
286          || p_sys->s_current_param.i_pict_height != p_sys->s_current_param.i_desk_height
287          || p_sys->s_current_param.i_pict_width  != (int) p_fmt_in->i_width
288          || p_sys->s_current_param.i_pict_height != (int) p_fmt_in->i_height )
289         return NULL;
290
291     vlc_mutex_lock( &p_sys->lock );
292
293     /* check if we have to compute initial data */
294     if ( p_sys->b_change_param || p_sys->b_bake_request || !p_sys->b_init ) {
295         if ( p_sys->s_allocated.i_rows != p_sys->s_new_param.i_rows
296                 || p_sys->s_allocated.i_cols != p_sys->s_new_param.i_cols
297                 || p_sys->s_allocated.i_rotate != p_sys->s_new_param.i_rotate
298                 || p_sys->s_allocated.i_mode != p_sys->s_new_param.i_mode
299                 || p_sys->b_bake_request  || !p_sys->b_init )
300         {
301             p_sys->b_bake_request = true;
302             p_sys->b_init = false;
303             p_sys->b_shuffle_rqst = true;
304             p_sys->b_shape_init = false;
305         }
306
307         if ( p_sys->s_current_param.i_border != p_sys->s_new_param.i_border
308                 || p_sys->s_current_param.i_shape_size != p_sys->s_new_param.i_shape_size )
309         {
310             p_sys->b_bake_request = true;
311             p_sys->b_shape_init = false;
312         }
313
314         /* depending on the game selected, set associated internal flags */
315         switch ( p_sys->s_new_param.i_mode )
316         {
317           case 0:  /* jigsaw puzzle */
318             p_sys->s_new_param.b_advanced    = true;
319             p_sys->s_new_param.b_blackslot   = false;
320             p_sys->s_new_param.b_near        = false;
321             break;
322           case 1:  /* sliding puzzle */
323             p_sys->s_new_param.b_advanced    = false;
324             p_sys->s_new_param.b_blackslot   = true;
325             p_sys->s_new_param.b_near        = true;
326             break;
327           case 2:  /* swap puzzle */
328             p_sys->s_new_param.b_advanced    = false;
329             p_sys->s_new_param.b_blackslot   = false;
330             p_sys->s_new_param.b_near        = true;
331             break;
332           case 3:  /* exchange puzzle */
333             p_sys->s_new_param.b_advanced    = false;
334             p_sys->s_new_param.b_blackslot   = false;
335             p_sys->s_new_param.b_near        = false;
336             break;
337         }
338         p_sys->s_current_param.i_mode = p_sys->s_new_param.i_mode;
339
340         if ( p_sys->s_current_param.b_blackslot != p_sys->s_new_param.b_blackslot
341                 && p_sys->i_selected == NO_PCE
342                 && p_sys->s_current_param.b_blackslot )
343             p_sys->i_selected = 0;
344
345         if ( p_sys->s_current_param.i_auto_shuffle_speed != p_sys->s_new_param.i_auto_shuffle_speed )
346             p_sys->i_auto_shuffle_countdown_val = init_countdown(p_sys->s_new_param.i_auto_shuffle_speed);
347
348         if ( p_sys->s_current_param.i_auto_solve_speed != p_sys->s_new_param.i_auto_solve_speed )
349             p_sys->i_auto_solve_countdown_val = init_countdown(p_sys->s_current_param.i_auto_solve_speed);
350
351         p_sys->s_current_param.i_rows       = p_sys->s_new_param.i_rows;
352         p_sys->s_current_param.i_cols       = p_sys->s_new_param.i_cols;
353         p_sys->s_current_param.i_pieces_nbr = p_sys->s_current_param.i_rows * p_sys->s_current_param.i_cols;
354         p_sys->s_current_param.b_advanced   = p_sys->s_new_param.b_advanced;
355         if (!p_sys->s_new_param.b_advanced) {
356             p_sys->s_current_param.b_blackslot   = p_sys->s_new_param.b_blackslot;
357             p_sys->s_current_param.b_near        = p_sys->s_new_param.b_near || p_sys->s_new_param.b_blackslot;
358             p_sys->s_current_param.i_border      = 0;
359             p_sys->s_current_param.b_preview     = false;
360             p_sys->s_current_param.i_preview_size= 0;
361             p_sys->s_current_param.i_shape_size  = 0;
362             p_sys->s_current_param.i_auto_shuffle_speed  = 0;
363             p_sys->s_current_param.i_auto_solve_speed    = 0;
364             p_sys->s_current_param.i_rotate      = 0;
365         }
366         else
367         {
368             p_sys->s_current_param.b_blackslot = false;
369             p_sys->s_current_param.b_near      = false;
370             p_sys->s_current_param.i_border    = p_sys->s_new_param.i_border;
371             p_sys->s_current_param.b_preview   = p_sys->s_new_param.b_preview;
372             p_sys->s_current_param.i_preview_size        = p_sys->s_new_param.i_preview_size;
373             p_sys->s_current_param.i_shape_size          = p_sys->s_new_param.i_shape_size;
374             p_sys->s_current_param.i_auto_shuffle_speed  = p_sys->s_new_param.i_auto_shuffle_speed;
375             p_sys->s_current_param.i_auto_solve_speed    = p_sys->s_new_param.i_auto_solve_speed;
376             p_sys->s_current_param.i_rotate     = p_sys->s_new_param.i_rotate;
377         }
378         p_sys->b_change_param = false;
379     }
380
381     vlc_mutex_unlock( &p_sys->lock );
382
383     /* generate initial puzzle data when needed */
384     if ( p_sys->b_bake_request ) {
385         if (!p_sys->b_shuffle_rqst) {
386             /* here we have to keep the same position
387              * we have to save locations before generating new data
388              */
389             save_game_t *ps_save_game = puzzle_save(p_filter);
390             if (!ps_save_game)
391                 return CopyInfoAndRelease( p_pic_out, p_pic_in );
392             i_ret = puzzle_bake( p_filter, p_pic_out, p_pic_in );
393             if ( i_ret != VLC_SUCCESS )
394             {
395                 free(ps_save_game->ps_pieces);
396                 free(ps_save_game);
397                 return CopyInfoAndRelease( p_pic_out, p_pic_in );
398             }
399             puzzle_load( p_filter, ps_save_game);
400             free(ps_save_game->ps_pieces);
401             free(ps_save_game);
402         }
403         else {
404             i_ret = puzzle_bake( p_filter, p_pic_out, p_pic_in );
405             if ( i_ret != VLC_SUCCESS )
406                 return CopyInfoAndRelease( p_pic_out, p_pic_in );
407         }
408     }
409
410     /* shuffle the desk and generate pieces data  */
411     if ( p_sys->b_shuffle_rqst && p_sys->b_init ) {
412         i_ret = puzzle_bake_piece ( p_filter );
413         if (i_ret != VLC_SUCCESS)
414             return CopyInfoAndRelease( p_pic_out, p_pic_in );
415     }
416
417     /* preset output pic */
418     if ( !p_sys->b_bake_request && !p_sys->b_shuffle_rqst && p_sys->b_init && !p_sys->b_finished )
419         puzzle_preset_desk_background(p_pic_out, 0, 127, 127);
420     else {
421         /* copy src to dst during init & bake process */
422         for( uint8_t i_plane = 0; i_plane < p_pic_out->i_planes; i_plane++ )
423             memcpy( p_pic_out->p[i_plane].p_pixels, p_pic_in->p[i_plane].p_pixels,
424                 p_pic_in->p[i_plane].i_pitch * (int32_t) p_pic_in->p[i_plane].i_visible_lines );
425     }
426
427     vlc_mutex_lock( &p_sys->pce_lock );
428
429     /* manage the game, adjust locations, groups and regenerate some corrupted data if any */
430     for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 4 )
431                              && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
432                              && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
433     {
434         puzzle_solve_pces_accuracy( p_filter );
435     }
436
437     for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 4 )
438                              && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
439                              && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
440     {
441         puzzle_solve_pces_group( p_filter );
442     }
443
444     if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
445             && p_sys->s_current_param.b_advanced )
446         puzzle_count_pce_group( p_filter);
447     if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
448             && p_sys->s_current_param.b_advanced ) {
449         i_ret = puzzle_sort_layers( p_filter);
450         if (i_ret != VLC_SUCCESS)
451             return CopyInfoAndRelease( p_pic_out, p_pic_in );
452     }
453
454     for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 24 )
455                             && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
456                             && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
457     {
458         p_sys->i_calc_corn_loop++;
459         p_sys->i_calc_corn_loop %= p_sys->s_allocated.i_pieces_nbr;
460         puzzle_calculate_corners( p_filter, p_sys->i_calc_corn_loop );
461     }
462
463     /* computer moves some piece depending on auto_solve and auto_shuffle param */
464     if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
465              && p_sys->ps_puzzle_array != NULL && p_sys->s_current_param.b_advanced )
466     {
467         puzzle_auto_shuffle( p_filter );
468         puzzle_auto_solve( p_filter );
469     }
470
471     vlc_mutex_unlock( &p_sys->pce_lock );
472
473     /* draw output pic */
474     if ( !p_sys->b_bake_request && p_sys->b_init  && p_sys->ps_puzzle_array != NULL ) {
475
476         puzzle_draw_borders(p_filter, p_pic_in, p_pic_out);
477
478         p_sys->i_pointed_pce = NO_PCE;
479         puzzle_draw_pieces(p_filter, p_pic_in, p_pic_out);
480
481         /* when puzzle_draw_pieces() has not updated p_sys->i_pointed_pce,
482          * use puzzle_find_piece to define the piece pointed by the mouse
483          */
484         if (p_sys->i_pointed_pce == NO_PCE)
485             p_sys->i_mouse_drag_pce = puzzle_find_piece( p_filter, p_sys->i_mouse_x, p_sys->i_mouse_y, -1);
486         else
487             p_sys->i_mouse_drag_pce = p_sys->i_pointed_pce;
488
489         if (p_sys->s_current_param.b_preview )
490             puzzle_draw_preview(p_filter, p_pic_in, p_pic_out);
491
492         /* highlight the selected piece when not playing jigsaw mode */
493         if ( p_sys->i_selected != NO_PCE && !p_sys->s_current_param.b_blackslot
494                 && !p_sys->s_current_param.b_advanced )
495         {
496             int32_t c = (p_sys->i_selected % p_sys->s_allocated.i_cols);
497             int32_t r = (p_sys->i_selected / p_sys->s_allocated.i_cols);
498
499             puzzle_draw_rectangle(p_pic_out,
500                 p_sys->ps_puzzle_array[r][c][0].i_x,
501                 p_sys->ps_puzzle_array[r][c][0].i_y,
502                 p_sys->ps_puzzle_array[r][c][0].i_width,
503                 p_sys->ps_puzzle_array[r][c][0].i_lines,
504                 255, 127, 127);
505         }
506
507         /* draw the blackslot when playing sliding puzzle mode */
508         if ( p_sys->i_selected != NO_PCE && p_sys->s_current_param.b_blackslot
509                 && !p_sys->s_current_param.b_advanced )
510         {
511             int32_t c = (p_sys->i_selected % p_sys->s_allocated.i_cols);
512             int32_t r = (p_sys->i_selected / p_sys->s_allocated.i_cols);
513
514             puzzle_fill_rectangle(p_pic_out,
515                 p_sys->ps_puzzle_array[r][c][0].i_x,
516                 p_sys->ps_puzzle_array[r][c][0].i_y,
517                 p_sys->ps_puzzle_array[r][c][0].i_width,
518                 p_sys->ps_puzzle_array[r][c][0].i_lines,
519                 0, 127, 127);
520         }
521
522         /* Draw the 'puzzle_shuffle' button if the puzzle is finished */
523         if ( p_sys->b_finished )
524             puzzle_draw_sign(p_pic_out, 0, 0, SHUFFLE_WIDTH, SHUFFLE_LINES, ppsz_shuffle_button, false);
525
526         /* draw an arrow at mouse pointer to indicate current action (rotation...) */
527         if ((p_sys->i_mouse_drag_pce != NO_PCE) && !p_sys->b_mouse_drag
528                 && !p_sys->b_finished && p_sys->s_current_param.b_advanced )
529         {
530             vlc_mutex_lock( &p_sys->pce_lock );
531
532             int32_t i_delta_x;
533
534             if (p_sys->s_current_param.i_rotate != 3)
535                 i_delta_x = 0;
536             else if ( (p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_actual_angle & 1) == 0)
537                 i_delta_x = p_sys->ps_desk_planes[0].i_pce_max_width / 6;
538             else
539                 i_delta_x = p_sys->ps_desk_planes[0].i_pce_max_lines / 6;
540
541             if (p_sys->s_current_param.i_rotate == 0)
542                 p_sys->i_mouse_action = 0;
543             else if (p_sys->s_current_param.i_rotate == 1)
544                 p_sys->i_mouse_action = 2;
545             else if ( p_sys->i_mouse_x >= ( p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_center_x + i_delta_x) )
546                 p_sys->i_mouse_action = -1;  /* rotate counterclockwise */
547             else if ( p_sys->i_mouse_x <= ( p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_center_x - i_delta_x) )
548                 p_sys->i_mouse_action = +1;
549             else
550                 p_sys->i_mouse_action = 4;   /* center click: only mirror */
551
552             if ( p_sys->i_mouse_action == +1 )
553                 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
554                                  p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_rot_arrow_sign, false);
555             else if ( p_sys->i_mouse_action == -1 )
556                 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
557                                  p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_rot_arrow_sign, true);
558             else if ( p_sys->i_mouse_action == 4 )
559                 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
560                                  p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_mir_arrow_sign, false);
561
562             vlc_mutex_unlock( &p_sys->pce_lock );
563         }
564     }
565
566     return CopyInfoAndRelease( p_pic_out, p_pic_in );
567 }
568
569 /*****************************************************************************
570  * Misc stuff...
571  *****************************************************************************/
572 int puzzle_Callback( vlc_object_t *p_this, char const *psz_var,
573                            vlc_value_t oldval, vlc_value_t newval,
574                            void *p_data )
575 {
576     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
577     filter_sys_t *p_sys = (filter_sys_t *)p_data;
578
579     vlc_mutex_lock( &p_sys->lock );
580     if( !strcmp( psz_var, CFG_PREFIX "rows" ) ) {
581         p_sys->s_new_param.i_rows = __MAX( 1, newval.i_int );
582     }
583     else if( !strcmp( psz_var, CFG_PREFIX "cols" ) ) {
584         p_sys->s_new_param.i_cols = __MAX( 1, newval.i_int );
585     }
586     else if( !strcmp( psz_var, CFG_PREFIX "border" ) ) {
587         p_sys->s_new_param.i_border = __MAX( 0, newval.i_int );
588     }
589     else if( !strcmp( psz_var, CFG_PREFIX "preview" ) ) {
590         p_sys->s_new_param.b_preview = newval.b_bool;
591     }
592     else if( !strcmp( psz_var, CFG_PREFIX "preview-size" ) ) {
593         p_sys->s_new_param.i_preview_size = newval.i_int;
594     }
595     else if( !strcmp( psz_var, CFG_PREFIX "shape-size" ) ) {
596         p_sys->s_new_param.i_shape_size = newval.i_int;
597     }
598     else if( !strcmp( psz_var, CFG_PREFIX "auto-shuffle" ) ) {
599         p_sys->s_new_param.i_auto_shuffle_speed = newval.i_int;
600     }
601     else if( !strcmp( psz_var, CFG_PREFIX "auto-solve" ) ) {
602         p_sys->s_new_param.i_auto_solve_speed = newval.i_int;
603     }
604     else if( !strcmp( psz_var, CFG_PREFIX "rotation" ) ) {
605         p_sys->s_new_param.i_rotate = newval.i_int;
606     }
607     else if( !strcmp( psz_var, CFG_PREFIX "mode" ) ) {
608         p_sys->s_new_param.i_mode = newval.i_int;
609     }
610
611     p_sys->b_change_param = true;
612     vlc_mutex_unlock( &p_sys->lock );
613
614     return VLC_SUCCESS;
615 }
616
617 /* mouse callback */
618 int puzzle_mouse( filter_t *p_filter, vlc_mouse_t *p_mouse,
619                   const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
620 {
621     filter_sys_t *p_sys = p_filter->p_sys;
622     const video_format_t  *p_fmt_in = &p_filter->fmt_in.video;
623
624     /* Only take events inside the puzzle area */
625     if( p_new->i_x < 0 || p_new->i_x >= (int)p_fmt_in->i_width ||
626         p_new->i_y < 0 || p_new->i_y >= (int)p_fmt_in->i_height )
627         return VLC_EGENERIC;
628
629     if (! p_sys->b_init || p_sys->b_change_param) {
630         *p_mouse = *p_new;
631         return VLC_SUCCESS;
632     }
633
634     p_sys->i_mouse_x = p_new->i_x;
635     p_sys->i_mouse_y = p_new->i_y;
636
637     /* If the puzzle is finished, shuffle it if needed */
638     if( p_sys->b_finished ) {
639         p_sys->b_mouse_drag = false;
640         p_sys->b_mouse_mvt = false;
641         if( vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT ) &&
642             p_new->i_x < SHUFFLE_WIDTH && p_new->i_y < SHUFFLE_LINES )
643         {
644             p_sys->b_shuffle_rqst = true;
645             return VLC_EGENERIC;
646         }
647         else
648         {
649             /* otherwise we can forward the mouse */
650             *p_mouse = *p_new;
651             return VLC_SUCCESS;
652         }
653     }
654
655     if ( !p_sys->s_current_param.b_advanced ) {
656         /* "square" game mode (sliding puzzle, swap...) */
657         const bool b_clicked = vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT );
658
659         if( b_clicked )
660         {
661             /* */
662             const int32_t i_border_width = p_fmt_in->i_width * p_sys->s_current_param.i_border / 100 / 2;
663             const int32_t i_border_height = p_fmt_in->i_height * p_sys->s_current_param.i_border / 100 / 2;
664             const int32_t i_pos_x = (p_new->i_x - i_border_width) * p_sys->s_allocated.i_cols / (p_fmt_in->i_width - 2*i_border_width);
665             const int32_t i_pos_y = (p_new->i_y - i_border_height) * p_sys->s_allocated.i_rows / (p_fmt_in->i_height - 2*i_border_height);
666
667             const int32_t i_pos = i_pos_y * p_sys->s_allocated.i_cols + i_pos_x;
668             p_sys->i_mouse_drag_pce = i_pos;
669
670             /* do not take into account if border clicked */
671             if ((p_new->i_x <= i_border_width) || (p_new->i_y <=  i_border_height) || (p_new->i_x >= (int) p_fmt_in->i_width -  i_border_width) || (p_new->i_y >= (int) p_fmt_in->i_height -  i_border_height ) )
672             {
673                 *p_mouse = *p_new;
674                 return VLC_SUCCESS;
675             }
676             else if( p_sys->i_selected == NO_PCE )
677                 p_sys->i_selected = i_pos;
678             else if( p_sys->i_selected == i_pos && !p_sys->s_current_param.b_blackslot )
679                 p_sys->i_selected = -1;
680             else if( ( p_sys->i_selected == i_pos + 1 && p_sys->i_selected%p_sys->s_allocated.i_cols != 0 )
681                   || ( p_sys->i_selected == i_pos - 1 && i_pos % p_sys->s_allocated.i_cols != 0 )
682                   || p_sys->i_selected == i_pos +  p_sys->s_allocated.i_cols
683                   || p_sys->i_selected == i_pos -  p_sys->s_allocated.i_cols
684                   || !p_sys->s_current_param.b_near )
685
686             {
687                 /* Swap two pieces */
688                 int32_t a = p_sys->pi_order[ p_sys->i_selected ];
689                 p_sys->pi_order[ p_sys->i_selected ] = p_sys->pi_order[ i_pos ];
690                 p_sys->pi_order[ i_pos ] = a;
691
692                 /* regen piece location from updated pi_order */
693                 if ( p_sys->ps_pieces != NULL && p_sys->pi_order != NULL )
694                 {
695                     int32_t i = 0;
696                     for (int32_t row = 0; row < p_sys->s_allocated.i_rows; row++) {
697                         for (int32_t col = 0; col < p_sys->s_allocated.i_cols; col++) {
698                             int32_t orow = p_sys->pi_order[i] / (p_sys->s_allocated.i_cols);
699                             int32_t ocol = p_sys->pi_order[i] % (p_sys->s_allocated.i_cols);
700
701                             p_sys->ps_pieces[i].i_original_row = orow;
702                             p_sys->ps_pieces[i].i_original_col = ocol;
703                             p_sys->ps_pieces[i].i_top_shape    = 0;
704                             p_sys->ps_pieces[i].i_btm_shape    = 0;
705                             p_sys->ps_pieces[i].i_right_shape  = 0;
706                             p_sys->ps_pieces[i].i_left_shape   = 0;
707                             p_sys->ps_pieces[i].i_actual_angle = 0;
708                             p_sys->ps_pieces[i].i_actual_mirror = +1;
709                             p_sys->ps_pieces[i].b_overlap      = false;
710                             p_sys->ps_pieces[i].b_finished     = false;
711                             p_sys->ps_pieces[i].i_group_ID     = i;
712
713                             for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
714                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_width     = p_sys->ps_puzzle_array[row][col][i_plane].i_width;
715                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_lines     = p_sys->ps_puzzle_array[row][col][i_plane].i_lines;
716                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_x = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_x;
717                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_y = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_y;
718                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_x   = p_sys->ps_puzzle_array[row][col][i_plane].i_x;
719                                 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_y   = p_sys->ps_puzzle_array[row][col][i_plane].i_y;
720                             }
721                             i++;
722                         }
723                     }
724                 }
725
726                 p_sys->i_selected = p_sys->s_current_param.b_blackslot ? i_pos : NO_PCE;
727                 p_sys->b_finished = puzzle_is_finished( p_sys, p_sys->pi_order );
728             }
729         }
730     }
731     else /* jigsaw puzzle mode */
732     {
733         if ((p_sys->ps_desk_planes == NULL)  || (p_sys->ps_pict_planes == NULL)  || (p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL)) {
734             *p_mouse = *p_new;
735             return VLC_SUCCESS;
736         }
737
738         if( vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT ) )
739         {
740
741             vlc_mutex_lock( &p_sys->pce_lock );
742
743             if (p_sys->i_mouse_drag_pce != NO_PCE) {
744                 int i_ret = puzzle_piece_foreground( p_filter, p_sys->i_mouse_drag_pce);
745                 if (i_ret != VLC_SUCCESS)
746                     return i_ret;
747                 p_sys->i_mouse_drag_pce = 0;
748
749                 uint32_t i_group_ID = p_sys->ps_pieces[0].i_group_ID;
750                 for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
751                     if ( i_group_ID == p_sys->ps_pieces[i].i_group_ID ) {
752                         p_sys->ps_pieces[i].b_finished = false;
753                     }
754                     else {
755                         break;
756                     }
757                 }
758
759                 p_sys->b_mouse_drag = true;
760                 p_sys->b_mouse_mvt = false;
761             }
762             else {
763             /* player click an empty area then search a piece which is overlapping another one and place it here */
764                 p_sys->b_mouse_drag = false;
765                 for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++)
766                     if ( p_sys->ps_pieces[i].b_overlap ) {
767                         puzzle_move_group( p_filter, i, p_new->i_x - p_sys->ps_pieces[i].i_center_x,  p_new->i_y - p_sys->ps_pieces[i].i_center_y );
768                         p_sys->ps_pieces[i].b_overlap = false;
769                         break;
770                     }
771                 p_sys->b_mouse_drag = false;
772             }
773
774             vlc_mutex_unlock( &p_sys->pce_lock );
775
776         }
777         else if( vlc_mouse_HasReleased( p_old, p_new, MOUSE_BUTTON_LEFT ) )
778         {
779             if ( !p_sys->b_mouse_mvt && p_sys->b_mouse_drag ) {
780                 /* piece clicked without any mouse mvt => rotate it or mirror */
781                 if ( p_sys->s_current_param.i_rotate != 0) {
782                     vlc_mutex_lock( &p_sys->pce_lock );
783
784                     uint32_t i_group_ID = p_sys->ps_pieces[0].i_group_ID;
785
786                     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++)
787                         if ( i_group_ID == p_sys->ps_pieces[i].i_group_ID )
788                             puzzle_rotate_pce( p_filter, i, p_sys->i_mouse_action, p_sys->ps_pieces[0].i_center_x, p_sys->ps_pieces[0].i_center_y, p_sys->i_mouse_action != 4 ? true : false );
789
790                     vlc_mutex_unlock( &p_sys->pce_lock );
791                 }
792             }
793             p_sys->b_mouse_drag = false;
794             p_sys->b_mouse_mvt = false;
795         }
796         else /* no action on left button */
797         {
798             /* check if the mouse is in the preview area */
799             switch ( p_sys->i_preview_pos )
800             {
801               case 0:
802                 if ( p_new->i_x < (int)p_fmt_in->i_width / 2 && p_new->i_y < (int)p_fmt_in->i_height / 2 )
803                     p_sys->i_preview_pos++;
804                 break;
805               case 1:
806                 if ( p_new->i_x > (int)p_fmt_in->i_width / 2 && p_new->i_y < (int)p_fmt_in->i_height / 2 )
807                     p_sys->i_preview_pos++;
808                 break;
809               case 2:
810                 if ( p_new->i_x > (int)p_fmt_in->i_width / 2 && p_new->i_y > (int)p_fmt_in->i_height / 2 )
811                     p_sys->i_preview_pos++;
812                 break;
813               case 3:
814                 if ( p_new->i_x < (int)p_fmt_in->i_width / 2 && p_new->i_y > (int)p_fmt_in->i_height / 2 )
815                     p_sys->i_preview_pos++;
816                 break;
817             }
818             p_sys->i_preview_pos %= 4;
819
820             if ( !vlc_mouse_IsLeftPressed( p_new ) )
821                 p_sys->b_mouse_drag = false;
822
823             int i_dx, i_dy;
824             vlc_mouse_GetMotion( &i_dx, &i_dy, p_old, p_new );
825             if ( i_dx != 0 || i_dy != 0 )
826                 p_sys->b_mouse_mvt = true;
827
828             if (p_sys->b_mouse_drag) {
829                 if ( ( p_new->i_x <= 0 ) || ( p_new->i_y <=  0 ) || ( p_new->i_x >= (int) p_fmt_in->i_width )
830                         || ( p_new->i_y >= (int) p_fmt_in->i_height ) )
831                 {
832                     /* if the mouse is outside the window, stop moving the piece/group */
833                     p_sys->b_mouse_drag = false;
834                     p_sys->b_mouse_mvt = true;
835                 }
836                 else if ( i_dx != 0 || i_dy != 0 )
837                 {
838                     vlc_mutex_lock( &p_sys->pce_lock );
839
840                     puzzle_move_group( p_filter, p_sys->i_mouse_drag_pce, i_dx, i_dy);
841
842                     vlc_mutex_unlock( &p_sys->pce_lock );
843                 }
844             }
845         }
846     }
847     return VLC_EGENERIC;
848 }