Translate

Wednesday, September 11, 2013

Adjusting Gedit's embedded terminal plugin

Hello again!

This is another very quick tip! At this time, let's focus on Gedit's embedded terminal.

Well, when you install Gedit and load its terminal plugin you get really frustrated with its default colors as they suck a lot! They *really suck!*

Let's see how we can make this useful plugin look *a lot better* :-)

First things first!
Install the dconf-tools package and (if you didn't it already) the gedit-plugins.

apt-get install dconf-tools gedit-plugins

Now, open dconf-editor and go to:

org->gnome->gedit->plugins->terminal

And make sure all the settings look like this:


 If you did everything right, your embedded terminal should look like this one (except for the bash color theme ;-) )


That's it!

By the way, this is Debian (of course it's Debian!) Wheezy with XFCE.

Have fun! =)

Wednesday, August 28, 2013

Wifi connection via interfaces file on Debian

Hi there!
This is a very short guide on how to setup your WiFi connection using the /etc/network/interfaces file on Debian. This is a good trick as you'll have WiFi network access even before your graphical interface comes up! ;-)

I don't use any login managers nor network managers. This is enough for me. I hope it helps! :-)

Once you have wireless interface driver loaded and working, check what's your interface with the command "iwconfig".

# This is the wireless interface
auto wlan0
iface wlan0 inet dhcp 
    # This is your network SSID
    wpa-ssid linksys
    # This is your AP password
    wpa-psk 12345678

Now all you have to do is protect that file as it has your network's secret key ;-)
 
chmod 0600 /etc/network/interfaces
ls /etc/network/interfaces
-rw------- 1 root root 324 Jul 15 19:14 /etc/network/interfaces 
 
I told you it would be very short! Have fun! ;-)

Wednesday, June 26, 2013

Installing/Upgrading E17 on Debian 7.0 (Stable) - Updated 07/18/2013

Hi everyone.

I just installed E17 on my Wheezy box at work, and it works very well.
Box specs we are interested in are:
* Debian version: Wheezy (amd64)
* Custom kernel: Linux optimus 3.2.0-4-amd64 #1 SMP Debian 3.2.41-2+deb7u2 x86_64 GNU/Linux
* CPU: AMD Phenom(tm) II X2 B55 Processor
* RAM: 8 GB
* Video: ATI RV730 PRO [Radeon HD 4650]

All you have to do is to read the documentation on E17's official page. It also includes commands for installing E17 on Ubuntu 12.04. Well, to make things easier, I just adapted the procedures and made a very simple script, following the instructions.

This is the upgraded and improved version of the script.
I used it to install E17 on my home computer and to upgrade E17 on my laptop computer.
Box specs we are interested in are:
* Debian version: Wheezy (amd64)
* Custom kernel: Linux blackstar 3.8.6-blackstar-x6 #2 SMP PREEMPT x86_64 GNU/Linux
* CPU: AMD Phenom(tm) II X6 1090T Processor
* RAM: 16 GB
* Video: NVIDIA Corporation GF114 [GeForce GTX 560]

I hope it helps.


Warning: as this will modify your box, use it at your own risk!

#!/bin/bash
#
# This script will install E17 on a computer
# 
# You can use it to upgrade E17 whenever a new version comes out.
# (I used it from 0.17.0 to 0.17.3 without problems)
#
# For every successful build/install it creates a file_OK 
# and a file_ERROR is created for every failure.
#
#
#
# All info and credits: http://www.enlightenment.org/p.php?p=about/e17&l=en
#
# Gabriel Marques - snortt@gmail.com
#

# VERSION - Change whenever you want to upgrade E17 and its EFL
LIBS_VERSION="1.7.7"
E17_VERSION="0.17.3"
CONNMAN_VER="1.16"

# Where to get it 
E17_PKG_HOST="http://download.enlightenment.org/releases"
CONNMAN_SRC_URL="http://www.kernel.org/pub/linux/network/connman"

# Which format to get
LIBS_FILE_EXT="tar.gz"
E17_SRC_FILE_EXT="tar.gz"
CONNMAN_SRC_FILE_EXT="tar.gz"

# What to get 
E17_LIBS="eina eet evas embryo ecore eio edje efreet e_dbus evas_generic_loaders ethumb eeze emotion elementary"
E17_SRC_FILE="enlightenment-${E17_VERSION}.${E17_SRC_FILE_EXT}"
CONNMAN_SRC_FILE="connman-${CONNMAN_VER}.${CONNMAN_SRC_FILE_EXT}"

# ----------------------------------------
# You don't need to change anything ahead
# ----------------------------------------------------------------------------
SYS_DEPS="gcc doxygen libc6-dev make gdb wget libpam0g-dev libfreetype6-dev libpng-dev 
libjpeg-dev zlib1g-dev libdbus-1-dev libtiff-dev librsvg2-dev libgif-dev 
libcurl4-openssl-dev libasound2-dev libudev-dev libspectre-dev libpoppler-dev 
libraw-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfribidi-dev 
libexif-dev liblua5.1-0-dev libx11-dev libxcursor-dev libxrender-dev libxrandr-dev 
libxfixes-dev libxdamage-dev libxcomposite-dev libxss-dev libxp-dev libxext-dev 
libxinerama-dev libxkbfile-dev libxtst-dev libxcb-shape0-dev libxcb-keysyms1-dev 
mesa-common-dev iptables-dev zlib1g-dev libfreetype6-dev fontconfig libfribidi-dev 
libpng12-dev  libpng++-dev libpnglite-dev libgif-dev libgiftiio-dev librsvg2-dev  
libx11-dev libxext-dev libxrender-dev libxcomposite-dev libxdamage-dev 
libxfixes-dev libxrandr-dev libxinerama-dev libxss-dev libxp-dev 
libxpm-dev libxcb-dpms0-dev libxext-dev libxcb-xtest0-dev 
libxdo-dev libxcursor-dev x11proto-print-dev libxp-dev libxkbfile-dev libxcb1-dev 
libxcb-composite0-dev libxcb-glx0-dev libxcb-render0-dev libxcb-screensaver0-dev 
libxcb-sync0-dev libxcb-xinerama0-dev libxcb-xprint0-dev libxcb-util0-dev 
libxcb-image0-dev libxcb-render-util0-dev libxcb-keysyms1-dev libxcb-shape0-dev 
libudev-dev  libdbus-1-dev libasound2-dev libpoppler-dev libraw-dev libspectre-dev 
libgstreamer0.10-dev libxine2-dev libvlc-dev libvlccore-dev  liblua5.2-dev 
libwebp-dev libbulletml-dev libmount-dev libssl-dev libdbus-c++-dev 
libdbus-glib-1-dev libdbusmenu-glib-dev libpulse-dev libblkid libblkid1-dev"

OPENGL_DEPS="libgl1-mesa-dev"

SLEEP_TIME="2"

if [ "$(id -u)" -ne "0" ]; then
    echo -e "You need to be root"
    exit 201
fi

echo -e "Updating system packages lists..."
apt-get update && apt-get dist-upgrade

echo -e "Installing system packages..."
apt-get install ${SYS_DEPS}

echo -e "Installing OpenGL packages..."
apt-get install ${OPENGL_DEPS}

echo -e " "
echo -e "Getting EFL"
for lib in ${E17_LIBS}
do
  if [ -f ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT} ]; then
    echo -e "File already exists: ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}"
  else
    echo -e "Getting file: ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}"
    touch ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}_incomplete_download 
    wget ${E17_PKG_HOST}/${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT} && \
      rm ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}_incomplete_download 2> /dev/null 
  fi
done

echo -e " "
echo -e "Getting E17..."
if [ -f ${E17_SRC_FILE} ]; then
  echo -e "File already exists: ${E17_SRC_FILE}"
else
  echo -e "Getting file: ${E17_SRC_FILE}"
  touch ${E17_SRC_FILE}_incomplete_download 
  wget ${E17_PKG_HOST}/${E17_SRC_FILE} && \ 
    rm ${E17_SRC_FILE}_incomplete_download 2> /dev/null 
fi

# Now it's time to compile everything
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export CFLAGS="-O3 -fvisibility=hidden -ffast-math"

echo -e " "
echo -e "Building EFL..."
for lib in ${E17_LIBS}
do
  if [ "$lib" == "evas_generic_loaders" ]; then
    configure_opts="--disable-gnutls --disable-poppler"
  else
    configure_opts="--disable-gnutls"
  fi 

  # See if its already built/installed
  if [ -f ${lib}-${LIBS_VERSION}_OK ]; then
    echo -e "*** ${lib}-${LIBS_VERSION} is already built/installed ***"
    echo -e "\tRemove ${lib}-${LIBS_VERSION}_OK before rebuilding/reinstalling"
    echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
    echo -e " "
  else
    tar zxf ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}
    cd ${lib}-${LIBS_VERSION}
    ./configure ${configure_opts} && make && sudo make install && cd .. && \
      rm -rf ${lib}-${LIBS_VERSION} && touch ${lib}-${LIBS_VERSION}_OK || \
      touch ${lib}-${LIBS_VERSION}_ERROR

    # See if it worked or stop!
    if [ -f ${lib}-${LIBS_VERSION}_ERROR} ]; then
      exit
    fi 
  fi 
