/* * This file is part of firk's window manager for x11 (fwm) * Copyright (C) 2022 firk * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include #include #include #include #include "main.h" #include "config.h" #include "fsysd.h" static int batfd = -1, blfd = -1; extern void fsysd_reopen(void) { if(batfd>=0) { close(batfd); batfd = -1; } if(blfd>=0) { close(blfd); blfd = -1; } } extern int fsysd_get_pwr(fsysd_pwr *r) { char buf[60]; char *b, *b1; int k, j; if(!BATTERY_STATUS_FILE || !*BATTERY_STATUS_FILE) return 0; if(batfd<0 && (batfd=open(BATTERY_STATUS_FILE,O_RDONLY|O_NOCTTY))<0) return -1; k = pread(batfd, buf, sizeof(buf), 0); if(k<0) return -1; for(j=0; jcharge_pctt = strtol(buf, &b, 10); if(b==buf || *b!=' ') return -2; r->is_ac = strtol(b, &b1, 10); if(b==b1 || *b1!=' ') return -2; r->is_charging = strtol(b1, &b, 10); if(b==b1 || *b!=' ') return -2; r->ac_err = strtol(b, &b1, 10); if(b==b1 || *b1!=' ') return -2; r->bat_err = strtol(b1, &b, 10); if(b==b1 || *b!=' ') return -2; return 1; } extern int fsysd_get_bl(void) { char buf[60]; int r; if(!BACKLIGHT_FILE || !*BACKLIGHT_FILE) return -1; if(blfd<0 && (blfd=open(BACKLIGHT_FILE,O_RDWR|O_NOCTTY))<0) return -1; r = pread(blfd, buf, sizeof(buf)-1, 0); if(r<0) return -1; buf[r] = 0; if(*buf<'0' || *buf>'9') return -2; return atoi(buf); } extern int fsysd_set_bl(int bl) { char buf[60]; int len; if(!BACKLIGHT_FILE || !*BACKLIGHT_FILE) return -1; if(blfd<0 && (blfd=open(BACKLIGHT_FILE,O_RDWR|O_NOCTTY))<0) return -1; snprintf(buf, sizeof(buf), "%d\n", bl); len = strlen(buf); if(pwrite(blfd, buf, len, 0)!=len) return -2; if(ftruncate(blfd, len)<0) return -2; return 0; }