pvbrowser, pvb® - The Process Visualiation Browser.

  • English
  • Deutsch

Anatomy of a pvserver in Lua script language

A pvserver in Lua can be created without installing the Qt SDK and a C++ compiler. All you need is within our installation package. The structure of a Lua pvserver is very similar to the C/C++ version, Our libraries are used via a language binding that is created with swig. Because of this the reference manual for the pvslib and the rllib can also be used for Lua. There is a main program called pvslua which will read file main.lua and execute the lua script. Within main.lua there is a function luaMain(ptr) where the handling of a pvbrowser client starts. In fact pvslua is a C/C++ program with the same structure as a pvserver in C/C++. But within pvMain() pvslua starts processing main.lua.

You start a pvserver in Lua with the following command:

    pvslua main.lua

With a pvserver in Lua it is possible to change the Lua code while the pvserver is running. A newly connected pvbrowser client will immediately see the modified version.


Whenever a pvbrowser client connects pvslua creates a new thread for handling that client and executes file main.lua. The function showMask1() will show and handle mask1.

-- pvserver in lua  : pvslua -port=5050 -cd=/your/directory/with/your/lua/code
trace = 1              -- here you may put variables global for all your masks

dofile("mask1.lua")    -- include your masks here

function luaMain(ptr)  -- pvserver Lua Main Program

  p = pv.getParam(ptr) -- get the PARAM structure
  pv.pvSetCaption(p,string.format("Hello Lua %d",123))
  print("Inital mask = ", p.initial_mask)

  ret = 1
  while 1 do           -- show your masks
    if     (ret==1) then
      ret = showMask1(p)
      ret = 1

  pv.pvThreadFatal(p,"Lua calling ThreadFatal")
  return 0



Definition of mask1 in Lua

showMask1() is generated by pvdevelop. You do not have to write it manually. It will show mask1. On the begin of that function the names of the widgets you have designed are listed. Also the toolTip and whatsThis properties of the widgets are listed and the widgetType is available.

Then you see the calls to pv functions that create the widget tree in the pvbrowser client.

Finally the function goes to the event loop where the events send by the pvbrowser client are interpreted and the according slotFunctions are called.

-- this file is generated by pvdevelop. DO NOT EDIT !!!

function showMask1(p)
  --- begin variables that are private to this mask --------------------------
  iarray = pv.IntegerArray()          -- see pv.getIntegers(text,iarray) below
  farray = pv.FloatArray()            -- see pv.getFloats(text,farray) below
  --- begin construction of our mask -----------------------------------------
  PushButtonBack = 1

  toolTip = {}
  toolTip[0] = ""
  toolTip[1] = ""

  whatsThis = {}
  whatsThis[0] = ""
  whatsThis[1] = ""

  widgetType = {}
  widgetType[0] = pv.TQWidget
  widgetType[1] = pv.TQPushButton


  pv.pvSetText(p,PushButtonBack,"Lua test")

  --- end construction of our mask -------------------------------------------
  --- end variables that are private to this mask ----------------------------
  dofile("mask1_slots.lua")                   -- include our slot functions

  if trace == 1 then print("show mask1") end
  pv.pvClearMessageQueue(p)                   -- clear all pending events
  ret = slotInit(p)                           -- intitialize our variables
  if ret ~= 0 then return ret end             -- return number of next mask to call
  while(1)                                    -- event loop
    event  = pv.pvGetEvent(p)                 -- get the next event
    result = pv.pvParseEventStruct(p,event)   -- parse the event
    id     = result.event
    i      = result.i
    text   = result.text
                                              -- now call the according slot function
    if     id == pv.NULL_EVENT then
        ret = slotNullEvent(p)
    elseif id == pv.BUTTON_EVENT then    
        if trace==1 then print("BUTTON_EVENT id=", i) end
        ret = slotButtonEvent(p,i)
    elseif id == pv.BUTTON_PRESSED_EVENT then
        if trace == 1 then print("BUTTON_PRESSED_EVENT id=",i) end
    elseif id == pv.BUTTON_RELEASED_EVENT then
        if trace == 1 then print("BUTTON_RELEASED_EVENT id=",i) end
    elseif id == pv.TEXT_EVENT then
    --- snip ...

slotFunctions in Lua

The task of the developer of a visualization is to code the slotFunctions. The framework was generated by pvdevelop when you created the mask.

-- mask1_slots.lua       Please edit this file in order to define your logic
                         -- here you may define variables local for your mask
                         -- also see the variables in the generated maskX.lua

function slotInit(p)     -- this function will be called before the event loop
  print("slotInit called")
  return 0

function slotNullEvent(p)
  print("slotNullEvent called")
  return 0

function slotButtonEvent(p,id)
  if (id == PushButtonBack) then 
    pv.pvSetText(p,PushButtonBack,"I have been clicked")
  return 0

function slotButtonPressedEvent(p,id)
  return 0

function slotButtonReleasedEvent(p,id)
  return 0
--- snip ...