Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets/external_ip: port to javaScripCore #27

Closed
wants to merge 4 commits into from

Conversation

ricardomv
Copy link
Contributor

this creates a new object "externalIp" with one method "get_address"
witch can be accessed with externalIp.get_address()

@ricardomv
Copy link
Contributor Author

this is the simplest widget we have. @Lokaltog How do you want to do the merge? all the widgets at the same time and you update the theme too or you want to update the theme one by one?

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 3, 2014

I think it'll be more practical to update the theme in one go, after porting all the widgets.

};

/* Callback - JavaScript window object has been cleared */
void add_class_extennal_ip(WebKitWebView *web_view,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: extennal -> external

@ricardomv
Copy link
Contributor Author

fixed typo, what happens when you call an object that it's not defined in js? how are we going to handle not having the widget that exists in the theme?

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 3, 2014

With the webview it will only print an error to the console. We can wrap every API call in a try...catch block and it won't be an issue.

@ricardomv
Copy link
Contributor Author

This patch is wrong(i can't add more widgets) im going to have to call each widget in window_object_cleared_cb

@ricardomv
Copy link
Contributor Author

@Lokaltog can you take a look this patch i updated it, now the volume class is also visible.
i am just having trouble with updating it, i nedd to create a thread or use something other than alsa . i'm not very comfortable with glib, can you fix it? the way i am testing this is setting the bar height to 260 and droping this file into it

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 3, 2014

I'd still prefer if wkline used threads like they do currently, and provide data updates through JS callbacks. Your JS file asks for C to call the onChange callback, but I can't see it in the C code in the patch.

Side note: Is it even possible to have several widgets waiting for system information without using threads?

@ricardomv
Copy link
Contributor Author

the call to onChange should be on the thread i can't create. as it is now none of the widgets locks the program to do anyting even load them. the idea to use jscore is to avoid threads when we dont need them for example the external_ip example and talking to dbus

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 3, 2014

I'm sorry, I'm just a bit confused regarding how this is supposed to work. Have I understood it correctly if:

  • Widgets that don't wait for data (external_ip, weather) will expose an object with a data retrieving method (e.g. Weather.get_temperature(), ExternalIP.get_address()) to Javascript, and the JS theme will call this method on an interval, like this:
setInterval(function () {
   var temp = Weather.get_temperature();
   updateWeatherWidget(temp);
}, 600000); // 10 minutes
  • C widgets that wait for data (volume, mpd) will still have a thread in C(?) that waits for the data, and exposes a method like Volume.onChange to JS that accepts a JS function as a callback. C will then call this callback from the C thread that waits for data and provide updated data to JS, like this:
Volume.onChange(function (volume) {
   updateVolumeWidget(volume);
});
// pseudocode
function store_callback(js_function *callback) {
   // store ptr to js function here, will be called in the C thread
   js_callback volume_change_callback = callback;
}
js_add_method("onChange", store_callback);

// in thread:
function update_volume () {
   while (wait_for_change) {
      // get new volume
      volume_change_callback(volume); // will call the callback function set in .onChange() in the JS file above
   }
}

This is how I picture stuff to work, but I might have completely misunderstood. Let me know if I'm wrong! I'm calling it a night, I'll see if I can get some work done on this issue tomorrow morning.

@ricardomv
Copy link
Contributor Author

the js update can be something more like this

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
  <h1>Extending JavaScript with WebKit. externalIp class.</h1>
  <script type="text/javascript">
    document.write("IP: " + externalIp.get_address() + "<br>");
    var display = function () {
    document.write("IP: " + Volume.get_volume() + "<br>");
    }
    var volume = new Volume();
    volume.onChange = display;
    display();
  </script>
</body>
</html>

and in the thread what the C code does is calling the js function assigned to "onChange",
we dont save the ptr to "onChange" because it can change every time we need to update (if you do in js Volume.onChange = notdisplay).
the thread would be more like

while (1) {
      //wait_for_change
      //call function to update js which will get the volume
   }

corrupted double linked list
@ricardomv
Copy link
Contributor Author

i give up alsa is just not for me

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 4, 2014

Ah, I think I understand what you mean now. The problem is that there's a pretty strict convention in JS to provide the updated data when a callback is called, so requiring the user to manually do Volume.get_volume() in the callback to actually get the volume isn't ideal. But the part of assigning the callback to a variable like Volume.onUpdate = function is totally fine.

Worst case we could rename onUpdate and wrap the functionality in JS like so:

Volume.onDataChanged = function () { // notify event sent to this method
   var volume = Volume.get_volume()
   if (typeof this.onUpdate === 'function')
      this.onUpdate(volume)
}

I'll look into the issue and see if I can come up with a solution.

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 4, 2014

Looking at the example code from the tutorial it appears it should be fairly simple to pass arguments to an onChange callback function. We just need to provide the volume data as a JSValue array here.

@ricardomv
Copy link
Contributor Author

my problem is just with alsa the jscore is working fine (i think), it's just that alsa is not thread safe and even if have it all in the same function (waiting and getting the volume) it crashes anyway. i want to keep the js as simple as possible. please see my code

@Lokaltog
Copy link
Owner

Lokaltog commented Mar 4, 2014

I'll look into it, I'll do some experimenting and see if I can get it working tonight.

@ricardomv
Copy link
Contributor Author

@Lokaltog can you take a look at the code i just committed, i finally got volume to work but i can't get it to update the html code. i added a test html file (changed in the last commit) can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants