File: image.c

package info (click to toggle)
telak 0.5-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 140 kB
  • ctags: 66
  • sloc: ansic: 821; makefile: 69
file content (290 lines) | stat: -rw-r--r-- 5,346 bytes parent folder | download | duplicates (3)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * telak - A program that display pictures in root window
 * (c) 2005 - Julien Danjou <julien@danjou.info>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 */

/* C stuff */
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

/* gcrypt stuff */
#include <gcrypt.h>

#include "telak.h"
#include "image.h"
#include "fetch.h"

extern struct deskitem *head;
extern struct config conf;
extern Display *disp;
extern Window win;


void
display(struct deskitem *di)
{
  if(!di->loaded)
	return;

  imlib_context_set_image(di->image);
  
  imlib_render_image_on_drawable_at_size(di->x,
										 di->y,
										 di->w,
										 di->h);
}

void
draw()
{
  struct deskitem *probe;
  XEvent ev;
  struct timeval tv_current;

  /* Init clocks */
  probe = head;
  do
  {
	gettimeofday(&(probe->tv), NULL);
	probe = probe->next;
  }
  while(probe != NULL);

  /* Display everything ! */
  for(probe = head; probe != NULL; probe = probe->next)
	display(probe);

  /* Main loop, should never end */
  while(1)
  {
	/* X events ? */
	while(XPending(disp))
	{
	  XNextEvent(disp, &ev);
	  
	  if(ev.type == Expose)
		for(probe = head; probe != NULL; probe = probe->next)
		  display(probe);
	}

	/* What time is it ? */
	if(gettimeofday(&tv_current, NULL))
	{
	  perror("Error while getting time of day. Buy a clock.");
	  exit(EXIT_FAILURE);
	}

	/* Check timers */
	probe = head;
	do
	{
	  if(probe->refresh != 0 &&
		 tv_current.tv_sec - probe->tv.tv_sec >= probe->refresh)
	  {
		load_img(probe);
		
		display(probe);

		gettimeofday(&(probe->tv), NULL);

	  }
	  probe = probe->next;
	}
	while(probe != NULL);
	
	/* Sleep a little ZzzZZzz */
	usleep(1000);
  }
}

int
load(struct deskitem *img)
{
  struct deskitem * probe;

  /* if there isn't a linked list yet, start one */
  if (head == NULL)
  {
	head = (struct deskitem *) malloc(sizeof(struct deskitem));
	
	if(head == NULL)
	{
	  perror("Cannot allocate memory!");
	  exit(EXIT_FAILURE);
	}
	
	probe = head;
	probe->next = NULL;
  }
  else
  {
	/* find the end of the linked list and add a new deskitem */
	probe = head;
	  
	while (probe->next != NULL)
	  probe = probe->next;
	
	probe->next = (struct deskitem *) malloc(sizeof(struct deskitem));
	  
	if(probe->next == NULL)
	{
	  perror("Cannot allocate memory!");
	  exit(EXIT_FAILURE);
	}
	
	probe = probe->next;
	probe->next = NULL;
  }
  
  probe->url = strdup(img->url);
  probe->file = NULL;
  probe->image = NULL;
  probe->loaded = 0;

  /* set reverse */
  probe->reverse = img->reverse;

  /* Load img->file */
  load_img(probe);

  probe->w = img->w;
  probe->h = img->h;

  if(probe->loaded)
  {
	/* if no size is given, use default one */
	if(!probe->h)
	  probe->h = imlib_image_get_height();
	
	if(!probe->w)
	  probe->w = imlib_image_get_width();
  }
  
  /* set the x,y location*/
  probe->x = img->x;
  probe->y = img->y;

  /* set refresh time */
  probe->refresh = img->refresh;

  return 0;
}

int
load_img(struct deskitem *img)
{
  unsigned char * buf;
  unsigned int i, hash_size;
  char * md5;
  char tmp[3];
  struct stat st;
  DATA8 r_table[256];
  DATA8 g_table[256];
  DATA8 b_table[256];
  DATA8 a_table[256];
  Imlib_Color_Modifier color_mod;
  
  if(img->url[0] != '/')
  {
	if(!img->file)
	{
	  hash_size = gcry_md_get_algo_dlen(GCRY_MD_MD5);
	  
	  /* Compute md5 hash of url */
	  buf = (char *) malloc(hash_size * sizeof(char));
	  md5 = (char *) malloc(sizeof(char) * (hash_size * 2 + 1));
	  
	  gcry_md_hash_buffer(GCRY_MD_MD5, buf, (char *) img->url, strlen(img->url));
	  
	  for(i = 0 ; i < hash_size ; i++)
	  {
		snprintf(tmp, 3, "%.2x", buf[i]);
		md5[i*2] = tmp[0];
		md5[i*2+1] = tmp[1];
	  }
	  md5[hash_size*2] = '\0';
	  
	  img->file = (char *) malloc(sizeof(char) *
								  (strlen(conf.cache_dir) + strlen(md5) + 2));
	  strcpy(img->file, conf.cache_dir);
	  strcat(img->file, "/");
	  strcat(img->file, md5);
	}
	
	fetch(img->url, img->file);
  }
  else
	img->file = strdup(img->url);
  
  
  /* Check if the file is openable */
  if(stat(img->file, &st))
  {
	fprintf(stderr, "%s ", img->file);
	perror("unable to stat() file");
	return 1;
  }
  
  /* We do not load weird things with imlib */
  if(!S_ISREG(st.st_mode))
  {
	fprintf(stderr, "%s is not a regular file.\n", img->file);
	return 1;
  }

  /* Unload if already loaded */
  if(img->loaded)
  {
	imlib_context_set_image(img->image);
	imlib_free_image_and_decache();
  }

  /* Now really load the file with imlib */
  img->image = imlib_load_image(img->file);
  
  if(img->image)
  {
	imlib_context_set_image(img->image);
	img->loaded = 1;

	color_mod = imlib_create_color_modifier();
	imlib_context_set_color_modifier(color_mod);
	imlib_reset_color_modifier();


	if(img->reverse)
	{
	  imlib_get_color_modifier_tables(r_table, g_table, b_table, a_table);
	  
	  for(i = 0; i <= 255; i++)
	  {
		r_table[255-i] = i;
		g_table[255-i] = i;
		b_table[255-i] = i;
	  }
	  
	  imlib_set_color_modifier_tables(r_table, g_table, b_table, a_table);
	  
	  imlib_apply_color_modifier();
	}
	
	imlib_free_color_modifier();
	
	return 0;
  }
  
  return 1;
}