done

echo -e " "
echo -e "Configuring libraries dependencies..."
ldconfig

echo -e " "
echo -e "Building E17..."
if [ -f ${E17_SRC_FILE%.*.*}_OK ]; then
  echo -e "*** ${E17_SRC_FILE%.*.*} is already built/installed ***"
  echo -e "\tRemove ${E17_SRC_FILE%.*.*}_OK before rebuilding/reinstalling"
  echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
  echo -e " "
else
  tar zxf ${E17_SRC_FILE}
  cd ${E17_SRC_FILE%.*.*}
  ./configure && make && sudo make install && cd .. && \
    rm -rf ${E17_SRC_FILE%.*.*} && touch ${E17_SRC_FILE%.*.*}_OK || \
    touch ${E17_SRC_FILE%.*.*}_ERROR

  # See if it worked or stop!
  if [ -f ${E17_SRC_FILE%.*.*}_ERROR ]; then
    exit
  fi 

  echo -e " "  
  echo -e "Inserting E17 on xsessions list..."
  if [ -f /usr/share/xsessions/enlightenment.desktop ]; then
    echo -e "Already there"
  else
    echo -e "Enlightenment is now on xsessions list"
    ln -s /usr/local/share/xsessions/enlightenment.desktop /usr/share/xsessions/enlightenment.desktop
  fi
fi 

echo -e " "
echo -e "Getting ConnMan..."
if [ -f ${CONNMAN_SRC_FILE} ]; then
  echo -e "File ${CONNMAN_SRC_FILE} already exists"
else
  echo -e "Getting file ${CONNMAN_SRC_FILE}" 
  touch ${CONNMAN_SRC_FILE}_incomplete_download 
  wget ${CONNMAN_SRC_URL}/${CONNMAN_SRC_FILE} && \
    rm ${CONNMAN_SRC_FILE}_incomplete_download 2> /dev/null 
fi 

echo -e " "
echo -e "Building ConnMan..."
if [ -f ${CONNMAN_SRC_FILE%.*.*}_OK ]; then
  echo -e "*** ${CONNMAN_SRC_FILE%.*.*} is already built/installed ***"
  echo -e "\tRemove ${CONNMAN_SRC_FILE%.*.*}_OK before rebuilding/reinstalling"
  echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
  echo -e " "
else
  tar zxf ${CONNMAN_SRC_FILE}
  cd ${CONNMAN_SRC_FILE%.*.*}
  ./configure && make && sudo make install && cd .. && \
    rm -rf ${CONNMAN_SRC_FILE%.*.*} && touch ${CONNMAN_SRC_FILE%.*.*}_OK || \
    touch ${CONNMAN_SRC_FILE%.*.*}_ERROR

  # See if it worked or stop!
  if [ -f ${CONNMAN_SRC_FILE%.*.*}_ERROR ]; then
    exit
  fi 

  echo -e "Trying connman..."
  service network-manager stop
  /usr/local/sbin/connmand &

  echo -e "Adding ConnMan to system startup..."
  if grep '/usr/local/sbin/connmand' /etc/rc.local > /dev/null 2>&1; then
    echo -e "Already there"
  else
    echo -e "Adding ConnMan to /etc/rc.local"
    sed 's:exit 0:(sleep 9;/usr/local/sbin/connmand) \&\nexit 0:g' /etc/rc.local > /tmp/temp.$$
    cp /tmp/temp.$$ /etc/rc.local && rm /tmp/temp.$$
  fi
  
  echo -e "Disabling Network Manager"
  update-rc.d network-manager disable
fi 

echo -e "Have Fun! ;-)"

This is what you should get at the end of the process.



Tuesday, June 18, 2013

Thursday, June 13, 2013

Variadic Functions

Hi there again!
This time I'm gonna show you a very simple program to demonstrate how you can use variadic functions.
They are functions that accept a variable number of arguments and are very useful for many mathematical and logical operations programming.

 1 /* 
 2  *    Even being very basic, this program shows how to use variadic functions
 3  *
 4  *    Gabriel Marques 
 5  *    snortt@gmail.com
 6  *
 7  */
 8 
 9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 /* This one is needed by variadic functions */
