LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-01-2022, 09:40 PM   #1
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Rep: Reputation: Disabled
Getting a Bash variable into GTK3


Bash variables are loosely typed, sorta like javascript. GTK3 (well, c) uses actual types like int, char[]. Now suppose I'm writing a program where I want to display the computer's IP address in a GTK_LABEL, but actually the address comes out of something like
Code:
myip=$(dig +short myip.opendns.com @resolver1.opendns.com)
(dig example from the web to read a current IP).

An ip4 IP can be at most 20 chars long but can I use char[21]? How do I get from dig's output to GTK3's input? I can't use system(). Is there a more practical way to read the IP?

Oh wait, I could probably do something like
Code:
rslt=system(somevar=$(dig +short myip.opendns.com @resolver1.opendns.com)
but system only returns an int which had me stumped. Somevar could maybe be a char[21].

Last edited by ab1jx; 01-01-2022 at 09:57 PM. Reason: add myip, a bash variable, somevar
 
Old 01-02-2022, 12:30 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,882
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
To get the output of the command, popen(3) might be better than system(3). Then you can use inet_aton(3) or inet_pton(3).
 
Old 01-02-2022, 10:11 AM   #3
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
You can use getenv () to read environment variables.
Ed
 
Old 01-02-2022, 10:25 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,255

Rep: Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326
If you're using GTK, wouldn't you call g_spawn_command_line_sync or something similar?
 
Old 01-02-2022, 12:19 PM   #5
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
I'm just barely learning GTK, I don't know about such things yet.

This program:
Code:
#include <stdio.h>
FILE *p;  // pipe

int main(void) {
  p = popen("$(dig +short myip.opendns.com @resolver1.opendns.com)","r");
  if (p) printf("Not null\n");
  pclose(p);
  return 0;
}
Returns:
Code:
./pas
Not null
sh: 1: 172.58.160.58: not found
So the IP gets dumped out on a command line and sh doesn't know why. I guess it's a start.

I want to set up a Raspberry Pi with an 8 inch touch screen display (no mouse or keyboard) as an ad blocking wifi AP fed from a CAT5 internet connection. Right now I have something similar fed by this Pi, which is fed by Easytether and a USB cable from a cell phone. So I want useful stuff on the touchscreen I can read and tap on. I was trying to use Whiptail but I don't think that's going to cut it.
 
Old 01-02-2022, 01:05 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,882
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
It's a good start, here is an improved version:
Code:
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

int main(void) {
  FILE *p;  // pipe
  char strip[32];
  unsigned l;
  struct in_addr ip;

  p = popen("dig +short myip.opendns.com @resolver1.opendns.com","r");
  if (!p) {
     printf("error in popen: errno=%d %s\n", errno, strerror(errno));
     exit(12);
  }
  if (!fgets(strip, sizeof strip, p)) {
     printf("error in fgets: errno=%d %s\n", errno, strerror(errno));
     exit(13);
  }
  pclose(p);

  l= strlen(strip);
  while (l>0 && isspace(strip[l-1])) strip[--l] = '\0';
  printf ("\"%s\"\n", strip);

  if (inet_aton(strip, &ip)!=1) {
     printf("inet_aton(%s) failed\n", strip);
     exit(13);
  }

  printf("ok, IP=%s\n", inet_ntoa(ip));

  return 0;
}
 
1 members found this post helpful.
Old 01-06-2022, 08:18 PM   #7
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
I'm newish to GTK but I like GTK3. I've done some xlib stuff, and bunches of Borland Delphi in another life. Making a fake GTK form here. I can turn off blocking with symlink games with the hosts file. So there's my real IP at top, beginnings of a button (1 of 2), and 2 labels for (fake) uptime and data age.
Attached Thumbnails
Click image for larger version

Name:	snap_blocking.png
Views:	7
Size:	5.3 KB
ID:	38022  
 
1 members found this post helpful.
Old 01-09-2022, 09:08 AM   #8
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Quote:
Originally Posted by ab1jx View Post
I'm newish to GTK but I like GTK3.
I too like GTK 3. It is the best of the GUI toolkits (IMO).

You may want to look into Glade to create complex GUIs.
Ed
 
Old 01-09-2022, 09:50 AM   #9
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
I've used Glade, or tried to. I've been trying since GTK 1 and 2, maybe 20 years, it just never clicked before. Now I can pull out my checklist or an old program I wrote and actually make stuff happen. I do xlib that way now too, there's a set of steps which have to be in the right order with none forgotten. https://sourceforge.net/projects/epidemicsim/

I'd like to see somebody write an unglade which could remove glade's tokens and replace them with straight GTK code so it would compile without libglade. Glade is "cheating" in my book. I haven't tried GTK4 yet.
 
Old 01-09-2022, 12:22 PM   #10
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Libglade has been replaced by GtkBuilder. GTK 3 reads the Glade XML files directly.

Calling GTK to set up widgets becomes unwieldly after more than a half-dozen or so. On complex GUIs, Glade can be a significant time-saver.
Ed
 
Old 01-09-2022, 12:29 PM   #11
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
What it looks like so far.

Semi-related question: I make my 2 pushbuttons for blocking on and off with gtk_button_new_with_label(). I use signal_connect_swapped and write callbacks which I can trigger when they're clicked. But I want to change some colors on the faces of the buttons to show the state of the blocking. I can send markup to them except I need a handle or pointer or something that refers to the labels in the buttons.

Just tried
Code:
onLabel = btn_on->get_child();
but ‘GtkWidget’ {aka ‘struct _GtkWidget’} has no member named ‘get_child’
Attached Thumbnails
Click image for larger version

Name:	blockctl2.png
Views:	3
Size:	9.0 KB
ID:	38054  
 
Old 01-09-2022, 12:35 PM   #12
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Are you using the C API? You can change the label by calling gtk_button_set_label ().
Ed
 
Old 01-10-2022, 03:16 PM   #13
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
Hmm, it's not so much changing the label I want as having a cursor, something that indicates which item is selected. I don't see a reasonable way to change the markup, I was going to change the text color on the selected item. I suppose I could add an * or something to the selected one and take it away again. (Blocking on/Blocking off buttons). I'm looking at Devhelp for info. Maybe there's a way to change the markup but it belongs to the label that's on the button, I don't see anything under gtk_button that looks right.
 
Old 01-10-2022, 06:20 PM   #14
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
You want a GtkCheckButton.
Ed
 
Old 01-11-2022, 02:30 PM   #15
ab1jx
Member
 
Registered: Feb 2017
Posts: 93

Original Poster
Rep: Reputation: Disabled
I'm so far sticking with my original idea of changing the text color by changing markup. I do
Code:
list1 = gtk_container_get_children(GTK_CONTAINER(btn_off));
gtk_label_set_markup(GTK_LABEL(list1->data),markup1);
Which sort of works, except the way I'm getting a handle/pointer/whatever for the labels on the buttons is by doing gtk_container_get_children. This is clumsy if there's more than one child, you'd really want more like list1->data[0] which doesn't work. Get_children returns a glist but how do you move around the list? I was using list1, markup1, etc. expecting a list for each button.

Code:
  char *format1;
  char *format2;
  gchar *markup1;
  gchar *markup2;
  format1 = "<span foreground=\"#000000\">%s</span>"; // black
  format2 = "<span foreground=\"#00ff00\">%s</span>"; // green
  markup1 = g_markup_printf_escaped(format1,"Blocking Off");
  markup2 = g_markup_printf_escaped(format2,"Blocking On");
  gtk_label_set_markup(GTK_LABEL(list2->data),markup2);
It works until I try to subscript the list, then it segfaults when I click my buttons.
Attached Thumbnails
Click image for larger version

Name:	red.png
Views:	4
Size:	10.0 KB
ID:	38078   Click image for larger version

Name:	green.png
Views:	3
Size:	9.9 KB
ID:	38079  
 
  


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
[SOLVED] Get bc script variable into a bash script variable? 14hei Programming 6 02-26-2016 07:34 AM
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 01:30 PM

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

All times are GMT -5. The time now is 01:25 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
Open Source Consulting | Domain Registration