Skip to main content
Version: Dev (main)

GUI Panel Operations

What you build​

A small program that puts a web panel into the shared QAROS room. It subscribes to the panel's updates, then moves, resizes, minimizes and navigates the panel, and prints each distinct update the subscription delivers. It then lists every panel in the session and closes its own.

What you will learn:

  • how a panel's common name makes creation idempotent,
  • how to subscribe to one panel's updates, and why the callback needs a lock,
  • why an update tells you that the panel changed, not what you should apply,
  • when it is safe to end a subscription and free the state its callback uses,
  • which handles you destroy, and in which order.

Before you start​

  • Windows, with QAROS installed on the Hub machine. The program runs on the Hub machine. Joining as a local app starts the Hub when it is not running.
  • Joining. The program joins the Hub as a local app: click Allow in the QAROS approval prompt on the first run of the executable. Nothing is stored, and later runs join without a prompt while the executable is unchanged.
  • The examples built, with the repository's CMake presets (MSVC).

Run it​

From the repository root, in a Developer PowerShell for Visual Studio:

 cmake --preset x64-windows
 cmake --build --preset x64-windows-debug --target gui_panel_operations
 .\build\x64-windows\qar-streaming-c\examples\Debug\gui_panel_operations.exe

The program loads the runtime of the installed QAROS. To load another one, such as a development build, add --qaros-library=PATH with the path of its qar-streaming-c.dll. Any other argument prints the usage and exits with 1.

Expected output after the join messages (<...> stands for values of your own run; the library's own log lines are left out):

 qar_gui_panels_get_or_create: SUCCESS (code=0)
 GUI panel id: <16 hex bytes>
 qar_gui_panels_update_pose: SUCCESS (code=0)
 qar_gui_panels_change_size: SUCCESS (code=0)
 qar_gui_panels_set_state: SUCCESS (code=0)
 qar_gui_panels_navigate_to_uri: SUCCESS (code=0)
 Update: position(<x>, <y>, <z>) size(<w> x <h>) state=<STATE> uri='<uri>'
 Update: ...
 qar_cancel_token_cancel: SUCCESS (code=0)
 Received <n> updates, printed <m>: the others matched the update printed before them.
 - Panel id: <16 hex bytes> name='<name>' position(<x>, <y>, <z>) size(<w> x <h>) state=<STATE> uri='<uri>'
 qar_gui_panels_close_panel: SUCCESS (code=0)
 qar_library_destroy: SUCCESS (code=0)

In the room you see the panel appear, shrink and minimize, then disappear when the program closes it.

When no update arrives within 10 s of the changes, the line No update of the panel arrived within 10 s of the changes. replaces the Update: lines, the listing is left out, and the program exits with 1.

The Hub delivers updates on its own schedule and reports your own changes back to you, often several times. A few changes can arrive as dozens of updates, most of them the same to the millimeter, and an update can carry an older state than the one you set. That is why the program prints only an update that differs from the last one it printed, and why the Update: lines need not match the calls one to one. When no update repeated another, the Received line ends after printed <m>.

Exit codes: 0 when every step succeeded; 1 for an unknown argument, when a call failed (the line before names it and its status), or when no update of the panel arrived within 10 s of the changes; 2 when no runtime loaded; 3 to 7 for a refused local-app join (the program prints what to do): 3 when it could not start or reach the Hub, 4 when the approval was denied, 5 when nobody answered in time, 6 when the program answering is not the installed QAROS, 7 when the executable no longer matches its approval.

Walkthrough​

1. Get a session​

A panel is shared session state: every peer sees the same panel at the same place. So everything starts with a session. The program loads the runtime, creates a runtime handle and joins with the helpers of common.h: example_join_session joins the Hub on this machine as a local app with the program's app tag.

  static const char* const known_flags[] = { "--qaros-library=" };
  argc,
  argv,
  known_flags,
  sizeof(known_flags) / sizeof(known_flags[0]),
  "[--qaros-library=PATH]"
  ))
  {
  return 1;
  }
  const char* library_path = example_library_path(argc, argv);
 
  int exit_code = example_load_library(library_path);
  if(exit_code != 0)
  {
  return exit_code;
  }
  QarRuntime* runtime = NULL;
  if(!example_create_runtime(NULL, &runtime))
  {
  return 1;
  }
  QarSession* session = NULL;
  exit_code = example_join_session(runtime, "GUI Panel Peer", APP_TAG, &session);
  if(exit_code == 0)
  {
  exit_code = run_panel_steps(session);
  }
 
  /* Children before parents: session, runtime, then the library. */
  if(session != NULL)
  {
  }
  return exit_code;

