1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
… | |
… | |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * |
20 | * |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | // |
24 | // |
25 | // cfperl.h perl interface |
25 | // cfperl.h perl interface |
26 | // |
26 | // |
… | |
… | |
35 | |
35 | |
36 | #include <EXTERN.h> |
36 | #include <EXTERN.h> |
37 | #include <perl.h> |
37 | #include <perl.h> |
38 | #include <XSUB.h> |
38 | #include <XSUB.h> |
39 | |
39 | |
40 | #include <EventAPI.h> |
40 | #include <EVAPI.h> |
41 | #include <CoroAPI.h> |
41 | #include <CoroAPI.h> |
42 | |
42 | |
43 | #include "util.h" |
43 | #include "util.h" |
44 | #include "keyword.h" |
44 | #include "keyword.h" |
45 | #include "dynbuf.h" |
45 | #include "dynbuf.h" |
… | |
… | |
489 | |
489 | |
490 | static void wait_for_tick (); |
490 | static void wait_for_tick (); |
491 | static void wait_for_tick_begin (); |
491 | static void wait_for_tick_begin (); |
492 | }; |
492 | }; |
493 | |
493 | |
494 | struct watcher_base |
494 | struct evapi |
495 | { |
495 | { |
496 | static struct EventAPI *GEventAPI; |
496 | static struct EVAPI *GEVAPI; |
497 | }; |
497 | }; |
498 | |
498 | |
499 | template<class base> |
|
|
500 | struct watcher : watcher_base |
|
|
501 | { |
|
|
502 | base *pe; |
|
|
503 | |
|
|
504 | void start (bool repeat = false) { GEventAPI->start ((pe_watcher *)pe, repeat); } |
|
|
505 | void stop (bool cancel_events = false) { GEventAPI->stop ((pe_watcher *)pe, cancel_events); } |
|
|
506 | void now () { GEventAPI->now ((pe_watcher *)pe); } |
|
|
507 | void suspend () { GEventAPI->suspend ((pe_watcher *)pe); } |
|
|
508 | void resume () { GEventAPI->resume ((pe_watcher *)pe); } |
|
|
509 | |
|
|
510 | void prio (int new_prio) { ((pe_watcher *)pe)->prio = new_prio; } |
|
|
511 | |
|
|
512 | ~watcher () |
|
|
513 | { |
|
|
514 | cancel (); |
|
|
515 | } |
|
|
516 | |
|
|
517 | private: |
|
|
518 | void cancel () { GEventAPI->cancel ((pe_watcher *)pe); } // private |
|
|
519 | }; |
|
|
520 | |
|
|
521 | struct iw : watcher<pe_idle>, callback<void (iw &)> |
|
|
522 | { |
|
|
523 | template<class O, class M> |
|
|
524 | iw (O object, M method) |
|
|
525 | : callback<void (iw &)> (object, method) |
|
|
526 | { |
|
|
527 | alloc (); |
|
|
528 | } |
|
|
529 | |
|
|
530 | private: |
|
|
531 | void alloc (); |
|
|
532 | }; |
|
|
533 | |
|
|
534 | struct iow : watcher<pe_io>, callback<void (iow &, int)> |
499 | struct iow : ev_io, evapi, callback<void (iow &, int)> |
535 | { |
500 | { |
|
|
501 | static void thunk (struct ev_io *w_, int revents) |
|
|
502 | { |
|
|
503 | iow &w = *static_cast<iow *>(w_); |
|
|
504 | |
|
|
505 | w (w, revents); |
|
|
506 | } |
|
|
507 | |
536 | template<class O, class M> |
508 | template<class O, class M> |
537 | iow (O object, M method) |
509 | iow (O object, M method) |
538 | : callback<void (iow &, int)> (object, method) |
510 | : callback<void (iow &, int)> (object, method) |
539 | { |
511 | { |
540 | alloc (); |
512 | ev_init ((ev_io *)this, thunk); |
541 | } |
513 | } |
542 | |
514 | |
543 | void fd (int fd); |
515 | void set (int fd, int events) |
544 | int poll (); |
516 | { |
|
|
517 | ev_io_set ((ev_io *)this, fd, events); |
|
|
518 | } |
|
|
519 | |
|
|
520 | int poll () const { return events; } |
|
|
521 | |
545 | void poll (int events); |
522 | void poll (int events); |
546 | |
523 | |
547 | private: |
524 | void start () |
548 | void alloc (); |
525 | { |
|
|
526 | ev_io_start ((ev_io *)this); |
|
|
527 | } |
|
|
528 | |
|
|
529 | void stop () |
|
|
530 | { |
|
|
531 | ev_io_stop ((ev_io *)this); |
|
|
532 | } |
|
|
533 | |
|
|
534 | ~iow () |
|
|
535 | { |
|
|
536 | stop (); |
|
|
537 | } |
549 | }; |
538 | }; |
550 | |
539 | |
551 | #endif |
540 | #endif |
552 | |
541 | |