comparison src/X_supp.c @ 870:512204bcafba

Export the function to create a runtime for an existed window for X. For some application, we need to create and associate a runtime with an existed window, and free it the runtime without close the window, later.
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 24 Sep 2010 11:19:53 +0800
parents c18058fb48ee
children 3fe8054457a8
comparison
equal deleted inserted replaced
869:c18058fb48ee 870:512204bcafba
670 * - win 670 * - win
671 * - display 671 * - display
672 * - visual 672 * - visual
673 */ 673 */
674 static int 674 static int
675 X_MB_init_with_win(X_MB_runtime_t *xmb_rt) { 675 X_MB_init_with_win_internal(X_MB_runtime_t *xmb_rt) {
676 mb_img_ldr_t *img_ldr; 676 mb_img_ldr_t *img_ldr;
677 int w, h; 677 int w, h;
678 678
679 w = xmb_rt->w; 679 w = xmb_rt->w;
680 h = xmb_rt->h; 680 h = xmb_rt->h;
727 /*! \brief Initialize a MadButterfy runtime for Xlib. 727 /*! \brief Initialize a MadButterfy runtime for Xlib.
728 * 728 *
729 * It setups a runtime environment to run MadButterfly with Xlib. 729 * It setups a runtime environment to run MadButterfly with Xlib.
730 * Users should specify width and height of the opening window. 730 * Users should specify width and height of the opening window.
731 */ 731 */
732 static int X_MB_init(const char *display_name, 732 static int X_MB_init(X_MB_runtime_t *xmb_rt, const char *display_name,
733 int w, int h, X_MB_runtime_t *xmb_rt) { 733 int w, int h) {
734 int r; 734 int r;
735 735
736 memset(xmb_rt, 0, sizeof(X_MB_runtime_t)); 736 memset(xmb_rt, 0, sizeof(X_MB_runtime_t));
737 737
738 xmb_rt->w = w; 738 xmb_rt->w = w;
740 r = X_init_connection(display_name, w, h, &xmb_rt->display, 740 r = X_init_connection(display_name, w, h, &xmb_rt->display,
741 &xmb_rt->visual, &xmb_rt->win); 741 &xmb_rt->visual, &xmb_rt->win);
742 if(r != OK) 742 if(r != OK)
743 return ERR; 743 return ERR;
744 744
745 r = X_MB_init_with_win(xmb_rt); 745 r = X_MB_init_with_win_internal(xmb_rt);
746
747 return r;
748 }
749
750 /*! \brief Initialize a MadButterfly runtime for a window of X.
751 *
752 * Runtimes initialized with this function should be destroyed with
753 * X_MB_destroy_keep_win().
754 */
755 static int
756 X_MB_init_with_win(X_MB_runtime_t *xmb_rt,
757 Display *display, Window win) {
758 XWindowAttributes attrs;
759 int r;
760
761 r = XGetWindowAttributes(display, win, &attrs);
762 if(r == 0)
763 return ERR;
764
765 memset(xmb_rt, 0, sizeof(X_MB_runtime_t));
766
767 xmb_rt->display = display;
768 xmb_rt->win = win;
769 xmb_rt->visual = attrs.visual;
770 xmb_rt->w = attrs.width;
771 xmb_rt->h = attrs.height;
772
773 r = X_MB_init_with_win_internal(xmb_rt);
746 774
747 return r; 775 return r;
748 } 776 }
749 777
750 static void X_MB_destroy(X_MB_runtime_t *xmb_rt) { 778 static void X_MB_destroy(X_MB_runtime_t *xmb_rt) {
775 XCloseDisplay(xmb_rt->display); 803 XCloseDisplay(xmb_rt->display);
776 804
777 X_kb_destroy(&xmb_rt->kbinfo); 805 X_kb_destroy(&xmb_rt->kbinfo);
778 } 806 }
779 807
808 /*! \brief Destroy a MadButterfly runtime initialized with
809 * X_MB_init_with_win().
810 *
811 * Destroying a runtime with this function prevent the window and
812 * display associated with the runtime being closed.
813 */
814 static void
815 X_MB_destroy_keep_win(X_MB_runtime_t *xmb_rt) {
816 Display *display;
817 Window win;
818
819 display = xmb_rt->display;
820 xmb_rt->display = NULL;
821 win = xmb_rt->win;
822 xmb_rt->win = 0;
823
824 X_MB_destroy(xmb_rt);
825
826 xmb_rt->display = display;
827 xmb_rt->win = win;
828 }
829
780 void *X_MB_new(const char *display_name, int w, int h) { 830 void *X_MB_new(const char *display_name, int w, int h) {
781 X_MB_runtime_t *rt; 831 X_MB_runtime_t *rt;
782 int r; 832 int r;
783 833
784 rt = O_ALLOC(X_MB_runtime_t); 834 rt = O_ALLOC(X_MB_runtime_t);
785 if(rt == NULL) 835 if(rt == NULL)
786 return NULL; 836 return NULL;
787 837
788 r = X_MB_init(display_name, w, h, rt); 838 r = X_MB_init(rt, display_name, w, h);
789 if(r != OK) { 839 if(r != OK) {
790 free(rt); 840 free(rt);
791 return NULL; 841 return NULL;
792 } 842 }
793 843
794 return rt; 844 return rt;
795 } 845 }
796 846
847 /*! \brief Create a new runtime for existed window for X.
848 *
849 * The object returned by this function must be free with
850 * X_MB_free_keep_win() to prevent the window from closed.
851 */
852 void *X_MB_new_with_win(Display *display, Window win) {
853 X_MB_runtime_t *rt;
854 int r;
855
856 rt = O_ALLOC(X_MB_runtime_t);
857 if(rt == NULL)
858 return NULL;
859
860 r = X_MB_init_with_win(rt, display, win);
861 if(r != OK) {
862 free(rt);
863 return NULL;
864 }
865
866 return rt;
867 }
868
797 void X_MB_free(void *rt) { 869 void X_MB_free(void *rt) {
798 X_MB_destroy((X_MB_runtime_t *) rt); 870 X_MB_destroy((X_MB_runtime_t *) rt);
871 free(rt);
872 }
873
874 /*! \brief Free runtime created with X_MB_new_with_win().
875 */
876 void
877 X_MB_free_keep_win(void *rt) {
878 X_MB_destroy_keep_win((X_MB_runtime_t *) rt);
799 free(rt); 879 free(rt);
800 } 880 }
801 881
802 subject_t *X_MB_kbevents(void *rt) { 882 subject_t *X_MB_kbevents(void *rt) {
803 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt; 883 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;