LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-09-2009, 02:25 AM   #1
nasr-noor
LQ Newbie
 
Registered: Aug 2005
Posts: 15
Blog Entries: 1

Rep: Reputation: 0
Question How to SendMessage?


Hi all,
I want to have a x window program that get keyboard and mouse event.
I do this with peekmessage and other message loop.But I want to have my own message and send them when needed and peek them at message loop.That's easy in windows, but in linux I don't know.
can you tell me how create, send and receive my own message in linux?
Thanks
 
Old 06-09-2009, 05:24 AM   #2
thangappan
Member
 
Registered: May 2009
Posts: 52

Rep: Reputation: 16
Re:Send message

Which programming language are you expecting?
Where do you need to send a message?

Could you please clarify your question?
 
Old 06-09-2009, 10:16 AM   #3
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
The fundamental messaging system under X11 deals with structures of type XEvent . The main difference to Windows is that X11 also requires a connection to an X11 display to exchange messages ('XOpenDisplay' et al). There's 'XSendEvent' for sending events and 'XNextEvent' and related for receiving . I think these infos should suffice to help you find more informations about this system on your own

If you don't want to go this deep, there are of course also the common toolkits like GTK, Qt etc., which take care of the low-level stuff by themselves already, and provide interfaces which might be easier to use in the long run.
 
Old 06-10-2009, 12:51 AM   #4
nasr-noor
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Hi,
I have a window that create with x window command, and some subwindow on it.
I read serial port and show some value on subwindow.
I want to send a message to my message loop to repaint a subwindow when receive a value from serial.
Now How can I create and send this message?
Like SendMessage(WM_PAINT,,) in windows.
Thanks
 
Old 06-10-2009, 08:52 AM   #5
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Code:
// setup event data
XExposeEvent ev = { Expose, 0, 1, xdisplay, xsubwin, 0, 0, xsubwinwidth, xsubwinheight, 0 };

// send event to display connection
XSendEvent(xdisplay, xsubwin, False, ExposureMask, (XEvent *) &ev);

// if you want the message to arrive immediately add a call to XFlush(xdisplay) here ...
 
Old 06-10-2009, 06:02 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
In XT for instance There is a callback function you can specify on a file.
so that when there is input on the file the callback is
run,

man XtAppAddInput

I think GTK has similar, probably Qt
depends what X framework you are using.
 
Old 06-13-2009, 02:11 AM   #7
nasr-noor
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Hi fantas,
I have 2 class one for subwin and other for win, the window of win class is parent of subwin window.
I create a window and have 4 subwindow, and want to repaint window.
There are some of my code below:
#1#
In SubWindow class

XEvent EventPaint;
void SubWindow::Redisplay()
{
EventPaint.type = Expose;
EventPaint.xexpose.display = display;
EventPaint.xexpose.window = window;
EventPaint.xexpose.send_event = false;
EventPaint.xexpose.count = 1;
EventPaint.xexpose.x = winX;
EventPaint.xexpose.y = winY;
EventPaint.xexpose.width = Width;
EventPaint.xexpose.height = Height;
EventPaint.xexpose.serial = 0;
XSendEvent(display,window,false,ExposureMask,&EventPaint);
XFlush(display);
}
#2#
In Window class

XNextEvent(display,&event);
switch(event.type)
{
case Expose:
// return subwindow number for subwindow Window
WindowNumber = RepaintWindow(event.xexpose.window); switch( WindowNumber )
{
case 0:
Paint();
break;
case 1:
sub1.Paint();
break;
...
}
#########
In section 1, I printf the EventPaint.xexpose.window and that has correct value,
but in section 2, that is the main program when I printf the EventPaint.xexpose.window,I have only the main window number. And only my main(parent) window redisplay.
I think that send message don't work!

Why when I receive message I receive main window insted of subwindow window??

Thanks.
 
Old 06-13-2009, 07:40 AM   #8
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Do you use the same display connection ? (i.e. the 'Display *' returned by 'XOpenDisplay')

What are the values for winX and winY ? These are local window coordinates, so for example if you want to redraw the whole window they should be 0.
 
Old 06-13-2009, 09:36 AM   #9
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
And why not use my example code ? The values I put in are the ones that will work for the default case (just replace the ones that are obviously different).
 
Old 06-13-2009, 10:49 PM   #10
nasr-noor
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Oh, Sorry
I find my mistake,in line below I should use "parent" insted of "window"

XSendEvent(display,window,false,ExposureMask,&EventPaint);

I send message to subwindow! Not to main window.

Thanks fantas.
 
Old 06-14-2009, 10:46 PM   #11
nasr-noor
LQ Newbie
 
Registered: Aug 2005
Posts: 15

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Hi fantas
I have another question.
When close,minimize or maximize icon of window clicked,which message sends to server? I want to manage them myself. And when resize window with mouse which message send?
 
Old 06-15-2009, 03:27 PM   #12
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
The functions you mention are closely dependent on the window manager you are using. In general, you first query the window manager setting and then send a client message to the display connection with the new settings.

Here is an example code (copied from the Motif FAQ) to showcase how it's done e.g. for minimizing a window:

Code:
void
IconifyMe (dpy, win)
Display *dpy;
Window win;     /* toplevel window to iconify */
{
    Atom xa_WM_CHANGE_STATE;
    XClientMessageEvent ev;

    xa_WM_CHANGE_STATE = XInternAtom (dpy,
                            "WM_CHANGE_STATE", False);

    ev.type = ClientMessage;
    ev.display = dpy;
    ev.message_type = xa_WM_CHANGE_STATE;
    ev.format = 32;
    ev.data.l[0] = IconicState;
    ev.window = win;

    XSendEvent (dpy,
            RootWindow (dpy, DefaultScreen(dpy)),
            True,
            (SubstructureRedirectMask | SubstructureNotifyMask),
            &ev);
    XFlush (dpy);
}
This should help you get started.
 
  


Reply



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



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

All times are GMT -5. The time now is 10:54 AM.

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
Open Source Consulting | Domain Registration