2013-07-29 107 views
0

我正在使用Safari Plugin(Objective c)。我想從我的插件更改屏幕亮度。我用「IODisplayGetFloatParameter」和「IODisplaySetFloatParameter」試了一下。但它給我錯誤「無法獲得顯示0x1b5098cc(錯誤-536870201)的亮度」。如何解決這個錯誤?如何在Mac OS x中更改屏幕亮度?

+0

你可能會發現你的答案在這裏: http://stackoverflow.com/questions/9721582/adjust-mac-display-brightness- objective-c –

+0

感謝Taha回覆。但我自己使用Macintosh。 – Nik

回答

0

這裏是線程的答案Adjust Mac display brightness Objective-C?

// almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/ 
- (float) get_brightness { 
    CGDirectDisplayID display[kMaxDisplays]; 
    CGDisplayCount numDisplays; 
    CGDisplayErr err; 
    err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays); 

    if (err != CGDisplayNoErr) 
     printf("cannot get list of displays (error %d)\n",err); 
    for (CGDisplayCount i = 0; i < numDisplays; ++i) { 


     CGDirectDisplayID dspy = display[i]; 
     CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy); 
     if (originalMode == NULL) 
      continue; 
     io_service_t service = CGDisplayIOServicePort(dspy); 

     float brightness; 
     err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness, 
             &brightness); 
     if (err != kIOReturnSuccess) { 
      fprintf(stderr, 
        "failed to get brightness of display 0x%x (error %d)", 
        (unsigned int)dspy, err); 
      continue; 
     } 
     return brightness; 
    }  
    return -1.0;//couldn't get brightness for any display 
} 


// almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/ 
- (void) set_brightness:(float) new_brightness { 
    CGDirectDisplayID display[kMaxDisplays]; 
    CGDisplayCount numDisplays; 
    CGDisplayErr err; 
    err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays); 

    if (err != CGDisplayNoErr) 
     printf("cannot get list of displays (error %d)\n",err); 
    for (CGDisplayCount i = 0; i < numDisplays; ++i) { 


     CGDirectDisplayID dspy = display[i]; 
     CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy); 
     if (originalMode == NULL) 
      continue; 
       io_service_t service = CGDisplayIOServicePort(dspy); 

     float brightness; 
     err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness, 
             &brightness); 
     if (err != kIOReturnSuccess) { 
      fprintf(stderr, 
        "failed to get brightness of display 0x%x (error %d)", 
        (unsigned int)dspy, err); 
      continue; 
     } 

     err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness, 
             new_brightness); 
     if (err != kIOReturnSuccess) { 
      fprintf(stderr, 
        "Failed to set brightness of display 0x%x (error %d)", 
        (unsigned int)dspy, err); 
      continue; 
     } 

     if(brightness > 0.0){ 
     }else{ 
     } 
    }  

}