The Local App Onboarding tutorial explains the local-app join, its approval prompt and its app tag.

2. Get or create the panel​

A panel is identified by its common name, not by a handle. qar_gui_panels_get_or_create() returns the panel with that name and creates it the first time, so the same call is safe on every start: a restarted app reattaches to its panel instead of adding a second one. The id derives from the name. Use a DNS-style, app-scoped name and keep it stable across versions.

 static bool
 get_or_create_panel(QarSession* session, QarGuiPanelId* out_panel_id)
 {
  /* The identity: stable, ASCII, scoped to your app. */
  panel_init.common_name = "tutorial-panel.examples.qaros";
  panel_init.display_name = "Tutorial Panel";
  panel_init.pose = qar_pose_default();
  panel_init.pose.position.x = 0.5f;
  panel_init.pose.position.y = 1.5f;
  panel_init.pose.position.z = -1.2f;
  panel_init.size = qar_gui_panel_size_default();
  panel_init.size.width_meters = 1.2f;
  panel_init.size.height_meters = 0.7f;
 
  *out_panel_id = qar_gui_panel_id_default();
  "qar_gui_panels_get_or_create",
  qar_gui_panels_get_or_create(session, &panel_init, out_panel_id)
  ))
  {
  return false;
  }
  printf("GUI panel id: ");
  print_hex_id(out_panel_id->data, QAR_MAX_ID_LENGTH);
  printf("\n");
  return true;
 }

The call does not say whether it created the panel or found it. This program closes its panel at the end, so a normal run creates it; after a run that ended early, it finds the one left behind.

The pose is the midpoint of the panel's top edge in room space, not its center; the surface hangs below it (Coordinate Systems). QarGuiPanelSize::content_scale zooms the web content; the default keeps it at 1. With no content extension chained to the init, the panel starts with the default website content until you navigate it.

What can go wrong: the common name must be ASCII, and the runtime rejects any other. Put other text in display_name. When the call fails, the program stops: every later call needs the panel id.

3. Subscribe before you change anything​

Other peers, other apps and users grabbing the panel all change it. A subscription tells you when. Subscribe before your own changes so you do not miss any.

The callback runs on a library thread, while your main thread does something else. It receives a QarGuiPanel handle that the library owns and that is valid only during the call: read what you need with the qar_gui_panel_get_* getters, copy it, and do not destroy the handle. Keep the callback short and do not call blocking QAROS functions from it.

 /** What the update callback shares with the main thread. */
 typedef struct PanelUpdateLog
 {
  unsigned received_count;
  unsigned printed_count;
 
 /** \brief Runs on a library thread for every update of the subscribed panel.
  * The handle belongs to the library and is valid during this call only:
  * copy what you need, never destroy it. */
 static void
 on_panel_update(QarGuiPanel* panel, void* user_state)
 {
  PanelUpdateLog* update_log = (PanelUpdateLog*)user_state;
  PanelSnapshot snapshot;
  QarResult read_result = read_panel_snapshot(panel, &snapshot);
 
  example_lock_acquire(&update_log->lock);
  update_log->received_count += 1;
  if(qar_result_is_error(read_result))
  {
  log_result("reading a GUI panel update", read_result);
  }
  else if(
  !update_log->has_last_printed || !same_panel_snapshot(&update_log->last_printed, &snapshot)
  )
  {
  /* The Hub repeats updates; print only one that differs from the last
  * one printed. */
  update_log->last_printed = snapshot;
  update_log->has_last_printed = true;
  update_log->printed_count += 1;
  printf("Update: ");
  print_panel_snapshot(&snapshot);
  printf("\n");
  }
  example_lock_release(&update_log->lock);
 }

The callback and the main thread share the update log, so a lock guards it. The callback skips an update that matches the last one it printed: the Hub repeats updates, and a repeat tells you nothing new.

A repeat need not be equal to the bit. Its pose and size can differ from the earlier report below any precision you print or see, so comparing floats with == takes most repeats for changes. Compare at the precision your app cares about. This program allows a millimeter for pose and size, and compares the state and the address exactly:

 /** \brief Whether two updates show the same panel: pose and size at the
  * precision of example_same_pose, state and address exactly. */
 static bool
 same_panel_snapshot(const PanelSnapshot* first, const PanelSnapshot* second)
 {
  return example_same_pose(&first->pose, &second->pose)
  )
  )
  )
  && first->state == second->state && strcmp(first->content_uri, second->content_uri) == 0;
 }

