Changeset 548 for trunk


Ignore:
Timestamp:
07/01/09 20:11:51 (15 years ago)
Author:
Nicholas Riley
Message:

brightness.m: Port command-line argument syntax from brightness.c.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LocationDo/brightness.m

    r547 r548  
    1 /* gcc -o brightness -framework Cocoa -framework DisplayServices -F/System/Library/PrivateFrameworks brightness.m */
     1/* gcc -std=c99 -o brightness -framework Cocoa -framework DisplayServices -F/System/Library/PrivateFrameworks brightness.m */
    22
    33#import <Foundation/Foundation.h>
     4#include <crt_externs.h>
    45
    56@interface O3Manager : NSObject
     
    2021const int kMaxDisplays = 16;
    2122
    22 int main(int argc, const char *argv[])
    23 {
    24   CGDirectDisplayID display[kMaxDisplays];
    25   CGDisplayCount numDisplays;
    26   CGDisplayCount i;
    27   CGDisplayErr err;
     23#define APP_NAME *(_NSGetProgname())
     24
     25void errexit(const char *fmt, ...) {
     26  va_list ap;
     27  va_start(ap, fmt);
     28  fprintf(stderr, "%s: ", APP_NAME);
     29  vfprintf(stderr, fmt, ap);
     30  fprintf(stderr, "\n");
     31  exit(1);
     32}
     33
     34void usage() {
     35  fprintf(stderr, "usage: %s [-m|-d display] [-v] <brightness>\n", APP_NAME);
     36  fprintf(stderr, "   or: %s -l [-v]\n", APP_NAME);
     37  exit(1);
     38}
     39
     40int main(int argc, char * const argv[]) {
     41  if (argc == 1)
     42    usage();
    2843 
     44  BOOL verbose = NO;
     45  unsigned long displayToSet = 0;
     46  enum { ACTION_LIST, ACTION_SET_ALL, ACTION_SET_ONE } action = ACTION_SET_ALL;
     47  extern char *optarg;
     48  extern int optind;
     49  int ch;
     50
     51  while ( (ch = getopt(argc, argv, "lmvd:")) != -1) {
     52    switch (ch) {
     53    case 'l':
     54      if (action == ACTION_SET_ONE) usage();
     55      action = ACTION_LIST;
     56      break;
     57    case 'v':
     58      verbose = YES;
     59      break;
     60    case 'm':
     61      if (action != ACTION_SET_ALL) usage();
     62      action = ACTION_SET_ONE;
     63      displayToSet = (unsigned long)CGMainDisplayID();
     64      break;
     65    case 'd':
     66      if (action != ACTION_SET_ALL) usage();
     67      action = ACTION_SET_ONE;
     68      errno = 0;
     69      displayToSet = strtoul(optarg, NULL, 0);
     70      if (errno == EINVAL || errno == ERANGE)
     71        errexit("display must be an integer index (0) or a hexadecimal ID (0x4270a80)");
     72      break;
     73    default: usage();
     74    }
     75  }
     76 
     77  argc -= optind;
     78  argv += optind;
     79
     80  float brightness;
     81  if (action == ACTION_LIST) {
     82    if (argc > 0) usage();
     83  } else {
     84    if (argc != 1) usage();
     85
     86    errno = 0;
     87    brightness = strtof(argv[0], NULL);
     88    if (errno == ERANGE)
     89      usage();
     90    if (brightness < 0 || brightness > 1)
     91      errexit("brightness must be between 0 and 1");
     92  }
     93
    2994  [[NSAutoreleasePool alloc] init];
    3095  [O3Manager initialize];
    3196
     97  CGDirectDisplayID display[kMaxDisplays];
     98  CGDisplayCount numDisplays;
     99  CGDisplayErr err;
    32100  err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);
    33   if (err != CGDisplayNoErr) {
    34       NSLog(@"Cannot get displays (%d)", err);
    35       exit(1);
    36   }
    37   printf("%d displays found", (int)numDisplays);
    38   for ( i = 0; i < numDisplays; ++i ) {
    39       CGDirectDisplayID dspy = display[i];
    40       CFDictionaryRef originalMode;
     101  if (err != CGDisplayNoErr)
     102    errexit("cannot get list of displays (error %d)\n", err);
    41103
    42       originalMode = CGDisplayCurrentMode(dspy);
    43       if (originalMode == NULL)
    44         continue;
     104  for (CGDisplayCount i = 0; i < numDisplays; ++i) {
     105    CGDirectDisplayID dspy = display[i];
     106    CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
     107    if (originalMode == NULL)
     108      continue;
    45109
    46       NSLog(@"Display 0x%x: %@", (unsigned int)dspy, originalMode);
    47 
    48       if ([[(NSDictionary *)originalMode objectForKey: @"RefreshRate"] intValue] == 0) {
    49         id<BrightnessEngineWireProtocol> engine =
    50           [O3Manager engineOfClass: @"BrightnessEngine" forDisplayID: dspy];
    51         NSLog(@"Engine: %@", engine);
    52         NSLog(@"Brightness was %f", [engine brightness]);
    53         if (argc == 2) {
    54           float newBrightness = [[NSString stringWithCString: argv[1]] floatValue];
    55           if (newBrightness < 0. || newBrightness > 1.) {
    56             NSLog(@"Brightness should be between 0 and 1");
    57             exit(1);
    58           }
    59           [engine setBrightness: newBrightness];
    60           NSLog(@"Brightness is now %f", [engine brightness]);
    61         }
     110    if (action == ACTION_LIST) {
     111      printf("display %d: ", i);
     112      if (CGMainDisplayID() == dspy)
     113        printf("main display, ");
     114      printf("ID 0x%x\n", (unsigned int)dspy);
     115      if (verbose) {
     116        puts([[(NSDictionary *)originalMode description] UTF8String]);
    62117      }
    63118    }
    64   exit(0);
     119
     120    if ([[(NSDictionary *)originalMode objectForKey: @"RefreshRate"] intValue] != 0)
     121      continue;
     122
     123    id<BrightnessEngineWireProtocol> engine =
     124      [O3Manager engineOfClass: @"BrightnessEngine" forDisplayID: dspy];
     125
     126    switch (action) {
     127    case ACTION_SET_ONE:
     128      if ((CGDirectDisplayID)displayToSet != dspy && displayToSet != i)
     129        continue;
     130    case ACTION_SET_ALL:
     131      [engine setBrightness: brightness];
     132      if (!verbose) continue;
     133    case ACTION_LIST:
     134      printf("display %d: brightness %f\n", i, [engine brightness]);
     135    }
     136  }
     137  return 0;
    65138}
Note: See TracChangeset for help on using the changeset viewer.