Skip to content

Add C interface for Notifly with shared library support - #6

Merged
draugvar merged 2 commits into
mainfrom
copilot/fix-5
Aug 19, 2025
Merged

Add C interface for Notifly with shared library support#6
draugvar merged 2 commits into
mainfrom
copilot/fix-5

Conversation

Copilot AI commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

This PR implements a complete C interface for the Notifly notification center library, enabling usage from C programs through a shared library (DLL/SO). The C interface wraps the existing C++ API while maintaining all functionality and performance.

Key Features

  • Shared Library: Builds libnotifly_c.so (Linux) / notifly_c.dll (Windows) via CMake
  • Handle-based API: Uses opaque pointers for type safety across the C/C++ boundary
  • Function Pointer Callbacks: Replaces C++ std::function with C-compatible function pointers
  • Complete Functionality: Supports all core features including sync/async notifications, multiple instances, and observer management
  • Thread Safety: Inherits thread safety from the underlying C++ implementation

API Overview

// Instance management
notifly_handle notifly_default(void);
notifly_handle notifly_create(void);
void notifly_destroy(notifly_handle handle);

// Observer management  
int notifly_add_observer(notifly_handle handle, int notification_id, 
                        notifly_callback callback, void* user_data);
int notifly_remove_observer(notifly_handle handle, int observer_id);

// Notification posting
int notifly_post_notification(notifly_handle handle, int notification_id, void* data);
int notifly_post_notification_async(notifly_handle handle, int notification_id, void* data);

Usage Example

#include "notifly_c.h"

void sensor_callback(int notification_id, void* data, void* user_data) {
    printf("Sensor data received: notification %d\n", notification_id);
}

int main() {
    notifly_handle notifly = notifly_default();
    int observer_id = notifly_add_observer(notifly, 1001, sensor_callback, NULL);
    notifly_post_notification(notifly, 1001, &sensor_data);
    notifly_remove_observer(notifly, observer_id);
    return 0;
}

Files Added

  • include/notifly_c.h - C API header with complete interface definitions
  • src/notifly_c.cpp - C++ implementation wrapping the existing API
  • test/test_c_interface.c - Comprehensive test suite covering all functionality
  • example/c_example.c - Practical sensor monitoring example
  • docs/C_INTERFACE.md - Complete API documentation and usage guide

Validation

  • All existing C++ tests continue to pass (26/26) ✅
  • Complete C interface test suite passes (5/5) ✅
  • Both C++ and C examples demonstrate correct functionality ✅
  • Shared library has minimal dependencies and proper linking ✅

The implementation maintains backward compatibility while providing a clean, efficient C interface that preserves all the power and features of the original C++ API.

Fixes #5.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI commented Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

@draugvar 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

…tation

Co-authored-by: draugvar <12036000+draugvar@users.noreply.github.com>
Copilot AI changed the title [WIP] make a C interface Add C interface for Notifly with shared library support Aug 19, 2025
Copilot AI requested a review from draugvar August 19, 2025 21:14
@draugvar
draugvar marked this pull request as ready for review August 19, 2025 21:21
@draugvar
draugvar merged commit 9d5edb0 into main Aug 19, 2025
@draugvar
draugvar deleted the copilot/fix-5 branch August 19, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make a C interface

2 participants