Debian Codegraph @

slider/src/applet-slider.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <time.h>
26
27 #ifdef HAVE_EXIF
28 #include <libexif/exif-data.h>
29 #endif
30
31 #include "cairo-dock.h"
32
33 #include "applet-struct.h"
34 #include "applet-config.h"
35 #include "applet-transitions.h"
36 #include "applet-slider.h"
37
38 static void _cd_slider_get_exif_props (SliderImage *pImage);
39
40
41 //////////////
42 /// IMAGES ///
43 //////////////
44
45 static void cd_slider_free_image (SliderImage *pImage) {
46 if (pImage == NULL)
47 return;
48 g_free (pImage->cPath);
49 g_free (pImage);
50 }
51
52 static void cd_slider_free_images_list (GList *pList) {
53 g_list_foreach (pList, (GFunc) cd_slider_free_image, NULL);
54 g_list_free (pList);
55 }
56
57
58 static void _cd_slider_load_image (GldiModuleInstance *myApplet)
59 {
60 g_return_if_fail (myData.pElement != NULL);
61 SliderImage *pImage = myData.pElement->data;
62 gchar *cImagePath = pImage->cPath;
63 if (!pImage->bGotExifData && myData.iSidExifIdle == 0) // no exif data yet and no process currently retrieving them.
64 _cd_slider_get_exif_props (pImage);
65 cd_debug (" Slider - loading %s (size %dbytes, orientation:%d)", cImagePath, pImage->iSize, pImage->iOrientation);
66 //\_______________ On definit comment charger l'image.
67 double fImgX, fImgY, fImgW=0, fImgH=0;
68 CairoDockLoadImageModifier iLoadingModifier = 0; // CAIRO_DOCK_FILL_SPACE
69 if (pImage->iOrientation != 0)
70 iLoadingModifier |= ((pImage->iOrientation-1) << 3);
71 if (! myConfig.bFillIcon)
72 iLoadingModifier |= CAIRO_DOCK_DONT_ZOOM_IN;
73 if (myConfig.bNoStretch)
74 iLoadingModifier |= CAIRO_DOCK_KEEP_RATIO;
75
76 //\_______________ On cree la surface cairo.
77 int iLineWidth = 0;
78 if (myConfig.iBackgroundType == 2)
79 {
80 iLineWidth = _get_frame_linewidth (myApplet);
81 }
82
83 myData.pCairoSurface = cairo_dock_create_surface_from_image (cImagePath,
84 1.,
85 myData.iSurfaceWidth - iLineWidth, // iLineWidth/2 de chaque cote
86 myData.iSurfaceHeight - iLineWidth, // idem
87 iLoadingModifier,
88 &fImgW, &fImgH,
89 NULL, NULL);
90
91 //\_______________ On garde l'aire de la surface/texture.
92 fImgX = (myData.iSurfaceWidth - fImgW) / 2;
93 fImgY = (myData.iSurfaceHeight - fImgH) / 2;
94 myData.slideArea.fImgX = fImgX;
95 myData.slideArea.fImgY = fImgY;
96 myData.slideArea.fImgW = fImgW;
97 myData.slideArea.fImgH = fImgH;
98 cd_debug (" %s loaded", cImagePath);
99 }
100
101 static gboolean _cd_slider_display_image (GldiModuleInstance *myApplet)
102 {
103 CD_APPLET_ENTER;
104 //\_______________ On cree la texture (en-dehors du thread).
105 if (g_bUseOpenGL)
106 {
107 myData.iTexture = cairo_dock_create_texture_from_surface (myData.pCairoSurface);
108 }
109
110 //\______________________ On choisit la transition.
111 if (myConfig.iAnimation == SLIDER_RANDOM) {
112 srand(time(NULL));
113 myData.iAnimation = 1 + (rand() % (SLIDER_RANDOM-1)); //Skip the default animation (1+)
114 }
115 else {
116 myData.iAnimation = myConfig.iAnimation;
117 }
118
119 //\______________________ On initialise la transition.
120 myData.iAnimCNT = 0;
121 myData.sens = 1;
122
123 //\______________________ On lance la transition.
124 if (myConfig.iAnimation != SLIDER_DEFAULT) // on lance l'animation.
125 {
126 cairo_dock_launch_animation (myContainer);
127 }
128 else // on dessine tout de suite et on attend l'image suivante.
129 {
130 cd_slider_draw_default (myApplet);
131 CD_APPLET_REDRAW_MY_ICON;
132 cd_slider_schedule_next_slide (myApplet);
133 }
134 CD_APPLET_LEAVE (FALSE);
135 }
136
137 void cd_slider_jump_to_next_slide (GldiModuleInstance *myApplet)
138 {
139 //\___________________________ stop any pending action.
140 if (myData.iTimerID != 0)
141 {
142 g_source_remove (myData.iTimerID);
143 myData.iTimerID = 0;
144 }
145 gldi_task_stop (myData.pMeasureImage);
146
147 //\___________________________ On recupere la nouvelle image a afficher.
148 if (myData.pElement == NULL) // debut
149 myData.pElement = myData.pList;
150 else
151 myData.pElement = cairo_dock_get_next_element (myData.pElement, myData.pList);
152
153 if (myData.pElement == NULL || myData.pElement->data == NULL)
154 {
155 cd_warning ("Slider stopped, empty list");
156 return;
157 }
158 SliderImage *pImage = myData.pElement->data;
159 cd_message ("Slider - load %s", pImage->cPath);
160
161 //\___________________________ On sauvegarde la surface actuelle.
162 if (myData.pPrevCairoSurface != NULL && myData.pPrevCairoSurface != myData.pCairoSurface)
163 cairo_surface_destroy (myData.pPrevCairoSurface);
164 myData.pPrevCairoSurface = myData.pCairoSurface;
165 myData.pCairoSurface = NULL;
166
167 if (myData.iPrevTexture != 0 && myData.iPrevTexture != myData.iTexture)
168 glDeleteTextures (1, &myData.iPrevTexture);
169 myData.iPrevTexture = myData.iTexture;
170 myData.iTexture = 0;
171
172 myData.prevSlideArea = myData.slideArea;
173
174 //\___________________________ On ecrit le nom de la nouvelle image en info.
175 if (myConfig.bImageName && myDesklet)
176 {
177 gchar *cFilePath = g_strdup (pImage->cPath);
178 gchar *cFileName = strrchr (cFilePath, '/');
179 if (cFileName)
180 cFileName ++;
181 else
182 cFileName = cFilePath;
183 gchar *ext = strrchr (cFileName, '.');
184 if (ext)
185 *ext = '\0';
186 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (cFileName);
187 //cd_debug ("Slider - Image path: %s", pImage->cPath);
188 g_free (cFilePath);
189 }
190
191 //\___________________________ On charge la nouvelle surface/texture et on lance l'animation de transition.
192 if (myConfig.bUseThread && CD_APPLET_MY_CONTAINER_IS_OPENGL && pImage->iFormat != SLIDER_SVG && // pour certains SVG, ca plante dans X :-(
193 ((pImage->iFormat == SLIDER_PNG && pImage->iSize > 100e3)
194 || (pImage->iFormat == SLIDER_JPG && pImage->iSize > 70e3)
195 || (pImage->iFormat == SLIDER_GIF && pImage->iSize > 100e3)
196 || (pImage->iFormat == SLIDER_XPM && pImage->iSize > 100e3)) )
197 {
198 cd_debug ("Slider - launch thread");
199 gldi_task_launch (myData.pMeasureImage);
200 }
201 else
202 {
203 _cd_slider_load_image (myApplet);
204 _cd_slider_display_image (myApplet);
205 }
206 }
207
208
209 static gboolean _next_slide (GldiModuleInstance *myApplet)
210 {
211 CD_APPLET_ENTER;
212
213 myData.iTimerID = 0; // we'll quit this loop anyway, so reset the timer now so that it doesn't disturb us.
214
215 if (! myData.bPause) // if paused, don't go to next slide.
216 {
217 cd_slider_jump_to_next_slide (myApplet);
218 }
219
220 CD_APPLET_LEAVE (FALSE);
221 }
222 void cd_slider_schedule_next_slide (GldiModuleInstance *myApplet)
223 {
224 if (myData.iTimerID == 0)
225 myData.iTimerID = g_timeout_add_seconds (myConfig.iSlideTime, (GSourceFunc) _next_slide, (gpointer) myApplet);
226 }
227
228
229 /////////////////
230 /// EXIF DATA ///
231 /////////////////
232
233 static void _cd_slider_get_exif_props (SliderImage *pImage)
234 {
235 #ifdef HAVE_EXIF
236 ExifData *pExifData;
237 ExifEntry *pExifEntry;
238 ExifByteOrder byteOrder;
239
240 if (pImage->iFormat == SLIDER_JPG)
241 {
242 //g_print ("Slider : %s\n", pImage->cPath);
243 pExifData = exif_data_new_from_file (pImage->cPath);
244 if (pExifData != NULL)
245 {
246 pExifEntry = exif_data_get_entry (pExifData, EXIF_TAG_ORIENTATION);
247 if (pExifEntry != NULL)
248 {
249 byteOrder = exif_data_get_byte_order (pExifData);
250 pImage->iOrientation = exif_get_short (pExifEntry->data, byteOrder);
251 //g_print ("Slider : %s -> orientation %d\n", pImage->cPath, pImage->iOrientation);
252 }
253
254 exif_data_unref (pExifData);
255 }
256 }
257 #endif
258 pImage->bGotExifData = TRUE;
259 }
260
261 static gboolean _cd_slider_get_exif_props_idle (GldiModuleInstance *myApplet)
262 {
263 #ifdef HAVE_EXIF
264 if (myData.pExifElement == NULL)
265 {
266 myData.iSidExifIdle = 0;
267 return FALSE;
268 }
269
270 SliderImage *pImage = myData.pExifElement->data;
271 _cd_slider_get_exif_props (pImage);
272
273 myData.pExifElement = myData.pExifElement->next;
274 return TRUE;
275 #else
276 myData.pExifElement = NULL;
277 myData.iSidExifIdle = 0;
278 return FALSE;
279 #endif
280 }
281
282
283 //////////////////////
284 /// FOLDER PARSING ///
285 //////////////////////
286
287 typedef struct {
288 gchar *cDirectory;
289 gboolean bSubDirs;
290 gboolean bRandom;
291 GList *pList; // the result
292 GldiModuleInstance *pApplet;
293 } CDListSharedMemory;
294
295 static void _free_shared_memory (CDListSharedMemory *pSharedMemory)
296 {
297 g_free (pSharedMemory->cDirectory);
298 // cd_slider_free_images_list (pSharedMemory->pList); // should be the same as myData.pList
299 g_free (pSharedMemory);
300 }
301
302 static int _compare_images_order (SliderImage *image2, SliderImage *image1)
303 {
304 if (image1->cPath == NULL)
305 return -1;
306 if (image2->cPath == NULL)
307 return 1;
308 gchar *cURI_1 = g_ascii_strdown (image1->cPath, -1);
309 gchar *cURI_2 = g_ascii_strdown (image2->cPath, -1);
310 int iOrder = strcmp (cURI_1, cURI_2);
311 g_free (cURI_1);
312 g_free (cURI_2);
313 return iOrder;
314 }
315 static int _cd_slider_random_compare (gconstpointer a, gconstpointer b, GRand *pRandomGenerator)
316 {
317 return (g_rand_boolean (pRandomGenerator) ? 1 : -1);
318 }
319 static GList *_list_directory (GList *pList, gchar *cDirectory, gboolean bRecursive, gboolean bSortAlpha)
320 {
321 cd_debug ("%s (%s)", __func__, cDirectory);
322 GError *erreur = NULL;
323 GDir *dir = g_dir_open (cDirectory, 0, &erreur);
324 if (erreur != NULL) {
325 cd_warning ("Slider : %s", erreur->message);
326 g_error_free (erreur);
327 return pList;
328 }
329 struct stat buf;
330 SliderImage *pImage;
331 SliderImageFormat iFormat;
332 const gchar *cFileName, *extension;
333 GString *sFilePath = g_string_new ("");
334 while ((cFileName = g_dir_read_name (dir)) != NULL) {
335 g_string_printf (sFilePath, "%s/%s", cDirectory, cFileName);
336 if (stat (sFilePath->str, &buf) != -1) {
337 if (S_ISDIR (buf.st_mode) && bRecursive) {
338 pList = _list_directory (pList, sFilePath->str, bRecursive, bSortAlpha);
339 }
340 else {
341 extension = strrchr(cFileName,'.');
342 if (extension != NULL) {
343 iFormat = SLIDER_UNKNOWN_FORMAT; // le but du format serait de definir un seuil de taille pour chaque format a partir duquel l'image devrait etre chargee par un thread.
344 if (g_ascii_strcasecmp(extension, ".png") == 0)
345 iFormat = SLIDER_PNG;
346 else if (g_ascii_strcasecmp(extension, ".jpg") == 0 || g_ascii_strcasecmp(extension, ".jpeg") == 0)
347 iFormat = SLIDER_JPG;
348 else if (g_ascii_strcasecmp(extension, ".svg") == 0)
349 iFormat = SLIDER_SVG;
350 else if (g_ascii_strcasecmp(extension, ".gif") == 0)
351 iFormat = SLIDER_GIF;
352 else if (g_ascii_strcasecmp(extension, ".xpm") == 0)
353 iFormat = SLIDER_XPM;
354
355 if (iFormat != SLIDER_UNKNOWN_FORMAT) {
356 cd_debug ("Slider - Adding %s to list", cFileName);
357 pImage = g_new0 (SliderImage, 1);
358 pImage->cPath = g_strdup (sFilePath->str);
359 pImage->iSize = buf.st_size;
360 pImage->iFormat = iFormat;
361 // exif orientation is got later.
362 if (bSortAlpha) // ordre alphabetique.
363 pList = g_list_insert_sorted (pList, pImage, (GCompareFunc) _compare_images_order);
364 else // on randomise a la fin.
365 pList = g_list_prepend (pList, pImage);
366 }
367
368 }
369 }
370 }
371 }
372
373 g_string_free (sFilePath, TRUE);
374 g_dir_close (dir);
375 return pList;
376 }
377
378 static void cd_slider_get_files_from_dir (CDListSharedMemory *pSharedMemory)
379 {
380 if (pSharedMemory->cDirectory == NULL)
381 {
382 cd_warning ("Slider : No directory to scan, halt.");
383 return;
384 }
385
386 // recursively scan the folder
387 pSharedMemory->pList = _list_directory (NULL, pSharedMemory->cDirectory, pSharedMemory->bSubDirs, ! pSharedMemory->bRandom);
388
389 // randomize the list if necessary
390 if (pSharedMemory->bRandom)
391 {
392 GRand *pRandomGenerator = g_rand_new ();
393 pSharedMemory->pList = g_list_sort_with_data (pSharedMemory->pList, (GCompareDataFunc) _cd_slider_random_compare, pRandomGenerator);
394 g_rand_free (pRandomGenerator);
395 }
396 }
397
398 static gboolean cd_slider_start_slide (CDListSharedMemory *pSharedMemory)
399 {
400 // grab the result
401 GldiModuleInstance *myApplet = pSharedMemory->pApplet;
402 myData.pList = pSharedMemory->pList;
403
404 // if needed, get the EXIF data now
405 if (myData.iSidExifIdle == 0 && myConfig.bGetExifDataAtOnce)
406 {
407 myData.pExifElement = myData.pList;
408 myData.iSidExifIdle = g_idle_add_full (G_PRIORITY_LOW, // on ne veut pas que ca vienne ralentir le chargement des icones, qui lui a une priorite normale.
409 (GSourceFunc) _cd_slider_get_exif_props_idle,
410 myApplet,
411 NULL);
412 }
413
414 // create the loading task that will be used later
415 myData.pMeasureImage = gldi_task_new (0,
416 (GldiGetDataAsyncFunc) _cd_slider_load_image,
417 (GldiUpdateSyncFunc) _cd_slider_display_image,
418 myApplet); // 0 <=> one shot task.
419
420 // display the first slide.
421 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
422 if (myData.pList != NULL)
423 {
424 cd_slider_jump_to_next_slide (myApplet);
425 }
426 else // no images found or folder not defined => no need to launch the loop, display a 'missing' icon
427 {
428 CD_APPLET_SET_IMAGE_ON_MY_ICON (GLDI_ICON_NAME_MISSING_IMAGE);
429 }
430 return FALSE;
431 }
432
433
434 void cd_slider_start (GldiModuleInstance *myApplet, gboolean bDelay)
435 {
436 gldi_task_discard (myData.pMeasureDirectory);
437
438 // remember the current params.
439 g_free (myData.cDirectory);
440 myData.cDirectory = g_strdup (myConfig.cDirectory);
441 myData.bSubDirs = myConfig.bSubDirs;
442 myData.bRandom = myConfig.bRandom;
443
444 // create a task to parse the folder.
445 CDListSharedMemory *pSharedMemory = g_new0 (CDListSharedMemory, 1);
446 pSharedMemory->bSubDirs = myConfig.bSubDirs;
447 pSharedMemory->bRandom = myConfig.bRandom;
448 pSharedMemory->cDirectory = g_strdup (myConfig.cDirectory);
449 pSharedMemory->pApplet = myApplet;
450
451 CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("..."); // Note: we could launch a 'busy' animation, but it's maybe not a good idea to add more CPU load (anyway, the animation should be launched with a g_idle_add, because animations may not be inited yet).
452 myData.pMeasureDirectory = gldi_task_new_full (0,
453 (GldiGetDataAsyncFunc) cd_slider_get_files_from_dir,
454 (GldiUpdateSyncFunc) cd_slider_start_slide,
455 (GFreeFunc) _free_shared_memory,
456 pSharedMemory); // 0 <=> one shot task.
457
458 // launch the parsing.
459 if (bDelay)
460 gldi_task_launch_delayed (myData.pMeasureDirectory, cairo_dock_is_loading () ? 1500. : 0.); // launch with a delay or just in the next main loop event.
461 else
462 gldi_task_launch (myData.pMeasureDirectory);
463 }
464
465 void cd_slider_stop (GldiModuleInstance *myApplet)
466 {
467 //Stop all processes
468 gldi_task_free (myData.pMeasureImage); // since it needs a cairo context, we can't let it live.
469 myData.pMeasureImage = NULL;
470 gldi_task_discard (myData.pMeasureDirectory);
471 myData.pMeasureDirectory = NULL;
472 if (myData.iSidExifIdle != 0)
473 {
474 g_source_remove(myData.iSidExifIdle);
475 myData.iSidExifIdle = 0;
476 }
477 if (myData.iScrollID != 0)
478 {
479 g_source_remove (myData.iScrollID);
480 myData.iScrollID = 0;
481 }
482 if (myData.iTimerID != 0)
483 {
484 g_source_remove(myData.iTimerID);
485 myData.iTimerID = 0;
486 }
487
488 // destroy current buffers.
489 if (myData.pCairoSurface)
490 {
491 cairo_surface_destroy (myData.pCairoSurface);
492 myData.pCairoSurface = NULL;
493 }
494 if (myData.pPrevCairoSurface)
495 {
496 cairo_surface_destroy (myData.pPrevCairoSurface);
497 myData.pPrevCairoSurface = NULL;
498 }
499 if (myData.iPrevTexture != 0)
500 {
501 _cairo_dock_delete_texture (myData.iPrevTexture);
502 myData.iPrevTexture = 0;
503 }
504 if (myData.iTexture != 0)
505 {
506 _cairo_dock_delete_texture (myData.iTexture);
507 myData.iTexture = 0;
508 }
509
510 cd_slider_free_images_list (myData.pList);
511 myData.pList = NULL;
512 myData.pElement = NULL;
513 myData.pExifElement = NULL;
514 myData.bPause = FALSE;
515 }