summaryrefslogtreecommitdiff
path: root/main/src/data/ivug-data-shuffle.c
blob: fa4d07db50dd93b3d840ad5c7a2d006563f84c6e (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "ivug-common.h"
#include "ivug-data.h"
#include "ivug-data-priv.h"
#include "ivug-data-shuffle.h"

static void _swap_eina_list(Eina_List* list, int i, int j)
{
	Eina_List *first;
	Eina_List *second;
	int temp;

	if ( i == j ) return ;

	first = eina_list_nth_list(list, i);
	second = eina_list_nth_list(list, j);

	temp = (int)(first->data);
	first->data = second->data;
	second->data = (void *)temp;
}

void _shuffle_eina_list(Eina_List *list, int count)
{
	int i;
	int r;

	IV_ASSERT(list != NULL);

	srand((unsigned)time(NULL));

	for(i=0; i<count; i++)
	{
		r = rand()%count;
		//MSG_UTIL_HIGH("rand = %d", r);
		_swap_eina_list(list, i, r);
	}
}

static Eina_List *_create_eina_list(int count)
{
	int i;

	Eina_List *list = NULL;

	for(i=1; i <= count; i++)	/* 1 ~ total count */
	{
		list = eina_list_append(list, (void *)i);
	}

	_shuffle_eina_list(list, count);

	return list;
}


bool ivug_data_init_shuffle_slide_show(Media_List *mList, slide_show_mode ss_mode, int cur_index, int total_count)
{
	IV_ASSERT(mList != NULL);

	Eina_List *item = NULL;
	_Media_List *_mList = (_Media_List *)mList;

	if(total_count == 1 && !(ss_mode & SLIDE_SHOW_MODE_REPEAT))
	{
		return false;
	}

	item = eina_list_data_find_list(_mList->ss_shuffle_header, (void *
)cur_index);
	if(item == NULL)
	{
		MSG_SDATA_ERROR("Can't find cur index item %d", cur_index);
		item = _mList->ss_shuffle_header;
	}
	 _mList->ss_shuffle_end = item;
	 _mList->ss_shuffle_current = eina_list_next(item);

	 if( _mList->ss_shuffle_current == NULL)
	 {
	 	_mList->ss_shuffle_current = _mList->ss_shuffle_header; /* back to first */
	 }

	 return true;
}

void ivug_data_deinit_shuffle_slide_show(Media_List *mList, int total_count)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	_mList->ss_shuffle_end = NULL;
	_mList->ss_shuffle_current = NULL;

	_shuffle_eina_list(_mList->ss_shuffle_header, total_count);
}

void ivug_data_create_shuffled_list(Media_List *mList, int total_count)
{
	_Media_List *_mList = (_Media_List *)mList;

	_mList->ss_shuffle_header = _create_eina_list(total_count);
	_shuffle_eina_list(_mList->ss_shuffle_header, total_count);		// Shuffle

}

int ivug_data_get_next_slide_show_index(Media_List *mList, slide_show_mode ss_mode, int next_index, int total_count, bool increase_flag)
{
	IV_ASSERT(mList != NULL);
	_Media_List *_mList = (_Media_List *)mList;
	int index = -1;
	Eina_List *item = NULL;
	Eina_List *list = NULL;

	if(ss_mode & SLIDE_SHOW_MODE_SHUFFLE)
	{
		item = _mList->ss_shuffle_current;

		list = eina_list_next(item);
		if ( list == NULL)
		{
			list = _mList->ss_shuffle_header;	/* back to start */
		}
		if(list == _mList->ss_shuffle_end && !(ss_mode & SLIDE_SHOW_MODE_REPEAT))
		{
			if(increase_flag == true)
			{
				_mList->ss_shuffle_current = NULL;
			}
			return -1;
		}
		if(increase_flag == true)
		{
			_mList->ss_shuffle_current = list;
		}
		index = (int)eina_list_data_get(list);
	}
	else
	{
		index = next_index + 1;
		if(index > total_count && (ss_mode & SLIDE_SHOW_MODE_REPEAT) )
		{
			index = 1;	/* back to start */
		}
	}
	return index;
}

Media_Item * ivug_data_get_current_slide_show_item(Media_List *mList, slide_show_mode ss_mode, int cur_index, int *next_index, int total_count)
{
	IV_ASSERT(mList != NULL);

	Eina_List *item = NULL;
	int index;
	Media_Item *slide_show_item = NULL;
	_Media_List *_mList = (_Media_List *)mList;

	if(ss_mode & SLIDE_SHOW_MODE_SHUFFLE)
	{
		item = _mList->ss_shuffle_current;

		index = (int)eina_list_data_get(item);
	}
	else
	{
		index = cur_index + 1;
		if(index > total_count && (ss_mode & SLIDE_SHOW_MODE_REPEAT) )
		{
			index = 1;	/* back to start */
		}
	}
	MSG_SDATA_HIGH("*******current shuffle index is %d", index);

	slide_show_item = _ivug_data_create_window_size_list(mList, index, total_count);
	if(slide_show_item == NULL)
	{
		MSG_SDATA_HIGH("*******current shuffle item is NULL");
		return NULL;
	}
	 *next_index = index;

	return slide_show_item;
}

Media_Item * ivug_data_set_next_slide_show_item(Media_List *mList, slide_show_mode ss_mode, int *next_index, int total_count)
{
	IV_ASSERT(mList != NULL);

	int index;
	Media_Item *slide_show_item = NULL;

	index = ivug_data_get_next_slide_show_index(mList, ss_mode, *next_index, total_count, false);
	if(index < 0)
	{
		MSG_SDATA_HIGH("*******Next slide show item is NULL");
		return NULL;
	}

	MSG_SDATA_HIGH("*******Next slide show index is %d", index);

	slide_show_item = _ivug_data_create_window_size_list(mList, index, total_count);
	if(slide_show_item == NULL)
	{
		MSG_SDATA_HIGH("*******Next slide show item is NULL");
		return NULL;
	}
	ivug_data_append_media_item(mList, slide_show_item);

	return slide_show_item;
}


Media_Item *
ivug_data_get_next_shuffle_item(Media_List *mList)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	return _mList->ss_shuffle_current;	// current is set at next slide
}