1 Replies - 710 Views - Last Post: 18 August 2009 - 03:52 PM Rate Topic: -----

#1 dr_spitfire   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 99
  • Joined: 13-August 09

Buttons, Mouse, unfamiliar code in C...

Posted 18 August 2009 - 02:40 PM

Ok, most of you know the story I joined the project I'm working on part way in, I'm designing in a language I'm unfamiliar with on a platform I'm unfamiliar with.

Now I was able to decipher a lot of very sloppy code and decided to rewrite the project to be cleaner (to which everyone exclaimed excitedly "do that!")

I got part way through though and there's some code that doesn't make sense to me and the person who wrote it is gone so I can't ask them questions about it (and from what I've seen of their code they may not be able to answer it anyway... cuz they're not a programmer they're a scientist)

So I've got a project that displays an object and on button presses or mouse clicks (depending on what's being done) the object will rotate or move around the screen...

So I go scanning for how those events are handled and I find the following code is in place to interpret these things...
void input(w, data, call)
	 Widget w;
	 caddr_t data;
	 XmDrawingAreaCallbackStruct *call;
   XEvent *event = call->event;
   XButtonEvent *xbutton = (XButtonEvent *)event;
   XKeyEvent *xkey = (XKeyEvent *) event;
   XMotionEvent *move = (XMotionEvent *) event;
   if (event->type == ButtonPress){
	  if (xbutton->button == Button1){
		 pressed =1;
		 oldx = xbutton->x-oldx;
		 oldy = xbutton->y-oldy;

/* from there on out until input closes it's more code that is either variations on what I've just posted or code I understand but for your sanity and mine let me leave that out for the moment*/

I don't understand what any of that means pretty much so I was going to keep that intact and assume the guy at least kind of knew what he was doing...

So that looks something like a function call (and since I don't really know C or how C handles events or things like that I'm praying it functions anything like a function would) and then I scour the code for something that calls it and the only reference to input in all the code is a line way down in main...
XtAddCallback(box, XminputCallBack, input, NULL);

now I've never seen anything like this before ever but since it's the only reference to input which I should mention contains like a fourth of code for the project

So my question is this
Is this what calls 'input' or do I need to go back and keep searching?
If this is what calls it, is there a way I could... I don't know... pass it a series of data that I was kind of hoping to initialize in main and wouldn't be accessible by it? Maybe a structure which I learned about in my other thread and realized that there were other good places where I could use it in my code to make things significantly easier on myself...
Is this even the best way to do it? Or should I just leave it alone and thank the coding gods it works because I don't know anything about it?

Is This A Good Question/Topic? 0
  • +

Replies To: Buttons, Mouse, unfamiliar code in C...

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Buttons, Mouse, unfamiliar code in C...

Posted 18 August 2009 - 03:52 PM

This is indeed a function that connects a callback to a specific event. In your case (I guess), when XminputCallBack event executes, the program calls input.

This post has been edited by Dogstopper: 18 August 2009 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1