/*
 * bbapm-0.0.1 patch-2 for FreeBSD-3.x and FreeBSD-4.x
 *
 * Copyright (c) 2000 by ISHIKAWA, Hidenori <hideishi@ops.dti.ne.jp>
 *
 * This patch may be used, modified, copied or distributed in both
 * source and binary as long as the above copyright and these terms
 * are retained. 
 *
 * Although the author has debugged this patch with great care,
 * under no circumstances is the author responsible for any damage
 * incurred with its use.
 *
 * In case if this patch is merged to the original bbapm-0.0.1,
 * this patch will also obey the license of the original bbapm-0.0.1,
 * i.e. GNU General Public License.
 *
 * patch-2 : Apr, 2000  Tested on FreeBSD-3.4-STABLE + PAO3
 */

/*
 * Usage:
 *        1. Prepare bbapm-0.0.1 source and deflate.
 *        2. Change the current directory to where the sources are.
 *        3. Apply this patch by following,
 *
 * bbapm-0.0.1% patch < bbapm-0.0.1-freebsd-patch-2.diff
 *
 *        4. Run ./configure and follow the instruction of bbapm.
 */

diff -urN bbapm-0.0.1.orig/Makefile.in bbapm-0.0.1/Makefile.in
--- bbapm-0.0.1.orig/Makefile.in	Sat Aug 21 07:28:11 1999
+++ bbapm-0.0.1/Makefile.in	Fri May  5 06:56:41 2000
@@ -73,7 +73,7 @@
 bbapm_SOURCES = bbapm.cc Image.cc LinkedList.cc 		 bbapm.hh Image.hh LinkedList.hh 		 resource.cc resource.hh 		 Baseresource.cc Baseresource.hh 		 Basewindow.cc Basewindow.hh 		 blackboxstyle.hh
 
 EXTRA_DIST = BUGS TODO
-bbapm_LDADD = @X_LIBS@ -lapm
+bbapm_LDADD = @X_LIBS@
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
 CONFIG_CLEAN_FILES =  version.h
@@ -88,7 +88,7 @@
 X_EXTRA_LIBS = @X_EXTRA_LIBS@
 X_PRE_LIBS = @X_PRE_LIBS@
 bbapm_OBJECTS =  bbapm.o Image.o LinkedList.o resource.o Baseresource.o \
-Basewindow.o
+Basewindow.o freebsd_apm.o
 bbapm_DEPENDENCIES = 
 bbapm_LDFLAGS = 
 CXXFLAGS = @CXXFLAGS@
diff -urN bbapm-0.0.1.orig/bbapm.cc bbapm-0.0.1/bbapm.cc
--- bbapm-0.0.1.orig/bbapm.cc	Sat Aug 21 07:20:45 1999
+++ bbapm-0.0.1/bbapm.cc	Fri May  5 06:58:14 2000
@@ -308,12 +308,12 @@
 
     if (!broken) {
 
-	if (apm_exists() < 0)
-	    broken = True;
-	else {
+//	if (apm_exists() < 0)
+//	    broken = True;
+//	else {
 	    if ((apm_read(&apm))<0)
 		broken = True;
-	}
+//	}
     }
 
     if (redraw)
