davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

adjust terminal window position & size opened by C program

Mon Apr 25, 2016 4:51 pm

hello,
how can one adjust terminal window position & size opened by C program when writing to console?
often it's too small, and sometimes it overlaps with a openvg window (or even vanishes completely behind it), so a fixed customized postion and size would be helpful to define in the program.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Mon Apr 25, 2016 7:24 pm

If your program thinks it's running from a terminal then it has no concept of the X11 window, that is entirely down to whatever terminal emulator is running it.

As a quick hack you could have your program execute the wmctrl program using system(), that can modify any window's position

Code: Select all

system("wmctrl -r Terminal -e 0,40,40,-1,-1");
will move the first window named "Terminal" to 40,40 and leave it's size alone. To be more flexible you'd need to use another call to wmctrl to determine the exact window to move rather than just the first one of a given name, read the man page for wmctrl to see how to use it.

Note: wmctrl isn't installed by default, you'll need to install it first with apt-get or whatever.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Mon Apr 25, 2016 8:14 pm

re: "If your program thinks it's running from a terminal .."

actually I doubt that my program can think, either of a window size, or of anything ;)

The program is not running from a terminal AFAIK. It's simply nothing more than sth like a

Code: Select all

printf("Hello World");
getchar();
in that case, started from Geany by F5 or by sudo./helloworld a window appears at a more or less arbitrary, random position.
so system("wmctrl -r Terminal -e 0,40,40,-1,-1") will fix this new position to 40,40,
Is that what you mean?

Can I pre-define it's size too?
the man wmctrl is actually, well, sort of messed-up and - slightly - oversized ... :-/
http://blog.spiralofhope.com/1042/wmctr ... ation.html
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 12:00 am

davenull wrote:re: "If your program thinks it's running from a terminal .."

actually I doubt that my program can think, either of a window size, or of anything ;)

The program is not running from a terminal AFAIK. It's simply nothing more than sth like a

Code: Select all

printf("Hello World");
getchar();
in that case, started from Geany by F5 or by sudo./helloworld a window appears at a more or less arbitrary, random position.
so system("wmctrl -r Terminal -e 0,40,40,-1,-1") will fix this new position to 40,40,
Is that what you mean?

Can I pre-define it's size too?
the man wmctrl is actually, well, sort of messed-up and - slightly - oversized ... :-/
http://blog.spiralofhope.com/1042/wmctr ... ation.html
That's not the actual man page, but it does show you sample output that the man pages don't. The actual manpage is more like http://linux.die.net/man/1/wmctrl