14 #include <stdarg.h>
15 
16 /* 
17  * Prototypes
18  * 
19  * As you can see, variadic functions are declared with a coma and the ellipsis (", ...")
20  */
21 void print_message(const char *, ...);
22 int sum_of(int, ...);
23 
24 int main(int argc, char *argv[]) {
25 
26     print_message("This is a test %s %c", "...", '\n');
27     printf("Sum of 1 2 3 = %d\n", sum_of(3,1,2,3));
28     
29     exit (EXIT_SUCCESS);
30 }
31 
32 void print_message(const char *str, ...) {
33     /* This creates the pointer to the args list*/
34     va_list args;
35     
36     /* This initializes the args list. Points to first element AFTER str */
37     va_start(args, str);
38 
39     /* Now we print all he args by making use of the proper function */
40     vprintf(str, args);
41 
42     /* Now we clean up the mess we made inside the program stack */
43     va_end(args);
44 }
45 
46 /* This function receives the count of values to be added and the values themselves */
47 int sum_of(int count, ...) {
48     
49     int i, sum = 0;
50     /* This creates the pointer to the args list*/
51     va_list args;
52 
53     /* This initializes the args list. Points to first element AFTER count */
54     va_start(args, count);
55 
56    for (i = 0; i < count; i++) {
57         /* Steps through the args list and adds each value it finds until it reaches the limit (count) */
58         sum += va_arg(args, int);
59     }
60     
61     /* Now we clean up the mess we made inside the program stack */
62     va_end(args);
63 
64     return sum;
65 }

I hope this quick text could help you!


Bitshifting

.: Bit shifting using C Language :.

This is a small, but yet funny program, that demonstrates how to use bit shifting for simple operations using the C Language.

Try to keep this in mind:

  • Move 1 bit to the left (<): multiply by 2
  • Move 1 bit to the right (>): divide by 2
For more information on Bit shifting and Bitwise operations take a look at:
Bitwise operations 



  1 /*
  2  * Flag bits test.
  3  * This program shows how to implement
  4  * flags operations using the eight bits
  5  * of a char variable.
  6  *
  7  * Gabriel Marques
  8  * snortt@gmail.com
  9  *
 10  */
 11 
 12 /*
 13  * Imagine you need to use flags to control
 14  * the state of something. Think of 8 flags,
 15  * for instance, for controlling 8 basic
 16  * functions of a super advanced robot! ;-)
 17  *
 18  * Things that require binary state, such as
 19  * Unit_Status, Engage_Battle, Force_Field, etc
 20  * require ON or OFF control.
 21  *
 22  * You could use variables to control each state,
 23  * but think of the huge waste you would have
 24  * as all of these states are binary values!
 25  *
 26  * char Unity_Status[] = "On"; <-- This would require
 27  * much more space than a simple 0 or 1 as it would
 28  * allocate memory for 3bytes! ('O', 'n', '\n').
 29  * And you would also need to deal with strings
 30  * comparison to test each status!
 31  *
 32  * Now, think of a single bit for each status.
 33  * As a char variable is 1 byte long, we would
 34  * have 8 bits to store information on a single char! :-)
 35  *
 36  * Using bit shifts we could manipulate each bit inside
 37  * our 1 byte (8 bits) char:
 38  *
 39  * You can see the operation and the bit it affects below:
 40  *
 41  *  1<<0 --> 00000001 << 0    00000001 --> Bit 0
 42  *  1<<1 --> 00000001 << 1    00000010 --> Bit 1
 43  *  1<<2 --> 00000001 << 2    00000100 --> Bit 2
 44  *  1<<3 --> 00000001 << 3    00001000 --> Bit 3
 45  *  1<<4 --> 00000001 << 4    00010000 --> Bit 4
 46  *  1<<5 --> 00000001 << 5    00100000 --> Bit 5
 47  *  1<<6 --> 00000001 << 6    01000000 --> Bit 6
 48  *  1<<7 --> 00000001 << 7    10000000 --> Bit 7
 49  *
 50  *
 51  * Let's take a look at a simple example, regarding our
 52  * super advanced robot and its behavior.
 53  *
 54  */
 55 
 56 #include <stdio.h>
 57 #include <stdlib.h>
 58 
 59 /* Definition of each funcionality flag */
 60 const unsigned char UNITY_STATUS             = (1<<0); /* Bit 0 */
 61 const unsigned char UNITY_ENGAGED_FOR_BATTLE = (1<<1); /* Bit 1 */
 62 const unsigned char UNITY_DEFLECTOR_SHIELD   = (1<<2); /* Bit 2 */
 63 const unsigned char UNITY_LASER_CANNON       = (1<<3); /* Bit 3 */
 64 const unsigned char UNITY_MISSLE_ARMED       = (1<<4); /* Bit 4 */
 65 const unsigned char UNITY_MOVE               = (1<<5); /* Bit 5 */
 66 const unsigned char UNITY_AI_SYSTEM_FAILURE  = (1<<6); /* Bit 6 */
 67 const unsigned char UNITY_GENERAL_FAILURE    = (1<<7); /* Bit 7 */
 68 
 69 /*
 70  * Now that we have the flags defined we can manipulate
 71  * them to test if our robot (UNITY) has some modes ON or OFF
 72  * as you will see later inside the main program.
 73  */
 74 
 75 int main() {
 76 
 77     unsigned char unity_control_flags;   /* This controls our robot ;-) */
 78     unity_control_flags = 0;             /* This disables everything */
 79 
 80     /* Let's turn ON our robot */
 81     unity_control_flags |= UNITY_STATUS;
 82 
 83     /* Let's make our unity start its deflector shield */
 84     unity_control_flags |= UNITY_DEFLECTOR_SHIELD;
 85 
 86     /* During a battle, let's suppose our system is damaged */
 87     unity_control_flags |= UNITY_GENERAL_FAILURE;
 88 
 89     /*
 90      * Now, let's test some status flags of our robot
 91      * We do this by testing if some bits are set to 1
 92      * what will result in a TRUE test.
 93      */
 94 
 95     /* Let's see if it is turned on*/
 96     if ( (unity_control_flags & UNITY_STATUS) != 0 )
 97         printf("Unity is on and waiting for instructions!\n");
 98     else
 99         printf("Unity is off!\n");
100 
101     /* Let's test if the unity has its shield on */
102     if ((unity_control_flags & UNITY_DEFLECTOR_SHIELD) != 0)
103         printf("Unity has its deflector shield on!\n");
104     else
105         printf("Deflector shield is down! Unity is vulnerable!\n");
106 
107     /* And we do the same test for a general system failure */
108     if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
109         printf("Warning! General system failure!\n");
110     else
111         printf("Unity system is healthy!\n");
112 
113     /*
114      * Now let's suppose we need to turn some features off
115      * Let's pretend we've just fixed your system failure problem.
116      * We need to reset the corresponding flag!
117      *
118      * We do this by using the flags variable AND NOT the
119      * desired functionality (~)
120      */
121      unity_control_flags &= ~UNITY_GENERAL_FAILURE;
122 
123      /* Let's check if it worked */
124      /* And we do the same test for a general system failure */
125      if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
126         printf("Warning! General system failure!\n");
127      else
128         printf("Unity system is healthy!\n");
129 
130 
131      return 0;
132 }

