LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-31-2007, 07:52 AM   #1
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Rep: Reputation: 15
Backend for a Glade-app


I've just started with Glade, and I've buildt a simple frontend.

I would like the app to make a local folder based on user input.

I've got a simple entry-box and a button.

If the user enter ex. /home/myname/newdir in the entry-box and then click the button the mkdir command are supposed to be executed.

I belive I need some c-code, but I have never done any programming (except php and shell).

Can anyone help me getting startet? Know of any tutorial for executing shell-commands with a glade app (if possible)....

I've found a lot of tutorials for making the GUI (frontend) with glade, but I haven't found any regarding my problem....

Thanks!

@ngelot
 
Old 05-31-2007, 10:49 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Glade is both a GUI designer, and to a lesser extent, a code generator. The code generated is a basic template that you finish off by completing the functionality of the event handlers for each widget. Depending on the version of Glade, the code generated is either C or C++. It is possible, in C/C++, to invoke shell commands using the system() function. For something as simple as creating a directory, it is easier to simply use library/system functions, such as mkdir().
I realize that none of this will make much sense unless you have some knowledge of C/C++. The C/C++ code generated by Glade won't make too much sense to a C/C++ programmer either, without having learned a bit about GTK and its underpinnings, which is quite well documented on the GTK website.
--- rod.
 
Old 05-31-2007, 07:52 PM   #3
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Original Poster
Rep: Reputation: 15
I've done som work, and now I can execute a command.

This is a cutout of callbacks.c:

Code:
GtkWidget *path_entry = lookup_widget(GTK_WIDGET(button), "path_entry");
gchar *path = gtk_editable_get_chars(GTK_EDITABLE(path_entry), 0, -1);

const gchar *ProgramName="/usr/bin/gmplayer";

gchar *argv[4];

argv[0]=(gchar *) ProgramName;
argv[1]="audio.mp3";
argv[2]=0;

execv(ProgramName, argv);
I run in to two problems with this:

1. I would like gmplayer to open audio.mp3 in the path from userinput path_entry.

I've tried with:
Code:
argv[1]=((gchar *) path, "audio.mp3");
But that won't work :-)
How can I write this?

2. After execution my app is killed and only gmplayer is left...
How can I make my app to stay open? fork? clone?

@angelot
 
Old 05-31-2007, 10:55 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The filename argument to mplayer must be a full filespec, with path:
Code:
char filespec[512];  /* Naive, but for demonstation... */

strcpy( filespec, path );
strcat( filespec, "audio.mp3" );
argv[1] = filespec;
Use fork() + exec() to keep your parent process:
Code:
if( fork() ){

}
else{
    execv( Progname, argv );
}
--- rod.

EDIT:
PS. Nice work for a non C programmer.

Last edited by theNbomr; 05-31-2007 at 10:57 PM.
 
Old 06-01-2007, 07:27 AM   #5
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by theNbomr
PS. Nice work for a non C programmer.
Thanks :-) Google is fantastic....

That did the trick, and I've got this code:
Code:
void
on_start_button_clicked (GtkButton *button, gpointer user_data)
{

GtkWidget *path_entry = lookup_widget(GTK_WIDGET(button), "path_entry");
gchar *path = gtk_editable_get_chars(GTK_EDITABLE(path_entry), 0, -1);

GtkWidget *audiofile_entry = lookup_widget(GTK_WIDGET(button), "audiofile_entry");
gchar *audiofile = gtk_editable_get_chars(GTK_EDITABLE(audiofile_entry), 0, -1);

const gchar *ProgramName="/usr/bin/gmplayer";

gchar filespec[512]; 

gchar *argv[4];

strcpy( filespec, path );
strcat( filespec, "audio.mp3" );
argv[1] = filespec;

if ( vfork() )

{

}

else

{
    execv( ProgramName, argv );
}

}
1. When I launch my app from a terminal, enter path_entry and click the start_button mplayer (not gmplayer btw!) open in the same terminal window I executed my app. Then when I kill mplayer with Ctrl-C this also kills my app. I've tried both fork and vfork but it doesn't seem to matter...

2. Why can't I do this
Code:
strcat( filespec, audiofile );
instead of
Code:
strcat( filespec, "audio.mp3" );
3. Is
Code:
gchar filespec[512];
supposed to be
Code:
gchar *filespec[512];
Thanks a lot for helping me understand how tis works!

@ngelot
 
