Hello All

the following is an alternative to  Ludovic Rousseau's
keyboard_brigthness shell-script (1).
Advantages:
- slightly shorter name
- allows toggle
- can be set SUID and bound to keyboard shortcuts via xbindkeys

Let me know what you think!

cheers
Hannes

1) 
http://mactel-linux.svn.sourceforge.net/viewvc/mactel-linux/trunk/tools/keyboard_brigthness?view=markup
/*
 * Apple Macbook Pro keyboard illumination control
 *
 * Copyright (C) 2006 Hannes Wyss <[EMAIL PROTECTED]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BRIGHTNESS "/sys/class/leds/smc:kbd_backlight/brightness"
#define WARNFMT "Warning: invalid brightness value '%i'\n"
#define STORE "/var/lib/keylight"

void usage(char* argv0) { 
	printf("Apple Macbook (pro) keyboard illumination " VERSION);
	printf("\n");
	printf("Usage:\n");
	printf("%s -h    : display usage information\n", argv0);
	printf("%s       : read current brightness value\n", argv0);
	printf("%s n     : set brightness to n [0-255]\n", argv0);
	printf("%s +n    : increase brightness by n\n", argv0);
	printf("%s -n    : decrease brightness by n\n", argv0);
	printf("%s toggle: toggle between current brightness value and 0\n", 
      argv0);
}

int set_level(int value) {
  FILE *fptr;
  if((fptr = fopen(BRIGHTNESS, "w"))) {
    if(value < 0) {
      fprintf(stderr, WARNFMT, value);
      value = 0;
    }
    if(value > 255) {
      fprintf(stderr, WARNFMT, value);
      value = 255;
    }
    fprintf(fptr, "%i", value);
    return value;
  } else {
    err(errno, "%s", BRIGHTNESS);
  }
}

int write_store(int value) {
  FILE *fptr;
  int bytes;
  if((fptr = fopen(STORE, "w"))) {
    bytes = fprintf(fptr, "%i", value);
    fclose(fptr);
    return bytes;
  } else {
    warn("%s", STORE);
    return 0;
  }
}

int read_store() {
  FILE *fptr;
  char stored[16];
  int value = -1;
  if((fptr = fopen(STORE, "r"))) {
    if(fgets(stored, 16, fptr)) {
      value = atoi(stored);
    }
    fclose(fptr);
  } else {
    warn("%s", STORE);
  }
  return value;
}

int toggle(int value) {
  int stored;
  if(value == 0) {
    stored = read_store();
    if(stored >= 0) {
      value = set_level(stored);
    } else {
      fprintf(stderr, "Warning: No valid value found in: %s\n", 
          STORE);
    }
  } else {
    value = set_level(0);
  }
  return value;
}

int dispatch(char** argv, int value) {
  char *input = argv[1];
  if(strncmp("-h", input, 2) == 0) {
    usage(argv[0]); 
    exit(0);
  } else if(strncmp("toggle", input, 6) == 0) {
    value = toggle(value);
  } else if(strncmp("-", input, 1) == 0
      || strncmp("+", input, 1) == 0) {
    value = set_level(value + atoi(input));
    write_store(value);
  } else {
    value = set_level(atoi(input));
    write_store(value);
  }
  return value;
}

int main(int argc, char** argv) {

  FILE *fptr;
  char sysvalue[16];
  int value;

  if((fptr = fopen(BRIGHTNESS, "r"))) {
    if(fgets(sysvalue, 16, fptr)) {
      value = atoi(sysvalue);
    }
    fclose(fptr);
    if(argc > 1) {
      value = dispatch(argv, value);
    }
    printf("%i\n", value);
  } else {
    err(errno, "%s", BRIGHTNESS);
  }
  return 0;
}

Attachment: Makefile
Description: Binary data

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Mactel-linux-users mailing list
Mactel-linux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mactel-linux-users

Reply via email to