karamba_python.cpp

00001 /****************************************************************************
00002 *  karamba_python.cpp  -  Functions for calling python scripts
00003 *
00004 *  Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se>
00005 *  Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org>
00006 *  Copyright (c) 2004 Petri Damst� <damu@iki.fi>
00007 *  Copyright (c) 2004 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
00008 *
00009 *  This file is part of SuperKaramba.
00010 *
00011 *  SuperKaramba is free software; you can redistribute it and/or modify
00012 *  it under the terms of the GNU General Public License as published by
00013 *  the Free Software Foundation; either version 2 of the License, or
00014 *  (at your option) any later version.
00015 *
00016 *  SuperKaramba is distributed in the hope that it will be useful,
00017 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 *  GNU General Public License for more details.
00020 *
00021 *  You should have received a copy of the GNU General Public License
00022 *  along with SuperKaramba; if not, write to the Free Software
00023 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00024 ****************************************************************************/
00025 
00026 #ifdef _XOPEN_SOURCE
00027 #undef _XOPEN_SOURCE
00028 #endif
00029 
00030 #include <Python.h>
00031 #include "karambaapp.h"
00032 #include "themefile.h"
00033 
00034 #include "karamba_python.h"
00035 #include "meter_python.h"
00036 #include "bar_python.h"
00037 #include "graph_python.h"
00038 #include "textlabel_python.h"
00039 #include "richtextlabel_python.h"
00040 #include "imagelabel_python.h"
00041 #include "widget_python.h"
00042 #include "menu_python.h"
00043 #include "config_python.h"
00044 #include "task_python.h"
00045 #include "systray_python.h"
00046 #include "svcgrp_python.h"
00047 #include "misc_python.h"
00048 #include "input_python.h"
00049 
00050 /*******************************************
00051  * Python methods are defined here.
00052  *   Each method accessible from python should have:
00053  *     - A wrapper function that returns a PyObject or appropriate python type
00054  *     - A C++ implementation of the python method, named the same as the python call
00055  *     - An entry in the python methods array so the call is accessible from python
00056  *
00057  *   Example:
00058  *     py_move_systay - wrapper function
00059  *     moveSystray - actual implementation of method
00060  *     {"moveSystray", py_move_systray, METH_VARARGS, "Move the Systray"} - array entry
00061  */
00062 
00063 static PyMethodDef karamba_methods[] = {
00064     // Bar - bar_python.cpp
00065     {(char*)"createBar", py_createBar, METH_VARARGS, (char*)"Create new Bar."},
00066     {(char*)"deleteBar", py_deleteBar, METH_VARARGS, (char*)"Delete Bar."},
00067     {(char*)"getThemeBar", py_getThemeBar, METH_VARARGS, (char*)"Get Bar from .theme using it's name."},
00068     {(char*)"getBarSize", py_getBarSize, METH_VARARGS, (char*)"Get Bar size."},
00069     {(char*)"resizeBar", py_resizeBar, METH_VARARGS, (char*)"Resize Bar."},
00070     {(char*)"getBarPos", py_getBarPos, METH_VARARGS, (char*)"Get Bar position."},
00071     {(char*)"moveBar", py_moveBar, METH_VARARGS, (char*)"Move Bar."},
00072     {(char*)"hideBar", py_hideBar, METH_VARARGS, (char*)"Hide Bar."},
00073     {(char*)"showBar", py_showBar, METH_VARARGS, (char*)"Show Bar."},
00074     {(char*)"getBarSensor", py_getBarSensor, METH_VARARGS, (char*)"Get Bar sensor."},
00075     {(char*)"setBarSensor", py_setBarSensor, METH_VARARGS, (char*)"Set Bar sensor."},
00076     {(char*)"setBarImage", py_setBarImage, METH_VARARGS, (char*)"Set bar image"},
00077     {(char*)"getBarImage", py_getBarImage, METH_VARARGS, (char*)"Get bar image"},
00078     {(char*)"setBarVertical", py_setBarVertical, METH_VARARGS, (char*)"Set bar orientation"},
00079     {(char*)"getBarVertical", py_getBarVertical, METH_VARARGS, (char*)"Get bar orientation"},
00080     {(char*)"setBarValue", py_setBarValue, METH_VARARGS, (char*)"Set bar value"},
00081     {(char*)"getBarValue", py_getBarValue, METH_VARARGS, (char*)"Get bar value"},
00082     {(char*)"setBarMinMax", py_setBarMinMax, METH_VARARGS, (char*)"Set bar min & max"},
00083     {(char*)"getBarMinMax", py_getBarMinMax, METH_VARARGS, (char*)"Get bar min & max"},
00084     {(char*)"getIncomingData", py_get_incoming_data, METH_VARARGS, (char*)"Get incoming data passed from another theme"},
00085     {(char*)"setIncomingData", py_set_incoming_data, METH_VARARGS, (char*)"Set incoming data passed in another theme"},
00086 
00087     // Graph - graph_python.cpp
00088     {(char*)"createGraph", py_createGraph, METH_VARARGS, (char*)"Create new Graph."},
00089     {(char*)"deleteGraph", py_deleteGraph, METH_VARARGS, (char*)"Delete Graph."},
00090     {(char*)"getThemeGraph", py_getThemeGraph, METH_VARARGS, (char*)"Get Graph from .theme using it's name."},
00091     {(char*)"getGraphSize", py_getGraphSize, METH_VARARGS, (char*)"Get Graph size."},
00092     {(char*)"resizeGraph", py_resizeGraph, METH_VARARGS, (char*)"Resize Graph."},
00093     {(char*)"getGraphPos", py_getGraphPos, METH_VARARGS, (char*)"Get Graph position."},
00094     {(char*)"moveGraph", py_moveGraph, METH_VARARGS, (char*)"Move Graph."},
00095     {(char*)"hideGraph", py_hideGraph, METH_VARARGS, (char*)"Hide Graph."},
00096     {(char*)"showGraph", py_showGraph, METH_VARARGS, (char*)"Show Graph."},
00097     {(char*)"getGraphSensor", py_getGraphSensor, METH_VARARGS, (char*)"Get Graph sensor."},
00098     {(char*)"setGraphSensor", py_setGraphSensor, METH_VARARGS, (char*)"Set Graph sensor."},
00099     {(char*)"setGraphValue", py_setGraphValue, METH_VARARGS, (char*)"Set graph value"},
00100     {(char*)"getGraphValue", py_getGraphValue, METH_VARARGS, (char*)"Get graph value"},
00101     {(char*)"setGraphMinMax", py_setGraphMinMax, METH_VARARGS, (char*)"Set graph min & max"},
00102     {(char*)"getGraphMinMax", py_getGraphMinMax, METH_VARARGS, (char*)"Get graph min & max"},
00103     {(char*)"setGraphColor", py_setGraphColor, METH_VARARGS, (char*)"Change a Graph Sensor's Color"},
00104     {(char*)"getGraphColor", py_getGraphColor, METH_VARARGS, (char*)"Get a Graph Sensor's Color"},
00105 
00106     // TextLabel - textlabel_python.cpp
00107     {(char*)"createText", py_createText, METH_VARARGS, (char*)"Create new Text."},
00108     {(char*)"deleteText", py_deleteText, METH_VARARGS, (char*)"Delete Text."},
00109     {(char*)"getThemeText", py_getThemeText, METH_VARARGS, (char*)"Get Text from .theme using it's name."},
00110     {(char*)"getTextSize", py_getTextSize, METH_VARARGS, (char*)"Get Text size."},
00111     {(char*)"resizeText", py_resizeText, METH_VARARGS, (char*)"Resize Text."},
00112     {(char*)"getTextPos", py_getTextPos, METH_VARARGS, (char*)"Get Text position."},
00113     {(char*)"moveText", py_moveText, METH_VARARGS, (char*)"Move Text."},
00114     {(char*)"hideText", py_hideText, METH_VARARGS, (char*)"Hide Text."},
00115     {(char*)"showText", py_showText, METH_VARARGS, (char*)"Show Text."},
00116     {(char*)"getTextSensor", py_getTextSensor, METH_VARARGS, (char*)"Get Text sensor."},
00117     {(char*)"setTextSensor", py_setTextSensor, METH_VARARGS, (char*)"Set Text sensor."},
00118     {(char*)"changeText", py_setTextValue, METH_VARARGS, (char*)"Change a Text Sensor's Text"},
00119     {(char*)"getTextValue", py_getTextValue, METH_VARARGS, (char*)"Get Text value"},
00120     {(char*)"changeTextShadow", py_setTextShadow, METH_VARARGS, (char*)"Change a Text Shadow size"},
00121     {(char*)"getTextShadow", py_getTextShadow, METH_VARARGS, (char*)"Get a Text Shadow size"},
00122     {(char*)"changeTextFont", py_setTextFont, METH_VARARGS, (char*)"Change a Text Sensor's Font"},
00123     {(char*)"getTextFont", py_getTextFont, METH_VARARGS, (char*)"Get a Text Sensor's Font"},
00124     {(char*)"changeTextColor", py_setTextColor, METH_VARARGS, (char*)"Change a Text Sensor's Color"},
00125     {(char*)"getTextColor", py_getTextColor, METH_VARARGS, (char*)"Get a Text Sensor's Color"},
00126     {(char*)"changeTextSize", py_setTextFontSize, METH_VARARGS, (char*)"Change a Text Sensor's Font Size"},
00127     {(char*)"getTextFontSize", py_getTextFontSize, METH_VARARGS, (char*)"Get a Text Sensor's Font Size"},
00128     {(char*)"getTextAlign", py_getTextAlign, METH_VARARGS, (char*)"Get Text alignment."},
00129     {(char*)"setTextAlign", py_setTextAlign, METH_VARARGS, (char*)"Set Text alignment."},
00130     {(char*)"setTextScroll", py_setTextScroll, METH_VARARGS, (char*)"Set Text scroll."},
00131 
00132     // RichTextLabel - richtextlabel_python.cpp
00133     {(char*)"createRichText", py_createRichText, METH_VARARGS, (char*)"Create a Rich Text Sensor"},
00134     {(char*)"deleteRichText", py_deleteRichText, METH_VARARGS, (char*)"Deletes a Rich Text Sensor"},
00135     {(char*)"getThemeRichText", py_getThemeRichText, METH_VARARGS, (char*)"Get Rich Text from .theme using it's name."},
00136     {(char*)"getRichTextSize", py_getRichTextSize, METH_VARARGS, (char*)"Get the (width, height) of a Rich Text Sensor"},
00137     {(char*)"resizeRichText", py_resizeRichText, METH_VARARGS, (char*)"Resize Rich Text."},
00138     {(char*)"setRichTextWidth", py_set_rich_text_width, METH_VARARGS, (char*)"Sets the width of a Rich Text Sensor"},
00139     {(char*)"getRichTextPos", py_getRichTextPos, METH_VARARGS, (char*)"Get Rich Text position."},
00140     {(char*)"moveRichText", py_moveRichText, METH_VARARGS, (char*)"Moves a Rich Text Sensor"},
00141     {(char*)"hideRichText", py_hideRichText, METH_VARARGS, (char*)"hides a Rich Text Sensor"},
00142     {(char*)"showRichText", py_showRichText, METH_VARARGS, (char*)"shows a Rich Text Sensor"},
00143     {(char*)"getRichTextSensor", py_getRichTextSensor, METH_VARARGS, (char*)"Get Rich Text sensor."},
00144     {(char*)"setRichTextSensor", py_setRichTextSensor, METH_VARARGS, (char*)"Set Rich Text sensor."},
00145     {(char*)"changeRichText", py_setRichTextValue, METH_VARARGS, (char*)"Change the content of a Rich Text Sensor"},
00146     {(char*)"getRichTextValue", py_getRichTextValue, METH_VARARGS, (char*)"Get Rich Text value"},
00147     {(char*)"changeRichTextFont", py_setRichTextFont, METH_VARARGS, (char*)"Change a Rich Text Sensor's Font"},
00148     {(char*)"getRichTextFont", py_getRichTextFont, METH_VARARGS, (char*)"Get a Rich Text Sensor's Font"},
00149     {(char*)"changeRichTextSize", py_setRichTextFontSize, METH_VARARGS, (char*)"Change a Rich Text Sensor's Font Size"},
00150     {(char*)"getRichTextFontSize", py_getRichTextFontSize, METH_VARARGS, (char*)"Get a Rich Text Sensor's Font Size"},
00151 
00152     // ImageLabel - imagelabel_python.cpp
00153     {(char*)"createImage", py_createImage, METH_VARARGS, (char*)"Create an Image"},
00154     {(char*)"createTaskIcon", py_createTaskIcon, METH_VARARGS, (char*)"Create an Image of the Icon for a Task"},
00155     {(char*)"createBackgroundImage", py_createBackgroundImage, METH_VARARGS, (char*)"Create an Image (only redraw it when background changes)"},
00156     {(char*)"deleteImage", py_deleteImage, METH_VARARGS, (char*)"Delete an Image"},
00157     {(char*)"getThemeImage", py_getThemeImage, METH_VARARGS, (char*)"Get image meter from .theme using it's name"},
00158     {(char*)"getImageSize", py_getImageSize, METH_VARARGS, (char*)"Get Image size."},
00159     {(char*)"getImageWidth", py_getImageWidth, METH_VARARGS, (char*)"Get the width of an Image"},
00160     {(char*)"getImageHeight", py_getImageHeight, METH_VARARGS, (char*)"Get the height of an Image"},
00161     {(char*)"getImagePos", py_getImagePos, METH_VARARGS, (char*)"Get Image position."},
00162     {(char*)"moveImage", py_moveImage, METH_VARARGS, (char*)"Move an Image"},
00163     {(char*)"hideImage", py_hideImage, METH_VARARGS, (char*)"Hide an Image"},
00164     {(char*)"showImage", py_showImage, METH_VARARGS, (char*)"Show an Image"},
00165     {(char*)"getImagePath", py_getImageValue, METH_VARARGS, (char*)"Get Image path."},
00166     {(char*)"setImagePath", py_setImageValue, METH_VARARGS, (char*)"Set Image path."},
00167     {(char*)"getImageSensor", py_getImageSensor, METH_VARARGS, (char*)"Get Image sensor."},
00168     {(char*)"setImageSensor", py_setImageSensor, METH_VARARGS, (char*)"Set Image sensor."},
00169     {(char*)"addImageTooltip", py_addImageTooltip, METH_VARARGS, (char*)"Create a Tooltip for an Image"},
00170     {(char*)"resizeImage", py_resizeImage, METH_VARARGS, (char*)"Scale an Image"},
00171     {(char*)"resizeImageSmooth", py_resizeImageSmooth, METH_VARARGS, (char*)"Scale an Image (slower, better looking)"},
00172     {(char*)"rotateImage", py_rotateImage, METH_VARARGS, (char*)"Rotate an Image"},
00173     {(char*)"removeImageTransformations", py_removeImageTransformations, METH_VARARGS, (char*)"Restore original size and orientation of an Image"},
00174     {(char*)"removeImageEffects", py_removeImageEffects, METH_VARARGS, (char*)"Remove Effects of an Image"},
00175     {(char*)"changeImageIntensity", py_changeImageIntensity, METH_VARARGS, (char*)"Change Intensity of an Image"},
00176     {(char*)"changeImageChannelIntensity", py_changeImageChannelIntensity, METH_VARARGS, (char*)"Change Intensity of an Image Channel"},
00177     {(char*)"changeImageToGray", py_changeImageToGray, METH_VARARGS, (char*)"Converts an Image to Grayscale"},
00178 
00179     // Menu - menu_python.cpp
00180     {(char*)"createMenu", py_create_menu, METH_VARARGS, (char*)"Create a popup menu"},
00181     {(char*)"deleteMenu", py_delete_menu, METH_VARARGS, (char*)"Delete a popup menu"},
00182     {(char*)"addMenuItem", py_add_menu_item, METH_VARARGS, (char*)"Add a popup menu entry"},
00183     {(char*)"addMenuSeparator", py_add_menu_separator, METH_VARARGS, (char*)"Add a popup menu seperator item"},
00184     {(char*)"removeMenuItem", py_remove_menu_item, METH_VARARGS, (char*)"Remove a popup menu entry"},
00185     {(char*)"popupMenu", py_popup_menu, METH_VARARGS, (char*)"Popup a menu at a specified location"},
00186 
00187     // Config - config_python.cpp
00188     {(char*)"addMenuConfigOption", py_add_menu_config_option, METH_VARARGS, (char*)"Add a configuration entry to the menu"},
00189     {(char*)"setMenuConfigOption", py_set_menu_config_option, METH_VARARGS, (char*)"Set a configuration entry in the menu"},
00190     {(char*)"readMenuConfigOption", py_read_menu_config_option, METH_VARARGS, (char*)"Read a configuration entry in the menu"},
00191     {(char*)"readConfigEntry", py_read_config_entry, METH_VARARGS, (char*)"Read a configuration entry"},
00192     {(char*)"writeConfigEntry", py_write_config_entry, METH_VARARGS, (char*)"Writes a configuration entry"},
00193 
00194     // Widget - widget_python.cpp
00195     {(char*)"moveWidget", py_move_widget, METH_VARARGS, (char*)"Move Widget to x,y"},
00196     {(char*)"resizeWidget", py_resize_widget, METH_VARARGS, (char*)"Resize Widget to width,height"},
00197     {(char*)"createWidgetMask", py_create_widget_mask, METH_VARARGS, (char*)"Create a clipping mask for this widget"},
00198     {(char*)"redrawWidget", py_redraw_widget, METH_VARARGS, (char*)"Update Widget to reflect your changes"},
00199     {(char*)"redrawWidgetBackground", py_redraw_widget_background, METH_VARARGS, (char*)"Update Widget to reflect background image changes"},
00200     {(char*)"getWidgetPosition", py_get_widget_position, METH_VARARGS, (char*)"Get Widget Position"},
00201     {(char*)"toggleWidgetRedraw", py_toggle_widget_redraw, METH_VARARGS, (char*)"Toggle Widget redrawing"},
00202 
00203     // Task - task_python.cpp
00204     {(char*)"getStartupList", py_get_startup_list, METH_VARARGS, (char*)"Get the system startup list"},
00205     {(char*)"getStartupInfo", py_get_startup_info, METH_VARARGS, (char*)"Get all the info for a startup"},
00206     {(char*)"getTaskList", py_get_task_list, METH_VARARGS, (char*)"Get the system task list"},
00207     {(char*)"getTaskNames", py_get_task_names, METH_VARARGS, (char*)"Get the system task list in name form"},
00208     {(char*)"getTaskInfo", py_get_task_info, METH_VARARGS, (char*)"Get all the info for a task"},
00209     {(char*)"performTaskAction", py_perform_task_action, METH_VARARGS, (char*)"Do something with a task, such as minimize it"},
00210 
00211     // System Tray - systray_python.cpp
00212     {(char*)"createSystray", py_create_systray, METH_VARARGS, (char*)"Create a Systray"},
00213     {(char*)"hideSystray", py_hide_systray, METH_VARARGS, (char*)"Hide the Systray"},
00214     {(char*)"showSystray", py_show_systray, METH_VARARGS, (char*)"Show the Systray"},
00215     {(char*)"moveSystray", py_move_systray, METH_VARARGS, (char*)"Move the Systray"},
00216     {(char*)"getCurrentWindowCount", py_get_current_window_count, METH_VARARGS, (char*)"Get current Window count"},
00217     {(char*)"updateSystrayLayout", py_update_systray_layout, METH_VARARGS, (char*)"Update Systray layout"},
00218 
00219     // Misc - misc_python.cpp
00220     {(char*)"getThemePath", py_get_theme_path, METH_VARARGS,  (char*)"Get the file path of the theme"},
00221     {(char*)"readThemeFile", py_read_theme_file, METH_VARARGS,
00222         (char*)"Read file from theme."},
00223     {(char*)"language", py_language, METH_VARARGS,
00224         (char*)"Return default language of a translation file."},
00225     {(char*)"userLanguage", py_userLanguage, METH_VARARGS,
00226         (char*)"Return user language."},
00227     {(char*)"userLanguages", py_userLanguages, METH_VARARGS,
00228         (char*)"Return preferred user languages."},
00229     {(char*)"openTheme", py_open_theme, METH_VARARGS,
00230         (char*)"Open a new theme"},
00231     {(char*)"reloadTheme", py_reload_theme, METH_VARARGS,
00232             (char*)"Reload current theme"},
00233     {(char*)"acceptDrops", py_accept_drops, METH_VARARGS,
00234             (char*)"Allows widget to receive Drop (I.E. Drag and Drop) events"},
00235     {(char*)"toggleShowDesktop", py_toggle_show_desktop, METH_VARARGS,
00236             (char*)"Show/Hide the desktop"},
00237     {(char*)"execute", py_execute_command, METH_VARARGS, (char*)"Execute a command"},
00238     {(char*)"executeInteractive", py_execute_command_interactive, METH_VARARGS, (char*)"Execute a command and get it's output (stdout)"},
00239     {(char*)"attachClickArea", (PyCFunction)py_attach_clickArea, METH_VARARGS|METH_KEYWORDS, (char*)"Add a clickArea to the given text or image"},
00240     {(char*)"createClickArea", py_create_click_area, METH_VARARGS, (char*)"Create a Click Area Sensor"},
00241     {(char*)"getNumberOfDesktops", py_get_number_of_desktops, METH_VARARGS, (char*)"Get current number of virtual desktops"},
00242     {(char*)"getIp", py_get_ip, METH_VARARGS, (char*)"Get current host's IP address"},
00243     {(char*)"translateAll", py_translate_all, METH_VARARGS, (char*)"Translate all widgets in a theme"},
00244     {(char*)"show", py_show, METH_VARARGS, (char*)"Show theme"},
00245     {(char*)"hide", py_hide, METH_VARARGS, (char*)"Hide theme"},
00246 
00247     // Input Box - input_python.cpp
00248     {(char*)"createInputBox", py_createInputBox, METH_VARARGS,
00249         (char*)"Create new Input Box."},
00250     {(char*)"deleteInputBox", py_deleteInputBox, METH_VARARGS,
00251         (char*)"Delete Input Box."},
00252     {(char*)"getThemeInputBox", py_getThemeInputBox, METH_VARARGS,
00253         (char*)"Get Input Box from .theme using it's name."},
00254     {(char*)"getInputBoxValue", py_getInputBoxValue, METH_VARARGS,
00255         (char*)"Get Input Box value"},
00256     {(char*)"changeInputBox", py_setInputBoxValue, METH_VARARGS,
00257         (char*)"Change a Input Box Text"},
00258     {(char*)"hideInputBox", py_hideInputBox, METH_VARARGS,
00259         (char*)"Hide Input Box."},
00260     {(char*)"showInputBox", py_showInputBox, METH_VARARGS,
00261         (char*)"Show Input Box."},
00262     {(char*)"getInputBoxPos", py_getInputBoxPos, METH_VARARGS,
00263         (char*)"Get InputBox position."},
00264     {(char*)"moveInputBox", py_moveInputBox, METH_VARARGS,
00265         (char*)"Moves a Input Box"},
00266     {(char*)"getInputBoxSize", py_getInputBoxSize, METH_VARARGS,
00267         (char*)"Get the (width, height) of a Input Box"},
00268     {(char*)"resizeInputBox", py_resizeInputBox, METH_VARARGS,
00269         (char*)"Resize Input Box."},
00270     {(char*)"changeInputBoxFont", py_setInputBoxFont, METH_VARARGS,
00271         (char*)"Change a Input Box Font"},
00272     {(char*)"getInputBoxFont", py_getInputBoxFont, METH_VARARGS,
00273         (char*)"Get a Input Box Font"},
00274     {(char*)"changeInputBoxFontColor", py_setInputBoxFontColor, METH_VARARGS,
00275         (char*)"Change a Input Box Font Color"},
00276     {(char*)"getInputBoxFontColor", py_getInputBoxFontColor, METH_VARARGS,
00277         (char*)"Get a Input Box Font Color"},
00278     {(char*)"changeInputBoxSelectionColor", py_setInputBoxSelectionColor,
00279         METH_VARARGS, (char*)"Change a Input Box Selection Color"},
00280     {(char*)"getInputBoxSelectionColor", py_getInputBoxSelectionColor,
00281         METH_VARARGS, (char*)"Get a Input Box Selection Color"},
00282     {(char*)"changeInputBoxBackgroundColor", py_setInputBoxBGColor,
00283         METH_VARARGS, (char*)"Change a Input Box Background Color"},
00284     {(char*)"getInputBoxBackgroundColor", py_getInputBoxBGColor, METH_VARARGS,
00285         (char*)"Get a Input Box Background Color"},
00286     {(char*)"changeInputBoxFrameColor", py_setInputBoxFrameColor, METH_VARARGS,
00287         (char*)"Change a Input Box Frame Color"},
00288     {(char*)"getInputBoxFrameColor", py_getInputBoxFrameColor, METH_VARARGS,
00289         (char*)"Get a Input Box Frame Color"},
00290     {(char*)"changeInputBoxSelectedTextColor", py_setInputBoxSelectedTextColor,
00291         METH_VARARGS, (char*)"Change a Input Box Selected Text Color"},
00292     {(char*)"getInputBoxSelectedTextColor", py_getInputBoxSelectedTextColor,
00293         METH_VARARGS, (char*)"Get a Input Box Selected Text Color"},
00294     {(char*)"changeInputBoxFontSize", py_setInputBoxFontSize, METH_VARARGS,
00295         (char*)"Change a Input Box Font Size"},
00296     {(char*)"getInputBoxFontSize", py_getInputBoxFontSize, METH_VARARGS,
00297         (char*)"Get a Input Box Font Size"},
00298     {(char*)"setInputFocus", py_setInputFocus, METH_VARARGS,
00299         (char*)"Set the Input Focus to the Input Box"},
00300     {(char*)"clearInputFocus", py_clearInputFocus, METH_VARARGS,
00301         (char*)"Clear the Input Focus of the Input Box"},
00302     {(char*)"getInputFocus", py_getInputFocus, METH_VARARGS,
00303         (char*)"Get the Input Box currently focused"},
00304 
00305     {(char*)"setWidgetOnTop", py_set_widget_on_top, METH_VARARGS,
00306       (char*)"changes 'on top' status"},
00307     {(char*)"getSystraySize", py_get_systray_size, METH_VARARGS, 
00308       (char*)"Get the size of the Systray"},
00309     {(char*)"getPrettyThemeName", py_get_pretty_name, METH_VARARGS,  
00310       (char*)"Get the pretty name of the theme"},
00311     {(char*)"openNamedTheme", py_open_named_theme, METH_VARARGS, 
00312       (char*)"Open a new theme giving it a new name"},
00313     {(char*)"callTheme", py_call_theme, METH_VARARGS, 
00314       (char*)"Pass a string to another theme"},
00315     {(char*)"changeInterval", py_change_interval, METH_VARARGS, 
00316       (char*)"Change the refresh interval"},
00317     {(char*)"run", py_run_command, METH_VARARGS, 
00318       (char*)"Execute a command with KRun"},
00319     {(char*)"createServiceClickArea", py_create_service_click_area, METH_VARARGS, 
00320       (char*)"Create a Service-named Click Area Sensor"},
00321     {(char*)"removeClickArea", py_remove_click_area, METH_VARARGS, 
00322       (char*)"Remove a Click Area Sensor"},
00323     {(char*)"setUpdateTime", py_set_update_time, METH_VARARGS, 
00324       (char*)"Set last updated time"},
00325     {(char*)"getUpdateTime", py_get_update_time, METH_VARARGS, 
00326       (char*)"Get last updated time"},
00327     {(char*)"setWantRightButton", py_want_right_button, METH_VARARGS, 
00328       (char*)"Set to 1 to deactivate management popups"},
00329     {(char*)"setWantMeterWheelEvent", py_want_wheel_event, METH_VARARGS, 
00330       (char*)"Enables wheel events over meters."},
00331     {(char*)"managementPopup", py_management_popup, METH_VARARGS, 
00332       (char*)"Activates the Management Popup menu"},
00333 
00334   // service groups
00335     {(char*)"getServiceGroups", py_get_service_groups, METH_VARARGS, 
00336       (char*)"Get KDE Service Groups"},
00337 
00338     {NULL, NULL, 0 ,NULL}
00339 };
00340 
00341 PyThreadState* KarambaPython::mainThreadState = 0;
00342 
00343 KarambaPython::KarambaPython(const ThemeFile& theme, bool reloading):
00344   pythonThemeExtensionLoaded(false), pName(0), pModule(0), pDict(0)
00345 {
00346   PyThreadState* myThreadState;
00347   char pypath[1024];
00348 
00349   getLock(&myThreadState);
00350 
00351   // load the .py file for this .theme
00352   PyRun_SimpleString((char*)"import sys");
00353   //Add theme path to python path so that we can find the python file
00354   snprintf(pypath, 1023, "sys.path.insert(0, '%s')", theme.path().ascii());
00355   PyRun_SimpleString(pypath);
00356   PyRun_SimpleString((char*)"sys.path.insert(0, '')");
00357 
00358   PyImport_AddModule((char*)"karamba");
00359   Py_InitModule((char*)"karamba", karamba_methods);
00360 
00361   pName = PyString_FromString(theme.pythonModule().ascii());
00362   pModule = PyImport_Import(pName);
00363   
00364   fprintf(stderr, "%s\n", pypath);
00365   
00366   //Make sure the module is up to date.
00367   if (reloading)
00368     PyImport_ReloadModule(pModule);
00369 
00370   if (pModule != NULL)
00371   {
00372     pDict = PyModule_GetDict(pModule);
00373     if (pDict != NULL)
00374     {
00375       pythonThemeExtensionLoaded = true;
00376     }
00377   }
00378   else
00379   {
00380     PyErr_Print();
00381     fprintf(stderr,
00382             "------------------------------------------------------\n");
00383     fprintf(stderr, "What does ImportError mean?\n");
00384     fprintf(stderr, "\n");
00385     fprintf(stderr,
00386             "It means that I couldn't load a python add-on %s.py\n",
00387             theme.pythonModule().ascii());
00388     fprintf(stderr, "If this is a regular theme and doesn't use python\n");
00389     fprintf(stderr, "extensions, then nothing is wrong.\n");
00390     fprintf(stderr,
00391             "------------------------------------------------------\n");
00392   }
00393   releaseLock(myThreadState);
00394 }
00395 
00396 KarambaPython::~KarambaPython()
00397 {
00398   //Clean up Python references
00399   if (pythonThemeExtensionLoaded) {
00400     PyThreadState* myThreadState;
00401     getLock(&myThreadState);
00402 
00403     //Displose of current python module so we can reload in constructor.
00404     Py_DECREF(pModule);
00405     Py_DECREF(pName);
00406 
00407     releaseLock(myThreadState);
00408   }
00409 }
00410 
00411 void KarambaPython::initPython()
00412 {
00413   // initialize Python
00414   Py_Initialize();
00415 
00416   // initialize thread support
00417   PyEval_InitThreads();
00418 
00419   // save a pointer to the main PyThreadState object
00420   mainThreadState = PyThreadState_Get();
00421 
00422   // release the lock
00423   PyEval_ReleaseLock();
00424 }
00425 
00426 void KarambaPython::shutdownPython()
00427 {
00428   // shut down the interpreter
00429   PyInterpreterState * mainInterpreterState = mainThreadState->interp;
00430   // create a thread state object for this thread
00431   PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
00432   PyThreadState_Swap(myThreadState);
00433   PyEval_AcquireLock();
00434   Py_Finalize();
00435 }
00436 
00437 void KarambaPython::getLock(PyThreadState** myThreadState)
00438 {
00439   // get the global lock
00440   PyEval_AcquireLock();
00441 
00442   // create a thread state object for this thread
00443   *myThreadState = PyThreadState_New(mainThreadState->interp);
00444   PyThreadState_Swap(*myThreadState);
00445 }
00446 
00447 void KarambaPython::releaseLock(PyThreadState* myThreadState)
00448 {
00449   // swap my thread state out of the interpreter
00450   PyThreadState_Swap(NULL);
00451   // clear out any cruft from thread state object
00452   PyThreadState_Clear(myThreadState);
00453   // delete my thread state object
00454   PyThreadState_Delete(myThreadState);
00455   // release the lock
00456   PyEval_ReleaseLock();
00457 }
00458 
00459 PyObject* KarambaPython::getFunc(const char* function)
00460 {
00461   PyObject* pFunc = PyDict_GetItemString(pDict, (char*)function);
00462   if (pFunc && PyCallable_Check(pFunc))
00463     return pFunc;
00464   return NULL;
00465 }
00466 
00467 bool KarambaPython::callObject(const char* func, PyObject* pArgs, bool lock)
00468 {
00469   bool result = false;
00470   PyThreadState* myThreadState;
00471 
00472   //qDebug("Calling %s", func);
00473 
00474   if (lock)
00475     getLock(&myThreadState);
00476   PyObject* pFunc = getFunc(func);
00477 
00478   if (pFunc != NULL)
00479   {
00480     PyObject* pValue = PyObject_CallObject(pFunc, pArgs);
00481 
00482     if (pValue != NULL)
00483     {
00484       Py_DECREF(pValue);
00485       result = true;
00486     }
00487     else
00488     {
00489       qWarning("Call to %s failed", func);
00490       PyErr_Print();
00491     }
00492   }
00493   Py_DECREF(pArgs);
00494   if (lock)
00495     releaseLock(myThreadState);
00496   return result;
00497 }
00498 
00499 bool KarambaPython::initWidget(karamba* k)
00500 {
00501   PyObject* pArgs = Py_BuildValue((char*)"(l)", k);
00502   return callObject("initWidget", pArgs);
00503 }
00504 
00505 bool KarambaPython::widgetUpdated(karamba* k)
00506 {
00507   PyObject* pArgs = Py_BuildValue((char*)"(l)", k);
00508   return callObject("widgetUpdated", pArgs);
00509 }
00510 
00511 bool KarambaPython::widgetClosed(karamba* k)
00512 {
00513   PyObject* pArgs = Py_BuildValue((char*)"(l)", k);
00514   return callObject("widgetClosed", pArgs);
00515 }
00516 
00517 bool KarambaPython::menuOptionChanged(karamba* k, QString key, bool value)
00518 {
00519   PyObject* pArgs = Py_BuildValue((char*)"(lsi)", k, key.ascii(), (int)value);
00520   return callObject("menuOptionChanged", pArgs);
00521 }
00522 
00523 bool KarambaPython::menuItemClicked(karamba* k, KPopupMenu* menu, long id)
00524 {
00525   PyObject* pArgs = Py_BuildValue((char*)"(lll)", k, menu, id);
00526   return callObject("menuItemClicked", pArgs);
00527 }
00528 
00529 bool KarambaPython::meterClicked(karamba* k, Meter* meter, int button)
00530 {
00531   PyObject* pArgs = Py_BuildValue((char*)"(lli)", k, meter, button);
00532   return callObject("meterClicked", pArgs);
00533 }
00534 
00535 bool KarambaPython::meterClicked(karamba* k, QString anchor, int button)
00536 {
00537   PyObject* pArgs = Py_BuildValue((char*)"(lsi)", k, anchor.ascii(), button);
00538   return callObject("meterClicked", pArgs);
00539 }
00540 
00541 bool KarambaPython::widgetClicked(karamba* k, int x, int y, int button)
00542 {
00543   PyObject* pArgs = Py_BuildValue((char*)"(liii)", k, x, y, button);
00544   return callObject("widgetClicked", pArgs);
00545 }
00546 
00547 bool KarambaPython::keyPressed(karamba* k, const Meter* meter,
00548                                const QString& text)
00549 {
00550   PyObject* pArgs = Py_BuildValue((char*)"(lls)", k, meter, text.ucs2());
00551   return callObject("keyPressed", pArgs);
00552 }
00553 
00554 bool KarambaPython::widgetMouseMoved(karamba* k, int x, int y, int button)
00555 {
00556   PyObject* pArgs = Py_BuildValue((char*)"(liii)", k, x, y, button);
00557   return callObject("widgetMouseMoved", pArgs);
00558 }
00559 
00560 bool KarambaPython::activeTaskChanged(karamba* k, Task* t)
00561 {
00562   PyObject* pArgs = Py_BuildValue((char*)"(ll)", k, t);
00563   return callObject("activeTaskChanged", pArgs);
00564 }
00565 
00566 bool KarambaPython::taskAdded(karamba* k, Task* t)
00567 {
00568   PyObject* pArgs = Py_BuildValue((char*)"(ll)", k, t);
00569   return callObject("taskAdded", pArgs);
00570 }
00571 
00572 bool KarambaPython::taskRemoved(karamba* k, Task* t)
00573 {
00574   PyObject* pArgs = Py_BuildValue((char*)"(ll)", k, t);
00575   return callObject("taskRemoved", pArgs);
00576 }
00577 
00578 bool KarambaPython::startupAdded(karamba* k, Startup* t)
00579 {
00580   PyObject* pArgs = Py_BuildValue((char*)"(ll)", k, t);
00581   return callObject("startupAdded", pArgs);
00582 }
00583 
00584 bool KarambaPython::startupRemoved(karamba* k, Startup* t)
00585 {
00586   PyObject* pArgs = Py_BuildValue((char*)"(ll)", k, t);
00587   return callObject("startupRemoved", pArgs);
00588 }
00589 
00590 bool KarambaPython::commandOutput(karamba* k, int pid, char *buffer)
00591 {
00592   PyObject* pArgs = Py_BuildValue((char*)"(lis)", k, pid, buffer);
00593   return callObject("commandOutput", pArgs);
00594 }
00595 
00596 bool KarambaPython::commandFinished(karamba* k, int pid)
00597 {
00598   PyObject* pArgs = Py_BuildValue((char*)"(li)", k, pid);
00599   return callObject("commandFinished", pArgs);
00600 }
00601 
00602 bool KarambaPython::itemDropped(karamba* k, QString text, int x, int y)
00603 {
00604   PyObject* pArgs = Py_BuildValue((char*)"(lOii)", k, QString2PyString(text), x, y);
00605   return callObject("itemDropped", pArgs);
00606 }
00607 
00608 bool KarambaPython::themeNotify(karamba* k, const char *from, const char *str)
00609 {
00610   // WARNING WARNING WARNING i had to switch off thread locking to get
00611   // this to work.  callNotify is called from INSIDE another locked thread,
00612   // so can never complete because themeNotify will expect locking to be
00613   // released...
00614   //
00615   PyObject* pArgs = Py_BuildValue((char*)"(lss)", k, from, str);
00616   return callObject("themeNotify", pArgs, false);
00617 }
00618 
00619 bool KarambaPython::systrayUpdated(karamba* k)
00620 {
00621   PyObject* pArgs = Py_BuildValue((char*)"(l)", k);
00622   return callObject("systrayUpdated", pArgs);
00623 }
00624 
00625 bool KarambaPython::desktopChanged(karamba* k, int desktop)
00626 {
00627   PyObject* pArgs = Py_BuildValue((char*)"(li)", k, desktop);
00628   return callObject("desktopChanged", pArgs);
00629 }
00630 
00631 bool KarambaPython::wallpaperChanged(karamba* k, int desktop)
00632 {
00633   PyObject* pArgs = Py_BuildValue((char*)"(li)", k, desktop);
00634   return callObject("wallpaperChanged", pArgs);
00635 }
KDE Home | KDE Accessibility Home | Description of Access Keys