Program arguments on the command line

.: Program arguments on the command line :.

Although you can make your own routines to read and process the command line used to invoke your
program, everything is more practical when using the C library functions, as in the case of getopt_long ().

The example code that follows is a simple program that processes its arguments and shows on screen.


 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4 
 5 #define _GNU_SOURCE
 6 #include <getopt.h>
 7 
 8 int main(int argc, char *argv[]) {
 9 
10  int opt;
11  int verbose_flag=0;
12 
13  struct option longopts[] = {
14  {"help", 0, NULL, 'h'},
15  {"file", 1, NULL, 'f'},
16  {"verbose",0,&verbose_flag,1},
17  {0,0,0,0}};
18 
19     while((opt = getopt_long(argc, argv, ":hf:", longopts, NULL)) != -1) {
20         switch(opt) {
21         case 'h':
22             printf("option: %c\n", opt);
23    printf("%s [-h|--help] [--verbose] [-f|--file <file>] [args]\n",argv[0]);
24             break;
25         case 'f':
26             printf("option %c filename: %s\n", opt, optarg);
27             break;
28         case ':':
29             printf("option %c needs a value\n", optopt);
30             break;
31         case '?':
32             printf("unknown option: %c\n", optopt);
33             break;
34         }
35     }
36 
37  if (verbose_flag != 0)
38   printf("Verbose mode ON!\n");
39  
40     for(; optind < argc; optind++) {
41   printf("optind: %d < argc: %d\n", optind, argc);
42         printf("argument: %s (argv[%d])\n", argv[optind], optind);
43   printf("\n");
44  }
45  
46  while (optind < argc) {
47   printf("optind: %d < argc: %d\n", optind, argc);
48         printf("argument: %s (argv[%d])\n", argv[optind], optind);
49   printf("\n");
50   optind++;
51  }
52 
53  printf("Out of the loop.\nflag: %d\noptind: %d e argc: %d\n", verbose_flag, optind, argc);
54 
55     exit(0);
56 }