LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2012, 12:59 AM   #1
cin_
Member
 
Registered: Dec 2010
Posts: 277

Rep: Reputation: 24
C : Current Screen Rotation Value :: xrandr() XRR Functions


Compiled a solution.

I am simply trying to write a program that prints the current orientation`gravity`rotation of X.

I am having no luck.
Here is my attempt.

I hoped someone could see what I was trying to do and help me to do it. The functions or libraries here are not necessary just where I began looking.

Code:
#include <stdio.h>

#include <X11/Xlib.h>	//For XGravityEvent
#include <X11/Xutil.h>	//For XWMGeometry
#include <X11/extensions/Xrandr.h>	//for XRROutputChangeNotifyEvent


//Declarations for XWMGeometry
Display *display;
int screen; 
char *user_geom; 
char *def_geom; 
unsigned int bwidth; 
XSizeHints hints; 
int x_return; 
int y_return; 
int width_return; 
int height_return; 
int gravity_return;


//Function To Print Rotation Name	
static char *
rotation_name (Rotation rotation)
{
	int i;
	printf(" %s	.. XRROutputChangeNotifyEvent Rotation Function Call\n",rotation);
	if ((rotation & 0xf) == 1)
		printf("normal");
		return "normal";
	return "invalid rotation";
}

//Xrandr.h  typedef struct XRROutputChangeNotifyEvent
XRROutputChangeNotifyEvent xrrchange;

//Xlib.h  typedef struct XGravityEvent
XGravityEvent xgrav;


int main(){

rotation_name(xrrchange.rotation);

//Xutil.h XWMGeometry
int XWMGeometry(display, screen, user_geom, def_geom, bwidth, hints, x_return, y_return, width_return, height_return, gravity_return);


printf(" %s	.. XWMGeometry Gravity\n",gravity_return);
printf(" %d	.. XGravityEvent X Origin\n",xgrav.x);
printf(" %d	.. XGravityEvent Y Origin\n",xgrav.y);
printf(" %s	.. XRROutputChangeNotifyEvent Rotation\n",xrrchange.rotation);

return 0;
}
... running it...

Code:
# gcc gravy.c -o gravy
gravy.c: In function `main:
gravy.c:30 warning: parameter names (without types) in function declaration
# ./gravy
 (null)	.. XRROutputChangeNotifyEvent Rotation Function Call
 (null)	.. XWMGeometry Gravity
 0	.. XGravityEvent X Origin
 0	.. XGravityEvent Y Origin
 (null)	.. XRROutputChangeNotifyEvent Rotation
# xrandr -o left
# ./gravy
 (null)	.. XRROutputChangeNotifyEvent Rotation Function Call
 (null)	.. XWMGeometry Gravity
 0	.. XGravityEvent X Origin
 0	.. XGravityEvent Y Origin
 (null)	.. XRROutputChangeNotifyEvent Rotation
I have just been trying anything. Any help would be greatly appreciated.

I assume the (null) implies I have not declared what it is I am calling...

Last edited by cin_; 02-26-2012 at 03:48 PM. Reason: gramm`err
 
Old 02-18-2012, 07:55 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,813

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
If you don't initialize a character array I don't understand how it would print as the characters "(null)". Are you somehow replacing the standard printf with a function from some other library?

In any case, when you call a function there are input parameters which have to be set and outputs which are set by the function. If you don't give it any input parameters it is unlikely to give you anything useful in the output.
 
Old 02-18-2012, 10:44 PM   #3
cin_
Member
 
Registered: Dec 2010
Posts: 277

Original Poster
Rep: Reputation: 24
(null)

For the XGravityEvent, from Xlib.h :
http://linux.die.net/man/3/xgravityevent
This is the struct
Code:
typedef struct {
     int type;                /* GravityNotify */
     unsigned long serial;    /* # of last request processed by server */
     Bool send_event;         /* true if this came from a SendEvent request */
     Display *display;        /* Display the event was read from */
     Window event;
     Window window;
     int x, y;
} XGravityEvent;
So here I was just trying to pull the x,y, hoping it would tell me something about the orientation, but regardless of the rotation it remains (0,0)

From Xrandr.h...
http://www.opensource.apple.com/sour...sions/Xrandr.h
This is the struct
Code:
typedef struct {
    int type;			/* event base */
    unsigned long serial;	/* # of last request processed by server */
    Bool send_event;		/* true if this came from a SendEvent request */
    Display *display;		/* Display the event was read from */
    Window window;		/* window which selected for this event */
    int subtype;		/* RRNotify_OutputChange */
    RROutput output;		/* affected output */
    RRCrtc crtc;	    	/* current crtc (or None) */
    RRMode mode;	    	/* current mode (or None) */
    Rotation rotation;		/* current rotation of associated crtc */
    Connection connection;	/* current connection status */
    SubpixelOrder subpixel_order;
} XRROutputChangeNotifyEvent;
Here I was trying to pull the current rotation.
My confusion here is with the crtc. Both what it is and how to assign it or use it specifically.
I have tried to look through xrandr.c, and still can not understand how to use this function.