The tolerances are in common.h, shared with App Volume Management :

 /* The Hub reports a value back that can differ from the one you set, and from
  * its own earlier report, below the precision anyone sees. Compared exactly,
  * such an echo looks like a change. So compare at the precision your app
  * cares about. These samples print centimeters; a millimeter is far below
  * what a viewer notices. */
 #define EXAMPLE_SAME_LENGTH_METERS 0.001f
 /* For each component of a unit quaternion: 0.0001 is about a hundredth of a
  * degree. */
 #define EXAMPLE_SAME_ROTATION 0.0001f
 /* For a factor such as a scale: one part in a thousand. */
 #define EXAMPLE_SAME_FACTOR 0.001f
 
 static bool
 example_nearly_equal(float first, float second, float tolerance)
 {
  return fabsf(first - second) <= tolerance;
 }
 
 /** \brief Whether two poses are the same at the precision above. */
 static bool
 example_same_pose(const QarPose* first, const QarPose* second)
 {
  const float length = EXAMPLE_SAME_LENGTH_METERS;
  const float rotation = EXAMPLE_SAME_ROTATION;
  return example_nearly_equal(first->position.x, second->position.x, length)
  && example_nearly_equal(first->position.y, second->position.y, length)
  && example_nearly_equal(first->position.z, second->position.z, length)
  && example_nearly_equal(first->orientation.x, second->orientation.x, rotation)
  && example_nearly_equal(first->orientation.y, second->orientation.y, rotation)
  && example_nearly_equal(first->orientation.z, second->orientation.z, rotation)
  && example_nearly_equal(first->orientation.w, second->orientation.w, rotation);
 }

Every qar_*_subscribe_* call hands its subscription to a cancel token. The token is how you end it (step 5).

  PanelUpdateLog update_log;
  memset(&update_log, 0, sizeof(update_log));
  example_lock_init(&update_log.lock);
 
  QarCancelToken* subscription_token = NULL;
  "qar_cancel_token_create", qar_cancel_token_create(&subscription_token)
  ))
  {
  example_lock_destroy(&update_log.lock);
  return false;
  }
  "qar_gui_panels_subscribe_panel_updates",
  session, panel_id, on_panel_update, &update_log, subscription_token
  )
  ))
  {
  qar_cancel_token_handle_destroy(subscription_token);
  example_lock_destroy(&update_log.lock);
  return false;
  }

qar_gui_panels_subscribe_updates() works the same way for every panel in the session; qar_gui_panels_subscribe_panel_updates() filters to one.

4. Change the panel​

Every change goes through the session and the panel id. Each call is checked, and the first failure stops the chain.

 static bool
 change_panel(QarSession* session, const QarGuiPanelId* panel_id)
 {
  QarPose new_pose = qar_pose_default();
  new_pose.position.x = 0.2f;
  new_pose.position.y = 1.6f;
  new_pose.position.z = -1.0f;
 
  new_size.width_meters = 1.0f;
  new_size.height_meters = 0.6f;
 
  "qar_gui_panels_update_pose",
  qar_gui_panels_update_pose(session, panel_id, &new_pose)
  )
  "qar_gui_panels_change_size",
  qar_gui_panels_change_size(session, panel_id, &new_size)
  )
  "qar_gui_panels_set_state",
  )
  "qar_gui_panels_navigate_to_uri",
  qar_gui_panels_navigate_to_uri(session, panel_id, "https://example.com/tutorial")
  );
 }

What can go wrong: QAR_STATUS_GUI_PANEL_INVALID_ID means no panel has that id, for example because another peer closed it. Call qar_gui_panels_get_or_create() again if your app still needs it. QAR_STATUS_GUI_PANEL_CHANGE_NOT_PERMITTED from the navigate call means the panel shows a stream, not a website.

5. Receive the updates, then end the subscription​

The program waits up to 10 s for the first update, then until no update has arrived for half a second, and then ends the subscription. The Hub can take a few seconds to deliver the first update, so a quiet period alone could end before anything arrived. When no update comes within 10 s, the program says so, skips the listing, closes the panel and exits with 1: a program that follows updates and sees none should not report success.

  const bool changed = change_panel(session, panel_id);
  bool updated = false;
  if(changed)
  {
  updated = example_wait_for_updates(&update_log.lock, &update_log.received_count);
  if(!updated)
  {
  printf(
  "No update of the panel arrived within %u s of the changes.\n",
  );
  }
  }

