Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

Refresh rate switching

RobGRobG Posts: 23Members
I wrote my first C program today.

I had tried to use cscreen to switch refresh rates on my Mac mini/Sony XBR5 combo, but it kept switching to 1080i with -r 60 switch. I did some poking around and came to the (now) obvious conclusion that CGDisplayBestModeForParameters() doesn't take interlaced/progressive into account. Poop.

The program below looks for a new display mode with the same resolution and bit depth as the current display mode, while ignoring interlaced modes. It only changes the refresh rate (in theory).

/*
 * refresh.c
 *
 * Copyright (c) 2010 Robin Gridley
 */

#include <ApplicationServices/ApplicationServices.h>

int main(int argc, char *argv[]) {
	
	double refresh;
	double cur_refresh;
	int i;
	
	CFNumberRef width;
	CFNumberRef height;
	CFNumberRef depth;
	CFNumberRef cur_width;
	CFNumberRef cur_height;
	CFNumberRef cur_depth;
	CFArrayRef modes;
	CFDictionaryRef mode;
	CGDisplayConfigRef config;
	
	if (argc != 3) {
		printf("Usage: refresh -r <rate>\n");
		return 0;
	}
	
	refresh = strtod(argv[2], 0);
	
	mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
		
	cur_width = CFDictionaryGetValue(mode, kCGDisplayWidth);		
	cur_height = CFDictionaryGetValue(mode, kCGDisplayHeight);
	cur_depth = CFDictionaryGetValue(mode, kCGDisplayBitsPerPixel);
	
	modes = CGDisplayAvailableModes(kCGDirectMainDisplay);
	
	for (i = 0; i < CFArrayGetCount(modes); i++) {
		
		mode = CFArrayGetValueAtIndex(modes, i);
		
		CFNumberGetValue(CFDictionaryGetValue(mode, kCGDisplayRefreshRate), kCFNumberDoubleType, &cur_refresh);
		
		width = CFDictionaryGetValue(mode, kCGDisplayWidth);		
		height = CFDictionaryGetValue(mode, kCGDisplayHeight);
		depth = CFDictionaryGetValue(mode, kCGDisplayBitsPerPixel);
		
		if ( ! CFDictionaryContainsKey(mode, kCGDisplayModeIsInterlaced) && 
		    CFNumberCompare(cur_width, width, NULL) == kCFCompareEqualTo && 
		    CFNumberCompare(cur_height, height, NULL) == kCFCompareEqualTo && 
		    CFNumberCompare(cur_depth, depth, NULL) == kCFCompareEqualTo && 
		    cur_refresh == refresh) {
		    
			CGBeginDisplayConfiguration(&config);
			CGConfigureDisplayMode(config, kCGDirectMainDisplay, mode);
			CGCompleteDisplayConfiguration(config, kCGConfigureForSession);
			break;
		}
	}
}


Save it as refresh.c and use the following command to compile it:

gcc -framework ApplicationServices -o refresh refresh.c


Or download it here.

Usage example:

refresh -r 24


So after writing this, I have to ask: why doesn't Plex do this automatically? Is there some techinical issue preventing this from happening? Or is it just a lack of spare time?
«13

