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.
8 /* #include <linux/kernel.h> */
9 /* #include <linux/sys.h> */
10 #include <sys/utsname.h>
11 /* #include <sys/vfs.h> */
13 /* #include <mntent.h> */
22 #include <WINGs/WINGsP.h>
24 #include <X11/cursorfont.h>
26 /* change this to your OS */
27 /* #define __Linux__ */
29 #if defined(__FreeBSD__)
32 #include <osreldate.h>
34 #include <sys/dkstat.h>
35 /* #include <sys/rlist.h> */
36 #include <sys/sysctl.h>
38 #include <sys/vmmeter.h>
40 typedef unsigned long ulong
;
41 #endif /* __FreeBSD__ */
43 #if defined(__SunOS__)
45 #include <sys/cpuvar.h>
50 /* #include "config.h" */
52 #include "filebrowser.h"
53 #include "FSFileButton.h"
55 #include "FSFileView.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"
66 #define DF_COMMAND "df -k"
69 const char *formatk(ulong k
);
71 static RContext
*rContext
= NULL
;
72 static RContextAttributes attributes
;
74 typedef struct _FSSystem
80 /* struct passwd *pwds; */
83 /* struct group *grps; */
88 Uncomment if you run Linux and want the RAM total
89 to appear in the Info Dialogue Box.
91 /* struct sysinfo sysinf; */
102 typedef struct _FSSystem FSSystem
;
105 static FSSystem
*info
;
106 static FSViewer
*fsViewer
;
107 static char buf
[MAX_LEN
];
108 static Cursor cursor
;
111 FSInitSystemInfo(FSViewer
*fsV
)
116 info
= (FSSystem
*) malloc(sizeof(FSSystem
));
117 memset(info
, 0, sizeof(FSSystem
));
119 if( (info
->user
= getpwuid(getuid())) == NULL
)
120 info
->haveUserInfo
= False
;
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; */
131 info
->haveSysinfo
= False
;
134 if(uname(&info
->uts
) >= 0)
136 if(!uname(&info
->uts
))
138 info
->haveUname
= True
;
140 info
->haveUname
= False
;
142 info
->umask
= umask(0);
144 info
->umask
= 0777777 ^ info
->umask
;
146 info
->imgTypes
= RSupportedFileFormats();
155 if ((info
->haveUname
== True
))
156 snprintf(buf
, MAX_LEN
, "%s", info
->uts
.machine
);
158 snprintf(buf
, MAX_LEN
, _("Unavailable"));
167 if ((info
->haveUname
== True
))
168 snprintf(buf
, MAX_LEN
, "%s %s", info
->uts
.sysname
, info
->uts
.release
);
170 snprintf(buf
, MAX_LEN
, _("Unavailable"));
179 if ((info
->haveUname
== True
))
180 snprintf(buf
, MAX_LEN
, "%s", info
->uts
.nodename
);
182 snprintf(buf
, MAX_LEN
, "/");
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); */
199 /* snprintf(buf, MAX_LEN, "Unavailable"); */
204 unsigned long buffers
;
209 unsigned long free
, shared
, used
, cached
;
210 #endif /* __linux__ */
212 #if defined(__FreeBSD__)
214 struct nlist nl
[] = {
223 #define VM_SWAPLIST 4
233 #define N_DK_NDRIVE 9
241 char errbuf
[_POSIX2_LINE_MAX
];
242 #endif /* __FreeBSD__ */
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
);
264 #endif /* __linux__ */
268 psize
= getpagesize();
269 for (pshift
= 0, psize
= getpagesize(); psize
>1; pshift
++, psize
>>=1);
271 psize
= getpagesize();
274 kvmd
= kvm_openfiles(NULL
, NULL
, NULL
, O_RDONLY
, errbuf
);
277 if (kvm_nlist(kvmd
, nl
) >= 0)
279 if (nl
[0].n_type
!= 0)
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__ */
295 i
= sysconf(_SC_PAGESIZE
); PageShift
= 0;
296 while ((i
>>= 1) > 0) { ++PageShift
; }
300 maxmem
= sysconf(_SC_PHYS_PAGES
) << PageShift
;
304 * maxmem is in megs already
305 * formatk assumes it is in kbytes
306 * so it has to be scaled to get M
314 snprintf(buf
, MAX_LEN
, "%s", formatk(total
));
316 snprintf(buf
, MAX_LEN
, "%s", _("Unavailable"));
325 ulong total
= 0; /* Total Space */
326 FILE* f
= popen(DF_COMMAND
, "r");
330 wwarning(_("%s %d: Can't run df, %s\n"), __FILE__
, __LINE__
,
335 /* Read in from the pipe until we hit the end */
338 int n
= 0; /* number of words */
340 char *fs
= NULL
; /* File System */
341 char *word
[10]; /* pointer to each word */
344 /* Read in line by line */
345 if (!fgets(buffer
, 1024, f
))
348 /* Strip the whitespace and break up the line */
349 for (p
= buffer
; n
< 10;)
351 // skip leading whitespace:
352 while (*p
&& isspace(*p
))
356 // skip over the word:
359 while (*p
&& !isspace(*p
))
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))
372 /* ok we found a line with a /dev at the start */
374 total
+= strtol(word
[n
-5], 0, 10);
381 sprintf(buf
, "%s", formatk(total
));
386 /* turn number of K into user-friendly text */
390 static char buffer
[10];
393 sprintf(buffer
,"%.4gG",(double)k
/(1024*1024));
395 sprintf(buffer
,"%.4gM",(double)k
/1024);
397 sprintf(buffer
,"%ldK",k
);
403 LocateImage(char *name
)
410 tmp
= (char *)wmalloc(strlen(name
)+8);
414 if(FSImageTypeIsSupported("TIFF"))
416 sprintf(tmp
, "%s.tiff", name
);
417 path
= WMPathForResourceOfType(tmp
, "tiff");
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")) */
429 /* sprintf(tmp, "%s.gif", name); */
430 /* path = WMPathForResourceOfType(tmp, "gif"); */
433 /* if(FSImageTypeIsSupported("JPEG")) */
435 /* sprintf(tmp, "%s.jpg", name); */
436 /* path = WMPathForResourceOfType(tmp, "jpg"); */
441 sprintf(tmp
, "%s.xpm", name
);
442 path
= WMPathForResourceOfType(tmp
, "xpm");
446 path
= WMPathForResourceOfType(name
, "");
451 return wstrdup(name
);
458 FSParseExecString(char *pathname
, char *execStr
, ...)
469 pathnameLen
= strlen(pathname
);
470 initBufferSize
= strlen(execStr
)+1;
472 /* This is an inefficient way of doing things */
473 buffer
= (char *) wmalloc(initBufferSize
);
476 va_start(ap
, execStr
);
477 for(; *execStr
; ++execStr
)
489 (void)va_arg(ap
, char *);
491 buffer
= (char *) wrealloc(buffer
, initBufferSize
+cnt
+pathnameLen
);
494 for (i
= 0; i
< pathnameLen
; ++i
)
496 *str
++ = pathname
[i
];
523 FSLaunchApp(FSViewer
*fsViewer
, AppEvent event
)
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
);
542 LaunchApp(FSViewer
*fsViewer
, FileInfo
*fileInfo
, AppEvent event
)
544 if(!isDirectory(fileInfo
->fileType
))
548 char *execStr
= NULL
;
552 if (chdir(fileInfo
->path
))
556 sprintf(s
, _("Error changing to %s but it's not that serious!"),
558 FSErrorDialog(_("Error File Operation"), s
);
561 pathname
= GetPathnameFromPathName(fileInfo
->path
, fileInfo
->name
);
562 extn
= GetFileExtn(fileInfo
->name
);
566 if( (exec
= FSGetStringForNameKey(extn
, "exec")) != NULL
)
567 execStr
= FSParseExecString("", exec
);
569 execStr
= FSParseExecString("", fileInfo
->name
);
571 else if(event
== AppView
)
573 if( (exec
= FSGetStringForNameKey(extn
, "viewer")) != NULL
)
574 execStr
= FSParseExecString(pathname
, exec
);
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");
610 execStr
= FSParseExecString(pathname
, exec
);
615 result
= FSRunAppInputPanel(fsViewer
, fileInfo
,
616 _("App Input Window"));
618 execStr
= FSParseExecString(pathname
, result
);
622 else if(event
== AppEdit
)
624 if( (exec
= FSGetStringForNameKey(extn
, "editor")) != NULL
)
625 execStr
= FSParseExecString(pathname
, exec
);
631 result
= FSRunAppInputPanel(fsViewer
, fileInfo
,
632 _("App Input Window"));
634 execStr
= FSParseExecString(pathname
, result
);
640 execCommand(execStr
);
650 FSSetButtonImageFromFile(WMButton
*btn
, char *imgName
)
656 color
.green
= 0xaa; /* aa ?*/
660 pixmap
= WMCreateBlendedPixmapFromFile(WMWidgetScreen(btn
),
667 wwarning(_("%s %d: Could not load icon file %s"),
668 __FILE__
, __LINE__
, imgName
);
672 WMSetButtonImage(btn
, pixmap
);
673 WMReleasePixmap(pixmap
);
681 /* pixmap = FSCreatePixmapWithBackingFromFile(WMWidgetScreen(btn),
683 pixmap
= WMCreateBlendedPixmapFromFile(WMWidgetScreen(btn
),
690 wwarning(_("%s %d: Could not load icon file %s"),
691 __FILE__
, __LINE__
, imgName
);
695 WMSetButtonAltImage(btn
, pixmap
);
696 WMReleasePixmap(pixmap
);
701 FSSetButtonImageFromXPMData(WMButton
*btn
, char **data
)
710 pixmap
= WMCreatePixmapFromXPMData(WMWidgetScreen(btn
), data
);
712 wwarning(_("%s %d: Could not create Pixmap from Data"),
715 WMSetButtonImage(btn
, pixmap
);
718 WMReleasePixmap(pixmap
);
724 pixmap
= WMCreatePixmapFromXPMData(WMWidgetScreen(btn
), data
);
726 wwarning(_("%s %d: Could not create Pixmap from Data"),
729 WMSetButtonAltImage(btn
, pixmap
);
732 WMReleasePixmap(pixmap
);
736 FSLoadIconPaths(WMList
*list
)
741 WMPropList
* pathList
;
743 WMPropList
* WindowMakerDB
;
745 path
= wdefaultspathfordomain("WindowMaker");
747 WindowMakerDB
= WMReadPropListFromFile(path
);
750 if (!WMIsPLDictionary(WindowMakerDB
))
752 WMReleasePropList(WindowMakerDB
);
754 snprintf(mbuf
, MAX_LEN
, _("Window Maker domain (%s) is corrupted!"),
756 WMRunAlertPanel(WMWidgetScreen(list
), NULL
, _("Error"),
757 mbuf
, _("OK"), NULL
, NULL
);
762 snprintf(mbuf
, MAX_LEN
, _("Could not load Window Maker defaults (%s)"),
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
));
783 FSRenderPixmap(WMScreen
*screen
, Pixmap d
, char **data
,
784 int width
, int height
)
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
]) {
799 XDrawPoint(dpy
, d
, whiteGC
, x
, y
);
805 XDrawPoint(dpy
, d
, lightGC
, x
, y
);
810 XDrawPoint(dpy
, d
, darkGC
, x
, y
);
816 XDrawPoint(dpy
, d
, blackGC
, x
, y
);
824 FSMakePixmap(WMScreen
*sPtr
, char **data
, int width
, int height
)
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
));
838 FSErrorDialog(char *title
, char *msg
)
840 WMScreen
*scr
= FSGetFSViewerScreen(fsViewer
);
842 WMRunAlertPanel(scr
, NULL
, title
, msg
, _("OK"),
847 FSConfirmationDialog(char *title
, char *msg
)
849 WMScreen
*scr
= FSGetFSViewerScreen(fsViewer
);
851 return WMRunAlertPanel(scr
, NULL
, title
, msg
, _("OK"),
856 FSUpdateFileView(FileAction action
, FileInfo
*src
, FileInfo
*dest
)
858 FSUpdateFileViewPath(FSGetFSViewerCurrentView(fsViewer
),
872 return info
->haveUserInfo
? info
->user
->pw_dir
: NULL
;
875 /* match a pattern with a filename, returning nonzero if the match was
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 */
884 FSStringMatch(char *pattern
, char *fn
)
888 for (;; fn
++, pattern
++)
900 if (FSStringMatch(pattern
,fn
))
913 else if( pattern
[0] == '-' &&
917 if (pattern
[-1] <= *fn
&& *fn
<= pattern
[1])
922 start
= (++pattern
)+1;
926 while (*fn
!= *pattern
);
928 while (*pattern
!= ']')
944 FSCreateBlendedPixmapFromFile(WMScreen *scr, char *fileName, RColor *color)
946 RImage *image = NULL;
947 RImage *clone = NULL;
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)),
964 image = RLoadImage(rContext, fileName, 0);
966 image = RGetImageFromXPMData(rContext, file_plain);
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);
987 FSCreatePixmapWithBackingFromFile(WMScreen
*scr
, char *fileName
, RColor
*color
)
989 RImage
*image
= NULL
;
990 RImage
*clone
= NULL
;
992 WMPixmap
*pixmap
= NULL
;
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
)),
1007 image
= RLoadImage(rContext
, fileName
, 0);
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
);
1039 FSCreateBlurredPixmapFromFile(WMScreen
*scr
, char *fileName
)
1044 RImage
*image
= NULL
;
1045 RImage
*clone
= NULL
;
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
)),
1059 image
= RLoadImage(rContext
, fileName
, 0);
1061 image
= RGetImageFromXPMData(rContext
, file_plain
);
1068 RClearImage(image
, &color
);
1069 pixmap
= WMCreatePixmapFromRImage(scr
, image
, 0);
1071 RReleaseImage(image
);
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
1092 int fileLen
, pathLen
;
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
);
1124 (void)va_arg(ap
, char *);
1126 buffer
= (char *) wrealloc(buffer
, initBufferSize
+cnt
+fileLen
);
1129 for (i
= 0; i
< fileLen
; ++i
)
1136 (void)va_arg(ap
, char *);
1138 buffer
= (char *) wrealloc(buffer
, initBufferSize
+cnt
+pathLen
);
1141 for (i
= 0; i
< pathLen
; ++i
)
1170 FSSetBusyCursor(WMWidget
*w
, Bool state
)
1172 Window wnd
= WMWidgetXID(w
);
1173 WMScreen
*scr
= WMWidgetScreen(w
);
1174 Display
*dpy
= WMScreenDisplay(scr
);
1180 cursor
= XCreateFontCursor(dpy
, XC_watch
);
1181 XDefineCursor(dpy
, wnd
, cursor
);
1184 XUndefineCursor(dpy
, wnd
);
1189 FSGetDNDType(FileInfo
*fileInfo
)
1193 char *pathname
= NULL
;
1195 if(fileInfo
== NULL
)
1198 switch (fileInfo
->fileType
)
1200 case S_LINK
: type
=DndLink
;
1204 case HOME
: type
=DndDir
;
1206 default : pathname
= GetPathnameFromPathName(fileInfo
->path
,
1208 magic_get_type(pathname
, buf
);
1209 if(strcmp("EXEC", buf
) == 0)
1211 else if(strcmp("ascii", buf
) == 0)
1213 else if(strcmp("ascii", buf
) == 0)
1224 FSExecCommand(char *path
, char *execStr
)
1230 sprintf(s
, _("Error changing to %s, \"%s\" cancelled."), path
);
1231 FSErrorDialog(_("Error File Operation"), s
);
1236 if (execCommand(execStr
))
1243 FSImageTypeIsSupported(char *imgType
)
1248 if(imgType
== NULL
|| info
->imgTypes
[0] == NULL
)
1251 for(i
=0; info
->imgTypes
[i
]; i
++)
1253 if(strcmp(imgType
, info
->imgTypes
[i
]) == 0)