echo "deb-src http://mirror.yandex.ru/ubuntu maverick main restricted universe multiverse" >> / etc / apt / sources.list
apt-get update && apt-get source lxpanel
cd lxpanel-0.5.6
apt-get build-dep lxpanel
LIBS = "-lX11" . / configure --prefix = / usr --with-plugins = cpufreq
make
ls src / plugins / cpufreq / .libs / cpufreq.so
vim src / plugins / cpufreq / my_plugin.c
- / **
- * My_plugin
- * Habrahabr
- * /
- #include <sys / types.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <glib / gi18n.h>
- #include <string.h>
- #include "panel.h"
- #include "misc.h"
- #include "plugin.h"
- // To compare strings
- #define STREQV (a, b) (* (a) == * (b) && strcmp ((a), (b)) == 0)
- // Declare the structure of our plugin
- typedef struct {
- // Widget that will contain all the icon and menu
- GtkWidget * namew ;
- } my_plugin ;
- // Create an object
- static my_plugin * mp ;
- // Plugin structure is declared in src / plugin.h
- // * mp_p - now plugin class
- // Which at least sets the parent container mp_p-> pwid
- // Our mp-> namew widget is "inserted" into it
- // And the icon, for example, is contained in mp-> namew
- // Long death hide
- static Plugin * mp_p ;
- // An announcement speaks for itself
- static char get_governor ( ) {
- FILE * fpipe ;
- const char governor [ 16 ] ;
- char line [ 16 ] ;
- // Problem with recognizing cpufreq-info output
- // Script / usr / local / bin / cpufreq-gov returns the name of the current mode
- fpipe = ( FILE * ) popen ( "/ usr / local / bin / cpufreq-gov" , "r" ) ;
- fgets ( line , sizeof ( line ) , fpipe ) ;
- pclose ( fpipe ) ;
- return line ;
- }
- // An announcement speaks for itself
- static char get_frequency ( ) {
- FILE * fpipe ;
- const char governor [ 16 ] ;
- char line [ 16 ] ;
- // Problem with recognizing cpufreq-info output
- // Script / usr / local / bin / cpufreq-gov returns the name of the current mode
- fpipe = ( FILE * ) popen ( "cpufreq-info -f -m" , "r" ) ;
- fgets ( line , sizeof ( line ) , fpipe ) ;
- pclose ( fpipe ) ;
- line [ strlen ( line ) - 1 ] = ' \ 0 ' ;
- return line ;
- }
- // Make the function update the icons when changing the mode
- static void refresh_icon ( ) {
- // Find out the current mode
- const char governor [ 16 ] ;
- char line [ 16 ] = get_governor ( ) ;
- sprintf ( governor , "% s" , line ) ;
- // mp_p-> pwid is the parent container of type GTKWidget, which contains mp-> namew
- // Remove the mp-> namew widget and change the icon
- gtk_container_remove ( GTK_CONTAINER ( mp_p -> pwid ) , mp -> namew ) ;
- // If the mode is "ondemand"
- if ( STREQV ( governor , "ondemand" ) )
- {
- // One badge
- mp -> namew = gtk_image_new_from_file ( "/usr/share/lxpanel/images/cpufreq_ond.png" ) ;
- }
- else
- {
- // Otherwise, another
- mp -> namew = gtk_image_new_from_file ( "/usr/share/lxpanel/images/cpufreq_perf.png" ) ;
- }
- // Insert the mp-> namew widget back into the mp_p-> pwid parent container
- gtk_container_add ( GTK_CONTAINER ( mp_p -> pwid ) , mp -> namew ) ;
- gtk_widget_show_all ( mp_p -> pwid ) ;
- }
- // Mode change function
- static void set_governor ( GtkWidget * widget , char * p ) {
- const char exec [ 32 ] ;
- sprintf ( exec , "cpufreq-set -g% s" , p ) ;
- system ( exec ) ;
- refresh_icon ( ) ;
- }
- // Create a menu
- static GtkWidget * mp_menu ( ) {
- GtkMenu * menu = gtk_menu_new ( ) ;
- GSList * group = NULL ;
- GtkWidget * menuitem , * radio1 , * radio2 ;
- FILE * fpipe ;
- const char governor [ 16 ] ;
- const char frequency [ 16 ] ;
- char line [ 16 ] ;
- line = get_governor ( ) ;
- sprintf ( governor , "% s" , line ) ;
- line = get_frequency ( ) ;
- sprintf ( frequency , "% s" , line ) ;
- // 1) Add the current frequency to the menu, make the item inactive
- menuitem = gtk_menu_item_new_with_label ( frequency ) ;
- gtk_menu_append ( menu , menuitem ) ;
- gtk_widget_set_sensitive ( menuitem , FALSE ) ;
- gtk_widget_show ( menuitem ) ;
- // 2) Add a separator
- menuitem = gtk_separator_menu_item_new ( ) ;
- gtk_menu_append ( menu , menuitem ) ;
- gtk_widget_show ( menuitem ) ;
- // 3) 2 radio buttons for switching between modes
- radio1 = gtk_radio_menu_item_new_with_label ( group , "Savings" ) ;
- group = gtk_radio_menu_item_get_group ( GTK_RADIO_MENU_ITEM ( radio1 ) ) ;
- if ( STREQV ( governor , "ondemand" ) )
- {
- gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM ( radio1 ) , TRUE ) ;
- }
- gtk_menu_append ( menu , radio1 ) ;
- gtk_widget_show ( radio1 ) ;
- // 4) The second button
- radio2 = gtk_radio_menu_item_new_with_label ( group , "Performance" ) ;
- if ( STREQV ( governor , "performance" ) )
- {
- gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM ( radio2 ) , TRUE ) ;
- }
- gtk_menu_append ( menu , radio2 ) ;
- gtk_widget_show ( radio2 ) ;
- // When you click on the radio buttons, the modes switch
- g_signal_connect ( G_OBJECT ( radio1 ) , "toggled" , G_CALLBACK ( set_governor ) , "ondemand" ) ;
- g_signal_connect ( G_OBJECT ( radio2 ) , "toggled" , G_CALLBACK ( set_governor ) , "performance" ) ;
- return menu ;
- }
- // Event handler for clicking on the plugin icon
- static gboolean clicked ( GtkWidget * widget , GdkEventButton * evt , Plugin * plugin ) {
- gtk_menu_popup ( mp_menu ( ) , NULL , NULL , NULL , NULL , evt -> button , evt -> time ) ;
- return TRUE ;
- }
- // Object constructor proper
- static int constructor ( Plugin * p ) {
- // Create an object
- mechanism_mp = g_new0 ( my_plugin , 1 ) ;
- p -> priv = mechanism_mp ;
- // Make the parent widget sensitive (we follow the events)
- // And remove the window decoration from it
- p -> pwid = gtk_event_box_new ( ) ;
- GTK_WIDGET_SET_FLAGS ( p -> pwid , GTK_NO_WINDOW ) ;
- gtk_container_set_border_width ( GTK_CONTAINER ( p -> pwid ) , 0 ) ;
- // Set the initial icon
- refresh_icon ( ) ;
- // When clicked, the clicked () handler function is triggered.
- g_signal_connect ( G_OBJECT ( p -> pwid ) , "button_press_event" , G_CALLBACK ( clicked ) , ( gpointer ) p ) ;
- gtk_widget_show ( mechanism_mp -> namew ) ;
- }
- // Destructor
- static void destructor ( Plugin * p ) {
- my_plugin * mechanism_mp = ( my_plugin * ) p -> priv ;
- g_free ( mechanism_mp ) ;
- }
- // And here we specify information about our plugin
- PluginClass mp_plugin_class = {
- PLUGINCLASS_VERSIONING ,
- type : "my_plugin" ,
- name : N_ ( "My super plugin" ) ,
- version : "1.0" ,
- description : N_ ( "Habrahabr" ) ,
- constructor : constructor ,
- destructor : destructor ,
- // In our plugin are not used,
- // but you can assign handlers for the configuration, save the configuration
- // And widget redrawing, when changing configuration
- config : NULL ,
- save : NULL ,
- panel_configuration_changed : NULL
- } ;
LIBS = "-lX11" . / configure --prefix = / usr --with-plugins = cpufreq
make
- #! / bin / sh
- echo ` cpufreq-info -p | awk '{print $ 3}' `
Source: https://habr.com/ru/post/109342/