Old 06-01-2007, 11:02 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
On the question of Ctrl-C killing your mplayer & its GUI parent, I think that the Ctrl-C signal gets sent to all processes whose controlling terminal (or maybe whose stdin) is that TTY. If that is correct, there are two approaches that I think should work (and I am spitballing, here). The easiest, probably, is to trap, using the signal() system call, the Ctrl-C signal, and handle it in some graceful way (probably ignoring it) in your GUI. I think an alternative, and probably a more graceful arrangement, would be to pipe() mplayer's stdin & stdout to your GUI. Then, create a button that issues a signal to the mplayer child process to kill it. You could also use this to grab mplayer's stdout, and display it in a GUI window, winning favor from your application's users, accolades from your colleagues, fame and glory (sorry, no cash; this is open source).
The exact mechanics of the first method can be mostly gleaned from the man pages for signal & kill(2). The second arrangement is a bit more complex, but for a decent description of how to use pipe()'s, I like Beej's Guide to IPC.

Code:
strcat( filespec, audiofile );
Should be perfectly valid, as long as the arguments are correctly initialized.

Code:
gchar filespec[512];
is an array of characters. Essentially, storage for a string.
Code:
gchar *filespec[512];
is an array of pointers to characters. Essentially, a bunch of pointers to one of those which is declared above. Until initialized, each of said pointers is a pointer to somewhere completely random, and dereferencing it would be likely to cause a Bad Thing.
Before calling string functions, don't forget to
Code:
#include <string.h>
GTK may have some wrapper library function that would possibly be better to use as an alternative.

--- rod.

Last edited by theNbomr; 06-01-2007 at 11:04 AM.
 
Old 06-04-2007, 03:30 AM   #7
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Original Poster
Rep: Reputation: 15
Thanks for the tip - I'll look into it....

I still can't execute the file. I've switched to gedit now, and trying to open a text-file.

EDIT: I found the error:

I redused gchar *argv[4]; to gchar *argv[2]; and then nothing worked....

When pushing it up to four again - everything worked. What is it with the [] ?

My headers:
Code:
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "errno.h"
#include "string.h"
Is there someone missing - do I need everyone? I've just added them on the fly beliving more is better :-)
And - is the syntax correct? I've seen both <name.h> and "name.h" in tutorials....

Last edited by @ngelot; 06-04-2007 at 03:52 AM.
 
Old 06-04-2007, 04:23 AM   #8
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Original Poster
Rep: Reputation: 15
Well - I fond another error....

When running the app after make I got it working, but after exiting (with the button I made) and rerun the app it wouldn't open the file.

If I use the menu File/Quit it works everytime....

This code kills the app correctly:
Code:
gboolean on_gims_app_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
gtk_main_quit();
return FALSE;
}

void
on_quit1_activate (GtkMenuItem *menuitem, gpointer user_data)
{
gtk_main_quit();

}
But this won't:
Code:
void
on_quit_button_clicked (GtkToolButton *toolbutton, gpointer user_data)
{
gtk_main_quit();

}
I've tried with return FALSE; in her too, but I get an error when compiling...

Do I need another gboolean for my toolbutton?
 
Old 06-04-2007, 10:29 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It looks like you need to understand what the argv[] argument is for. This is the list of arguments passed to the program. Each of the arguments, pointers to strings, should be initialized to point to strings that make some sense in the context of the program being launched. In the case of execv(), the argument list is terminated by a NULL pointer, '(char *)0'. The first element of argv[] should be a duplicate of the 'path' argument. Failing to correctly initialize the elements of your argv[] argument list will result in a Bad Thing.
To put the explanation in a different context, the argv[] array corresponds to '$1', '$2', '$3'.... in a bash script. To confirm that your arguments are being correctly configured, you could make a testbed launcher that launches a bash script, and that bash script could simply echo its commandline arguments. If the output from that looks good, then use that format for your real program launcher.
I think your last question is a symptom of the same problem.

--- rod.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
MythTV Backend Jongi SUSE / openSUSE 3 04-23-2006 08:42 PM
backend device kpachopoulos Linux - General 4 01-05-2006 03:59 PM
failing to compile a simple glade app florg Programming 1 08-06-2005 03:25 AM
Glade 2.5.0 and Perl-Glade-Two - a little help please Lake-end Programming 1 03-07-2005 04:47 AM
Moving Glade app to another computer gnosis Linux - Software 2 06-04-2004 05:29 PM

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

All times are GMT -5. The time now is 06: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