Comments

  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    RobG wrote on 01 June 2010 - 03:34 PM:

    I wrote my first C program today.

    ...

    So after writing this, I have to ask: why doesn't Plex do this automatically? Is there some techinical issue preventing this from happening? Or is it just a lack of spare time?

    Because Elan hasn't released a build with the refresh rate switching I wrote last week. :) Previously it was lack of time (and a 24p TV).

    I've taken a similar approach to you. Basically now Plex will check to see if a matching refresh rate is available for the currently playing video at the same resolution, and switch to that if possible. However I'm using the 10.6 API, so this will only be available on 10.6. I don't think resolution change is a good idea, better just to run at your highest resolution and let Plex / OS X handle the scaling.

    -Ryan
  • RobGRobG Posts: 23Members
    edited June 2010
    tokyovigilante wrote on 01 June 2010 - 06:52 AM:
    However I'm using the 10.6 API, so this will only be available on 10.6. I don't think resolution change is a good idea, better just to run at your highest resolution and let Plex / OS X handle the scaling.


    I started with the 10.6 API, since my mini is running 10.6.3, but I couldn't figure out a way to detect if the display mode is interlaced. Unfortunately, on my setup, when you loop over the array returned by CGDisplayCopyAllDisplayModes(), the first matching 1920x1080 @ 60Hz mode is interlaced. :(

    I like how Apple deprecates all of the old functions, provides new 10.6 specific functions, but doesn't bother to document them. I was stuck poking through header files.
  • RockStarRockStar Posts: 2,027Members, Plex Pass Plex Pass
    Great work from both of you. Looking forward to this being implemented
  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    RobG wrote on 02 June 2010 - 03:37 AM:

    I started with the 10.6 API, since my mini is running 10.6.3, but I couldn't figure out a way to detect if the display mode is interlaced. Unfortunately, on my setup, when you loop over the array returned by CGDisplayCopyAllDisplayModes(), the first matching 1920x1080 @ 60Hz mode is interlaced. :(

    I like how Apple deprecates all of the old functions, provides new 10.6 specific functions, but doesn't bother to document them. I was stuck poking through header files.

    That's a good point. Have you asked on the quartz-api mailing list? I'll check in with them if you haven't.
  • RobGRobG Posts: 23Members
    tokyovigilante wrote on 01 June 2010 - 02:40 PM:

    That's a good point. Have you asked on the quartz-api mailing list? I'll check in with them if you haven't.


    I have not. Thanks!
  • Sergio AzenhaSergio Azenha Posts: 3Members
    Any news regarding this issue?

    Lack of auto refresh rate switching on Plex is the only thing preventing me from switching from my Windows based HTPC to the Mac Mini.
  • NEINEI Posts: 94Members, Plex Pass Plex Pass
    Yeah, where's the binary to test this... ;-)

    Thanks in advance!
  • RobGRobG Posts: 23Members
    My code is for a command line program that can be used as part of a script to switch refresh rates.

    I imagine you won't see anything from tokyovigilante's work until Plex 9 is released. Speaking of which, what did the quartz-api mailing list have to say about detecting (and skipping) interlaced modes?
  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    RobG wrote on 18 June 2010 - 09:09 AM:

    My code is for a command line program that can be used as part of a script to switch refresh rates.

    I imagine you won't see anything from tokyovigilante's work until Plex 9 is released. Speaking of which, what did the quartz-api mailing list have to say about detecting (and skipping) interlaced modes?

    Nothing sadly, I fear it's been left out of the CGDisplay API. I may fall pack to the 10.5 API if I can't work it out.

    -Ryan
  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    tokyovigilante wrote on 20 June 2010 - 06:57 PM:

    Nothing sadly, I fear it's been left out of the CGDisplay API. I may fall pack to the 10.5 API if I can't work it out.

    -Ryan

    Ah, voila.

    kDisplayModeInterlacedFlag = 0x00000040
    
  • stevenDstevenD Posts: 49Members ✭✭
    it's great to read that there is work going on on the refresh rate issue. Do you think that this will be inckuded in Plex 0.9?
  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    stevenD wrote on 20 June 2010 - 09:54 PM:

    it's great to read that there is work going on on the refresh rate issue. Do you think that this will be inckuded in Plex 0.9?

    Yes. In fact it's in there already. ;)
  • maerskmaersk Posts: 146Members, Plex Pass Plex Pass
    tokyovigilante thank you for this great future within Plex nine and thank you for all your hard work, just as all developers of Plex.
    Much appreciated by my girlfriend and also myself. We will wait patiently for Plex .9
  • RobGRobG Posts: 23Members
    tokyovigilante wrote on 20 June 2010 - 02:10 AM:

    Ah, voila.

    kDisplayModeInterlacedFlag = 0x00000040
    



    Awesome.
  • belongamickbelongamick Posts: 14Members
    tokyovigilante wrote on 20 June 2010 - 09:23 AM:

    Yes. In fact it's in there already. ;)


    Great to hear that! Just bought a new Mac Mini for use as my new HTPC running plex. Been trying to setup manual scripts to change refresh rates with some success but haven't managed to set interlaced/progressive.

    i.e. When using scripts to change to 1920 x 1080 24Hz it would choose Progessive but with 50Hz and 60Hz it would always choose Interlaced. Even though I can manually set 50Hz and 60Hz in System Preferences and it uses Progressive Mode.

    But, think I'll just wait for Plex v9 for this functionality.

    Keep up the great work!

    Cheers
  • RobGRobG Posts: 23Members
    belongamick wrote on 22 June 2010 - 05:29 PM:

    Great to hear that! Just bought a new Mac Mini for use as my new HTPC running plex. Been trying to setup manual scripts to change refresh rates with some success but haven't managed to set interlaced/progressive.

    i.e. When using scripts to change to 1920 x 1080 24Hz it would choose Progessive but with 50Hz and 60Hz it would always choose Interlaced. Even though I can manually set 50Hz and 60Hz in System Preferences and it uses Progressive Mode.

    But, think I'll just wait for Plex v9 for this functionality.

    Keep up the great work!

    Cheers


    See first post. The program I wrote will allow you to setup your scripts and avoid them switching back to interlaced.
  • belongamickbelongamick Posts: 14Members
    Doh! How did I miss that :huh:

    Will give that a go tonight, cheers!
  • belongamickbelongamick Posts: 14Members
    RobG wrote on 24 June 2010 - 03:07 AM:

    See first post. The program I wrote will allow you to setup your scripts and avoid them switching back to interlaced.


    Nice coding, works perfectly!

    Many thanks
  • RobGRobG Posts: 23Members
    edited September 2010
    Next version of Plex 9 maybe?

    Edit:

    The new library and Plex for iOS are awesome. I'm watching Dragnet on my iPhone. :D
  • tokyovigilantetokyovigilante Plex Dev Team Posts: 363Members
    RobG wrote on 09 September 2010 - 03:01 AM:

    Next version of Plex 9 maybe?

    Edit:

    The new library and Plex for iOS are awesome. I'm watching Dragnet on my iPhone. :D

    Yes :).
«13
Sign In or Register to comment.