Author Topic: [DEV] HK functions you can use  (Read 3669 times)

lethys

  • Moderator
  • Jr. Member
  • *****
  • Posts: 78
[DEV] HK functions you can use
« on: 2010-12-29 13:04:32 »
Since I cleaned up my plugins a bit I created a library that holds commonly used functions and allows the actual functionality I added to be used without having to use my "display" classes, you can do the follow things:

Load the library class:
Code: [Select]
require("hk")
Convert from RGBA table (255, 255, 255, 255) to cairo format (1, 1, 1, 1)
Code: [Select]
mycolor = {255, 255, 255, 255 ]
local red, green, blue, alpha = hk.convert_rgb(mycolor)

Get the current IP:
Code: [Select]
hk.storemyip() -- Will create a file that holds the current IP/Will refresh that file
local myvar = hk.getmyip() -- Will read that file and store the IP in myvar

Execute a program without a cmd.com window popping up:
Code: [Select]
local mycmd="notepad.exe"
local myparam="C:\test\test.txt"
hk.ExecuteMe(mycmd, myparam)

Break a line into multiple lines (stored as table fields) at most maxlength characters long:
Code: [Select]
-- font has to be selected before this
mytable=hk.multiline(text,maxlength)

Display a multiline of arbitrary length on screen with only the actually filled lines having a background:
Code: [Select]
hk.paintmultiline(cr, multiline,width,line) -- width being the width of the plugin and automatically detected; cairo.move_to(cr, 0, line)
Write a text in cairo with one function call instead of lots:
Code: [Select]
hk.writetextincairo(cr, text, line, hk.text_color, xoffset ) -- see hk.text_color for the format; cairo.move_to(cr, xoffset, line)
Display a scrollbar that scrolls to the end, waits a few calls long and scrolls back:
Code: [Select]
local currentposition=0
local waitfor=5 -- how many calls to wait at end of text
local tmpwaitfor=waitfor -- sort of ugly
local scrolllength=40 -- how many characters do you expect to be able to display (depends on the font)
loop
  text="some text, one liners only"
  length=string.len(text)
  currentposition, tmpwaitfor = hk.scrollbar_backandforth(text, waitfor, length, scrolllength, currentposition)
  hk.writetextincairo(cr, text, 1, hk.text_color, currentposition ) -- see hk.text_color for the format
end
Known Issues:
*utf8 characters are counted as two characters as far as length is concerned

Get the song now playing in foobar2k (requires now playing simple foobar2k component):
Code: [Select]
local mysong, mysonglengthincharacters = hk.getfb2ksong()
Get any active speakers in your TS3 channel (requires honkiformer TS3 lua plugin):
Code: [Select]
local issomeonespeaking, whoisspeaking = hk.getts3active()
Miranda (requires msg-export for miranda):
Get the most recent miranda message:
Code: [Select]
message, nick, hour, minute, message = hk.getmirrecent()
Get the currently "active" message (see displayfor, mynick, mirnickspacer and mirmsgspacer in hk.lua):
Code: [Select]
message, hour, minute, second, messagelength, msgchanged = hk.getmiractive() --msgchanged is 0 when there is a new message, message is 0 if there is no "active" message at all
« Last Edit: 2010-12-31 03:12:33 by lethys »

SirReal

  • Administrator
  • Hero Member
  • *****
  • Posts: 735
    • linkdata.se
Re: [DEV] HK functions you can use
« Reply #1 on: 2010-12-29 15:28:34 »
I'm impressed by the amount of functionality you're managing to squeeze out of Lua, given the not-even-half-finished state of the LH_Lua plugin.

lethys

  • Moderator
  • Jr. Member
  • *****
  • Posts: 78
Re: [DEV] HK functions you can use
« Reply #2 on: 2010-12-29 22:07:32 »
I had some experience with lua while writing a small script for Runes of Magic (mmorpg) so I knew the basic concepts, but I had no idea it would be possible to load arbitrary libraries and call their C functions directly.
Most of the functionality is still based on reading from files that hold the information provided by plugins for the respective applications. Hopefully I can obsolete some of these after understand how to use the C functions within the applications directly and set up a callback  ::)

lethys

  • Moderator
  • Jr. Member
  • *****
  • Posts: 78
Re: [DEV] HK functions you can use
« Reply #3 on: 2011-01-03 07:01:12 »
Deleted. In fact the changes in 21 will be quite big since many seperate functions have now been reimplemented as classes.

Let's start with data gathering classes, so far there is only one: LogAccess.
LogAccess will be able to return you the last 50 messages in your logfile in a nice table and if you want to only those are, say, not yet 30 seconds old.
You can create a new object of LogAccess like this:
Code: [Select]
mylog=hk.LogAccess:New(filename) -- filename could be: C:\\mylogfile.txtAfter this you will want to initalize your log once, this will read the whole content of the log as it currently is into memory and convert the entries into a table. Based on the format the log file itself has you have two choices:
Code: [Select]
mylog:MIR_Initialize()Will initialize your object for a log file that has a format like this:
[nick][some spaces][HH:MM:SS][space][text]
The resulting table will have this format:
Code: [Select]
table[i][1]=hours,table[i][2]=minutes,table[i][3]=seconds,table[i][4]=nick,table[i][5]=messageWhere i is the number of the logentry with 1 being the most recent one
Beware that MIR will filter out any messages with a nick in hk.ignorelist and may return less than 50 messages!

Code: [Select]
mylog:TS3EVENT_Initialize()Will initialize your object for a log file that has a simpler format like this:
[HH:MM:SS:][text]
The resulting table will have this format:
Code: [Select]
table[i][1]=hours,table[i][2]=minutes,table[i][3]=seconds,table[i][4]=messageWhere i is the number of the logentry with 1 being the most recent one

After some time your logfile will change, and you will want to update your object to reflect the newest state, this can be done quite often without too much cpu usage. I prefer to use the M.obj_polling function of LCDHost like this:
Code: [Select]
function M.obj_polling( self )
  mylog:MIR_Update() -- or TS3EVENT_Update()
  collectgarbage("collect") -- Will keep RAM usage much much lower
  return 300
end
Finally you will want to read from your log, there are two different functions for that:
Code: [Select]
logentries=mylog:Read() -- Will return the 50 most recent logentries

deltatime={1,30} -- 1 minute, 30 seconds
activelogentries=mylog:ReadActive(deltatime) -- Will return only messages that have a timestamp less than 1:30 ago


There is also the class MultiLine and two classes for pagers, feel free to ask me about those if it isn't clear to you from the source code.
« Last Edit: 2011-01-08 04:57:50 by lethys »