Added basic LED stuff to libpnd pnd_device
[pandora-libraries.git] / lib / pnd_desktop.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <string.h>
4 #include <unistd.h> /* for unlink */
5 #include <stdlib.h> /* for free */
6
7 #include "pnd_apps.h"
8 #include "pnd_container.h"
9 #include "pnd_pxml.h"
10 #include "pnd_discovery.h"
11 #include "pnd_pndfiles.h"
12 #include "pnd_conf.h"
13 #include "pnd_desktop.h"
14
15 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
16   char filename [ FILENAME_MAX ];
17   char buffer [ 1024 ];
18   FILE *f;
19
20   // specification
21   // http://standards.freedesktop.org/desktop-entry-spec
22
23   // validation
24
25   if ( ! p -> unique_id ) {
26     return ( 0 );
27   }
28
29   if ( ! p -> exec ) {
30     return ( 0 );
31   }
32
33   if ( ! targetpath ) {
34     return ( 0 );
35   }
36
37   if ( ! pndrun ) {
38     return ( 0 );
39   }
40
41   // set up
42
43   sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
44
45   // emit
46
47   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
48
49   f = fopen ( filename, "w" );
50
51   if ( ! f ) {
52     return ( 0 );
53   }
54
55   fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
56
57   if ( p -> title_en ) {
58     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
59     fprintf ( f, "%s", buffer );
60   }
61
62   fprintf ( f, "Type=Application\n" );
63   fprintf ( f, "Version=1.0\n" );
64
65   if ( p -> icon ) {
66     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
67     fprintf ( f, "%s", buffer );
68   }
69
70   if ( p -> unique_id ) {
71     fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
72   }
73
74 #if 1
75   if ( p -> desc_en && p -> desc_en [ 0 ] ) {
76     snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
77     fprintf ( f, "%s", buffer );
78   }
79 #endif
80
81 #if 0 // we let pnd_run.sh handle this
82   if ( p -> startdir ) {
83     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
84     fprintf ( f, "%s", buffer );
85   } else {
86     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
87   }
88 #endif
89
90   if ( p -> exec ) {
91     char *nohup;
92
93     if ( p -> option_no_x11 ) {
94       nohup = "/usr/bin/nohup ";
95     } else {
96       nohup = "";
97     }
98
99     // basics
100     if ( p -> object_type == pnd_object_type_directory ) {
101       snprintf ( buffer, 1020, "Exec=%s%s -p %s -e %s -b %s",
102                  nohup, pndrun, p -> object_path, p -> exec, p -> unique_id );
103     } else if ( p -> object_type == pnd_object_type_pnd ) {
104       snprintf ( buffer, 1020, "Exec=%s%s -p %s/%s -e %s -b %s",
105                  nohup, pndrun, p -> object_path, p -> object_filename, p -> exec, p -> unique_id );
106     }
107
108     // start dir
109     if ( p -> startdir ) {
110       strncat ( buffer, " -s ", 1020 );
111       strncat ( buffer, p -> startdir, 1020 );
112     }
113
114     // exec options
115     if ( p -> option_no_x11 ) {
116       strncat ( buffer, " -x ", 1020 );
117     }
118
119     // newline
120     strncat ( buffer, "\n", 1020 );
121
122     // emit
123     fprintf ( f, "%s", buffer );
124   }
125
126   // categories
127   {
128     char cats [ 512 ] = "";
129     int n;
130     pnd_conf_handle c;
131     char *confpath;
132
133     // uuuuh, defaults?
134     // "Application" used to be in the standard and is commonly supported still
135     // Utility and Network should ensure the app is visible 'somewhere' :/
136     char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;
137
138     // determine searchpath (for conf, not for apps)
139     confpath = pnd_conf_query_searchpath();
140
141     // inhale the conf file
142     c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );
143
144     // if we can find a default category set, pull it in; otherwise assume
145     // the hardcoded one
146     if ( pnd_conf_get_as_char ( c, "default" ) ) {
147       defaults = pnd_conf_get_as_char ( c, "default" );
148     }
149
150     // ditch the confpath
151     free ( confpath );
152
153     // attempt mapping
154     if ( c ) {
155
156       n = pnd_map_dotdesktop_categories ( c, cats, 511, p );
157
158       if ( n ) {
159         fprintf ( f, "Categories=%s\n", cats );
160       } else {
161         fprintf ( f, "Categories=%s\n", defaults );
162       }
163
164     } else {
165       fprintf ( f, "Categories=%s\n", defaults );
166     }
167
168   }
169
170   fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
171
172   fclose ( f );
173
174   return ( 1 );
175 }
176
177 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
178   char buffer [ FILENAME_MAX ]; // target filename
179   char from [ FILENAME_MAX ];   // source filename
180   char bits [ 8 * 1024 ];
181   unsigned int bitlen;
182   FILE *pnd, *target;
183
184   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
185   if ( ( p -> object_type == pnd_object_type_pnd ) &&
186        ( ! p -> pnd_icon_pos ) )
187   {
188     return ( 0 ); // discover code didn't find it, so FAIL
189   }
190
191   // determine filename for target
192   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
193
194   /* first.. open the source file, by type of application:
195    * are we looking through a pnd file or a dir?
196    */
197   if ( p -> object_type == pnd_object_type_directory ) {
198     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
199   } else if ( p -> object_type == pnd_object_type_pnd ) {
200     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
201   }
202
203   pnd = fopen ( from, "r" );
204
205   if ( ! pnd ) {
206     return ( 0 );
207   }
208
209   unsigned int len;
210
211   target = fopen ( buffer, "wb" );
212
213   if ( ! target ) {
214     fclose ( pnd );
215     return ( 0 );
216   }
217
218   fseek ( pnd, 0, SEEK_END );
219   len = ftell ( pnd );
220   //fseek ( pnd, 0, SEEK_SET );
221
222   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
223
224   len -= p -> pnd_icon_pos;
225
226   while ( len ) {
227
228     if ( len > (8*1024) ) {
229       bitlen = (8*1024);
230     } else {
231       bitlen = len;
232     }
233
234     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
235       fclose ( pnd );
236       fclose ( target );
237       unlink ( buffer );
238       return ( 0 );
239     }
240
241     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
242       fclose ( pnd );
243       fclose ( target );
244       unlink ( buffer );
245       return ( 0 );
246     }
247
248     len -= bitlen;
249   } // while
250
251   fclose ( pnd );
252   fclose ( target );
253
254   return ( 1 );
255 }
256
257 #if 1 // we switched direction to freedesktop standard categories
258 // if no categories herein, return 0; otherwise, if some category-like-text, return 1
259 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
260   char *t;
261   char *match;
262
263   // clear target so we can easily append
264   memset ( target_buffer, '\0', len );
265
266   // for each main-cat and sub-cat, including alternates, just append them all together
267   // we'll try mapping them, since the categories file is there, but we'll default to
268   // copying over; this lets the conf file do merging or renaming of cagtegories, which
269   // could still be useful, but we can leave the conf file empty to effect a pure
270   // trusted-PXML-copying
271
272   // it would be sort of cumbersome to copy all the freedesktop.org defined categories (as
273   // there are hundreds), and would also mean new ones and peoples custom ones would
274   // flop
275
276   /* attempt primary category chain
277    */
278   #define MAPCAT(field)                                     \
279     if ( ( t = d -> field ) ) {                             \
280       match = pnd_map_dotdesktop_category ( c, t );         \
281       strncat ( target_buffer, match ? match : t, len );    \
282       strncat ( target_buffer, ";", len );                  \
283     }
284
285   MAPCAT(main_category);
286   MAPCAT(main_category1);
287   MAPCAT(main_category2);
288   MAPCAT(alt_category);
289   MAPCAT(alt_category1);
290   MAPCAT(alt_category2);
291
292   if ( target_buffer [ 0 ] ) {
293     return ( 1 ); // I guess its 'good'?
294   }
295
296 #if 0
297   if ( ( t = d -> main_category ) ) {
298     match = pnd_map_dotdesktop_category ( c, t );
299     strncat ( target_buffer, match ? match : t, len );
300     strncat ( target_buffer, ";", len );
301   }
302 #endif
303
304   return ( 0 );
305 }
306 #endif
307
308 #if 0 // we switched direction
309 //int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_pxml_handle h ) {
310 int pnd_map_dotdesktop_categories ( pnd_conf_handle c, char *target_buffer, unsigned short int len, pnd_disco_t *d ) {
311   unsigned short int n = 0; // no. matches
312   char *t;
313   char *match;
314
315   // clear target so we can easily append
316   memset ( target_buffer, '\0', len );
317
318   /* attempt primary category chain
319    */
320   match = NULL;
321
322   if ( ( t = d -> main_category ) ) {
323     match = pnd_map_dotdesktop_category ( c, t );
324   }
325
326   if ( ( ! match ) &&
327        ( t = d -> main_category1 ) )
328   {
329     match = pnd_map_dotdesktop_category ( c, t );
330   }
331
332   if ( ( ! match ) &&
333        ( t = d -> main_category2 ) )
334   {
335     match = pnd_map_dotdesktop_category ( c, t );
336   }
337   
338   if ( match ) {
339     strncat ( target_buffer, match, len );
340     len -= strlen ( target_buffer );
341     n += 1;
342   }
343
344   /* attempt secondary category chain
345    */
346   match = NULL;
347
348   if ( ( t = d -> alt_category ) ) {
349     match = pnd_map_dotdesktop_category ( c, t );
350   }
351
352   if ( ( ! match ) &&
353        ( t = d -> alt_category1 ) )
354   {
355     match = pnd_map_dotdesktop_category ( c, t );
356   }
357
358   if ( ( ! match ) &&
359        ( t = d -> alt_category2 ) )
360   {
361     match = pnd_map_dotdesktop_category ( c, t );
362   }
363   
364   if ( match ) {
365     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
366       strcat ( target_buffer, ";" );
367       len -= 1;
368     }
369     strncat ( target_buffer, match, len );
370     len -= strlen ( target_buffer );
371     n += 1;
372   }
373
374 #if 0 // doh, originally I was using pxml-t till I realized I pre-boned myself on that one
375   match = NULL;
376
377   if ( ( t = pnd_pxml_get_main_category ( h ) ) ) {
378     match = pnd_map_dotdesktop_category ( c, t );
379   }
380
381   if ( ( ! match ) &&
382        ( t = pnd_pxml_get_subcategory1 ( h ) ) )
383   {
384     match = pnd_map_dotdesktop_category ( c, t );
385   }
386
387   if ( ( ! match ) &&
388        ( t = pnd_pxml_get_subcategory2 ( h ) ) )
389   {
390     match = pnd_map_dotdesktop_category ( c, t );
391   }
392   
393   if ( match ) {
394     strncat ( target_buffer, match, len );
395     len -= strlen ( target_buffer );
396     n += 1;
397   }
398
399   /* attempt secondary category chain
400    */
401   match = NULL;
402
403   if ( ( t = pnd_pxml_get_altcategory ( h ) ) ) {
404     match = pnd_map_dotdesktop_category ( c, t );
405   }
406
407   if ( ( ! match ) &&
408        ( t = pnd_pxml_get_altsubcategory1 ( h ) ) )
409   {
410     match = pnd_map_dotdesktop_category ( c, t );
411   }
412
413   if ( ( ! match ) &&
414        ( t = pnd_pxml_get_altsubcategory2 ( h ) ) )
415   {
416     match = pnd_map_dotdesktop_category ( c, t );
417   }
418   
419   if ( match ) {
420     if ( target_buffer [ 0 ] != '\0' && len > 0 ) {
421       strcat ( target_buffer, ";" );
422       len -= 1;
423     }
424     strncat ( target_buffer, match, len );
425     len -= strlen ( target_buffer );
426     n += 1;
427   }
428 #endif
429
430   if ( n && len ) {
431     strcat ( target_buffer, ";" );
432   }
433
434   return ( n );
435 }
436 #endif
437
438 // given category 'foo', look it up in the provided config map. return the char* reference, or NULL
439 char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_category ) {
440   char *key;
441   char *ret;
442
443   key = malloc ( strlen ( single_category ) + 4 + 1 );
444
445   sprintf ( key, "map.%s", single_category );
446
447   ret = pnd_conf_get_as_char ( c, key );
448
449   free ( key );
450
451   return ( ret );
452 }
453
454 unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
455   // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
456   // with a fwrite at the end...
457   char from [ FILENAME_MAX ];   // source filename
458   char bits [ 8 * 1024 ];
459   unsigned int bitlen;
460   FILE *pnd = NULL;
461   unsigned char *target = NULL, *targiter = NULL;
462
463   // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
464   if ( ( p -> object_type == pnd_object_type_pnd ) &&
465        ( ! p -> pnd_icon_pos ) )
466   {
467     return ( NULL ); // discover code didn't find it, so FAIL
468   }
469
470   /* first.. open the source file, by type of application:
471    * are we looking through a pnd file or a dir?
472    */
473   if ( p -> object_type == pnd_object_type_directory ) {
474     sprintf ( from, "%s/%s", p -> object_path, p -> icon );
475   } else if ( p -> object_type == pnd_object_type_pnd ) {
476     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
477   }
478
479   pnd = fopen ( from, "r" );
480
481   if ( ! pnd ) {
482     return ( NULL );
483   }
484
485   // determine length of file, then adjust by icon position to find begin of icon
486   unsigned int len;
487
488   fseek ( pnd, 0, SEEK_END );
489   len = ftell ( pnd );
490   //fseek ( pnd, 0, SEEK_SET );
491
492   fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
493
494   len -= p -> pnd_icon_pos;
495
496   // create target buffer
497   target = malloc ( len );
498
499   if ( ! target ) {
500     fclose ( pnd );
501     return ( 0 );
502   }
503
504   targiter = target;
505
506   if ( r_buflen ) {
507     *r_buflen = len;
508   }
509
510   // copy over icon to target
511   while ( len ) {
512
513     if ( len > (8*1024) ) {
514       bitlen = (8*1024);
515     } else {
516       bitlen = len;
517     }
518
519     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
520       fclose ( pnd );
521       free ( target );
522       return ( NULL );
523     }
524
525     memmove ( targiter, bits, bitlen );
526     targiter += bitlen;
527
528     len -= bitlen;
529   } // while
530
531   fclose ( pnd );
532
533   return ( target );
534 }