@@ -336,7 +336,7 @@
     // loadbar to show that the battery is loading. A bit like GSM. But
     // in this case the label will show how far the loading is done.
     if ((resource->show.loading) &&
-	(apm.ac_line_status==1) &&
+	(apm.ai_acline==1) &&
 	(resource->show.percentage)) {
 	for (i=0; i < resource->loadbar.number_of_bars; i++) {
 	    if (noLoadBarFilled>=i) {
@@ -357,11 +357,11 @@
 	if (resource->show.percentage) {
 #ifdef DEBUG
 	    cerr << "Toolwindow::Redraw: drawing percentage " <<
-		apm.battery_percentage << " " <<
+		apm.ai_batt_life << " " <<
 		resource->loadbar.number_of_bars << endl;
 #endif
 	    for (i = 0; i < resource->loadbar.number_of_bars; i++) {
-		if (apm.battery_percentage >= load[i].level)
+		if (apm.ai_batt_life >= load[i].level)
 		    XCopyArea(dpy, pixmap.loadbar_active, framewin, frameGC,
 			      2, load[i].y - 1,
 			      load[i].width, load[i].height, load[i].x,
@@ -389,7 +389,7 @@
 	    XSetForeground(dpy, frameGC,
 			   resource->label.percentage_textColor.pixel);
 
-	    sprintf(t, "%03d%%", apm.battery_percentage);
+	    sprintf(t, "%03d%%", apm.ai_batt_life);
 	    XDrawString(dpy, labelwin, frameGC, label.space_width,
 			(label.height + resource->label.font->ascent -
 			 resource->label.font->descent) / 2,
@@ -413,7 +413,7 @@
 	    XSetForeground(dpy, frameGC,
 			   resource->label.time_textColor.pixel);
 
-	    sprintf(t, "%02d:%02d", apm.battery_time/60, apm.battery_time%60);
+	    sprintf(t, "%02d:%02d", apm.ai_batt_time/3600, (apm.ai_batt_time/60)%60);
 	    XDrawString(dpy, labelwin, frameGC, xposition,
 			(label.height + resource->label.font->ascent -
 			 resource->label.font->descent) / 2, t, strlen(t));
@@ -519,7 +519,7 @@
 		}
 	    }
 
-	    if (apm.ac_line_status==1) {
+	    if (apm.ai_acline==1) {
 		if (time(NULL) - lastLoadingTime > resource->show.loadingTime) {
 		    ReadAPM();
 		    lastLoadingTime = time(NULL);
@@ -565,6 +565,9 @@
 int
 main(int argc, char **argv)
 {
+    /* APM initialization */
+    apm_exists();
+
     ToolWindow     *
 	AppWindow = new ToolWindow(argc, argv);
 
diff -urN bbapm-0.0.1.orig/bbapm.hh bbapm-0.0.1/bbapm.hh
--- bbapm-0.0.1.orig/bbapm.hh	Tue Aug 17 03:10:23 1999
+++ bbapm-0.0.1/bbapm.hh	Fri May  5 06:56:41 2000
@@ -22,7 +22,7 @@
 #define __MAIN_HH
 
 extern "C" {
-#include <apm.h>
+#include <freebsd_apm.h>
 }
 
 #include "Image.hh"
diff -urN bbapm-0.0.1.orig/freebsd_apm.c bbapm-0.0.1/freebsd_apm.c
--- bbapm-0.0.1.orig/freebsd_apm.c	Thu Jan  1 09:00:00 1970
+++ bbapm-0.0.1/freebsd_apm.c	Fri May  5 06:56:41 2000
@@ -0,0 +1,47 @@
+/*
+ * bbapm-0.0.1 patch-1 for FreeBSD-3.x and FreeBSD-4.x
+ *
+ * Copyright (c) 2000 by ISHIKAWA, Hidenori <hideishi@ops.dti.ne.jp>
+ *
+ * This patch may be used, modified, copied or distributed in both
+ * source and binary as long as the above copyright and these terms
+ * are retained. 
+ *
+ * Although the author has debugged this patch with great care,
+ * under no circumstances is the author responsible for any damage
+ * incurred with its use.
+ *
+ * In case if this patch is merged to the original bbapm-0.0.1,
+ * this patch will also obey the license of the original bbapm-0.0.1,
+ * i.e. GNU General Public License.
+ *
+ * patch-1 : Apr, 2000  Tested on FreeBSD-3.4-STABLE + PAO3
+ */
+
+#include <stdio.h>
+#include <sys/file.h>
+#include <sys/ioctl.h>
+
+#include <machine/apm_bios.h>
+#define APMDEV  "/dev/apm"
+
+static	int	apmfd = 0;
+
+void
+apm_exists(void)
+{
+	if ((apmfd = open(APMDEV, O_RDWR)) == -1){
+		printf("cannot open APM device\n");
+		exit(1);
+	}
+}
+
+int
+apm_read(apm_info_t info)
+{
+	if (ioctl(apmfd, APMIO_GETINFO, info) == -1)
+		return -1;
+	else
+		return 0;
+
+}
diff -urN bbapm-0.0.1.orig/freebsd_apm.h bbapm-0.0.1/freebsd_apm.h
--- bbapm-0.0.1.orig/freebsd_apm.h	Thu Jan  1 09:00:00 1970
+++ bbapm-0.0.1/freebsd_apm.h	Fri May  5 06:56:42 2000
@@ -0,0 +1,25 @@
+/*
+ * bbapm-0.0.1 patch-1 for FreeBSD-3.x and FreeBSD-4.x
+ *
+ * Copyright (c) 2000 by ISHIKAWA, Hidenori <hideishi@ops.dti.ne.jp>
+ *
+ * This patch may be used, modified, copied or distributed in both
+ * source and binary as long as the above copyright and these terms
+ * are retained. 
+ *
+ * Although the author has debugged this patch with great care,
+ * under no circumstances is the author responsible for any damage
+ * incurred with its use.
+ *
+ * In case if this patch is merged to the original bbapm-0.0.1,
+ * this patch will also obey the license of the original bbapm-0.0.1,
+ * i.e. GNU General Public License.
+ *
+ * patch-1 : Apr, 2000  Tested on FreeBSD-3.4-STABLE + PAO3
+ */
+
+#include <machine/apm_bios.h>
+#define	APMDEV	"/dev/apm"
+
+void apm_exists(void);
+int apm_read(apm_info_t info);
diff -urN bbapm-0.0.1.orig/resource.cc bbapm-0.0.1/resource.cc
--- bbapm-0.0.1.orig/resource.cc	Sat Aug 21 05:43:11 1999
+++ bbapm-0.0.1/resource.cc	Fri May  5 06:56:42 2000
@@ -20,7 +20,7 @@
 // 
 
 
-#include <iostream>
+//#include <iostream>
 #include "resource.hh"
 #include "blackboxstyle.hh"
 