The wait lives in common.h, shared with App Volume Management. The callback counts updates under the lock, so the wait reads the count under the same lock:

 /* The Hub delivers the first update on its own schedule, which can take a few
  * seconds after the subscription. Ten seconds leaves room for that and still
  * ends a run in which no update comes. */
 #define EXAMPLE_FIRST_UPDATE_TIMEOUT_MS 10000u
 /* After the first update, how long the wait lasts without one more. */
 #define EXAMPLE_UPDATE_QUIET_PERIOD_MS 500u
 #define EXAMPLE_UPDATE_POLL_INTERVAL_MS 50u
 
 static unsigned
 example_read_locked_count(ExampleLock* lock, const unsigned* count)
 {
  const unsigned value = *count;
  return value;
 }
 
 /** \brief Wait for the first update, then until no update has arrived for
  * EXAMPLE_UPDATE_QUIET_PERIOD_MS. received_count is the counter a callback
  * increments while holding lock. Returns false when no update arrived within
  * EXAMPLE_FIRST_UPDATE_TIMEOUT_MS. */
 static bool
 example_wait_for_updates(ExampleLock* lock, const unsigned* received_count)
 {
  unsigned waited_milliseconds = 0;
  while(example_read_locked_count(lock, received_count) == 0)
  {
  if(waited_milliseconds >= EXAMPLE_FIRST_UPDATE_TIMEOUT_MS)
  {
  return false;
  }
  waited_milliseconds += EXAMPLE_UPDATE_POLL_INTERVAL_MS;
  }
 
  unsigned last_count = example_read_locked_count(lock, received_count);
  unsigned quiet_milliseconds = 0;
  while(quiet_milliseconds < EXAMPLE_UPDATE_QUIET_PERIOD_MS)
  {
  const unsigned count = example_read_locked_count(lock, received_count);
  if(count != last_count)
  {
  last_count = count;
  quiet_milliseconds = 0;
  }
  else
  {
  quiet_milliseconds += EXAMPLE_UPDATE_POLL_INTERVAL_MS;
  }
  }
  return true;
 }
  /* Once this returns, no callback runs and none starts: the log is ours. */
  const bool cancelled = example_logged_success(
  "qar_cancel_token_cancel", qar_cancel_token_cancel(subscription_token)
  );
  qar_cancel_token_handle_destroy(subscription_token);
  printf("Received %u updates, printed %u", update_log.received_count, update_log.printed_count);
  if(update_log.printed_count < update_log.received_count)
  {
  printf(": the others matched the update printed before them");
  }
  printf(".\n");
  example_lock_destroy(&update_log.lock);

A real application does not wait for a quiet period. It reads what the callback stored from its own loop, such as a UI frame, and never blocks a callback waiting for that loop.

Treat an update as news that the panel changed, not as an instruction. Expect your own changes to come back, repeated and sometimes older than what you set: skip an update that matches the last one you handled, and do not take an update for a user's action, since it may be the echo of your own call.

When qar_cancel_token_cancel() returns, a callback that was running on another thread has finished, and no callback starts afterwards. That is what makes it safe to read the counts without the lock, and to free the update log once the function returns. The guarantee comes with one rule: never end a subscription while holding a lock its callback takes. Ending it waits for the running callback; if that callback waits for your lock, neither returns. Ending a subscription from inside its own callback is allowed, and does not wait for that callback. The full rules are under Ending a subscription in the API reference.

6. List the panels​

