|
Post by konaexpress on Jul 25, 2012 19:43:16 GMT 1
Ok, I know my code is a mess but this is my first attempt to program using BaCon. I am trying to port it from another Language due to dep-hell (big sigh). I am trying to make a logout manager for Openbox that some users I know can use in their "User Remasters". I can't seem to post a photo so I will give a link to what it should look like: dl.dropbox.com/u/41965771/openbox_logout.pngWhat is the command to make a button make a shell command? Is it just "shell" or is it the "callback" thing? I will post my code here so that you can see what I am trying to do. If I am headed in the wrong direction then please let me know. (I tried to comment the code to say what I was attempting to do but the ' thing wouldn't work.) 'Openbox Logout Window 'Created with: BaCon @ www.basic-conveter.org 'By: Konaexpress
INCLUDE "/usr/share/BaCon/hug_imports.bac INIT
Mainwin = Window( "Openbox Logout" , 380, 320 )
The_image = IMAGE( "/usr/share/Openbox_Logout/openbox_text.png", 187, 33 )
ATTACH( Mainwin, The_image, -75, -20 )
OB_label = MARK( "Logout Of Openbox?", 150, 30 )
ATTACH( Mainwin, OB_lable, 91, 70 )
SD_btn = BUTTON( "Shutdown", 80, 25 ) SHELL "dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop"
RB_btn = BUTTON( "Reboot", 80, 25 ) SHELL "dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart"
LO_btn = BUTTON( "Logout", 80, 25 ) CALLBACK( LO_btn, Logout )
Cancel_btn = BUTTON( "Cancel", 80, 25 ) CALLBACK( Cancel_btn, QUIT )
ATTACH( Mainwin, SD_btn, 91, 80 )
ATTACH( Mainwin, RB_btn, 91, 120 )
ATTACH( Mainwin, LO_btn, 91, 150 )
ATTACH( Mainwin, Cancel_btn, 91, 175 )
Thanks for any advise on this and any ideas to make it better is eppreciated. -John
|
|
|
Post by alexfish on Jul 25, 2012 20:48:44 GMT 1
in first instance can try
reply$=EXEC$"your string command"
in the second instance you are doing a reboot command , so doubt in need a reply, the system may hang but so not sure
can introduce the & command to ensure fork from your app , else chances are it will hang the system
try use the SYSTEM "<your command> &"
concatenating the dbus structure can be complex using the shell commands
I did a sample of modem manager dbus on one forum , using bash here it is
#!/bin/bash
######################################################################## # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details: ######################################################################## # cisabfone-MM-Dbus-Test (0.1.b) # (0.1.b) added CDMA support (untested) # Copywrite (C) cisabfone@gmail.com # BUGS and ALL : ah.cisab@gmail.com ########################################################################
INTERFACE_PROPERTIES="org.freedesktop.DBus.Properties" MM_DBUS_SERVICE="org.freedesktop.ModemManager" DBUS_PATH="/org/freedesktop/ModemManager" DBUS_INTERFACE="org.freedesktop.ModemManager" MODEM="org.freedesktop.ModemManager.Modem" MODEM_CDMA="org.freedesktop.ModemManager.Modem.Cdma" GSM_CARD="org.freedesktop.ModemManager.Modem.Gsm.Card" GSM_NETWORK="org.freedesktop.ModemManager.Modem.Gsm.Network" MODEM_SIMPLE="org.freedesktop.ModemManager.Modem.Simple" MM="org.freedesktop.ModemManager" GET_INFO="org.freedesktop.ModemManager.Modem.GetInfo" GSM_NETWORK_SCAN="org.freedesktop.ModemManager.Modem.Gsm.Network.Scan" GSM_NETWORK_REG_INFO="org.freedesktop.ModemManager.Modem.Gsm.Network.GetRegistrationInfo" MODEM_ENABLE="org.freedesktop.ModemManager.Modem.Enable" Get_Esn="org.freedesktop.ModemManager.Modem.Cdma.GetEsn" Get_Serving_System="org.freedesktop.ModemManager.Modem.Cdma.GetServingSystem" Get_Registration_State="org.freedesktop.ModemManager.Modem.Cdma.GetRegistrationState" ###############################################################
################################################################ MM_ENUM=$(dbus-send --print-reply --system --dest=$MM \ $DBUS_PATH $MM.EnumerateDevices |grep org | cut -c19-) ################################################################# #***************** MAIN_LOOP_START + SET COUNTER 0 ************* ################################################################# COUNT=0 for DEV in $MM_ENUM do COUNT=`expr $COUNT + 1` DEV=$(echo $DEV |sed s:\"::g) MM_DEVICE_INFO=$(dbus-send --print-reply --system --dest=$MM $DEV $GET_INFO |grep string | cut -c14-) MM_DEVICE_INFO=$(echo $MM_DEVICE_INFO |sed s:\"::g) MM_DEVICE_TYPE=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Type" |tail -n1|cut -c25-) MM_DEVICE_DRIVER=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Driver" |tail -n1|cut -c25-) MM_DEVICE_DRIVER=$(echo $MM_DEVICE_DRIVER |sed s:\"::g) MM_DEVICE_MASTER=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"MasterDevice" |tail -n1|cut -c25-) MM_DEVICE_MASTER=$(echo $MM_DEVICE_MASTER |sed s:\"::g) MM_DATA_DEVICE=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Device" |tail -n1|cut -c25-) MM_DATA_DEVICE=$(echo $MM_DATA_DEVICE |sed s:\"::g) MM_DATA_DEVICE_PIN_STAT=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"UnlockRequired" |tail -n1|cut -c25-) MM_DATA_DEVICE_IP=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"IpMethod" |tail -n1|cut -c25-) MM_DATA_DEVICE_ENABLED=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Enabled" |tail -n1|cut -c25-) ######################################################################### # Need to see if device Enabled from MM_DATA_DEVICE_ENABLED true or false # if false enable the device : else network info not available : WARNING :If connect by Network-Manager this will drop the connection ######################################################################### echo echo " ----*-----* MM-TEST DEVICE_ENABLE_DISABLE *-----*----" echo if [ $MM_DATA_DEVICE_ENABLED = "true" ] then echo " DEVICE ENABLED : true" echo " ACTION :disconnect" MM_DEVICE_ENABLE=$(dbus-send --print-reply --system --dest=$MM $DEV $MODEM_ENABLE boolean:false) MM_DATA_DEVICE_ENABLED=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Enabled" |tail -n1|cut -c25-) fi ################################################ if [ $MM_DATA_DEVICE_ENABLED = "false" ] then echo " DEVICE ENABLED : false" echo " ACTION :enable" true = "(True)" MM_DEVICE_ENABLE=$(dbus-send --print-reply --system --dest=$MM $DEV $MODEM_ENABLE boolean:true) MM_DATA_DEVICE_ENABLED=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Enabled" |tail -n1|cut -c25-) echo " DEVICE ENABLE :$MM_DATA_DEVICE_ENABLED" fi if [ $MM_DATA_DEVICE_ENABLED = "false" ] then echo " SETTING ENABLE_DEVICE : fail" fi ################################################### echo echo " ----*-----* MM-TEST DEVICE_INFO *-----*----" echo echo " DEVICE :$COUNT" echo " MASTER_DEVICE :$MM_DEVICE_MASTER" echo " ENUMERATED DEVICE TYPE :$MM_DEVICE_TYPE" echo " DEVICE INFO :$MM_DEVICE_INFO" ################################################## if [ $MM_DEVICE_TYPE -eq 1 ] then MODEM_TYPE="GSM" echo " MODEM TYPE :$MODEM_TYPE" dial="*99#" echo " DIAL COMMAND :$dial" MM_GSM_NETWORK_INFO=$(dbus-send --print-reply --system --dest=$MM $DEV $GSM_NETWORK_REG_INFO |grep string | cut -c14-) MM_GSM_NETWORK_INFO=$(echo $MM_GSM_NETWORK_INFO |sed s:\"::g) data=0 for INFO in $MM_GSM_NETWORK_INFO do if [ $data -eq 0 ] then MCC=$(echo $INFO | cut -c -3) MNC=$(echo $INFO | cut -c 4-) echo " NETWORK MCC :$MCC" echo " NETWORK MNC :$MNC" fi ########################################## if [ $data -eq 1 ] then echo " PROVIDER :$INFO" fi ########################################## if [ $data -eq 2 ] then echo " REGISTERED :$INFO" fi ########################################## data=`expr $data + 1` done echo fi ################################################## if [ $MM_DEVICE_TYPE -eq 2 ] then MODEM_TYPE="CDMA" ######### UNTESTED ############################################## echo " MODEM TYPE :$MODEM_TYPE" dial="#777" echo " DIAL COMMAND :$dial" MM_DEVICE_ESN=$(dbus-send --print-reply --system --dest=$MM $DEV $Get_Esn) echo $MM_DEVICE_ESN MM_DEVICE_SERVING_SYSTEM=$(dbus-send --print-reply --system --dest=$MM $DEV $Get_Serving_System |grep uint | cut -c14-) # for INFO in $MM_DEVICE_SERVING_SYSTEM do echo $INFO done
echo fi ################################################## echo " ----*-----* MM-TEST MM-INFO *-----*-----" echo echo " DRIVER :$MM_DEVICE_DRIVER" echo " DEVICE :/dev/$MM_DATA_DEVICE" echo " PIN STATUS :$MM_DATA_DEVICE_PIN_STAT" ################################################## if [ $MM_DATA_DEVICE_IP -eq 0 ] then echo " ENUMERATED IP METHOD :Use PPP to get the address." fi ################################################## if [ $MM_DATA_DEVICE_IP -eq 1 ] then echo " ENUMERATED IP METHOD :Static configuration, the modem will provide IP information." fi ################################################## if [ $MM_DATA_DEVICE_IP -eq 2 ] then echo " ENUMERATED IP METHOD :Use DHCP." fi ############## END OF UNTESTED ######################### show true then set false echo " DEVICE ENABLED :$MM_DATA_DEVICE_ENABLED" echo " ACTION :disconnect" MM_DEVICE_ENABLE=$(dbus-send --print-reply --system --dest=$MM $DEV $MODEM_ENABLE boolean:false) MM_DATA_DEVICE_ENABLED=$(dbus-send --print-reply --system --dest=$MM $DEV $INTERFACE_PROPERTIES.Get string:$MODEM string:"Enabled" |tail -n1|cut -c25-) echo " DEVICE ENABLE :$MM_DATA_DEVICE_ENABLED" ################################################### echo echo done #***************** MAIN_LOOP_END ************************
for a reboot I would try
SYSTEM "reboot &"
|
|
|
Post by konaexpress on Jul 25, 2012 21:06:51 GMT 1
Thanks for the reply.
The reason I am using the Dbus lines is because I already went through that part trying to figure out what will and will not work from a program.
The part I was wondering about is how to start the line. Like do I start with SHELL or does BaCon use another command to do shell commands?
Thanks for the help, I will look at the code you posted.
-John
|
|
|
Post by alexfish on Jul 25, 2012 21:16:53 GMT 1
not sure if your using the bacongui , it has a help,
in the help the two man commands are
ADDED: the same is vice versa
if you saved that script in the user bin , then you could call it from the EXEC$ command the replies will be faster and easier to parse in the bacon script.
regards
alexfish
|
|
|
Post by bigbass on Jul 26, 2012 5:32:11 GMT 1
Hey konaexpress The image you posted was enough to capture what you wanted I didn't include the actual commands you can do that for your box They would go in the SUBS Here is the basic idea change it to whatever works for you *I used your original names so it would be easier to read Joe ' open-box.bac
' Include the files for making a GUI. INCLUDE "/usr/share/BaCon/hug_imports.bac" INIT
' we want to "echo out" which button was pressed ' this could launch another app
'---------- SUB demo1() '---------- PRINT "Shutdown" END SUB
'---------- SUB demo2() '---------- PRINT "Reboot" END SUB
'---------- SUB demo3() '---------- PRINT "Logout" END SUB
' 1.) Create your main working window, 380 pixels wide x 320 pixels high Mainwin = WINDOW( "Openbox Logout" , 300, 320 )
The_image = IMAGE( "/usr/share/pixmaps/logo.gif", 268, 62 ) ATTACH( Mainwin, The_image,15, 0 )
OB_label = MARK( "Logout of Openbox ?", 200, 30 ) FONT(OB_label, "sans 14") ATTACH( Mainwin, OB_label, 60, 60 )
SD_btn = BUTTON("Shutdown",200,30) ATTACH(Mainwin,SD_btn,50,99) CALLBACK(SD_btn,demo1)
RB_btn = BUTTON("Reboot",200,30) ATTACH(Mainwin,RB_btn,50,148) CALLBACK(RB_btn,demo2)
LO_btn = BUTTON("Logout",200,30) ATTACH(Mainwin,LO_btn,50,197) CALLBACK(LO_btn,demo3)
Cancel_btn = BUTTON("Cancel",200,30) ATTACH(Mainwin,Cancel_btn,50,246) CALLBACK(Cancel_btn,QUIT)
DISPLAY
|
|
|
Post by konaexpress on Jul 26, 2012 15:39:55 GMT 1
Holly cr@p your code looks so much better than mine does. Thank you so much for this example. Now I have something to use as a jump-off point so that I can finish this and others will be able to adapt the code to whatever they want to use it for.
But I have to ask so that I can understand what/why you made it like this.
1. Why do I put things in "SUB"? What is this for?
2. I see that you use the "PRINT" command, why? I guess this is not for printing to my printer.
3. What is the "DISPLAY" for at the end of the code?
4. I see that you put comments in the middle of the code. When I did this, it showed up as pink instead of blue like at the top. Is this normal?
Once again, thank you so much for this. I might have to go put up a copy of wordpress somewhere so that this code can be studied and used by others. When starting out new, any commented code is priceless to look at and study. I only got as far as I did because I followed your examples from the documentation thread.
-John
|
|
|
Post by bigbass on Jul 26, 2012 16:08:43 GMT 1
Hey John If you open a terminal and run ./open-box Click on the buttons you can "see" with the PRINT command if the button press will point you to the SUBS its a way you can debug quickly is only a clue to see if you coded correctly you should have the expected output in the terminal From inside the suboutine is were you decide to do some action or command lets say you want to call the system to log out from here you use a system command to do that in this way everything is organized into small sections of code "little blocks" that do one thing making the code easy to read and debug The shutdown and reboot are a bit aggressive for testing a demo but if you paste the logout command that you will use I will add it to the demo so you can get a feel of what to do to add the rest of the code needed The subs in this example is where the button press will point to the flow is this 1. window 2. widget 3. attach 4. callback -----> points to the sub or function that will run what you want to do the DISPLAY is required to see what the UI will look like it placed at the end of the code have fun Joe EDIT fixed typo
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Jul 26, 2012 16:20:29 GMT 1
1. Why do I put things in "SUB"? What is this for? Because the way to define actions for widgets (buttons etc.) is to place the code for it in SUBs or FUNCTIONs. You have a sub with a unique name and you can define that the code inside is executed if, for example, one of your buttons is clicked. 2. I see that you use the "PRINT" command, why? I guess this is not for printing to my printer. PRINT command display text in the terminal (in this example it's used to confirm that clicking on buttons works). 3. What is the "DISPLAY" for at the end of the code? DISPLAY displays your GUI interface. EDIT: Bigbas replied while I was composing my post. Hope my answer will be useful anyway.
|
|
|
Post by konaexpress on Jul 26, 2012 17:52:53 GMT 1
@tomaaz, Yes, this does help and thank you. bigbass, In the program/ide I was using, I would use the "Shell comand like this. Logout of Openbox: Shell "logout" I am thinking that "Shell" is the same as "EXEC$" in BaCon? This is easy for the logout part but to shutdown or to reboot gets more complicated as you have to be ROOT to do this. So this requires a long string(?) to do the same thing. Here is the reboot code. As ROOT: Shell "reboot" Not ROOT: SHELL "dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart" Who knew that my first project would be such a pain in the butt for such a small little app? It has taken me two weeks and now going on three to port it to BaCon. Yikes! Thanks for the help guys and I will do my best to understand all this. -John
|
|
|
Post by vovchik on Jul 26, 2012 19:02:07 GMT 1
Dear John, SYSTEM is more like SHELL. EXEC$ has the nice feature of capturing the output of the command in a string, e.g.: x$ = EXEC$("ls -1"). The output of "ls -1" will be contained in the x$ var, so you can then analyse and process it. You WILL get your prog to work. Just persevere and ask questions. This is a very friendly place - most of the time - and people will offer little bits of code and/or advice. Stay with it... With kind regards, vovchik
|
|
|
Post by alexfish on Jul 26, 2012 20:03:17 GMT 1
@tomaaz, Yes, this does help and thank you. bigbass, In the program/ide I was using, I would use the "Shell comand like this. Logout of Openbox: Shell "logout" I am thinking that "Shell" is the same as "EXEC$" in BaCon? This is easy for the logout part but to shutdown or to reboot gets more complicated as you have to be ROOT to do this. So this requires a long string(?) to do the same thing. Here is the reboot code. As ROOT: Shell "reboot" Not ROOT: SHELL "dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart" Who knew that my first project would be such a pain in the butt for such a small little app? It has taken me two weeks and now going on three to port it to BaCon. Yikes! Thanks for the help guys and I will do my best to understand all this. -John Hi John sometimes it be easier to change file or exec permisions sudo su chmod u+s /usr/sbin/<your program> IE sudo su chmod u+s /usr/sbin/reboot But as always with Linux as they , use at own risk SHELL ? can have a look here , What is Bash? - Bash Reference Manual www.gnu.org/software/bash/manual/html_node/What-is-Bash_003f.htmlalso check out the shebang in my previous post also look in the bacon help , ref shebang , this will be a good exercise for you another way of invoking password with ui is to invoke an "init script" to start your program safer than chmod to super user the call is gksu <myprogram> try it from the terminal regards alexfish
|
|
|
Post by konaexpress on Jul 26, 2012 20:41:27 GMT 1
[quote author=konaexpress board=code thread=325 post=2284 time=1343321573 another way of invoking password with ui is to invoke an "init script" to start your program safer than chmod to super user the call is gksu <myprogram> try it from the terminal regards alexfish Thanks for the help, I wanted to try and keep the user from having to add their password to log out but this is good information for another app I am thinking of doing. Reading Bigbass's examples of radio boxes and combo boxes has given me an idea to do something when I get done with this. -John
|
|
|
Post by konaexpress on Jul 26, 2012 20:44:31 GMT 1
Dear John, SYSTEM is more like SHELL. EXEC$ has the nice feature of capturing the output of the command in a string, e.g.: x$ = EXEC$("ls -1"). The output of "ls -1" will be contained in the x$ var, so you can then analyse and process it. You WILL get your prog to work. Just persevere and ask questions. This is a very friendly place - most of the time - and people will offer little bits of code and/or advice. Stay with it... With kind regards, vovchik Thanks, that is what I needed to know! I will go and change this. -John
|
|
|
Post by konaexpress on Jul 26, 2012 23:24:42 GMT 1
I was trying to compile in Geany using Tomaaz new files and I got this in the bottom window.
ERROR: missing file '/usr/share/BaCon/hug_imports.bac' for INCLUDE at line 5 in file 'open-box-logout.bac'!
Did I forget to add something or did I forget to change something?
Thanks -John
EDIT: Strange, the BaCon file is not there. Not sure how this happened because I downloaded the deb file from the BaCon main page and installed it. I didn't change any thing.
Can I make this file and download the hug-imports.bac file? I am running LXDE on CrunchBang, it is a Debian distro.
|
|
|
Post by bigbass on Jul 27, 2012 3:40:56 GMT 1
This would work for any major linux distro you could get compile and install everything you need automatically
|
|