Just tried on my pc, running from Geany will run your program in the default terminal (on mine it's gnome-terminal, the RPi is probably lxterminal). If you run it from the command line then you are already running in a terminal and no extra windows should be being opened.

The line I gave you will set the first window named "Terminal" (the name given by -r Terminal) to the geometry set by -e 0,40,40,-1,-1 which is gravity,xpos,ypos,width,height (just leave gravity as 0, and set width and height to -1 to say keep the current values).

Hmmm.. openbox configuration settings are in ~/.config/openbox/lxde-pi-rc.xml - I take it somewhere in there you can specify specific geometries for programs... Not sure if there is an editor to make editing it easier (I don't use the gui on my RPi).
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 8:09 am

yes, perhaps it's a LX terminal window (I don't understand much of the Linux details), and when I don't start my programs by Geany for testing, then I start it by a script from my desktop, e.g. robot.sh:
sudo ./ [path]/robot

(I would love to start it by the filemanager like I'm used to by the Windows Explorer, but that Linux filemanger is not capable of starting programs which require sudo :evil: )

so
"wmctrl -r Terminal -e 0,200,200,640,480"
will open a 640x480 window at 200,200, correct?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 10:07 am

davenull wrote:yes, perhaps it's a LX terminal window (I don't understand much of the Linux details), and when I don't start my programs by Geany for testing, then I start it by a script from my desktop, e.g. robot.sh:
sudo ./ [path]/robot

(I would love to start it by the filemanager like I'm used to by the Windows Explorer, but that Linux filemanger is not capable of starting programs which require sudo :evil: )

so
"wmctrl -r Terminal -e 0,200,200,640,480"
will open a 640x480 window at 200,200, correct?
It won't open one, it will move the first window it finds with that name. Go ahead and play with it, see what parameters you need to set for your use.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 11:01 am

I just wanted to install, but all related pages only refer to Ubuntu + Fedora - is it safe to apt-get install wmctrl also on Debian Jessie?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jahboater
Posts: 5228
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 11:10 am

davenull wrote: (I would love to start it by the filemanager like I'm used to by the Windows Explorer, but that Linux filemanger is not capable of starting programs which require sudo :evil: )
sudo chown root myprog
sudo chmod u+s myprog

Then "myprog" will run with effective user id root, no sudo required.
Last edited by jahboater on Tue Apr 26, 2016 12:08 pm, edited 1 time in total.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 11:23 am

ok, that is a secondary theater of war...
the point was:
being able to select an arbitrary executable file from the filemanager as I actually hate command lines!

But the main issue is first about how to install wmctrl safely for Jessie.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 11:57 am

davenull wrote:But the main issue is first about how to install wmctrl safely for Jessie.
By the standard apt-get method or from the menu Preferences->Add/Remove Software.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 5:24 pm

does not work...!

"wmctrl -r Terminal -e 0,200,200,-1,-1"
option -1, -1 invalid
window opened at 0,0 (bottom left)


"wmctrl -r Terminal -e 0,200,200,640,480"
"wmctrl -r Terminal -e 0,200,200,800,640"
window opened at 0,0 (bottom left)

always the same std size.

what is wrong?

(full HD HDMI, 1920x1080)

my test code -CORRECTED, UPDATED - :

Code: Select all

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string.h>


void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &dow) {
   time_t t = time(NULL);
   struct tm  tm = * localtime(&t);	
   
   year = (int)tm.tm_year+1900-2000, 
   mon  = (int)tm.tm_mon+1;
   day  = (int)tm.tm_mday;
   hour = (int)tm.tm_hour;
   min  = (int)tm.tm_min;
   sec  = (int)tm.tm_sec;
   dow  = (int)tm.tm_wday+1;

     	
}


int main()
{

   char sbuf[100];	
   
   int year, mon, day, hour, min, sec, dow;
   
   system("wmctrl -r LXTerminal -e 0,200,200,-1,-1");

   
   if (system( "ping -c1 -s1 www.google.de") ) {
	  printf("internet connx failed \n");
   }
   else {
	  printf("internet connx OK ! :) \n");
   }   
   
   getdatetime(year, mon, day, hour, min, sec, dow) ;
   sprintf( sbuf, "%d %d %d %d %d %d %d", year, mon, day, hour, min, sec, dow ); 
   printf(sbuf);
   
   getchar();
   return 0;
}




Last edited by davenull on Tue Apr 26, 2016 6:21 pm, edited 1 time in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 5:41 pm

Is the window name Terminal? I gave that as an example because that is the name given to all the terminal windows on my PC, the RPi might name them differently. Try using whatever name appears in the title bar of the window you want to move.

0,0 is top left not bottom left. No spaces between the commas and numbers after the -e. There is a space between the e and the 0 but none after.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 5:48 pm

I have no idea what a terminal window name is...
edit... LXTerminal?

LXTerminal gives also just errors or runs faulty, even after I corrected the spaces thing:
for -1,-1:
- - not valid

always starts at bottom left, not at 200,200

always the same std size
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Tue Apr 26, 2016 10:05 pm

Yes, it looks like it calls it LXTerminal. The string in your code is correct, if there's even one space between any of the 5 numbers then you'll get an error, spaces are used as separators between arguments and those 5 numbers are all one argument.

I've just tried with your example program. It looks like the problem is that you're executing the wmctrl command whilst X11 is still in the middle of opening the window so it never gets the message to move. Putting a short delay at the start gives it chance. I put a sleep(1) at the beginning and that worked, the window opens up somewhere in the middle of the screen and then jumps to the specified location. If there's other initializing stuff you could be doing then do that before calling wmctrl.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Wed Apr 27, 2016 8:05 am

thank you,
now I'm not quite sure yet, which one is correct:
sleep(1) before or after calling the wmctrl system command ?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 2842
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: adjust terminal window position & size opened by C progr

Wed Apr 27, 2016 12:22 pm

davenull wrote:thank you,
now I'm not quite sure yet, which one is correct:
sleep(1) before or after calling the wmctrl system command ?
Before, you need to wait for the window to actually open before making the call to wmctrl.
It takes the system a few moments to bring the window up in which time your program has already started running.
She who travels light — forgot something.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Wed Apr 27, 2016 2:13 pm

thanks, now I see -
I asssumed the 1st window to be opened not already at the start, but just at the moment I'm writing my print cmd or, instead, by wmctrl !
So I expected wmctrl to initialize and place the window, and after that writing to that window by printf....

well, me and Linux... :-/
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: adjust terminal window position & size opened by C progr

Wed Apr 27, 2016 2:23 pm

YES!
No it works! Very useful that thing, indeed!
thank you! 8-)

ps,
I now included your hint into my "Quick Guide to Raspberry Pi for Dummies".

To be honest, my "Quick Guide" now slowly, step by step, becomes more and more sort of a "Encyclopedia Galactica to Linux, Raspberry Pi, and Everything" ...
:roll:
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”