Fix problem with title bar
[fsviewer.git] / src / FSUtils.c
blob4791b0c25e83b662bfd734144b54e00e4dae0d28
1 /*
2 Need to ifdef these two #include statements.
3 I guess they won't work on other systems!!!
5 Uncomment if you run Linux and want the RAM total
6 to appear in the Info Dialogue Box.
7 */
8 /* #include <linux/kernel.h> */
9 /* #include <linux/sys.h> */
10 #include <sys/utsname.h>
11 /* #include <sys/vfs.h> */
12 #include <sys/stat.h>
13 /* #include <mntent.h> */
14 #include <pwd.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <WINGs/WINGsP.h>
23 #include <wraster.h>
24 #include <X11/cursorfont.h>
26 /* change this to your OS */
27 /* #define __Linux__ */
29 #if defined(__FreeBSD__)
30 #include <kvm.h>
31 #include <limits.h>
32 #include <osreldate.h>
33 #include <sys/conf.h>
34 #include <sys/dkstat.h>
35 /* #include <sys/rlist.h> */
36 #include <sys/sysctl.h>
37 #include <sys/time.h>
38 #include <sys/vmmeter.h>
39 #include <fcntl.h>
40 typedef unsigned long ulong;
41 #endif /* __FreeBSD__ */
43 #if defined(__SunOS__)
44 #include <kstat.h>
45 #include <sys/cpuvar.h>
46 #include <sys/swap.h>
47 #include <utmp.h>
48 #endif /*__SunOS__*/
50 /* #include "config.h" */
51 #include "files.h"
52 #include "filebrowser.h"
53 #include "FSFileButton.h"
54 #include "FSViewer.h"
55 #include "FSFileView.h"
56 #include "FSUtils.h"
57 #include "FSPanel.h"
58 #include "DnD.h"
59 #include "xpm/file_plain.xpm"
60 #include "xpm/backing2.xpm"
61 #include "xpm/transparent.xpm"
63 #if defined(__FreeBSD__)
64 #define DF_COMMAND "df"
65 #else
66 #define DF_COMMAND "df -k"
67 #endif
69 const char *formatk(ulong k);
71 static RContext *rContext = NULL;
72 static RContextAttributes attributes;
74 typedef struct _FSSystem
76 char *host;
77 /* char *cwd; */
78 struct passwd *user;
79 Bool haveUserInfo;
80 /* struct passwd *pwds; */
81 /* int npwds; */
82 /* time_t pwdtime; */
83 /* struct group *grps; */
84 /* int ngrps; */
85 /* time_t grptime; */
87 /*
88 Uncomment if you run Linux and want the RAM total
89 to appear in the Info Dialogue Box.
91 /* struct sysinfo sysinf; */
92 Bool haveSysinfo;
93 struct utsname uts;
94 Bool haveUname;
95 long size;
96 mode_t umask;
98 char **imgTypes;
100 } _FSSystem;
102 typedef struct _FSSystem FSSystem;
105 static FSSystem *info;
106 static FSViewer *fsViewer;
107 static char buf[MAX_LEN];
108 static Cursor cursor;
110 void
111 FSInitSystemInfo(FSViewer *fsV)
114 struct passwd *pwp;
116 info = (FSSystem *) malloc(sizeof(FSSystem));
117 memset(info, 0, sizeof(FSSystem));
119 if( (info->user= getpwuid(getuid())) == NULL)
120 info->haveUserInfo = False;
121 else
122 info->haveUserInfo = True;
125 Uncomment if you run Linux and want the RAM total
126 to appear in the Info Dialogue Box.
128 /* if(!sysinfo(&info->sysinf)) */
129 /* info->haveSysinfo = True; */
130 /* else */
131 info->haveSysinfo = False;
133 #ifdef __SunOS__
134 if(uname(&info->uts) >= 0)
135 #else
136 if(!uname(&info->uts))
137 #endif
138 info->haveUname = True;
139 else
140 info->haveUname = False;
142 info->umask = umask(0);
143 umask(info->umask);
144 info->umask = 0777777 ^ info->umask;
146 info->imgTypes = RSupportedFileFormats();
148 fsViewer = fsV;
151 char *
152 FSProcessor()
155 if ((info->haveUname == True))
156 snprintf(buf, MAX_LEN, "%s", info->uts.machine);
157 else
158 snprintf(buf, MAX_LEN, _("Unavailable"));
160 return wstrdup(buf);
163 char *
164 FSSystemRelease()
167 if ((info->haveUname == True))
168 snprintf(buf, MAX_LEN, "%s %s", info->uts.sysname, info->uts.release);
169 else
170 snprintf(buf, MAX_LEN, _("Unavailable"));
172 return wstrdup(buf);
175 char *
176 FSNodeName()
179 if ((info->haveUname == True))
180 snprintf(buf, MAX_LEN, "%s", info->uts.nodename);
181 else
182 snprintf(buf, MAX_LEN, "/");
184 return wstrdup(buf);
187 char *
188 FSMemory()
192 Uncomment if you run Linux and want the RAM total
193 to appear in the Info Dialogue Box.
195 /* if ((info->haveSysinfo == True)) */
196 /* snprintf(buf, MAX_LEN, "%.1fM", */
197 /* (float) info->sysinf.totalram / 1048576L); */
198 /* else */
199 /* snprintf(buf, MAX_LEN, "Unavailable"); */
201 /* return buf; */
203 ulong total = 0;
204 unsigned long buffers;
206 #ifdef __Linux__
207 FILE *fp_meminfo;
208 char temp[128];
209 unsigned long free, shared, used, cached;
210 #endif /* __linux__ */
212 #if defined(__FreeBSD__)
213 kvm_t *kvmd = NULL;
214 struct nlist nl[] = {
215 #define N_CNT 0
216 { "_cnt" },
217 #define N_BUFSPACE 1
218 { "_bufspace" },
219 #define N_CP_TIME 2
220 { "_cp_time" },
221 #define N_AVERUN 3
222 { "_averunnable" },
223 #define VM_SWAPLIST 4
224 { "_swaplist" },
225 #define VM_SWDEVT 5
226 { "_swdevt" },
227 #define VM_NSWAP 6
228 { "_nswap" },
229 #define VM_NSWDEV 7
230 { "_nswdev" },
231 #define VM_DMMAX 8
232 { "_dmmax" },
233 #define N_DK_NDRIVE 9
234 { "_dk_ndrive" },
235 #define N_DK_WDS 10
236 { "_dk_wds" },
237 { "" }
239 int psize;
240 int pshift;
241 char errbuf[_POSIX2_LINE_MAX];
242 #endif /* __FreeBSD__ */
244 #ifdef __SunOS__
245 long maxmem;
246 int PageShift, i;
247 #endif /*__SunOS__*/
249 #ifdef __Linux__
251 fp_meminfo = fopen("/proc/meminfo", "r");
252 while (fgets(temp, 128, fp_meminfo))
254 if (strstr(temp, "Mem:"))
256 sscanf(temp, "Mem: %ld %ld %ld %ld %ld %ld",
257 &total, &used, &free, &shared, &buffers, &cached);
258 total >>= 10;
259 break;
262 fclose(fp_meminfo);
264 #endif /* __linux__ */
266 #ifdef __FreeBSD__
268 psize = getpagesize();
269 for (pshift = 0, psize = getpagesize(); psize>1; pshift++, psize>>=1);
270 pshift -= 10;
271 psize = getpagesize();
273 if (kvmd==NULL)
274 kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
275 if (kvmd)
277 if (kvm_nlist(kvmd, nl) >= 0)
279 if (nl[0].n_type != 0)
281 struct vmmeter sum;
283 if ((kvm_read(kvmd, nl[N_CNT].n_value, (char *)&sum, sizeof(sum))==sizeof(sum)) &&
284 (kvm_read(kvmd, nl[N_BUFSPACE].n_value, (char *)&buffers, sizeof(buffers))==sizeof(buffers)))
286 total = (sum.v_page_count - (buffers / psize) - sum.v_wire_count - sum.v_cache_count) << pshift;
291 #endif /* __FreeBSD__ */
293 #ifdef __SunOS__
295 i = sysconf(_SC_PAGESIZE); PageShift = 0;
296 while ((i >>= 1) > 0) { ++PageShift; }
297 PageShift -= 10;
300 maxmem = sysconf(_SC_PHYS_PAGES) << PageShift;
301 maxmem >>= 10;
304 * maxmem is in megs already
305 * formatk assumes it is in kbytes
306 * so it has to be scaled to get M
307 * instead of K
309 total = maxmem*1024;
311 #endif /*__SunOS__*/
313 if(total > 0)
314 snprintf(buf, MAX_LEN, "%s", formatk(total));
315 else
316 snprintf(buf, MAX_LEN, "%s", _("Unavailable"));
318 return wstrdup(buf);
322 char *
323 FSDisk()
325 ulong total = 0; /* Total Space */
326 FILE* f = popen(DF_COMMAND, "r");
328 if (!f)
330 wwarning(_("%s %d: Can't run df, %s\n"), __FILE__, __LINE__,
331 strerror(errno));
332 exit(1);
335 /* Read in from the pipe until we hit the end */
336 for (;;)
338 int n = 0; /* number of words */
339 char *p = NULL;
340 char *fs = NULL; /* File System */
341 char *word[10]; /* pointer to each word */
342 char buffer[1024];
344 /* Read in line by line */
345 if (!fgets(buffer, 1024, f))
346 break;
348 /* Strip the whitespace and break up the line */
349 for (p = buffer; n < 10;)
351 // skip leading whitespace:
352 while (*p && isspace(*p))
353 p++;
354 if (!*p)
355 break;
356 // skip over the word:
357 word[n++] = p;
359 while (*p && !isspace(*p))
360 p++;
361 if (!*p)
362 break;
363 *p++ = 0;
366 /* Get the file system name */
367 fs = strdup(word[n-6]);
368 /* And make sure it is local */
369 if(strncmp(fs, "/dev", 4))
370 continue;
372 /* ok we found a line with a /dev at the start */
373 /* Total */
374 total += strtol(word[n-5], 0, 10);
376 if(fs)
377 free(fs);
379 pclose(f);
381 sprintf(buf, "%s", formatk(total));
383 return wstrdup(buf);
386 /* turn number of K into user-friendly text */
387 const char*
388 formatk(ulong k)
390 static char buffer[10];
392 if (k >= 1024*1024)
393 sprintf(buffer,"%.4gG",(double)k/(1024*1024));
394 else if(k >= 1024)
395 sprintf(buffer,"%.4gM",(double)k/1024);
396 else
397 sprintf(buffer,"%ldK",k);
399 return buffer;
402 char*
403 LocateImage(char *name)
405 char *path = NULL;
406 char *tmp = NULL;
407 char **types;
409 if(name)
410 tmp = (char *)wmalloc(strlen(name)+8);
411 else
412 return NULL;
414 if(FSImageTypeIsSupported("TIFF"))
416 sprintf(tmp, "%s.tiff", name);
417 path = WMPathForResourceOfType(tmp, "tiff");
419 if(!path)
421 sprintf(tmp, "%s.tif", name);
422 path = WMPathForResourceOfType(tmp, "tiff");
426 /* Uncomment if you want the app to support GIF or JPEG icons */
427 /* if(FSImageTypeIsSupported("GIF")) */
428 /* { */
429 /* sprintf(tmp, "%s.gif", name); */
430 /* path = WMPathForResourceOfType(tmp, "gif"); */
431 /* } */
433 /* if(FSImageTypeIsSupported("JPEG")) */
434 /* { */
435 /* sprintf(tmp, "%s.jpg", name); */
436 /* path = WMPathForResourceOfType(tmp, "jpg"); */
437 /* } */
439 if(!path)
441 sprintf(tmp, "%s.xpm", name);
442 path = WMPathForResourceOfType(tmp, "xpm");
445 if(!path)
446 path = WMPathForResourceOfType(name, "");
448 free(tmp);
450 if (!path)
451 return wstrdup(name);
453 return path;
457 char *
458 FSParseExecString(char *pathname, char *execStr, ...)
460 va_list ap;
461 char *str;
462 char *buffer;
463 int i, cnt;
464 int pathnameLen;
465 int initBufferSize;
468 cnt = 0;
469 pathnameLen = strlen(pathname);
470 initBufferSize = strlen(execStr)+1;
472 /* This is an inefficient way of doing things */
473 buffer = (char *) wmalloc(initBufferSize);
474 str = buffer;
476 va_start(ap, execStr);
477 for(; *execStr; ++execStr)
479 if (*execStr != '%')
481 *str++ = *execStr;
482 cnt++;
483 continue;
485 ++execStr;
486 switch(*execStr)
488 case 's':
489 (void)va_arg(ap, char *);
491 buffer = (char *) wrealloc(buffer, initBufferSize+cnt+pathnameLen);
492 str = buffer;
493 str += cnt;
494 for (i = 0; i < pathnameLen; ++i)
496 *str++ = pathname[i];
497 cnt++;
499 continue;
500 default :
501 if (*execStr != '%')
503 *str++ = '%';
504 cnt++;
506 if (*execStr)
508 *str++ = *execStr;
509 cnt++;
511 else
512 --execStr;
513 continue;
516 va_end(ap);
517 *str = '\0';
519 return buffer;
522 void
523 FSLaunchApp(FSViewer *fsViewer, AppEvent event)
525 char *pathname;
526 char *path;
527 char *name;
528 FileInfo *fileInfo;
530 fileInfo = FSCreateFileInfo();
532 pathname = FSGetFileViewPath(FSGetFSViewerCurrentView(fsViewer));
533 path = GetPathFromPathname(pathname);
534 name = GetNameFromPathname(pathname);
536 GetFileInfo(path, name, fileInfo);
538 LaunchApp(fsViewer, fileInfo, event);
541 void
542 LaunchApp(FSViewer *fsViewer, FileInfo *fileInfo, AppEvent event)
544 if(!isDirectory(fileInfo->fileType))
546 char *exec = NULL;
547 char *extn = NULL;
548 char *execStr = NULL;
549 char *pathname;
552 if (chdir(fileInfo->path))
554 char s[0xff];
556 sprintf(s, _("Error changing to %s but it's not that serious!"),
557 fileInfo->path);
558 FSErrorDialog(_("Error File Operation"), s);
561 pathname = GetPathnameFromPathName(fileInfo->path, fileInfo->name);
562 extn = GetFileExtn(fileInfo->name);
564 if(event == AppExec)
566 if( (exec = FSGetStringForNameKey(extn, "exec")) != NULL)
567 execStr = FSParseExecString("", exec);
568 else
569 execStr = FSParseExecString("", fileInfo->name);
571 else if(event == AppView)
573 if( (exec = FSGetStringForNameKey(extn, "viewer")) != NULL)
574 execStr = FSParseExecString(pathname, exec);
575 else
577 char buf[MAX_LEN];
580 This is a quick hack, a very quick hack.
581 The magic files method has some very good
582 potential, I just need to figure out how
583 to implement it for the whole app.
585 magic_get_type(pathname, buf);
587 if( !strcmp("ascii", buf) ||
588 !strcmp("HTML", buf) ||
589 !strcmp("MAIL", buf) )
591 exec = FSGetStringForNameKey("MAGICASCII", "exec");
593 else if( !strcmp("PBM", buf) ||
594 !strcmp("PGM", buf) ||
595 !strcmp("GIF", buf) ||
596 !strcmp("JPG", buf) ||
597 !strcmp("XPM", buf) ||
598 !strcmp("XBM", buf) ||
599 !strcmp("PPM", buf) ||
600 !strcmp("TIFF", buf) )
602 exec = FSGetStringForNameKey("MAGICIMAGE", "exec");
604 else if( !strcmp("PS", buf) )
606 exec = FSGetStringForNameKey("MAGICPS", "exec");
609 if(exec != NULL)
610 execStr = FSParseExecString(pathname, exec);
611 else
613 char *result = NULL;
615 result = FSRunAppInputPanel(fsViewer, fileInfo,
616 _("App Input Window"));
617 if(result)
618 execStr = FSParseExecString(pathname, result);
622 else if(event == AppEdit)
624 if( (exec = FSGetStringForNameKey(extn, "editor")) != NULL)
625 execStr = FSParseExecString(pathname, exec);
627 if(execStr == NULL)
629 char *result = NULL;
631 result = FSRunAppInputPanel(fsViewer, fileInfo,
632 _("App Input Window"));
633 if(result)
634 execStr = FSParseExecString(pathname, result);
638 if(execStr)
640 execCommand(execStr);
641 free(execStr);
644 if(pathname)
645 free(pathname);
649 void
650 FSSetButtonImageFromFile(WMButton *btn, char *imgName)
652 RColor color;
653 WMPixmap *pixmap;
655 color.red = 0xae;
656 color.green = 0xaa; /* aa ?*/
657 color.blue = 0xae;
658 color.alpha = 0;
659 if(imgName)
660 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(btn),
661 imgName, &color);
662 else
663 pixmap = NULL;
665 if (!pixmap)
667 wwarning(_("%s %d: Could not load icon file %s"),
668 __FILE__, __LINE__, imgName);
670 else
672 WMSetButtonImage(btn, pixmap);
673 WMReleasePixmap(pixmap);
676 color.red = 0xff;
677 color.green = 0xff;
678 color.blue = 0xff;
679 color.alpha = 0;
680 if(imgName)
681 /* pixmap = FSCreatePixmapWithBackingFromFile(WMWidgetScreen(btn),
682 imgName, &color);*/
683 pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(btn),
684 imgName, &color);
685 else
686 pixmap = NULL;
688 if (!pixmap)
690 wwarning(_("%s %d: Could not load icon file %s"),
691 __FILE__, __LINE__, imgName);
693 else
695 WMSetButtonAltImage(btn, pixmap);
696 WMReleasePixmap(pixmap);
700 void
701 FSSetButtonImageFromXPMData(WMButton *btn, char **data)
703 RColor color;
704 WMPixmap *pixmap;
706 color.red = 0xae;
707 color.green = 0xaa;
708 color.blue = 0xae;
709 color.alpha = 0;
710 pixmap = WMCreatePixmapFromXPMData(WMWidgetScreen(btn), data);
711 if (!pixmap)
712 wwarning(_("%s %d: Could not create Pixmap from Data"),
713 __FILE__, __LINE__);
715 WMSetButtonImage(btn, pixmap);
717 if (pixmap)
718 WMReleasePixmap(pixmap);
720 color.red = 0xff;
721 color.green = 0xff;
722 color.blue = 0xff;
723 color.alpha = 0;
724 pixmap = WMCreatePixmapFromXPMData(WMWidgetScreen(btn), data);
725 if (!pixmap)
726 wwarning(_("%s %d: Could not create Pixmap from Data"),
727 __FILE__, __LINE__);
729 WMSetButtonAltImage(btn, pixmap);
731 if (pixmap)
732 WMReleasePixmap(pixmap);
735 void
736 FSLoadIconPaths(WMList *list)
738 int i;
739 char mbuf[MAX_LEN];
740 char *path;
741 WMPropList* pathList;
742 WMPropList* val;
743 WMPropList* WindowMakerDB;
745 path = wdefaultspathfordomain("WindowMaker");
747 WindowMakerDB = WMReadPropListFromFile(path);
748 if (WindowMakerDB)
750 if (!WMIsPLDictionary(WindowMakerDB))
752 WMReleasePropList(WindowMakerDB);
753 pathList = NULL;
754 snprintf(mbuf, MAX_LEN, _("Window Maker domain (%s) is corrupted!"),
755 path);
756 WMRunAlertPanel(WMWidgetScreen(list), NULL, _("Error"),
757 mbuf, _("OK"), NULL, NULL);
760 else
762 snprintf(mbuf, MAX_LEN, _("Could not load Window Maker defaults (%s)"),
763 path);
764 WMRunAlertPanel(WMWidgetScreen(list), NULL, _("Error"),
765 mbuf, _("OK"), NULL, NULL);
768 pathList = WMGetFromPLDictionary(WindowMakerDB, WMCreatePLString("IconPath"));
769 if (pathList && WMIsPLArray(pathList))
771 for (i=0; i<WMGetPropListItemCount(pathList); i++)
773 val = WMGetFromPLArray(pathList, i);
774 WMAddListItem(list, WMGetFromPLString(val));
778 if(path)
779 free(path);
782 static void
783 FSRenderPixmap(WMScreen *screen, Pixmap d, char **data,
784 int width, int height)
786 int x, y;
787 Display *dpy = WMScreenDisplay(screen);
788 GC whiteGC = WMColorGC(WMWhiteColor(screen));
789 GC blackGC = WMColorGC(WMBlackColor(screen));
790 GC lightGC = WMColorGC(WMGrayColor(screen));
791 GC darkGC = WMColorGC(WMDarkGrayColor(screen));
794 for (y = 0; y < height; y++) {
795 for (x = 0; x < width; x++) {
796 switch (data[y][x]) {
797 case ' ':
798 case 'w':
799 XDrawPoint(dpy, d, whiteGC, x, y);
800 break;
802 case '=':
803 case '.':
804 case 'l':
805 XDrawPoint(dpy, d, lightGC, x, y);
806 break;
808 case '%':
809 case 'd':
810 XDrawPoint(dpy, d, darkGC, x, y);
811 break;
813 case '#':
814 case 'b':
815 default:
816 XDrawPoint(dpy, d, blackGC, x, y);
817 break;
823 WMPixmap*
824 FSMakePixmap(WMScreen *sPtr, char **data, int width, int height)
826 Pixmap pixmap;
828 pixmap = XCreatePixmap(WMScreenDisplay(sPtr), W_DRAWABLE(sPtr),
829 width, height, WMScreenDepth(sPtr));
831 FSRenderPixmap(sPtr, pixmap, data, width, height);
833 return WMCreatePixmapFromXPixmaps(sPtr, pixmap, None, width, height,
834 WMScreenDepth(sPtr));
837 void
838 FSErrorDialog(char *title, char *msg)
840 WMScreen *scr = FSGetFSViewerScreen(fsViewer);
842 WMRunAlertPanel(scr, NULL, title, msg, _("OK"),
843 NULL, NULL);
847 FSConfirmationDialog(char *title, char *msg)
849 WMScreen *scr = FSGetFSViewerScreen(fsViewer);
851 return WMRunAlertPanel(scr, NULL, title, msg, _("OK"),
852 _("Cancel"), NULL);
855 void
856 FSUpdateFileView(FileAction action, FileInfo *src, FileInfo *dest)
858 FSUpdateFileViewPath(FSGetFSViewerCurrentView(fsViewer),
859 action, src, dest);
863 mode_t
864 FSGetUMask()
866 return info->umask;
869 char *
870 FSGetHomeDir()
872 return info->haveUserInfo ? info->user->pw_dir : NULL;
875 /* match a pattern with a filename, returning nonzero if the match was
876 correct */
878 /* Currently only *, ? and [...] (character classes) are recognized, no curly
879 braces. An escape mechanism for metacharacters is also missing. This could
880 be implemented more efficiently, but the present simple backtracking
881 routine does reasonably well for the usual kinds of patterns. -ag */
883 int
884 FSStringMatch(char *pattern, char *fn)
886 char *start;
888 for (;; fn++, pattern++)
890 switch (*pattern)
892 case '?':
893 if (!*fn)
894 return 0;
895 break;
897 case '*':
898 pattern++;
900 if (FSStringMatch(pattern,fn))
901 return 1;
902 while (*fn++);
903 return 0;
905 case '[':
906 start = pattern+1;
909 next:
910 pattern++;
911 if (*pattern == ']')
912 return 0;
913 else if( pattern[0] == '-' &&
914 pattern > start &&
915 pattern[1] != ']' )
917 if (pattern[-1] <= *fn && *fn <= pattern[1])
918 break;
920 else
922 start = (++pattern)+1;
923 goto next;
926 while (*fn != *pattern);
928 while (*pattern != ']')
929 if (!*pattern++)
930 return 0;
931 break;
933 default:
934 if (*fn != *pattern)
935 return 0;
938 if (!*fn)
939 return 1;
943 WMPixmap*
944 FSCreateBlendedPixmapFromFile(WMScreen *scr, char *fileName, RColor *color)
946 RImage *image = NULL;
947 RImage *clone = NULL;
948 WMPixmap *pixmap;
949 RColor color1;
950 int x,y;
952 if(!rContext)
954 memset((void *) &attributes, 0, sizeof(RContextAttributes));
955 attributes.flags = (RC_RenderMode | RC_ColorsPerChannel);
956 attributes.render_mode = RM_DITHER;
957 attributes.colors_per_channel = 4;
959 rContext = RCreateContext(WMScreenDisplay(scr),
960 DefaultScreen(WMScreenDisplay(scr)),
961 &attributes);
963 if(fileName)
964 image = RLoadImage(rContext, fileName, 0);
965 if(!image)
966 image = RGetImageFromXPMData(rContext, file_plain);
968 color1.red = 0xa3;
969 color1.green = 0xa3;
970 color1.blue = 0xa3;
971 color1.alpha = 255;
973 RCombineImageWithColor(image, color);
975 / * Resize the width to 64x64 pixels * /
976 clone = RMakeCenteredImage(image, 64, 64, &color1);
977 pixmap = WMCreatePixmapFromRImage(scr, clone, 0);
979 RReleaseImage(image);
980 RReleaseImage(clone);
982 return pixmap;
986 WMPixmap*
987 FSCreatePixmapWithBackingFromFile(WMScreen *scr, char *fileName, RColor *color)
989 RImage *image = NULL;
990 RImage *clone = NULL;
991 int x, y;
992 WMPixmap *pixmap = NULL;
994 if(!rContext)
996 memset((void *) &attributes, 0, sizeof(RContextAttributes));
997 attributes.flags = (RC_RenderMode | RC_ColorsPerChannel);
998 attributes.render_mode = RM_DITHER;
999 attributes.colors_per_channel = 4;
1001 rContext = RCreateContext(WMScreenDisplay(scr),
1002 DefaultScreen(WMScreenDisplay(scr)),
1003 &attributes);
1006 if(fileName)
1007 image = RLoadImage(rContext, fileName, 0);
1009 if(!image)
1010 image = RGetImageFromXPMData(rContext, file_plain);
1012 clone = RGetImageFromXPMData(rContext, backing2_xpm);
1014 RCombineImageWithColor(image, color);
1017 * Using (clone->width - image->width)/2
1018 * gives out gibberish. For whatever reason
1019 * explicit structure below works
1021 x = clone->width/2 - image->width/2;
1022 y = clone->height/2 - image->height/2;
1024 /* Bad hack to make sure things fit the clone pixmap */
1025 if(x >= 0 && y >= 0)
1027 RCombineArea(clone, image, 0, 0, image->width, image->height, x, y);
1028 pixmap = WMCreatePixmapFromRImage(scr, clone, 0);
1031 RReleaseImage(clone);
1032 RReleaseImage(image);
1034 return pixmap;
1038 WMPixmap*
1039 FSCreateBlurredPixmapFromFile(WMScreen *scr, char *fileName)
1041 int x, y;
1042 RColor color;
1043 WMPixmap *pixmap;
1044 RImage *image = NULL;
1045 RImage *clone = NULL;
1047 if(!rContext)
1049 memset((void *) &attributes, 0, sizeof(RContextAttributes));
1050 attributes.flags = (RC_RenderMode | RC_ColorsPerChannel);
1051 attributes.render_mode = RM_DITHER;
1052 attributes.colors_per_channel = 4;
1054 rContext = RCreateContext(WMScreenDisplay(scr),
1055 DefaultScreen(WMScreenDisplay(scr)),
1056 &attributes);
1058 if(fileName)
1059 image = RLoadImage(rContext, fileName, 0);
1060 if(!image)
1061 image = RGetImageFromXPMData(rContext, file_plain);
1063 color.red = 0xff;
1064 color.green = 0xff;
1065 color.blue = 0xff;
1066 color.alpha = 200;
1068 RClearImage(image, &color);
1069 pixmap = WMCreatePixmapFromRImage(scr, image, 0);
1071 RReleaseImage(image);
1073 return pixmap;
1076 char *
1077 FSParseCmdField(FileInfo *fileInfo, char *txt, ...)
1080 Dynamically allocate buf and then return it.
1081 Use something along the basis of the size of
1082 *txt, then add in the size of file|path each time
1083 they are encountered. Use realloc
1086 va_list ap;
1087 char *str;
1088 char *file;
1089 char *path;
1090 char *buffer;
1091 int i, cnt;
1092 int fileLen, pathLen;
1093 int initBufferSize;
1096 cnt = 0;
1097 fileLen = strlen(fileInfo->name);
1098 file = (char *) wmalloc(fileLen+1);
1099 strcpy(file, fileInfo->name);
1101 pathLen = strlen(fileInfo->path);
1102 path = (char *) wmalloc(pathLen+1);
1103 strcpy(path, fileInfo->path);
1105 initBufferSize = strlen(txt)+1;
1106 /* This is an inefficient way of doing things */
1107 buffer = (char *) wmalloc(initBufferSize);
1108 str = buffer;
1110 va_start(ap, txt);
1111 for(; *txt; ++txt)
1113 if (*txt != '%')
1115 *str++ = *txt;
1116 cnt++;
1117 continue;
1119 ++txt;
1121 switch(*txt)
1123 case 'f':
1124 (void)va_arg(ap, char *);
1126 buffer = (char *) wrealloc(buffer, initBufferSize+cnt+fileLen);
1127 str = buffer;
1128 str += cnt;
1129 for (i = 0; i < fileLen; ++i)
1131 *str++ = file[i];
1132 cnt++;
1134 continue;
1135 case 'p':
1136 (void)va_arg(ap, char *);
1138 buffer = (char *) wrealloc(buffer, initBufferSize+cnt+pathLen);
1139 str = buffer;
1140 str += cnt;
1141 for (i = 0; i < pathLen; ++i)
1143 *str++ = path[i];
1144 cnt++;
1146 continue;
1147 default :
1148 if (*txt != '%')
1150 *str++ = '%';
1151 cnt++;
1153 if (*txt)
1155 *str++ = *txt;
1156 cnt++;
1158 else
1159 --txt;
1160 continue;
1163 va_end(ap);
1164 *str = '\0';
1166 return buffer;
1169 void
1170 FSSetBusyCursor(WMWidget *w, Bool state)
1172 Window wnd = WMWidgetXID(w);
1173 WMScreen *scr = WMWidgetScreen(w);
1174 Display *dpy = WMScreenDisplay(scr);
1176 if(state)
1178 Cursor cursor;
1180 cursor = XCreateFontCursor(dpy, XC_watch);
1181 XDefineCursor(dpy, wnd, cursor);
1183 else
1184 XUndefineCursor(dpy, wnd);
1189 FSGetDNDType(FileInfo *fileInfo)
1191 int type = DndFile;
1192 char buf[MAX_LEN];
1193 char *pathname = NULL;
1195 if(fileInfo == NULL)
1196 return;
1198 switch (fileInfo->fileType)
1200 case S_LINK : type=DndLink;
1201 break;
1202 case DIRECTORY :
1203 case ROOT :
1204 case HOME : type=DndDir;
1205 break;
1206 default : pathname = GetPathnameFromPathName(fileInfo->path,
1207 fileInfo->name);
1208 magic_get_type(pathname, buf);
1209 if(strcmp("EXEC", buf) == 0)
1210 type = DndExe;
1211 else if(strcmp("ascii", buf) == 0)
1212 type=DndFile;
1213 else if(strcmp("ascii", buf) == 0)
1214 type=DndText;
1215 else
1216 type=DndFile;
1217 break;
1220 return type;
1224 FSExecCommand(char *path, char *execStr)
1226 if (chdir(path))
1228 char s[0xff];
1230 sprintf(s, _("Error changing to %s, \"%s\" cancelled."), path);
1231 FSErrorDialog(_("Error File Operation"), s);
1233 return 1;
1236 if (execCommand(execStr))
1237 return 0;
1239 return 1;
1242 Bool
1243 FSImageTypeIsSupported(char *imgType)
1245 int i;
1246 Bool found = False;
1248 if(imgType == NULL || info->imgTypes[0] == NULL)
1249 return found;
1251 for(i=0; info->imgTypes[i]; i++)
1253 if(strcmp(imgType, info->imgTypes[i]) == 0)
1255 found = True;
1256 break;
1259 return found;