The listing is a snapshot of every panel in the session, read with the count-then-fetch pattern: ask for the count, allocate that many handles, fetch them. You own each handle the fetch returns: read it, then destroy it. A fetch that returns an error may still have written some handles: destroy those too.

 static void
 {
  char name[QAR_MAX_STRING_LENGTH] = { 0 };
  PanelSnapshot snapshot;
  QarResult read_result = qar_gui_panel_get_id(panel, &id);
  if(qar_result_is_success(read_result))
  {
  read_result = qar_gui_panel_get_display_name(panel, name, sizeof(name));
  }
  if(qar_result_is_success(read_result))
  {
  read_result = read_panel_snapshot(panel, &snapshot);
  }
 
  if(qar_result_is_success(read_result))
  {
  printf("- Panel id: ");
  printf(" name='%s' ", name);
  print_panel_snapshot(&snapshot);
  printf("\n");
  }
  else
  {
  log_result("reading a listed GUI panel", read_result);
  }
 }
 
 static bool
 {
  size_t panel_count = 0;
  "qar_query_gui_panels_count", qar_query_gui_panels_count(session, &panel_count)
  ))
  {
  return false;
  }
  if(panel_count == 0)
  {
  return true;
  }
 
  QarGuiPanel** panels = (QarGuiPanel**)calloc(panel_count, sizeof(QarGuiPanel*));
  if(panels == NULL)
  {
  fprintf(stderr, "Out of memory listing %zu panels.\n", panel_count);
  return false;
  }
  size_t written = 0;
  const bool listed = example_quiet_success(
  "qar_query_gui_panels", qar_query_gui_panels(session, panels, panel_count, &written)
  );
  /* The fetch handed us every handle it wrote, also when it then failed: we
  * destroy them all. */
  const size_t owned = written < panel_count ? written : panel_count;
  for(size_t i = 0; i < owned; ++i)
  {
  if(panels[i] == NULL)
  {
  continue;
  }
  if(listed)
  {
  print_listed_panel(panels[i]);
  }
  }
  free(panels);
  return listed;
 }

The listing can be stale the moment it returns. Use it at startup or for display, and the subscription to follow changes. A panel's display name can come back empty from the Hub, so tell panels apart by id, which derives from the common name, not by display name.

7. Close the panel​

  /* Close the panel even when a step failed, so no run leaves one behind. */
  succeeded = example_logged_success(
  "qar_gui_panels_close_panel", qar_gui_panels_close_panel(session, &panel_id)
  )
  && succeeded;

Closing removes the panel for every peer, since panels are shared. A later qar_gui_panels_get_or_create() with the same name creates it again.

Ownership and teardown in C​

ValueWho owns itHow it ends
QarRuntime*, QarSession*youqar_session_handle_destroy(), then qar_runtime_handle_destroy(), then example_unload_library (qar_library_destroy() and the unload)
QarCancelToken*youqar_cancel_token_cancel() ends the subscription; qar_cancel_token_handle_destroy() frees the token and also ends it
the update log (user_state)youfree it only after the subscription has ended; here it lives on the stack of the function that ends it
QarGuiPanel* in the callbackthe libraryvalid during the call only; never destroy it
QarGuiPanel* from qar_query_gui_panels()youqar_gui_panel_handle_destroy() after reading it, then free the array
QarGuiPanelIda plain valuenames the panel; not a handle, nothing to free
the panel itselfthe session, shared by every peerqar_gui_panels_close_panel(); destroying your session does not close it

qar_session_handle_destroy() and qar_library_destroy() also end every subscription still running, so a forgotten token does not outlive the session. Do not destroy the session or the library from a callback.

Troubleshooting​

SymptomCauseWhat to do
Could not start or reach QAROS on this machine. Start QAROS from the Start menu, then start this app again., exit code 3The local-app join could neither start the Hub nor reach itStart QAROS from the Start menu and run again
Exit code 4 or 5 after Joining the QAROS Hub on this machine as a local appThe approval prompt was denied, closed or not answered within 120 sRun again and click Allow
qar_gui_panels_get_or_create: ERRORThe common name is not ASCII, or the session is goneKeep the name ASCII; put other text in display_name
ERROR (code=305) from a setter: QAR_STATUS_GUI_PANEL_INVALID_IDThe panel was closed, possibly by another peerqar_gui_panels_get_or_create() again
No update of the panel arrived within 10 s of the changes., exit code 1The Hub delivered no update of the panel within 10 s after the changesCheck that the Hub is still running and run again. In your app, read updates as they come instead of waiting for them a fixed time
Many more updates received than printedThe Hub reports your own changes back, repeatedExpected; skip repeats as the callback does
Update: or listing values differ from what you setUpdates arrive asynchronously and can be older than your last callDo not apply an update's values blindly; treat it as a sign that the panel changed
name='' in the listingThe Hub returned an empty display nameIdentify panels by id, not by display name
The program hangs while ending the subscriptionA callback is waiting for a lock the ending thread holdsRelease the lock before qar_cancel_token_cancel()
No QAROS runtime loadedThe default lookup found no runtimeInstall QAROS, fix or unset QAR_INSTANCE, or pass --qaros-library=PATH

Next​


Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.