From Xutil.h
http://www.tigr.net/afterstep/X/xlib...MGeometry.html
Code:
int XWMGeometry(display, screen, user_geom, def_geom, bwidth, hints, x_return, y_return,
                width_return, height_return, gravity_return)
      Display *display;
      int screen;
      char *user_geom;
      char *def_geom;
      unsigned int bwidth;
      XSizeHints *hints;
      int *x_return, *y_return; 
      int *width_return;
      int *height_return;
      int *gravity_return;
My assumption was missing input parameters but I could not find any help as to what they are or how to use them.
I know am miss using these functions and I was hoping someone could show me how, preferably with a code example and explanation.
Thanks

Last edited by cin_; 02-18-2012 at 10:47 PM. Reason: gramm`err
 
Old 02-18-2012, 11:48 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,201

Rep: Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392
Code:
# gcc gravy.c -o gravy
gravy.c: In function `main:
gravy.c:30 warning: parameter names (without types) in function declaration
Fix the warnings first. You should compile with -Wall and -Wextra to get more warnings, there are a lot of problems with your code.

The warning you are currently receiving is because the line
Code:
int XWMGeometry(display, screen, user_geom, def_geom, bwidth, hints, x_return, y_return, width_return, height_return, gravity_return);
doesn't call XWMGeometry, but rather declares it. You should remove int from in front of the function name.


Quote:
Originally Posted by smallpond
If you don't initialize a character array I don't understand how it would print as the characters "(null)".
It's a "feature" of glibc's printf.
 
1 members found this post helpful.
Old 02-19-2012, 12:50 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,813

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
How is it going to give you geometry if you don't tell it which display or screen you want it for? X supports multiple displays each with multiple screens.
 
Old 02-19-2012, 07:23 PM   #6
cin_
Member
 
Registered: Dec 2010
Posts: 277

Original Poster
Rep: Reputation: 24
Spot On

ntbuski, thanks for the suggestion. This will help me to dig my way through the XWMGeometry issue.
My question now is how do you specify for printf() a nonstandard datatype. If the library creates a Rotation datatype, how do I print its value?
The -Wall extends the warning to show:
Code:
gravy.c:85: warning: format '%s' expects type 'char *', but argument 2 has type 'Rotation *'
Is there some information in all this?
From http://www.x.org/archive/X11R7.5/doc...r.3.html#sect3
Code:
Datatypes

Rotations/Reflections

Can be any of:

#define RR_Rotate_0             1
#define RR_Rotate_90            2
#define RR_Rotate_180           4
#define RR_Rotate_270           8
/* new in 1.0 protocol, to allow reflection of screen */
/* reflection is applied after rotation */
#define RR_Reflect_X            16
#define RR_Reflect_Y            32
typedef struct {
    int    width, height;
    int    mwidth, mheight;
} XRRScreenSize;
typedef struct {
    int type;                /* event base */
    unsigned long serial;    /* # of last request processed by server */
    Bool send_event;        /* true if this came from a SendEvent request */
    Display *display;        /* Display the event was read from */
    Window window;            /* window which selected for this event */
    Window root;            /* Root window for changed screen */
    Time timestamp;            /* when the screen change occurred */
    Time config_timestamp;    /* when the last configuration change */
    SizeID size_index;
    SubpixelOrder subpixel_order;
    Rotation rotation;
    int width;
    int height;
    int mwidth;
    int mheight;
} XRRScreenChangeNotifyEvent;
Do the degrees of two imply the Rotation datatype acts as an integer?
Or some helpful information in?:
http://www.x.org/archive/X11R7.5/doc...r.3.html#sect5



smallpond, I hear you, but my problem is I do not know how to specify and I am asking for help by showing some code on how it is supposed to be done. I hoped for some default value being current display, current screen, or the like. Anything to get going. When programming I know what I want to do and it becomes a matter of figuring how to do what I want to do.

I know I want to tell it what display and what screen; I am asking how I specify each in the syntax required.
This is even a vague approach to what I really want to do. I thought this could be a superficial problem for someone who looks at a library function and knows instinctively how to call it.
From seeing how someone would call this function I could broaden the application to what I really want to do.

This thread could be [solved] with someone who knows how writing a ten line piece of code showing a working example using the XWMGeometry function and any of the typedef struct implementations.

Thanks for all of the replies.

Last edited by cin_; 02-19-2012 at 07:28 PM. Reason: gramm`err
 
Old 02-19-2012, 10:37 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,201

Rep: Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392
Quote:
Originally Posted by cin_ View Post
My question now is how do you specify for printf() a nonstandard datatype. If the library creates a Rotation datatype, how do I print its value?
Quote:
XRRRotations returns both the possible set of rotations/reflections supported (as a bitmask) as the value of the function, along with the current rotation/reflection of the screen.
Bitmask implies an integer type, so you should use %x in printf to see the hex value.
 
Old 02-20-2012, 01:51 PM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,813

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
hostname, X display and screen are in your DISPLAY environment variable.

Quote:
echo $DISPLAY
localhost:16.0
and in your code:

Quote:
Display *dpy = XOpenDisplay(char * displayname);
 
Old 02-21-2012, 12:17 AM   #9
cin_
Member
 
Registered: Dec 2010
Posts: 277

Original Poster
Rep: Reputation: 24
Figured

The problem was ld().
By specifically linking Xrandr.so the functions inherent began to work and I could start logically working through the errors.

I was getting a lot of " undefined reference to `FUNCTION' ". It seems that linking was the solution.
Code:
# gcc gravy.c -Wall -o gravy
/tmp/ccU29hum.o: In function `main':
gravy.c:(.text+0x17): undefined reference to `XOpenDisplay'
gravy.c:(.text+0x97): undefined reference to `XRRRotations'
gravy.c:(.text+0xda): undefined reference to `XDisplayName'
colletc2: ld returned 1 exit status

# ./gravy
bash: ./gravy: No such file or directory

# gcc gravy.c -Wall -o gravy -lX11
/tmp/ccPNzTY.o: In function `main':
gravy.c:(.text+0x97): undefined reference to `XRRRotations'

# ./gravy
bash: ./gravy: No such file or directory

# gcc gravy.c /usr/lib/libXrandr.so -Wall -o gravy -lX11
# ./gravy
 1	.. ScreenCount
 0	.. XGravityEvent X Origin
 0	.. XGravityEvent Y Origin
 :0.0	.. Display Name
 0	.. Screen Number
 0	.. XRROutputChangeNotifyEvent Rotation
 1	.. XRRRotations Bitmask
 1	.. XRRRotations Integer
Current X Rotation is normal,0 ; 1

# xrandr -o left
# ./gravy
 1	.. ScreenCount
 0	.. XGravityEvent X Origin
 0	.. XGravityEvent Y Origin
 :0.0	.. Display Name
 0	.. Screen Number
 0	.. XRROutputChangeNotifyEvent Rotation
 2	.. XRRRotations Bitmask
 2	.. XRRRotations Integer
Current X Rotation is left,90 ; 2
hazzah









... final code ...
Code:
#include <stdio.h>

#include <X11/Xlib.h>	//For XGravityEvent
#include <X11/extensions/Xrandr.h>	//for XRR Functions


//Xrandr.h  typedef struct XRROutputChangeNotifyEvent
XRROutputChangeNotifyEvent xrrchange;

//Xlib.h  typedef struct XGravityEvent
XGravityEvent xgrav;

int main()
{

Display *dpy;
Window root;
int screen, screen_count;
Rotation rotation, current_rotation;

dpy = XOpenDisplay (NULL); //open current display
if (!dpy) { printf("3x17");}
if (dpy)
{
	screen = DefaultScreen(dpy);
	screen_count = ScreenCount(dpy);
	printf(" %d	.. ScreenCount\n",screen_count);
	root = RootWindow(dpy, screen_count);
}


rotation = XRRRotations (dpy, screen, &current_rotation);

printf(" %d	.. XGravityEvent X Origin\n",xgrav.x);
printf(" %d	.. XGravityEvent Y Origin\n",xgrav.y);
printf(" %s	.. Display Name\n",XDisplayName((char*)dpy));
printf(" %d	.. Screen Number\n",screen);
printf(" %d	.. XRROutputChangeNotifyEvent Rotation\n",xrrchange.rotation);
printf(" %x	.. XRRRotations Bitmask\n", current_rotation);
printf(" %d	.. XRRRotations Integer\n", current_rotation);

switch (current_rotation)
{
	case RR_Rotate_0:
		printf("Current X Rotation is normal,0 ; 1\n");
		break;
	case RR_Rotate_90:
		printf("Current X Rotation is left,90 ; 2\n");
		break;
	case RR_Rotate_180:
		printf("Current X Rotation is downside up,180 ; 4\n");
		break;
	case RR_Rotate_270:
		printf("Current X Rotation is right,270 ; 8\n");
		break;
	default:
		printf("3x17\n");
}
return 0;
}

Last edited by cin_; 02-21-2012 at 04:57 PM. Reason: gramm`err
 
  


Reply

Tags
xlib


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux Games: No Gravity LXer Syndicated Linux News 0 01-17-2012 09:10 AM
LXer: Zemlin's Linux Foundation:The New Center of Linux Gravity LXer Syndicated Linux News 0 08-22-2008 06:11 AM
Changing GkrellM plugin gravity... netcrawl Linux - Software 2 08-20-2004 05:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration