Mac OS X: Command for displaying/setting resolution (source program)

Source: Internet
Author: User

Note: The code for changing the actual resolution has been completed.

During the change, because 10.6 does not provide system functions such as setting the optimal resolution before 10.5, you need to write it yourself.

 

Set and display resolution-source code

Below is a set and display of the current system
Source code for displaying devices and resolutions
If you are interested, let's test how it works in your own environment.

Concepts:

Display: displays that are currently connected to the system.
Main display: the current active display device.
Mode: a memory that is automatically generated by the system to manage the connection between each display device and the system.
Object, resolution is a part of it. Each display device may have many modes, which form a list.

Compilation steps
:
First, you must install xcode for development.
Environment, copy the following source code to a text editor, such as textedit, and save it as text, such as the source code file.
Is screenresolution. m, which is saved on your desktop. Re-compile: Enter the command in terminal:
Cd ~ /Desktop <br/> C ++ screenresolution. M-framework applicationservices-O screenresolution-arch i386 <br/>
In this way, a program for sending screenresolution is generated on your desktop.

Usage
, Run in termianl:

  • Get help :~ /Desktop/screenresolution-H
  • Obtain the number of current displays :~ /Desktop/screenresolution-
  • Resolution of the current primary monitor :~ /Desktop/screenresolution
  • Obtain the resolution of the current 2nd monitors :~ /Desktop/screenresolution 2
  • Obtain the resolution list supported by the main display :~ /Desktop/screenresolution-l
  • Obtain the resolution list supported by the 2nd monitors :~ /Desktop/screenresolution-L 2
  • Set the resolution of the current primary monitor to 800x600 :~ /Desktop/screenresolution-s 800 600
  • Set the resolution of 2nd monitors to 800x600 :~ /Desktop/screenresolution-S 2 800 600


Note:

This version is for OS X 10.6 and later versions. on the 10.6 system, the code is compiled successfully and runs successfully. On the 10.5 system, the Code is not compiled. On the 10.6 system, the code is successfully compiled, I have never run it on the 10.5 system-it should not be able to run-who has the conditions to test it. Because 10.6 has changed the underlying functions of many 10.5.

Known:

 

  • The mirro status is not considered for both monitors
  • Set the resolution as much as possible to use the supported resolutions listed by the system. Otherwise, the setting may fail.

Others:

  • Reply to a tested email.
  • If you want new features, please refer
  • This program is mainly used to control the resolution of remote computers.


Source code

 

/* <Br/> * screenresolution. m <br/> * Description: <br/> * It set/get current online display resolutions. <br/> * the version on Webpage http://forums.macosxhints.com/showthread.php? T = 59575 <br/> * is before 10.6. this is for 10.6 and more functions. <br/> * Author: <br/> * Tony Liu, June 2011 <br/> * version history: <br/> * 2011-06-01: Add-A option. <br/> * 2011-06-08: Display adding flags and not display duplicate modes. <br/> * 2011-06-09: Adding set the best fit resolution function. <br/> * compile: <br/> * C ++ screenresolution. m-framework applic Ationservices-O screenresolution-arch i386 <br/> */<br/> # include <applicationservices/applicationservices. h> <br/> struct slist {<br/> double width, height; <br/> cgdisplaymoderef mode; <br/> }; <br/> typedef int (* compfn) (const void *, const void *); <br/> void listdisplays (uint32_t displaytotal ); <br/> void listdisplayallmode (cgdirectdisplayid displayid, int index); <br/> void printusage (cons T char * argv []); <br/> void printmodeparms (double width, double height, double depth, double freq, int flag ); <br/> int getmodeparms (cgdisplaymoderef mode, double * width, double * height, double * depth, double * freq, int * flag ); <br/> int getdisplayparms (cgdirectdisplayid disp, double * width, double * height, double * depth, double * freq, int * flag); <br/> bool getbestdisplaymod (cgdirectdisplayid displ Ay, double dwidth, double dheight); <br/> int modecompare (struct slist * elem1, struct slist * elem2); <br/> uint32_t maxdisplays = 20; <br/> cgdirectdisplayid onlinedisplayids [20]; <br/> uint32_t displaytotal; <br/> char * sysflags [] = {"unknown", "interlaced ,", "Multi-display,", "not preset,", "stretched," };< br/> char flagstring [200]; <br/> int main (INT argc, const char * argv []) <br/>{< br/> double width, Height, depth, freq; <br/> int flag; <br/> int displaynum; <br/> cgdirectdisplayid thedisplay; </P> <p> // 1. getting system info. <br/> If (cggetonlinedisplaylist (maxdisplays, onlinedisplayids, & displaytotal )! = Kcgerrorsuccess) {<br/> printf ("error on getting online display list. "); <br/> return-1; <br/>}</P> <p> If (argc = 1) {<br/> cgrect screenframe = cgdisplaybounds (kcgdirectmaindisplay); <br/> cgsize screensize = screenframe. size; <br/> printf ("%. 0f %. 0f/N ", screensize. width, screensize. height); <br/> return 0; <br/>}</P> <p> If (! Strcmp (argv [1], "-l") {<br/> If (argc = 2) {<br/> listdisplays (displaytotal ); <br/> return 0; <br/>}< br/> else if (argc = 3) {<br/> displaynum = atoi (argv [2]); <br/> If (displaynum <= displaytotal & displaynum> 0) {<br/> listdisplayallmode (onlinedisplayids [displayNum-1], 0 ); <br/>}< br/> return 0; <br/>}</P> <p> If (! Strcmp (argv [1], "-a") {<br/> printf ("total online displays: % d/N", displaytotal ); <br/> return 0; <br/>}</P> <p> If ((! Strcmp (argv [1], "-? ") | (! Strcmp (argv [1], "-h") {<br/> printusage (argv); <br/> return 0; <br/>}< br/> If (! Strcmp (argv [1], "-s") {<br/> If (argc = 4) {<br/> displaynum = 1; width = atoi (argv [2]); Height = atoi (argv [3]); <br/>}< br/> else if (argc = 5) {<br/> displaynum = atoi (argv [2]); width = atoi (argv [3]); Height = atoi (argv [4]); <br/>}< br/> If (displaynum <= displaytotal) <br/> flag = getbestdisplaymod (displayNum-1, width, height); <br/> return flag; <br/>}< br/> displaynum = atoi (argv [1]); <br/> If (d Isplaynum <= displaytotal) {<br/> getdisplayparms (onlinedisplayids [displayNum-1], & width, & height, & depth, & freq, & flag ); <br/> printmodeparms (width, height, depth, freq, flag); <br/> return 0; <br/>}< br/> else {<br/> fprintf (stderr, "error: Display number out of bounds; displays on this Mac: % d. /n ", displaytotal); <br/> return-1; <br/>}< br/> return 0; <br/>}< br/> void listdisplays (uint32_t display Total) <br/>{< br/> uint32_t I; <br/> cgdisplaymoderef mode; <br/> double width, height, depth, freq; <br/> int flag; </P> <p> // cgdirectdisplayid maindisplay = cgmaindisplayid (); <br/> printf ("total online displays: % d/N ", displaytotal); <br/> for (I = 0; I <displaytotal; I ++) {<br/> printf ("display % d (ID % d ):", I + 1, onlinedisplayids [I]); <br/> getdisplayparms (onlinedisplayids [I], & width, & height, & Depth, & freq, & flag); <br/> if (I = 0) printf ("(main)"); <br/> printmodeparms (width, height, depth, freq, flag); <br/>}< br/> void listdisplayallmode (cgdirectdisplayid displayid, int inum) <br/>{< br/> cfarrayref modelist; <br/> cgdisplaymoderef mode; <br/> cfindex index, count; <br/> double width, height, depth, freq; <br/> int flag; <br/> double width1, height1, depth1, freq1; <br/> int Flag1; </P> <p> modelist = cgdisplaycopyalldisplaymodes (displayid, null); <br/> If (modelist = NULL) return; <br/> COUNT = cfarraygetcount (modelist); <br/> width1 = 0; height1 = 0; depth1 = 0; freq1 = 0; flag1 = 0; <br/> If (inum <= 0) {<br/> for (Index = 0; index <count; index ++) <br/>{< br/> mode = (cgdisplaymoderef) cfarraygetvalueatindex (modelist, index); <br/> getmodeparms (mode, & width, & height, & depth, & Freq, & flag); <br/> printmodeparms (width, height, depth, freq, flag ); <br/>}< br/> else if (inum <= count) {<br/> mode = (cgdisplaymoderef) cfarraygetvalueatindex (modelist, iNum-1); <br/> getmodeparms (mode, & width, & height, & depth, & freq, & flag); <br/> printmodeparms (width, height, depth, freq, flag); <br/>}< br/> cfrelease (modelist); <br/>}< br/> void printmodeparms (double width, do Uble height, double depth, double freq, int flag) <br/>{< br/> printf ("% ld x % LD @ % LD Hz, <% d>/N ", (long INT) width, (long INT) height, (long INT) depth, (long INT) freq, flag ); <br/>}< br/> int getdisplayparms (cgdirectdisplayid disp, double * width, double * height, double * depth, double * freq, int * flag) <br/>{< br/> int ireturn = 0; <br/> cgdisplaymoderef mode = cgdisplaycopydisplaymode (disp); <br/> Ireturn = getmodeparms (mode, width, height, depth, freq, flag); <br/> cgdisplaymoderelease (mode); <br/> return ireturn; <br/>}< br/> int getmodeparms (cgdisplaymoderef mode, double * width, double * height, double * depth, double * freq, int * sflag) <br/>{< br/> * width = cgdisplaymodegetwidth (mode); <br/> * Height = cgdisplaymodegetheight (mode ); <br/> * freq = cgdisplaymodegetrefreshrate (mode); <br/> CFS Tringref pixelencoding = cgdisplaymodecopypixelencoding (mode); <br/> * depth = 0; <br/> If (pixelencoding = NULL) Return-1; <br/> If (pixelencoding = cfstr (io32bitdirectpixels) <br/> * depth = 32; <br/> else if (pixelencoding = cfstr (io16bitdirectpixels )) <br/> * depth = 16; <br/> else * depth = 8; </P> <p> * sflag = cgdisplaymodegetioflags (mode ); <br/> cfrelease (pixelencoding); <br/> return 0; <br/>}< br/> bool Getbestdisplaymod (cgdirectdisplayid display, double dwidth, double dheight) <br/>{< br/> cfarrayref modelist; <br/> cgdisplaymoderef mode; <br/> cfindex index, count, sindex, scount = 0; <br/> double width, height, depth, freq; <br/> double width1, height1, depth1, freq1; <br/> int flag, flag1; <br/> struct slist MList [100]; <br/> int ireturn = 0; </P> <p> modelist = cgdisplaycopyalldisplaymodes (display, Null); <br/> If (modelist = NULL) return; <br/> COUNT = cfarraygetcount (modelist); <br/> scount = 0; <br/> for (Index = 0; index <count; index ++) <br/>{< br/> mode = (cgdisplaymoderef) cfarraygetvalueatindex (modelist, index ); <br/> getmodeparms (mode, & width, & height, & depth, & freq, & flag ); <br/> // printf ("........ scount = % d/N ", (INT) scount); <br/> If (! (Width = width1) & (Height = height1) & (depth = depth1) & (freq = freq1) & (flag = flag1) {<br/> If (cgdisplaymodeisusablefordesktopgui (mode) {<br/> MList [scount]. mode = mode; MList [scount]. width = width; MList [scount]. height = height; <br/> width1 = width; height1 = height; depth1 = depth; freq1 = freq; flag1 = flag; <br/> scount ++; <br/>}< br/> mode = NULL; <br/> qsort (void *) MList, scount, sizeof (Struct slist), (compfn) modecompare); <br/> for (Index = 0; index <scount; index ++) <br/>{< br/> If (MList [Index]. width> = dwidth) {<br/> If (MList [Index]. height> = dheight) {<br/> mode = MList [Index]. mode; <br/> break; <br/>}</P> <p> cgdisplayconfigref pconfigref; <br/> cgconfigureoption option = kcgconfigurepermanently; <br/> If (mode! = NULL) & (cgbegindisplayconfiguration (& pconfigref) = kcgerrorsuccess) {<br/> cgconfiguredisplaywithdisplaymode (pconfigref, display, mode, null ); <br/> If (cgcompletedisplayconfiguration (pconfigref, option )! = Kcgerrorsuccess) cgcanceldisplayconfiguration (pconfigref); <br/>}< br/> else ireturn =-1; <br/> cfrelease (modelist); <br/> return ireturn; <br/>}< br/> int modecompare (struct slist * elem1, struct slist * elem2) <br/>{< br/> If (elem1-> width <elem2-> width) <br/> return-1; <br/> else if (elem1-> width> elem2-> width) return 1; <br/> If (elem1-> height <elem2-> height) Return-1; <br/> else if (elem1 -> Height> elem2-> height) return 1; <br/> else return 0; <br/>}< br/> void printusage (const char * argv []) <br/> {<br/> char * fname = strrchr (argv [0], '/') + 1; <br/> printf ("Screen Resolution V1.0, mac OS X 10.6 or later, i386/N "); <br/> printf (" Copyright 2010 Tony Liu. all rights reserved. june 1, 2010/N "); <br/> printf ("/nusage: "); <br/> printf (" % S-A/N ", fname ); <br/> printf ("% s [-L] [1 .. 9]/n ", fname); <Br/> printf ("% s-s [1 .. 9] hor_res vert_res/N", fname); <br/> printf ("% s -? |-H this help. /n ", fname); <br/> printf ("-l list resolution, depth and refresh rate/N "); <br/> printf (" 1 .. 9 display # (default: main display)/n "); <br/> printf ("-l 1-9 list all support for the display #/N "); <br/> printf ("-S set mode. /n "); <br/> printf (" hor_res horizontal resolution/N "); <br/> printf (" vert_res vertical resolution/n "); <br/> printf ("Examples:/N"); <br/> printf ("% s-a get online display number/N", fname ); <br/> printf ("% s get current main diplay resolution/N", fname ); <br/> printf ("% S 3 get current resolution of third display/N", fname); <br/> printf ("% S-l get resolution, bit depth and refresh rate of all displays/N ", fname); <br/> printf (" % s-L 1 get first display all supported mode/N ", fname ); <br/> printf ("% s-L 1 2 get first display the second supported mode/N", fname ); <br/> printf ("% s-s 800 600 set resolution of main display to 800x600/N", fname ); <br/> printf ("% s-s 2 800 600 set resolution of secondary display to 800x600/N", fname); <br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.